Include boot MAC address when listing nodes
[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 #Path to clone the metal3 dev env repo
16 M3PATH="$(go env GOPATH)/src/github.com/metal3-io"
17 #Path to clone the baremetal operator repo
18 BMOPATH="${M3PATH}/baremetal-operator"
19
20 IMAGE_URL=http://172.22.0.1/images/${BM_IMAGE}
21 IMAGE_CHECKSUM=http://172.22.0.1/images/${BM_IMAGE}.md5sum
22
23 function clone_repos {
24     mkdir -p "${M3PATH}"
25     if [[ -d ${BMOPATH} && "${FORCE_REPO_UPDATE}" == "true" ]]; then
26       rm -rf "${BMOPATH}"
27     fi
28     if [ ! -d "${BMOPATH}" ] ; then
29         pushd "${M3PATH}"
30         git clone "${BMOREPO}"
31         popd
32     fi
33     pushd "${BMOPATH}"
34     git checkout "${BMOBRANCH}"
35     git pull -r || true
36     popd
37 }
38
39 function deprovision_compute_node {
40     name="$1"
41     if kubectl get baremetalhost $name -n metal3 &>/dev/null; then
42         kubectl patch baremetalhost $name -n metal3 --type merge \
43         -p '{"spec":{"image":{"url":"","checksum":""}}}'
44     fi
45 }
46
47 # documentation for the values below may be found at
48 # https://cloudinit.readthedocs.io/en/latest/topics/modules.html
49 function create_userdata {
50     name="$1"
51     username="$2"
52     password="$3"
53     COMPUTE_NODE_FQDN="$name.akraino.icn.org"
54
55     # validate that the user isn't expecting the deprecated
56     # COMPUTE_NODE_PASSWORD to be used
57     if [ "$password" != "${COMPUTE_NODE_PASSWORD:-$password}" ]; then
58         cat <<EOF
59 COMPUTE_NODE_PASSWORD "$COMPUTE_NODE_PASSWORD" not equal to nodes.json $name password "$password".
60 Unset COMPUTE_NODE_PASSWORD and retry.
61 EOF
62         exit 1
63     fi
64
65     printf "#cloud-config\n" >  $name-userdata.yaml
66     if [ -n "$password" ]; then
67         if [ -n "$username" ]; then
68             passwd=$(mkpasswd --method=SHA-512 --rounds 4096 "$password")
69             printf "users:" >>  $name-userdata.yaml
70             printf "\n  - name: ""%s" "$username" >>  $name-userdata.yaml
71             printf "\n    lock_passwd: False" >>  $name-userdata.yaml # necessary to allow password login
72             printf "\n    passwd: ""%s" "$passwd" >>  $name-userdata.yaml
73             printf "\n    sudo: \"ALL=(ALL) NOPASSWD:ALL\"" >>  $name-userdata.yaml
74         else
75             printf "password: ""%s" "$password" >>  $name-userdata.yaml
76         fi
77         printf "\nchpasswd: {expire: False}\n" >>  $name-userdata.yaml
78         printf "ssh_pwauth: True\n" >>  $name-userdata.yaml
79     fi
80
81     if [ -n "$COMPUTE_NODE_FQDN" ]; then
82         printf "fqdn: ""%s" "$COMPUTE_NODE_FQDN" >>  $name-userdata.yaml
83         printf "\n" >>  $name-userdata.yaml
84     fi
85     printf "disable_root: false\n" >>  $name-userdata.yaml
86     printf "ssh_authorized_keys:\n  - " >>  $name-userdata.yaml
87
88     if [ ! -f $HOME/.ssh/id_rsa.pub ]; then
89         yes y | ssh-keygen -t rsa -N "" -f $HOME/.ssh/id_rsa
90     fi
91
92     cat $HOME/.ssh/id_rsa.pub >>  $name-userdata.yaml
93     cloud_init_scripts >> $name-userdata.yaml
94     printf "\n" >>  $name-userdata.yaml
95 }
96
97 create_networkdata() {
98     name="$1"
99     node_networkdata $name > $name-networkdata.json
100 }
101
102 function launch_baremetal_operator {
103     docker pull $IRONIC_BAREMETAL_IMAGE
104     kubectl apply -f bmo/namespace/namespace.yaml
105     kubectl apply -f bmo/rbac/service_account.yaml -n metal3
106     kubectl apply -f bmo/rbac/role.yaml -n metal3
107     kubectl apply -f bmo/rbac/role_binding.yaml
108     kubectl apply -f bmo/crds/metal3.io_baremetalhosts_crd.yaml
109     kubectl apply -f bmo/operator/no_ironic/operator.yaml -n metal3
110 }
111
112 function remove_baremetal_operator {
113     kubectl delete -f bmo/operator/no_ironic/operator.yaml -n metal3
114     kubectl delete -f bmo/crds/metal3.io_baremetalhosts_crd.yaml
115     kubectl delete -f bmo/rbac/role_binding.yaml
116     kubectl delete -f bmo/rbac/role.yaml -n metal3
117     kubectl delete -f bmo/rbac/service_account.yaml -n metal3
118     kubectl delete -f bmo/namespace/namespace.yaml
119 }
120
121 function cloud_init_scripts {
122     # set_dhcp_indentifier.sh:
123     #   The IP address assigned to the provisioning NIC will change
124     #   due to IPA using the MAC address as the client ID and systemd
125     #   using a different ID.  Tell systemd to use the MAC as the
126     #   client ID.  We can't do this in the network data as only the
127     #   JSON format is supported by metal3, and the JSON format does
128     #   not support the dhcp-identifier field.
129     # set_kernel_cmdline.sh:
130     #   The "intel_iommu=on iommu=pt" kernel command line is necessary
131     #   for QAT support.
132     cat << 'EOF'
133 write_files:
134 - path: /var/lib/cloud/scripts/per-instance/set_dhcp_identifier.sh
135   owner: root:root
136   permissions: '0777'
137   content: |
138     #!/usr/bin/env bash
139     set -eux -o pipefail
140     sed -i -e '/dhcp4: true$/!b' -e 'h;s/\S.*/dhcp-identifier: mac/;H;g' /etc/netplan/50-cloud-init.yaml
141     netplan apply
142 - path: /var/lib/cloud/scripts/per-instance/set_kernel_cmdline.sh
143   owner: root:root
144   permissions: '0777'
145   content: |
146     #!/usr/bin/env bash
147     set -eux -o pipefail
148     grub_file=${1:-"/etc/default/grub"}
149     kernel_parameters="intel_iommu=on iommu=pt"
150     sed -i~ "/^GRUB_CMDLINE_LINUX=/{h;s/\(=\".*\)\"/\1 ${kernel_parameters}\"/};\${x;/^$/{s//GRUB_CMDLINE_LINUX=\"${kernel_parameters}\"/;H};x}" "$grub_file"
151     update-grub
152     reboot
153 EOF
154 }
155
156 function apply_userdata_credential {
157     name="$1"
158     cat <<EOF > ./$name-user-data-credential.yaml
159 apiVersion: v1
160 data:
161   userData: $(base64 -w 0 $name-userdata.yaml)
162 kind: Secret
163 metadata:
164   name: $name-user-data
165   namespace: metal3
166 type: Opaque
167 EOF
168     kubectl apply -n metal3 -f $name-user-data-credential.yaml
169 }
170
171 apply_networkdata_credential() {
172     name="$1"
173     cat <<EOF > ./$name-network-data-credential.yaml
174 apiVersion: v1
175 data:
176   networkData: $(base64 -w 0 $name-networkdata.json)
177 kind: Secret
178 metadata:
179   name: $name-network-data
180   namespace: metal3
181 type: Opaque
182 EOF
183     kubectl apply -n metal3 -f $name-network-data-credential.yaml
184 }
185
186 function make_bm_hosts {
187     while IFS=',' read -r name ipmi_username ipmi_password ipmi_address boot_mac os_username os_password os_image_name; do
188         create_userdata $name $os_username $os_password
189         apply_userdata_credential $name
190         create_networkdata $name
191         apply_networkdata_credential $name
192
193         GO111MODULE=auto go run $GOPATH/src/github.com/metal3-io/baremetal-operator/cmd/make-bm-worker/main.go \
194            -address "ipmi://$ipmi_address" \
195            -password "$ipmi_password" \
196            -user "$ipmi_username" \
197            -boot-mac "$boot_mac" \
198            "$name" > $name-bm-node.yaml
199
200         printf "  image:" >> $name-bm-node.yaml
201         printf "\n    url: ""%s" "$IMAGE_URL" >> $name-bm-node.yaml
202         printf "\n    checksum: ""%s" "$IMAGE_CHECKSUM" >> $name-bm-node.yaml
203         printf "\n  userData:" >> $name-bm-node.yaml
204         printf "\n    name: ""%s" "$name""-user-data" >> $name-bm-node.yaml
205         printf "\n    namespace: metal3" >> $name-bm-node.yaml
206         printf "\n  networkData:" >> $name-bm-node.yaml
207         printf "\n    name: ""%s" "$name""-network-data" >> $name-bm-node.yaml
208         printf "\n    namespace: metal3" >> $name-bm-node.yaml
209         printf "\n  rootDeviceHints:" >> $name-bm-node.yaml
210         printf "\n    minSizeGigabytes: 48\n" >> $name-bm-node.yaml
211         kubectl apply -f $name-bm-node.yaml -n metal3
212     done
213 }
214
215 function configure_nodes {
216     if [ ! -d $IRONIC_DATA_DIR ]; then
217         mkdir -p $IRONIC_DATA_DIR
218     fi
219
220     #make sure nodes.json file in /opt/ironic/ are configured
221     if [ ! -f $IRONIC_DATA_DIR/nodes.json ]; then
222         cp $PWD/nodes.json.sample $IRONIC_DATA_DIR/nodes.json
223     fi
224 }
225
226 function remove_bm_hosts {
227     while IFS=',' read -r name ipmi_username ipmi_password ipmi_address boot_mac os_username os_password os_image_name; do
228         deprovision_compute_node $name
229     done
230 }
231
232 function cleanup {
233     while IFS=',' read -r name ipmi_username ipmi_password ipmi_address boot_mac os_username os_password os_image_name; do
234         kubectl delete --ignore-not-found=true bmh $name -n metal3
235         kubectl delete --ignore-not-found=true secrets $name-bmc-secret -n metal3
236         kubectl delete --ignore-not-found=true secrets $name-user-data -n metal3
237         if [ -f $name-bm-node.yaml ]; then
238             rm -rf $name-bm-node.yaml
239         fi
240
241         if [ -f $name-user-data-credential.yaml ]; then
242             rm -rf $name-user-data-credential.yaml
243         fi
244
245         if [ -f $name-userdata.yaml ]; then
246             rm -rf $name-userdata.yaml
247         fi
248     done
249 }
250
251 function clean_all {
252     list_nodes | cleanup
253     if [ -f $IRONIC_DATA_DIR/nodes.json ]; then
254         rm -rf $IRONIC_DATA_DIR/nodes.json
255     fi
256 }
257
258 function apply_bm_hosts {
259     list_nodes | make_bm_hosts
260 }
261
262 function deprovision_all_hosts {
263     list_nodes | remove_bm_hosts
264 }
265
266 if [ "$1" == "launch" ]; then
267     clone_repos
268     launch_baremetal_operator
269     exit 0
270 fi
271
272 if [ "$1" == "deprovision" ]; then
273     configure_nodes
274     deprovision_all_hosts
275     exit 0
276 fi
277
278 if [ "$1" == "provision" ]; then
279     configure_nodes
280     apply_bm_hosts
281     exit 0
282 fi
283
284 if [ "$1" == "clean" ]; then
285     configure_nodes
286     clean_all
287     exit 0
288 fi
289
290 if [ "$1" == "remove" ]; then
291     remove_baremetal_operator
292     exit 0
293 fi
294
295 echo "Usage: metal3.sh"
296 echo "launch      - Launch the metal3 operator"
297 echo "provision   - provision baremetal node as specified in common.sh"
298 echo "deprovision - deprovision baremetal node as specified in common.sh"
299 echo "clean       - clean all the bmh resources"
300 echo "remove      - remove baremetal operator"
301 exit 1