75719e33525bebeb9ec7e97b31081236a4733640
[icn.git] / deploy / metal3 / scripts / 01_metal3.sh
1 #!/bin/bash
2 set +ex
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_inteface_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_inteface_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 function create_userdata {
70     name="$1"
71     COMPUTE_NODE_FQDN="$name.akraino.icn.org"
72     printf "#cloud-config\n" >  $name-userdata.yaml
73     if [ -n "$COMPUTE_NODE_PASSWORD" ]; then
74     printf "password: ""%s" "$COMPUTE_NODE_PASSWORD" >>  $name-userdata.yaml
75     printf "\nchpasswd: {expire: False}\n" >>  $name-userdata.yaml
76     printf "ssh_pwauth: True\n" >>  $name-userdata.yaml
77     fi
78
79     if [ -n "$COMPUTE_NODE_FQDN" ]; then
80     printf "fqdn: ""%s" "$COMPUTE_NODE_FQDN" >>  $name-userdata.yaml
81     printf "\n" >>  $name-userdata.yaml
82     fi
83     printf "disable_root: false\n" >>  $name-userdata.yaml
84     printf "ssh_authorized_keys:\n  - " >>  $name-userdata.yaml
85
86     if [ ! -f $HOME/.ssh/id_rsa.pub ]; then
87     yes y | ssh-keygen -t rsa -N "" -f $HOME/.ssh/id_rsa
88     fi
89
90     cat $HOME/.ssh/id_rsa.pub >>  $name-userdata.yaml
91     network_config_files >> $name-userdata.yaml
92     printf "\n" >>  $name-userdata.yaml
93 }
94
95 function launch_baremetal_operator {
96     if [ ! -d $GOPATH/src/github.com/metal3-io/baremetal-operator ]; then
97         go get github.com/metal3-io/baremetal-operator
98         git checkout 3d40caa29dce82878d83aeb7f8dab4dc4a856160
99     fi
100
101     pushd $GOPATH/src/github.com/metal3-io/baremetal-operator
102     docker pull quay.io/metal3-io/baremetal-operator:master
103     make deploy
104     popd
105 }
106
107 function remove_baremetal_operator {
108     if [ ! -d $GOPATH/src/github.com/metal3-io/baremetal-operator ]; then
109         go get github.com/metal3-io/baremetal-operator
110         git checkout 3d40caa29dce82878d83aeb7f8dab4dc4a856160
111     fi
112
113     pushd $GOPATH/src/github.com/metal3-io/baremetal-operator
114         kubectl delete -f deploy/operator.yaml -n metal3
115         kubectl delete -f deploy/crds/metal3_v1alpha1_baremetalhost_crd.yaml
116         kubectl delete -f deploy/role_binding.yaml
117         kubectl delete -f deploy/role.yaml -n metal3
118         kubectl delete -f deploy/service_account.yaml -n metal3
119         kubectl delete ns metal3
120         docker rmi quay.io/metal3-io/baremetal-operator:master
121     popd
122 }
123
124 function network_config_files {
125     cat << 'EOF'
126 write_files:
127 - path: /opt/ironic_net.sh
128   owner: root:root
129   permissions: '0777'
130   content: |
131     #!/usr/bin/env bash
132     set -xe
133     for intf in /sys/class/net/*; do
134         sudo ifconfig `basename $intf` up
135         sudo dhclient -nw `basename $intf`
136     done
137 EOF
138 cat << EOF
139 - path: /opt/user_net.sh
140   owner: root:root
141   permissions: '0777'
142   content: |
143     #!/usr/bin/env bash
144     set -xe
145     route add default gw $PROVIDER_NETWORK_GATEWAY
146     sed -i -e 's/^#DNS=.*/DNS=$PROVIDER_NETWORK_DNS/g' /etc/systemd/resolved.conf
147     systemctl daemon-reload
148     systemctl restart systemd-resolved
149 runcmd:
150  - [ /opt/ironic_net.sh ]
151  - [ /opt/user_net.sh ]
152 EOF
153 }
154
155 function apply_userdata_credential {
156     name="$1"
157     cat <<EOF > ./$name-user-data-credential.yaml
158 apiVersion: v1
159 data:
160   userData: $(base64 -w 0 $name-userdata.yaml)
161 kind: Secret
162 metadata:
163   name: $name-user-data
164   namespace: metal3
165 type: Opaque
166 EOF
167     kubectl apply -n metal3 -f $name-user-data-credential.yaml
168 }
169
170 function make_bm_hosts {
171     while read -r name username password address; do
172         create_userdata $name
173         apply_userdata_credential $name
174
175         go run $GOPATH/src/github.com/metal3-io/baremetal-operator/cmd/make-bm-worker/main.go \
176            -address "ipmi://$address" \
177            -password "$password" \
178            -user "$username" \
179            "$name" > $name-bm-node.yaml
180
181         printf "  image:" >> $name-bm-node.yaml
182         printf "\n    url: ""%s" "$IMAGE_URL" >> $name-bm-node.yaml
183         printf "\n    checksum: ""%s" "$IMAGE_CHECKSUM" >> $name-bm-node.yaml
184         printf "\n  userData:" >> $name-bm-node.yaml
185         printf "\n    name: ""%s" "$name""-user-data" >> $name-bm-node.yaml
186         printf "\n    namespace: metal3\n" >> $name-bm-node.yaml
187         kubectl apply -f $name-bm-node.yaml -n metal3
188     done
189 }
190
191 function configure_nodes {
192     if [ ! -d $IRONIC_DATA_DIR ]; then
193         mkdir -p $IRONIC_DATA_DIR
194     fi
195
196     #make sure nodes.json file in /opt/ironic/ are configured
197     if [ ! -f $IRONIC_DATA_DIR/nodes.json ]; then
198         cp $PWD/nodes.json.sample $IRONIC_DATA_DIR/nodes.json
199     fi
200 }
201
202 function remove_bm_hosts {
203     while read -r name username password address; do
204         deprovision_compute_node $name
205     done
206 }
207
208 function cleanup {
209     while read -r name username password address; do
210         kubectl delete bmh $name -n metal3
211         kubectl delete secrets $name-bmc-secret -n metal3
212         kubectl delete secrets $name-user-data -n metal3
213     done
214 }
215
216 function clean_all {
217     list_nodes | cleanup
218 }
219
220 function apply_bm_hosts {
221     list_nodes | make_bm_hosts
222 }
223
224 function deprovision_all_hosts {
225     list_nodes | remove_bm_hosts
226 }
227
228 if [ "$1" == "launch" ]; then
229     launch_baremetal_operator
230     exit 0
231 fi
232
233 if [ "$1" == "deprovision" ]; then
234     configure_nodes
235     deprovision_all_hosts
236     exit 0
237 fi
238
239 if [ "$1" == "provision" ]; then
240     configure_nodes
241     apply_bm_hosts
242     exit 0
243 fi
244
245 if [ "$1" == "clean" ]; then
246     configure_nodes
247     clean_all
248     exit 0
249 fi
250
251 if [ "$1" == "remove" ]; then
252     remove_baremetal_operator
253     exit 0
254 fi
255
256 echo "Usage: metal3.sh"
257 echo "launch      - Launch the metal3 operator"
258 echo "provision   - provision baremetal node as specified in common.sh"
259 echo "deprovision - deprovision baremetal node as specified in common.sh"
260 echo "clean       - clean all the bmh resources"
261 echo "remove      - remove baremetal operator"
262 exit 1
263
264 #Following code is tested for the offline mode
265 #Will be intergrated for the offline mode for ICNi v.0.1.0 beta
266 #create_ssh_key
267 #create_userdata
268 #set_compute_key
269 #set_compute_ssh_config