4 LIBDIR="$(dirname "$(dirname "$(dirname "$PWD")")")"
8 source $LIBDIR/env/lib/common.sh
10 if [[ $EUID -ne 0 ]]; then
11 echo "This script must be run as root"
15 IMAGE_URL=http://172.22.0.1/images/${BM_IMAGE}
16 IMAGE_CHECKSUM=http://172.22.0.1/images/${BM_IMAGE}.md5sum
18 function clone_repos {
20 if [[ -d ${BMOPATH} && "${FORCE_REPO_UPDATE}" == "true" ]]; then
23 if [ ! -d "${BMOPATH}" ] ; then
25 git clone "${BMOREPO}"
29 git checkout "${BMOBRANCH}"
34 function deprovision_compute_node {
36 if kubectl get baremetalhost $name -n metal3 &>/dev/null; then
37 kubectl patch baremetalhost $name -n metal3 --type merge \
38 -p '{"spec":{"image":{"url":"","checksum":""}}}'
42 # documentation for the values below may be found at
43 # https://cloudinit.readthedocs.io/en/latest/topics/modules.html
44 function create_userdata {
48 COMPUTE_NODE_FQDN="$name.akraino.icn.org"
50 # validate that the user isn't expecting the deprecated
51 # COMPUTE_NODE_PASSWORD to be used
52 if [ "$password" != "${COMPUTE_NODE_PASSWORD:-$password}" ]; then
54 COMPUTE_NODE_PASSWORD "$COMPUTE_NODE_PASSWORD" not equal to nodes.json $name password "$password".
55 Unset COMPUTE_NODE_PASSWORD and retry.
60 printf "#cloud-config\n" > $name-userdata.yaml
61 if [ -n "$password" ]; then
62 if [ -n "$username" ]; then
63 passwd=$(mkpasswd --method=SHA-512 --rounds 4096 "$password")
64 printf "users:" >> $name-userdata.yaml
65 printf "\n - name: ""%s" "$username" >> $name-userdata.yaml
66 printf "\n lock_passwd: False" >> $name-userdata.yaml # necessary to allow password login
67 printf "\n passwd: ""%s" "$passwd" >> $name-userdata.yaml
68 printf "\n sudo: \"ALL=(ALL) NOPASSWD:ALL\"" >> $name-userdata.yaml
70 printf "password: ""%s" "$password" >> $name-userdata.yaml
72 printf "\nchpasswd: {expire: False}\n" >> $name-userdata.yaml
73 printf "ssh_pwauth: True\n" >> $name-userdata.yaml
76 if [ -n "$COMPUTE_NODE_FQDN" ]; then
77 printf "fqdn: ""%s" "$COMPUTE_NODE_FQDN" >> $name-userdata.yaml
78 printf "\n" >> $name-userdata.yaml
80 printf "disable_root: false\n" >> $name-userdata.yaml
81 printf "ssh_authorized_keys:\n - " >> $name-userdata.yaml
83 if [ ! -f $HOME/.ssh/id_rsa.pub ]; then
84 yes y | ssh-keygen -t rsa -N "" -f $HOME/.ssh/id_rsa
87 cat $HOME/.ssh/id_rsa.pub >> $name-userdata.yaml
88 cloud_init_scripts >> $name-userdata.yaml
89 printf "\n" >> $name-userdata.yaml
92 create_networkdata() {
94 node_networkdata $name > $name-networkdata.json
97 function launch_baremetal_operator {
98 docker pull $IRONIC_BAREMETAL_IMAGE
99 kubectl apply -f bmo/namespace/namespace.yaml
100 kubectl apply -f bmo/rbac/service_account.yaml -n metal3
101 kubectl apply -f bmo/rbac/role.yaml -n metal3
102 kubectl apply -f bmo/rbac/role_binding.yaml
103 kubectl apply -f bmo/crds/metal3.io_baremetalhosts_crd.yaml
104 kubectl apply -f bmo/operator/no_ironic/operator.yaml -n metal3
107 function remove_baremetal_operator {
108 kubectl delete -f bmo/operator/no_ironic/operator.yaml -n metal3
109 kubectl delete -f bmo/crds/metal3.io_baremetalhosts_crd.yaml
110 kubectl delete -f bmo/rbac/role_binding.yaml
111 kubectl delete -f bmo/rbac/role.yaml -n metal3
112 kubectl delete -f bmo/rbac/service_account.yaml -n metal3
113 kubectl delete -f bmo/namespace/namespace.yaml
116 function cloud_init_scripts {
117 # set_dhcp_indentifier.sh:
118 # The IP address assigned to the provisioning NIC will change
119 # due to IPA using the MAC address as the client ID and systemd
120 # using a different ID. Tell systemd to use the MAC as the
121 # client ID. We can't do this in the network data as only the
122 # JSON format is supported by metal3, and the JSON format does
123 # not support the dhcp-identifier field.
124 # set_kernel_cmdline.sh:
125 # The "intel_iommu=on iommu=pt" kernel command line is necessary
129 - path: /var/lib/cloud/scripts/per-instance/set_dhcp_identifier.sh
135 sed -i -e '/dhcp4: true$/!b' -e 'h;s/\S.*/dhcp-identifier: mac/;H;g' /etc/netplan/50-cloud-init.yaml
137 - path: /var/lib/cloud/scripts/per-instance/set_kernel_cmdline.sh
143 grub_file=${1:-"/etc/default/grub"}
144 kernel_parameters="intel_iommu=on iommu=pt"
145 sed -i~ "/^GRUB_CMDLINE_LINUX=/{h;s/\(=\".*\)\"/\1 ${kernel_parameters}\"/};\${x;/^$/{s//GRUB_CMDLINE_LINUX=\"${kernel_parameters}\"/;H};x}" "$grub_file"
151 function apply_userdata_credential {
153 cat <<EOF > ./$name-user-data-credential.yaml
156 userData: $(base64 -w 0 $name-userdata.yaml)
159 name: $name-user-data
163 kubectl apply -n metal3 -f $name-user-data-credential.yaml
166 apply_networkdata_credential() {
168 cat <<EOF > ./$name-network-data-credential.yaml
171 networkData: $(base64 -w 0 $name-networkdata.json)
174 name: $name-network-data
178 kubectl apply -n metal3 -f $name-network-data-credential.yaml
181 function make_bm_hosts {
182 while IFS=',' read -r name ipmi_username ipmi_password ipmi_address os_username os_password os_image_name; do
183 create_userdata $name $os_username $os_password
184 apply_userdata_credential $name
185 create_networkdata $name
186 apply_networkdata_credential $name
188 GO111MODULE=auto go run $GOPATH/src/github.com/metal3-io/baremetal-operator/cmd/make-bm-worker/main.go \
189 -address "ipmi://$ipmi_address" \
190 -password "$ipmi_password" \
191 -user "$ipmi_username" \
192 "$name" > $name-bm-node.yaml
194 printf " image:" >> $name-bm-node.yaml
195 printf "\n url: ""%s" "$IMAGE_URL" >> $name-bm-node.yaml
196 printf "\n checksum: ""%s" "$IMAGE_CHECKSUM" >> $name-bm-node.yaml
197 printf "\n userData:" >> $name-bm-node.yaml
198 printf "\n name: ""%s" "$name""-user-data" >> $name-bm-node.yaml
199 printf "\n namespace: metal3" >> $name-bm-node.yaml
200 printf "\n networkData:" >> $name-bm-node.yaml
201 printf "\n name: ""%s" "$name""-network-data" >> $name-bm-node.yaml
202 printf "\n namespace: metal3" >> $name-bm-node.yaml
203 printf "\n rootDeviceHints:" >> $name-bm-node.yaml
204 printf "\n minSizeGigabytes: 48\n" >> $name-bm-node.yaml
205 kubectl apply -f $name-bm-node.yaml -n metal3
209 function configure_nodes {
210 if [ ! -d $IRONIC_DATA_DIR ]; then
211 mkdir -p $IRONIC_DATA_DIR
214 #make sure nodes.json file in /opt/ironic/ are configured
215 if [ ! -f $IRONIC_DATA_DIR/nodes.json ]; then
216 cp $PWD/nodes.json.sample $IRONIC_DATA_DIR/nodes.json
220 function remove_bm_hosts {
221 while IFS=',' read -r name ipmi_username ipmi_password ipmi_address os_username os_password os_image_name; do
222 deprovision_compute_node $name
227 while IFS=',' read -r name ipmi_username ipmi_password ipmi_address os_username os_password os_image_name; do
228 kubectl delete --ignore-not-found=true bmh $name -n metal3
229 kubectl delete --ignore-not-found=true secrets $name-bmc-secret -n metal3
230 kubectl delete --ignore-not-found=true secrets $name-user-data -n metal3
231 if [ -f $name-bm-node.yaml ]; then
232 rm -rf $name-bm-node.yaml
235 if [ -f $name-user-data-credential.yaml ]; then
236 rm -rf $name-user-data-credential.yaml
239 if [ -f $name-userdata.yaml ]; then
240 rm -rf $name-userdata.yaml
247 if [ -f $IRONIC_DATA_DIR/nodes.json ]; then
248 rm -rf $IRONIC_DATA_DIR/nodes.json
252 function apply_bm_hosts {
253 list_nodes | make_bm_hosts
256 function deprovision_all_hosts {
257 list_nodes | remove_bm_hosts
260 if [ "$1" == "launch" ]; then
262 launch_baremetal_operator
266 if [ "$1" == "deprovision" ]; then
268 deprovision_all_hosts
272 if [ "$1" == "provision" ]; then
278 if [ "$1" == "clean" ]; then
284 if [ "$1" == "remove" ]; then
285 remove_baremetal_operator
289 echo "Usage: metal3.sh"
290 echo "launch - Launch the metal3 operator"
291 echo "provision - provision baremetal node as specified in common.sh"
292 echo "deprovision - deprovision baremetal node as specified in common.sh"
293 echo "clean - clean all the bmh resources"
294 echo "remove - remove baremetal operator"