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