Fix go error in package_prerequisite step
[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 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            "$name" > $name-bm-node.yaml
198
199         printf "  image:" >> $name-bm-node.yaml
200         printf "\n    url: ""%s" "$IMAGE_URL" >> $name-bm-node.yaml
201         printf "\n    checksum: ""%s" "$IMAGE_CHECKSUM" >> $name-bm-node.yaml
202         printf "\n  userData:" >> $name-bm-node.yaml
203         printf "\n    name: ""%s" "$name""-user-data" >> $name-bm-node.yaml
204         printf "\n    namespace: metal3" >> $name-bm-node.yaml
205         printf "\n  networkData:" >> $name-bm-node.yaml
206         printf "\n    name: ""%s" "$name""-network-data" >> $name-bm-node.yaml
207         printf "\n    namespace: metal3" >> $name-bm-node.yaml
208         printf "\n  rootDeviceHints:" >> $name-bm-node.yaml
209         printf "\n    minSizeGigabytes: 48\n" >> $name-bm-node.yaml
210         kubectl apply -f $name-bm-node.yaml -n metal3
211     done
212 }
213
214 function configure_nodes {
215     if [ ! -d $IRONIC_DATA_DIR ]; then
216         mkdir -p $IRONIC_DATA_DIR
217     fi
218
219     #make sure nodes.json file in /opt/ironic/ are configured
220     if [ ! -f $IRONIC_DATA_DIR/nodes.json ]; then
221         cp $PWD/nodes.json.sample $IRONIC_DATA_DIR/nodes.json
222     fi
223 }
224
225 function remove_bm_hosts {
226     while IFS=',' read -r name ipmi_username ipmi_password ipmi_address os_username os_password os_image_name; do
227         deprovision_compute_node $name
228     done
229 }
230
231 function cleanup {
232     while IFS=',' read -r name ipmi_username ipmi_password ipmi_address os_username os_password os_image_name; do
233         kubectl delete --ignore-not-found=true bmh $name -n metal3
234         kubectl delete --ignore-not-found=true secrets $name-bmc-secret -n metal3
235         kubectl delete --ignore-not-found=true secrets $name-user-data -n metal3
236         if [ -f $name-bm-node.yaml ]; then
237             rm -rf $name-bm-node.yaml
238         fi
239
240         if [ -f $name-user-data-credential.yaml ]; then
241             rm -rf $name-user-data-credential.yaml
242         fi
243
244         if [ -f $name-userdata.yaml ]; then
245             rm -rf $name-userdata.yaml
246         fi
247     done
248 }
249
250 function clean_all {
251     list_nodes | cleanup
252     if [ -f $IRONIC_DATA_DIR/nodes.json ]; then
253         rm -rf $IRONIC_DATA_DIR/nodes.json
254     fi
255 }
256
257 function apply_bm_hosts {
258     list_nodes | make_bm_hosts
259 }
260
261 function deprovision_all_hosts {
262     list_nodes | remove_bm_hosts
263 }
264
265 if [ "$1" == "launch" ]; then
266     clone_repos
267     launch_baremetal_operator
268     exit 0
269 fi
270
271 if [ "$1" == "deprovision" ]; then
272     configure_nodes
273     deprovision_all_hosts
274     exit 0
275 fi
276
277 if [ "$1" == "provision" ]; then
278     configure_nodes
279     apply_bm_hosts
280     exit 0
281 fi
282
283 if [ "$1" == "clean" ]; then
284     configure_nodes
285     clean_all
286     exit 0
287 fi
288
289 if [ "$1" == "remove" ]; then
290     remove_baremetal_operator
291     exit 0
292 fi
293
294 echo "Usage: metal3.sh"
295 echo "launch      - Launch the metal3 operator"
296 echo "provision   - provision baremetal node as specified in common.sh"
297 echo "deprovision - deprovision baremetal node as specified in common.sh"
298 echo "clean       - clean all the bmh resources"
299 echo "remove      - remove baremetal operator"
300 exit 1