Use username and password from "os" in nodes.json
[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 get_default_interface_ipaddress {
19     local _ip=$1
20     local _default_interface=$(awk '$2 == 00000000 { print $1 }' /proc/net/route)
21     local _ipv4address=$(ip addr show dev $_default_interface | awk '$1 == "inet" { sub("/.*", "", $2); print $2 }')
22     eval $_ip="'$_ipv4address'"
23 }
24
25 function create_ssh_key {
26     #ssh key for compute node to communicate back to bootstrap server
27     mkdir -p $BUILD_DIR/ssh_key
28     ssh-keygen -C "compute.icn.akraino.lfedge.org" -f $BUILD_DIR/ssh_key/id_rsa
29     cat $BUILD_DIR/ssh_key/id_rsa.pub >> $HOME/.ssh/authorized_keys
30 }
31
32 function set_compute_key {
33     _SSH_LOCAL_KEY=$(cat $BUILD_DIR/ssh_key/id_rsa)
34     cat << EOF
35 write_files:
36 - path: /opt/ssh_id_rsa
37     owner: root:root
38     permissions: '0600'
39     content: |
40     $_SSH_LOCAL_KEY
41 EOF
42 }
43
44 function deprovision_compute_node {
45     name="$1"
46     kubectl patch baremetalhost $name -n metal3 --type merge \
47     -p '{"spec":{"image":{"url":"","checksum":""}}}'
48 }
49
50 function set_compute_ssh_config {
51     get_default_interface_ipaddress default_addr
52     cat << EOF
53 - path: /root/.ssh/config
54     owner: root:root
55     permissions: '0600'
56     content: |
57     Host bootstrapmachine $default_addr
58     HostName $default_addr
59     IdentityFile /opt/ssh_id_rsa
60     User $USER
61 - path: /etc/apt/sources.list
62     owner: root:root
63     permissions: '0665'
64     content: |
65     deb [trusted=yes] ssh://$USER@$default_addr:$LOCAL_APT_REPO ./
66 EOF
67 }
68
69 # documentation for the values below may be found at
70 # https://cloudinit.readthedocs.io/en/latest/topics/modules.html
71 function create_userdata {
72     name="$1"
73     username="$2"
74     password="$3"
75     COMPUTE_NODE_FQDN="$name.akraino.icn.org"
76
77     # validate that the user isn't expecting the deprecated
78     # COMPUTE_NODE_PASSWORD to be used
79     if [ "$password" != "${COMPUTE_NODE_PASSWORD:-$password}" ]; then
80         cat <<EOF
81 COMPUTE_NODE_PASSWORD "$COMPUTE_NODE_PASSWORD" not equal to nodes.json $name password "$password".
82 Unset COMPUTE_NODE_PASSWORD and retry.
83 EOF
84         exit 1
85     fi
86
87     printf "#cloud-config\n" >  $name-userdata.yaml
88     if [ -n "$password" ]; then
89         if [ -n "$username" ]; then
90             passwd=$(mkpasswd --method=SHA-512 --rounds 4096 "$password")
91             printf "users:" >>  $name-userdata.yaml
92             printf "\n  - name: ""%s" "$username" >>  $name-userdata.yaml
93             printf "\n    lock_passwd: False" >>  $name-userdata.yaml # necessary to allow password login
94             printf "\n    passwd: ""%s" "$passwd" >>  $name-userdata.yaml
95             printf "\n    sudo: \"ALL=(ALL) NOPASSWD:ALL\"" >>  $name-userdata.yaml
96         else
97             printf "password: ""%s" "$password" >>  $name-userdata.yaml
98         fi
99         printf "\nchpasswd: {expire: False}\n" >>  $name-userdata.yaml
100         printf "ssh_pwauth: True\n" >>  $name-userdata.yaml
101     fi
102
103     if [ -n "$COMPUTE_NODE_FQDN" ]; then
104         printf "fqdn: ""%s" "$COMPUTE_NODE_FQDN" >>  $name-userdata.yaml
105         printf "\n" >>  $name-userdata.yaml
106     fi
107     printf "disable_root: false\n" >>  $name-userdata.yaml
108     printf "ssh_authorized_keys:\n  - " >>  $name-userdata.yaml
109
110     if [ ! -f $HOME/.ssh/id_rsa.pub ]; then
111         yes y | ssh-keygen -t rsa -N "" -f $HOME/.ssh/id_rsa
112     fi
113
114     cat $HOME/.ssh/id_rsa.pub >>  $name-userdata.yaml
115     network_config_files >> $name-userdata.yaml
116     printf "\n" >>  $name-userdata.yaml
117 }
118
119 function launch_baremetal_operator {
120     docker pull integratedcloudnative/baremetal-operator:v1.0-icn
121     kubectl apply -f bmo/namespace/namespace.yaml
122     kubectl apply -f bmo/rbac/service_account.yaml -n metal3
123     kubectl apply -f bmo/rbac/role.yaml -n metal3
124     kubectl apply -f bmo/rbac/role_binding.yaml
125     kubectl apply -f bmo/crds/metal3.io_baremetalhosts_crd.yaml
126     kubectl apply -f bmo/operator/no_ironic/operator.yaml -n metal3
127 }
128
129 function remove_baremetal_operator {
130     kubectl delete -f bmo/operator/no_ironic/operator.yaml -n metal3
131     kubectl delete -f bmo/crds/metal3.io_baremetalhosts_crd.yaml
132     kubectl delete -f bmo/rbac/role_binding.yaml
133     kubectl delete -f bmo/rbac/role.yaml -n metal3
134     kubectl delete -f bmo/rbac/service_account.yaml -n metal3
135     kubectl delete -f bmo/namespace/namespace.yaml
136 }
137
138 function network_config_files {
139     cat << 'EOF'
140 write_files:
141 - path: /opt/ironic_net.sh
142   owner: root:root
143   permissions: '0777'
144   content: |
145     #!/usr/bin/env bash
146     set -xe
147     for intf in /sys/class/net/*; do
148         sudo ifconfig `basename $intf` up
149         sudo dhclient -nw `basename $intf`
150     done
151 EOF
152 cat << EOF
153 - path: /opt/user_net.sh
154   owner: root:root
155   permissions: '0777'
156   content: |
157     #!/usr/bin/env bash
158     set -xe
159     route add default gw $PROVIDER_NETWORK_GATEWAY
160     sed -i -e 's/^#DNS=.*/DNS=$PROVIDER_NETWORK_DNS/g' /etc/systemd/resolved.conf
161     systemctl daemon-reload
162     systemctl restart systemd-resolved
163 runcmd:
164  - [ /opt/ironic_net.sh ]
165  - [ /opt/user_net.sh ]
166 EOF
167 }
168
169 function apply_userdata_credential {
170     name="$1"
171     cat <<EOF > ./$name-user-data-credential.yaml
172 apiVersion: v1
173 data:
174   userData: $(base64 -w 0 $name-userdata.yaml)
175 kind: Secret
176 metadata:
177   name: $name-user-data
178   namespace: metal3
179 type: Opaque
180 EOF
181     kubectl apply -n metal3 -f $name-user-data-credential.yaml
182 }
183
184 function make_bm_hosts {
185     while IFS=',' read -r name ipmi_username ipmi_password ipmi_address os_username os_password os_image_name; do
186         create_userdata $name $os_username $os_password
187         apply_userdata_credential $name
188
189         go run $GOPATH/src/github.com/metal3-io/baremetal-operator/cmd/make-bm-worker/main.go \
190            -address "ipmi://$ipmi_address" \
191            -password "$ipmi_password" \
192            -user "$ipmi_username" \
193            "$name" > $name-bm-node.yaml
194
195         printf "  image:" >> $name-bm-node.yaml
196         printf "\n    url: ""%s" "$IMAGE_URL" >> $name-bm-node.yaml
197         printf "\n    checksum: ""%s" "$IMAGE_CHECKSUM" >> $name-bm-node.yaml
198         printf "\n  userData:" >> $name-bm-node.yaml
199         printf "\n    name: ""%s" "$name""-user-data" >> $name-bm-node.yaml
200         printf "\n    namespace: metal3\n" >> $name-bm-node.yaml
201         kubectl apply -f $name-bm-node.yaml -n metal3
202     done
203 }
204
205 function configure_nodes {
206     if [ ! -d $IRONIC_DATA_DIR ]; then
207         mkdir -p $IRONIC_DATA_DIR
208     fi
209
210     #make sure nodes.json file in /opt/ironic/ are configured
211     if [ ! -f $IRONIC_DATA_DIR/nodes.json ]; then
212         cp $PWD/nodes.json.sample $IRONIC_DATA_DIR/nodes.json
213     fi
214 }
215
216 function remove_bm_hosts {
217     while IFS=',' read -r name ipmi_username ipmi_password ipmi_address os_username os_password os_image_name; do
218         deprovision_compute_node $name
219     done
220 }
221
222 function cleanup {
223     while IFS=',' read -r name ipmi_username ipmi_password ipmi_address os_username os_password os_image_name; do
224         kubectl delete bmh $name -n metal3
225         kubectl delete secrets $name-bmc-secret -n metal3
226         kubectl delete secrets $name-user-data -n metal3
227         if [ -f $name-bm-node.yaml ]; then
228             rm -rf $name-bm-node.yaml
229         fi
230
231         if [ -f $name-user-data-credential.yaml ]; then
232             rm -rf $name-user-data-credential.yaml
233         fi
234
235         if [ -f $name-userdata.yaml ]; then
236             rm -rf $name-userdata.yaml
237         fi
238     done
239 }
240
241 function clean_all {
242     list_nodes | cleanup
243     if [ -f $IRONIC_DATA_DIR/nodes.json ]; then
244         rm -rf $IRONIC_DATA_DIR/nodes.json
245     fi
246 }
247
248 function apply_bm_hosts {
249     list_nodes | make_bm_hosts
250 }
251
252 function deprovision_all_hosts {
253     list_nodes | remove_bm_hosts
254 }
255
256 if [ "$1" == "launch" ]; then
257     launch_baremetal_operator
258     exit 0
259 fi
260
261 if [ "$1" == "deprovision" ]; then
262     configure_nodes
263     deprovision_all_hosts
264     exit 0
265 fi
266
267 if [ "$1" == "provision" ]; then
268     configure_nodes
269     apply_bm_hosts
270     exit 0
271 fi
272
273 if [ "$1" == "clean" ]; then
274     configure_nodes
275     clean_all
276     exit 0
277 fi
278
279 if [ "$1" == "remove" ]; then
280     remove_baremetal_operator
281     exit 0
282 fi
283
284 echo "Usage: metal3.sh"
285 echo "launch      - Launch the metal3 operator"
286 echo "provision   - provision baremetal node as specified in common.sh"
287 echo "deprovision - deprovision baremetal node as specified in common.sh"
288 echo "clean       - clean all the bmh resources"
289 echo "remove      - remove baremetal operator"
290 exit 1
291
292 #Following code is tested for the offline mode
293 #Will be intergrated for the offline mode for ICNi v.0.1.0 beta
294 #create_ssh_key
295 #create_userdata
296 #set_compute_key
297 #set_compute_ssh_config