f268d8bd0e6d3fb3719482a3c3f0b50ef76d9e2e
[icn.git] / deploy / metal3 / scripts / metal3.sh
1 #!/bin/bash
2 set -ex
3
4 LIBDIR="$(dirname "$(dirname "$(dirname "$PWD")")")"
5
6 eval "$(go env)"
7
8 BM_OPERATOR="${BM_OPERATOR:-https://github.com/metal3-io/baremetal-operator.git}"
9
10 source $LIBDIR/env/lib/common.sh
11
12 if [[ $EUID -ne 0 ]]; then
13     echo "This script must be run as root"
14     exit 1
15 fi
16
17 function get_default_inteface_ipaddress() {
18     local _ip=$1
19     local _default_interface=$(awk '$2 == 00000000 { print $1 }' /proc/net/route)
20     local _ipv4address=$(ip addr show dev $_default_interface | awk '$1 == "inet" { sub("/.*", "", $2); print $2 }')
21     eval $_ip="'$_ipv4address'"
22 }
23
24 create_ssh_key() {
25         #ssh key for compute node to communicate back to bootstrap server
26         mkdir -p $BUILD_DIR/ssh_key
27         ssh-keygen -C "compute.icn.akraino.lfedge.org" -f $BUILD_DIR/ssh_key/id_rsa
28         cat $BUILD_DIR/ssh_key/id_rsa.pub >> $HOME/.ssh/authorized_keys
29 }
30
31 set_compute_key() {
32 _SSH_LOCAL_KEY=$(cat $BUILD_DIR/ssh_key/id_rsa)
33 cat << EOF
34 write_files:
35 - path: /opt/ssh_id_rsa
36   owner: root:root
37   permissions: '0600'
38   content: |
39     $_SSH_LOCAL_KEY
40 EOF
41 }
42
43 provision_compute_node() {
44         IMAGE_URL=http://172.22.0.1/images/${BM_IMAGE}
45         IMAGE_CHECKSUM=http://172.22.0.1/images/${BM_IMAGE}.md5sum
46
47         if [ ! -d $GOPATH/src/github.com/metal3-io/baremetal-operator ]; then
48                 go get github.com/metal3-io/baremetal-operator
49         fi
50
51         go run $GOPATH/src/github.com/metal3-io/baremetal-operator/cmd/make-bm-worker/main.go \
52            -address "ipmi://$COMPUTE_IPMI_ADDRESS" \
53                    -user "$COMPUTE_IPMI_USER" \
54            -password "$COMPUTE_IPMI_PASSWORD" \
55            "$COMPUTE_NODE_NAME" > $COMPUTE_NODE_NAME-bm-node.yaml
56
57         printf "  image:" >> $COMPUTE_NODE_NAME-bm-node.yaml
58         printf "\n    url: ""%s" "$IMAGE_URL" >> $COMPUTE_NODE_NAME-bm-node.yaml
59         printf "\n    checksum: ""%s" "$IMAGE_CHECKSUM" >> $COMPUTE_NODE_NAME-bm-node.yaml
60         printf "\n  userData:" >> $COMPUTE_NODE_NAME-bm-node.yaml
61         printf "\n    name: ""%s" "$COMPUTE_NODE_NAME""-user-data" >> $COMPUTE_NODE_NAME-bm-node.yaml
62         printf "\n    namespace: metal3\n" >> $COMPUTE_NODE_NAME-bm-node.yaml
63         kubectl apply -f $COMPUTE_NODE_NAME-bm-node.yaml
64 }
65
66 deprovision_compute_node() {
67         kubectl patch baremetalhost $COMPUTE_NODE_NAME -n metal3 --type merge \
68     -p '{"spec":{"image":{"url":"","checksum":""}}}'
69 }
70
71 set_compute_ssh_config() {
72 get_default_inteface_ipaddress default_addr
73 cat << EOF
74 - path: /root/.ssh/config
75   owner: root:root
76   permissions: '0600'
77   content: |
78     Host bootstrapmachine $default_addr
79     HostName $default_addr
80     IdentityFile /opt/ssh_id_rsa
81     User $USER
82 - path: /etc/apt/sources.list
83   owner: root:root
84   permissions: '0665'
85   content: |
86         deb [trusted=yes] ssh://$USER@$default_addr:$LOCAL_APT_REPO ./
87 EOF
88 }
89
90 create_userdata() {
91         printf "#cloud-config\n" > userdata.yaml
92         if [ -n "$COMPUTE_NODE_PASSWORD" ]; then
93                 printf "password: ""%s" "$COMPUTE_NODE_PASSWORD" >> userdata.yaml
94                 printf "\nchpasswd: {expire: False}\n" >> userdata.yaml
95                 printf "ssh_pwauth: True\n" >> userdata.yaml
96         fi
97
98         if [ -n "$COMPUTE_NODE_FQDN" ]; then
99                 printf "fqdn: ""%s" "$COMPUTE_NODE_FQDN" >> userdata.yaml
100                 printf "\n" >> userdata.yaml
101         fi
102
103         printf "ssh_authorized_keys:\n  - " >> userdata.yaml
104
105         if [ -f $HOME/.ssh/id_rsa.pub ]; then
106                 yes y | ssh-keygen -t rsa -N "" -f $HOME/.ssh/id_rsa
107         fi
108
109         cat $HOME/.ssh/id_rsa.pub >> userdata.yaml
110         printf "\n" >> userdata.yaml
111 }
112
113 apply_userdata_credential() {
114         cat <<EOF > ./$COMPUTE_NODE_NAME-user-data.yaml
115 apiVersion: v1
116 data:
117   userData: $(base64 -w 0 userdata.yaml)
118 kind: Secret
119 metadata:
120   name: $COMPUTE_NODE_NAME-user-data
121   namespace: metal3
122 type: Opaque
123 EOF
124         kubectl apply -n metal3 -f $COMPUTE_NODE_NAME-user-data.yaml
125 }
126
127 launch_baremetal_operator() {
128         if [ ! -d $GOPATH/src/github.com/metal3-io/baremetal-operator ]; then
129         go get github.com/metal3-io/baremetal-operator
130     fi
131
132         pushd $GOPATH/src/github.com/metal3-io/baremetal-operator
133                 make deploy
134         popd
135                 
136 }
137
138 if [ "$1" == "launch" ]; then
139     launch_baremetal_operator
140     exit 0
141 fi
142
143 if [ "$1" == "deprovision" ]; then
144     deprovision_compute_node
145     exit 0
146 fi
147
148 if [ "$1" == "provision" ]; then
149     create_userdata
150         apply_userdata_credential
151         provision_compute_node
152     exit 0
153 fi
154
155
156 echo "Usage: metal3.sh"
157 echo "launch      - Launch the metal3 operator"
158 echo "provision   - provision baremetal node as specified in common.sh"
159 echo "deprovision - deprovision baremetal node as specified in common.sh"
160 exit 1
161
162 #Following code is tested for the offline mode
163 #Will be intergrated for the offline mode for ICNi v.0.1.0 beta
164 #create_ssh_key
165 #create_userdata
166 #set_compute_key
167 #set_compute_ssh_config