8d0fc1b541a1548806ff7ff81e7dff9bacd9e9ad
[icn.git] / deploy / metal3 / scripts / 01_metal3.sh
1 #!/usr/bin/env bash
2 set -eu -o pipefail
3
4 LIBDIR="$(dirname "$(dirname "$(dirname "$PWD")")")"
5
6 eval "$(go env)"
7
8 source $LIBDIR/env/lib/common.sh
9
10 if [[ $EUID -ne 0 ]]; then
11     echo "This script must be run as root"
12     exit 1
13 fi
14
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
17
18 function deprovision_compute_node {
19     name="$1"
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":""}}}'
23     fi
24 }
25
26 # documentation for the values below may be found at
27 # https://cloudinit.readthedocs.io/en/latest/topics/modules.html
28 function create_userdata {
29     name="$1"
30     username="$2"
31     password="$3"
32     COMPUTE_NODE_FQDN="$name.akraino.icn.org"
33
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
37         cat <<EOF
38 COMPUTE_NODE_PASSWORD "$COMPUTE_NODE_PASSWORD" not equal to nodes.json $name password "$password".
39 Unset COMPUTE_NODE_PASSWORD and retry.
40 EOF
41         exit 1
42     fi
43
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
53         else
54             printf "password: ""%s" "$password" >>  $name-userdata.yaml
55         fi
56         printf "\nchpasswd: {expire: False}\n" >>  $name-userdata.yaml
57         printf "ssh_pwauth: True\n" >>  $name-userdata.yaml
58     fi
59
60     if [ -n "$COMPUTE_NODE_FQDN" ]; then
61         printf "fqdn: ""%s" "$COMPUTE_NODE_FQDN" >>  $name-userdata.yaml
62         printf "\n" >>  $name-userdata.yaml
63     fi
64     printf "disable_root: false\n" >>  $name-userdata.yaml
65     printf "ssh_authorized_keys:\n  - " >>  $name-userdata.yaml
66
67     if [ ! -f $HOME/.ssh/id_rsa.pub ]; then
68         yes y | ssh-keygen -t rsa -N "" -f $HOME/.ssh/id_rsa
69     fi
70
71     cat $HOME/.ssh/id_rsa.pub >>  $name-userdata.yaml
72     cloud_init_scripts >> $name-userdata.yaml
73     printf "\n" >>  $name-userdata.yaml
74 }
75
76 create_networkdata() {
77     name="$1"
78     node_networkdata $name > $name-networkdata.json
79 }
80
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
91     #   for QAT support.
92     cat << 'EOF'
93 write_files:
94 - path: /var/lib/cloud/scripts/per-instance/set_dhcp_identifier.sh
95   owner: root:root
96   permissions: '0777'
97   content: |
98     #!/usr/bin/env bash
99     set -eux -o pipefail
100     sed -i -e '/dhcp4: true$/!b' -e 'h;s/\S.*/dhcp-identifier: mac/;H;g' /etc/netplan/50-cloud-init.yaml
101     netplan apply
102 - path: /var/lib/cloud/scripts/per-instance/set_kernel_cmdline.sh
103   owner: root:root
104   permissions: '0777'
105   content: |
106     #!/usr/bin/env bash
107     set -eux -o pipefail
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"
111     update-grub
112     reboot
113 EOF
114 }
115
116 function apply_userdata_credential {
117     name="$1"
118     cat <<EOF > ./$name-user-data-credential.yaml
119 apiVersion: v1
120 data:
121   userData: $(base64 -w 0 $name-userdata.yaml)
122 kind: Secret
123 metadata:
124   name: $name-user-data
125   namespace: metal3
126 type: Opaque
127 EOF
128     kubectl apply -n metal3 -f $name-user-data-credential.yaml
129 }
130
131 apply_networkdata_credential() {
132     name="$1"
133     cat <<EOF > ./$name-network-data-credential.yaml
134 apiVersion: v1
135 data:
136   networkData: $(base64 -w 0 $name-networkdata.json)
137 kind: Secret
138 metadata:
139   name: $name-network-data
140   namespace: metal3
141 type: Opaque
142 EOF
143     kubectl apply -n metal3 -f $name-network-data-credential.yaml
144 }
145
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
153
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
161
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
174     done
175 }
176
177 function configure_nodes {
178     if [ ! -d $IRONIC_DATA_DIR ]; then
179         mkdir -p $IRONIC_DATA_DIR
180     fi
181
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
185     fi
186 }
187
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
191     done
192 }
193
194 function cleanup {
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
201         fi
202
203         if [ -f $name-user-data-credential.yaml ]; then
204             rm -rf $name-user-data-credential.yaml
205         fi
206
207         if [ -f $name-userdata.yaml ]; then
208             rm -rf $name-userdata.yaml
209         fi
210     done
211 }
212
213 function clean_all {
214     list_nodes | cleanup
215     if [ -f $IRONIC_DATA_DIR/nodes.json ]; then
216         rm -rf $IRONIC_DATA_DIR/nodes.json
217     fi
218 }
219
220 function apply_bm_hosts {
221     list_nodes | make_bm_hosts
222 }
223
224 function deprovision_all_hosts {
225     list_nodes | remove_bm_hosts
226 }
227
228 if [ "$1" == "deprovision" ]; then
229     configure_nodes
230     deprovision_all_hosts
231     exit 0
232 fi
233
234 if [ "$1" == "provision" ]; then
235     configure_nodes
236     apply_bm_hosts
237     exit 0
238 fi
239
240 if [ "$1" == "clean" ]; then
241     configure_nodes
242     clean_all
243     exit 0
244 fi
245
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"
250 exit 1