9a8f07932f15ce3424b9e9192ddbcb86a88554ec
[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/images/${BM_IMAGE}
16 IMAGE_CHECKSUM=http://172.22.0.1/images/${BM_IMAGE}.md5sum
17
18 function clone_repos {
19     mkdir -p "${M3PATH}"
20     if [[ -d ${BMOPATH} && "${FORCE_REPO_UPDATE}" == "true" ]]; then
21       rm -rf "${BMOPATH}"
22     fi
23     if [ ! -d "${BMOPATH}" ] ; then
24         pushd "${M3PATH}"
25         git clone "${BMOREPO}"
26         popd
27     fi
28     pushd "${BMOPATH}"
29     git checkout "${BMOBRANCH}"
30     git pull -r || true
31     popd
32 }
33
34 function deprovision_compute_node {
35     name="$1"
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":""}}}'
39     fi
40 }
41
42 # documentation for the values below may be found at
43 # https://cloudinit.readthedocs.io/en/latest/topics/modules.html
44 function create_userdata {
45     name="$1"
46     username="$2"
47     password="$3"
48     COMPUTE_NODE_FQDN="$name.akraino.icn.org"
49
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
53         cat <<EOF
54 COMPUTE_NODE_PASSWORD "$COMPUTE_NODE_PASSWORD" not equal to nodes.json $name password "$password".
55 Unset COMPUTE_NODE_PASSWORD and retry.
56 EOF
57         exit 1
58     fi
59
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
69         else
70             printf "password: ""%s" "$password" >>  $name-userdata.yaml
71         fi
72         printf "\nchpasswd: {expire: False}\n" >>  $name-userdata.yaml
73         printf "ssh_pwauth: True\n" >>  $name-userdata.yaml
74     fi
75
76     if [ -n "$COMPUTE_NODE_FQDN" ]; then
77         printf "fqdn: ""%s" "$COMPUTE_NODE_FQDN" >>  $name-userdata.yaml
78         printf "\n" >>  $name-userdata.yaml
79     fi
80     printf "disable_root: false\n" >>  $name-userdata.yaml
81     printf "ssh_authorized_keys:\n  - " >>  $name-userdata.yaml
82
83     if [ ! -f $HOME/.ssh/id_rsa.pub ]; then
84         yes y | ssh-keygen -t rsa -N "" -f $HOME/.ssh/id_rsa
85     fi
86
87     cat $HOME/.ssh/id_rsa.pub >>  $name-userdata.yaml
88     cloud_init_scripts >> $name-userdata.yaml
89     printf "\n" >>  $name-userdata.yaml
90 }
91
92 create_networkdata() {
93     name="$1"
94     node_networkdata $name > $name-networkdata.json
95 }
96
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
105 }
106
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
114 }
115
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
126     #   for QAT support.
127     cat << 'EOF'
128 write_files:
129 - path: /var/lib/cloud/scripts/per-instance/set_dhcp_identifier.sh
130   owner: root:root
131   permissions: '0777'
132   content: |
133     #!/usr/bin/env bash
134     set -eux -o pipefail
135     sed -i -e '/dhcp4: true$/!b' -e 'h;s/\S.*/dhcp-identifier: mac/;H;g' /etc/netplan/50-cloud-init.yaml
136     netplan apply
137 - path: /var/lib/cloud/scripts/per-instance/set_kernel_cmdline.sh
138   owner: root:root
139   permissions: '0777'
140   content: |
141     #!/usr/bin/env bash
142     set -eux -o pipefail
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"
146     update-grub
147     reboot
148 EOF
149 }
150
151 function apply_userdata_credential {
152     name="$1"
153     cat <<EOF > ./$name-user-data-credential.yaml
154 apiVersion: v1
155 data:
156   userData: $(base64 -w 0 $name-userdata.yaml)
157 kind: Secret
158 metadata:
159   name: $name-user-data
160   namespace: metal3
161 type: Opaque
162 EOF
163     kubectl apply -n metal3 -f $name-user-data-credential.yaml
164 }
165
166 apply_networkdata_credential() {
167     name="$1"
168     cat <<EOF > ./$name-network-data-credential.yaml
169 apiVersion: v1
170 data:
171   networkData: $(base64 -w 0 $name-networkdata.json)
172 kind: Secret
173 metadata:
174   name: $name-network-data
175   namespace: metal3
176 type: Opaque
177 EOF
178     kubectl apply -n metal3 -f $name-network-data-credential.yaml
179 }
180
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
187
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
193
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
206     done
207 }
208
209 function configure_nodes {
210     if [ ! -d $IRONIC_DATA_DIR ]; then
211         mkdir -p $IRONIC_DATA_DIR
212     fi
213
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
217     fi
218 }
219
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
223     done
224 }
225
226 function cleanup {
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
233         fi
234
235         if [ -f $name-user-data-credential.yaml ]; then
236             rm -rf $name-user-data-credential.yaml
237         fi
238
239         if [ -f $name-userdata.yaml ]; then
240             rm -rf $name-userdata.yaml
241         fi
242     done
243 }
244
245 function clean_all {
246     list_nodes | cleanup
247     if [ -f $IRONIC_DATA_DIR/nodes.json ]; then
248         rm -rf $IRONIC_DATA_DIR/nodes.json
249     fi
250 }
251
252 function apply_bm_hosts {
253     list_nodes | make_bm_hosts
254 }
255
256 function deprovision_all_hosts {
257     list_nodes | remove_bm_hosts
258 }
259
260 if [ "$1" == "launch" ]; then
261     clone_repos
262     launch_baremetal_operator
263     exit 0
264 fi
265
266 if [ "$1" == "deprovision" ]; then
267     configure_nodes
268     deprovision_all_hosts
269     exit 0
270 fi
271
272 if [ "$1" == "provision" ]; then
273     configure_nodes
274     apply_bm_hosts
275     exit 0
276 fi
277
278 if [ "$1" == "clean" ]; then
279     configure_nodes
280     clean_all
281     exit 0
282 fi
283
284 if [ "$1" == "remove" ]; then
285     remove_baremetal_operator
286     exit 0
287 fi
288
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"
295 exit 1