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:6180/images/${BM_IMAGE}
16 IMAGE_CHECKSUM=http://172.22.0.1:6180/images/${BM_IMAGE}.md5sum
18 function deprovision_compute_node {
20 if kubectl get baremetalhost $name -n metal3 &>/dev/null; then
21 kubectl patch baremetalhost $name -n metal3 --type merge \
22 -p '{"spec":{"image":{"url":"","checksum":""}}}'
26 # documentation for the values below may be found at
27 # https://cloudinit.readthedocs.io/en/latest/topics/modules.html
28 function create_userdata {
32 COMPUTE_NODE_FQDN="$name.akraino.icn.org"
34 # validate that the user isn't expecting the deprecated
35 # COMPUTE_NODE_PASSWORD to be used
36 if [ "$password" != "${COMPUTE_NODE_PASSWORD:-$password}" ]; then
38 COMPUTE_NODE_PASSWORD "$COMPUTE_NODE_PASSWORD" not equal to nodes.json $name password "$password".
39 Unset COMPUTE_NODE_PASSWORD and retry.
44 printf "#cloud-config\n" > $name-userdata.yaml
45 if [ -n "$password" ]; then
46 if [ -n "$username" ]; then
47 passwd=$(mkpasswd --method=SHA-512 --rounds 4096 "$password")
48 printf "users:" >> $name-userdata.yaml
49 printf "\n - name: ""%s" "$username" >> $name-userdata.yaml
50 printf "\n lock_passwd: False" >> $name-userdata.yaml # necessary to allow password login
51 printf "\n passwd: ""%s" "$passwd" >> $name-userdata.yaml
52 printf "\n sudo: \"ALL=(ALL) NOPASSWD:ALL\"" >> $name-userdata.yaml
54 printf "password: ""%s" "$password" >> $name-userdata.yaml
56 printf "\nchpasswd: {expire: False}\n" >> $name-userdata.yaml
57 printf "ssh_pwauth: True\n" >> $name-userdata.yaml
60 if [ -n "$COMPUTE_NODE_FQDN" ]; then
61 printf "fqdn: ""%s" "$COMPUTE_NODE_FQDN" >> $name-userdata.yaml
62 printf "\n" >> $name-userdata.yaml
64 printf "disable_root: false\n" >> $name-userdata.yaml
65 printf "ssh_authorized_keys:\n - " >> $name-userdata.yaml
67 if [ ! -f $HOME/.ssh/id_rsa.pub ]; then
68 yes y | ssh-keygen -t rsa -N "" -f $HOME/.ssh/id_rsa
71 cat $HOME/.ssh/id_rsa.pub >> $name-userdata.yaml
72 cloud_init_scripts >> $name-userdata.yaml
73 printf "\n" >> $name-userdata.yaml
76 create_networkdata() {
78 node_networkdata $name > $name-networkdata.json
81 function cloud_init_scripts {
82 # set_dhcp_indentifier.sh:
83 # The IP address assigned to the provisioning NIC will change
84 # due to IPA using the MAC address as the client ID and systemd
85 # using a different ID. Tell systemd to use the MAC as the
86 # client ID. We can't do this in the network data as only the
87 # JSON format is supported by metal3, and the JSON format does
88 # not support the dhcp-identifier field.
89 # set_kernel_cmdline.sh:
90 # The "intel_iommu=on iommu=pt" kernel command line is necessary
94 - path: /var/lib/cloud/scripts/per-instance/set_dhcp_identifier.sh
100 sed -i -e '/dhcp4: true$/!b' -e 'h;s/\S.*/dhcp-identifier: mac/;H;g' /etc/netplan/50-cloud-init.yaml
102 - path: /var/lib/cloud/scripts/per-instance/set_kernel_cmdline.sh
108 grub_file=${1:-"/etc/default/grub"}
109 kernel_parameters="intel_iommu=on iommu=pt"
110 sed -i~ "/^GRUB_CMDLINE_LINUX=/{h;s/\(=\".*\)\"/\1 ${kernel_parameters}\"/};\${x;/^$/{s//GRUB_CMDLINE_LINUX=\"${kernel_parameters}\"/;H};x}" "$grub_file"
116 function apply_userdata_credential {
118 cat <<EOF > ./$name-user-data-credential.yaml
121 userData: $(base64 -w 0 $name-userdata.yaml)
124 name: $name-user-data
128 kubectl apply -n metal3 -f $name-user-data-credential.yaml
131 apply_networkdata_credential() {
133 cat <<EOF > ./$name-network-data-credential.yaml
136 networkData: $(base64 -w 0 $name-networkdata.json)
139 name: $name-network-data
143 kubectl apply -n metal3 -f $name-network-data-credential.yaml
146 function make_bm_hosts {
147 kubectl create namespace metal3 --dry-run=client -o yaml | kubectl apply -f -
148 while IFS=',' read -r name ipmi_username ipmi_password ipmi_address boot_mac os_username os_password os_image_name; do
149 create_userdata $name $os_username $os_password
150 apply_userdata_credential $name
151 create_networkdata $name
152 apply_networkdata_credential $name
154 GOPATH=$GOPATH:$(echo ${BMOPATH} | cut -d/ -f-2) GO111MODULE=auto \
155 go run ${BMOPATH}/cmd/make-bm-worker/main.go \
156 -address "ipmi://$ipmi_address" \
157 -password "$ipmi_password" \
158 -user "$ipmi_username" \
159 -boot-mac "$boot_mac" \
160 "$name" > $name-bm-node.yaml
162 printf " image:" >> $name-bm-node.yaml
163 printf "\n url: ""%s" "$IMAGE_URL" >> $name-bm-node.yaml
164 printf "\n checksum: ""%s" "$IMAGE_CHECKSUM" >> $name-bm-node.yaml
165 printf "\n userData:" >> $name-bm-node.yaml
166 printf "\n name: ""%s" "$name""-user-data" >> $name-bm-node.yaml
167 printf "\n namespace: metal3" >> $name-bm-node.yaml
168 printf "\n networkData:" >> $name-bm-node.yaml
169 printf "\n name: ""%s" "$name""-network-data" >> $name-bm-node.yaml
170 printf "\n namespace: metal3" >> $name-bm-node.yaml
171 printf "\n rootDeviceHints:" >> $name-bm-node.yaml
172 printf "\n minSizeGigabytes: 48\n" >> $name-bm-node.yaml
173 kubectl apply -f $name-bm-node.yaml -n metal3
177 function configure_nodes {
178 if [ ! -d $IRONIC_DATA_DIR ]; then
179 mkdir -p $IRONIC_DATA_DIR
182 #make sure nodes.json file in /opt/ironic/ are configured
183 if [ ! -f $IRONIC_DATA_DIR/nodes.json ]; then
184 cp $PWD/nodes.json.sample $IRONIC_DATA_DIR/nodes.json
188 function remove_bm_hosts {
189 while IFS=',' read -r name ipmi_username ipmi_password ipmi_address boot_mac os_username os_password os_image_name; do
190 deprovision_compute_node $name
195 while IFS=',' read -r name ipmi_username ipmi_password ipmi_address boot_mac os_username os_password os_image_name; do
196 kubectl delete --ignore-not-found=true bmh $name -n metal3
197 kubectl delete --ignore-not-found=true secrets $name-bmc-secret -n metal3
198 kubectl delete --ignore-not-found=true secrets $name-user-data -n metal3
199 if [ -f $name-bm-node.yaml ]; then
200 rm -rf $name-bm-node.yaml
203 if [ -f $name-user-data-credential.yaml ]; then
204 rm -rf $name-user-data-credential.yaml
207 if [ -f $name-userdata.yaml ]; then
208 rm -rf $name-userdata.yaml
215 if [ -f $IRONIC_DATA_DIR/nodes.json ]; then
216 rm -rf $IRONIC_DATA_DIR/nodes.json
220 function apply_bm_hosts {
221 list_nodes | make_bm_hosts
224 function deprovision_all_hosts {
225 list_nodes | remove_bm_hosts
228 if [ "$1" == "deprovision" ]; then
230 deprovision_all_hosts
234 if [ "$1" == "provision" ]; then
240 if [ "$1" == "clean" ]; then
246 echo "Usage: metal3.sh"
247 echo "provision - provision baremetal node as specified in common.sh"
248 echo "deprovision - deprovision baremetal node as specified in common.sh"
249 echo "clean - clean all the bmh resources"