Use username and password from "os" in nodes.json
[icn.git] / deploy / metal3-vm / 03_launch_mgmt_cluster.sh
1 #!/usr/bin/env bash
2 set -eux -o pipefail
3
4 # shellcheck disable=SC1091
5 source lib/logging.sh
6 # shellcheck disable=SC1091
7 source lib/common.sh
8
9 eval "$(go env)"
10 export GOPATH
11 DEPLOYDIR="$(dirname "$PWD")"
12 BMODIR=$DEPLOYDIR/metal3/scripts/bmo
13
14 # Environment variables
15 # M3PATH : Path to clone the metal3 dev env repo
16 # BMOPATH : Path to clone the baremetal operator repo
17 #
18 # BMOREPO : Baremetal operator repository URL
19 # BMOBRANCH : Baremetal operator repository branch to checkout
20 # FORCE_REPO_UPDATE : discard existing directories
21 #
22 # BMO_RUN_LOCAL : run the baremetal operator locally (not in Kubernetes cluster)
23
24 M3PATH="${GOPATH}/src/github.com/metal3-io"
25 BMOPATH="${M3PATH}/baremetal-operator"
26
27 BMOREPO="${BMOREPO:-https://github.com/metal3-io/baremetal-operator.git}"
28 BMOBRANCH="${BMOBRANCH:-10eb5aa3e614d0fdc6315026ebab061cbae6b929}"
29 FORCE_REPO_UPDATE="${FORCE_REPO_UPDATE:-true}"
30
31 BMO_RUN_LOCAL="${BMO_RUN_LOCAL:-false}"
32 COMPUTE_NODE_PASSWORD="${COMPUTE_NODE_PASSWORD:-mypasswd}"
33 BM_IMAGE=${BM_IMAGE:-"bionic-server-cloudimg-amd64.img"}
34 IMAGE_URL=http://172.22.0.1/images/${BM_IMAGE}
35 IMAGE_CHECKSUM=http://172.22.0.1/images/${BM_IMAGE}.md5sum
36
37 function clone_repos {
38     mkdir -p "${M3PATH}"
39     if [[ -d ${BMOPATH} && "${FORCE_REPO_UPDATE}" == "true" ]]; then
40       rm -rf "${BMOPATH}"
41     fi
42     if [ ! -d "${BMOPATH}" ] ; then
43         pushd "${M3PATH}"
44         git clone "${BMOREPO}"
45         popd
46     fi
47     pushd "${BMOPATH}"
48     git checkout "${BMOBRANCH}"
49     git pull -r || true
50     popd
51 }
52
53 function launch_baremetal_operator {
54     docker pull integratedcloudnative/baremetal-operator:v1.0-icn
55     kubectl apply -f $BMODIR/namespace/namespace.yaml
56     kubectl apply -f $BMODIR/rbac/service_account.yaml -n metal3
57     kubectl apply -f $BMODIR/rbac/role.yaml -n metal3
58     kubectl apply -f $BMODIR/rbac/role_binding.yaml
59     kubectl apply -f $BMODIR/crds/metal3.io_baremetalhosts_crd.yaml
60     kubectl apply -f $BMODIR/operator/no_ironic/operator.yaml -n metal3
61 }
62
63 network_config_files() {
64 cat << 'EOF'
65 write_files:
66 - path: /opt/ironic_net.sh
67   owner: root:root
68   permissions: '0777'
69   content: |
70     #!/usr/bin/env bash
71     set -xe
72     for intf in /sys/class/net/*; do
73         sudo ifconfig `basename $intf` up
74         sudo dhclient -nw `basename $intf`
75     done
76 runcmd:
77  - [ /opt/ironic_net.sh ]
78 EOF
79 }
80
81 # documentation for the values below may be found at
82 # https://cloudinit.readthedocs.io/en/latest/topics/modules.html
83 create_userdata() {
84     name="$1"
85     COMPUTE_NODE_FQDN="$name.akraino.icn.org"
86     printf "#cloud-config\n" > $name-userdata.yaml
87     if [ -n "$COMPUTE_NODE_PASSWORD" ]; then
88         printf "password: ""%s" "$COMPUTE_NODE_PASSWORD" >>  $name-userdata.yaml
89         printf "\nchpasswd: {expire: False}\n" >>  $name-userdata.yaml
90         printf "ssh_pwauth: True\n" >>  $name-userdata.yaml
91     fi
92
93     if [ -n "$COMPUTE_NODE_FQDN" ]; then
94         printf "fqdn: ""%s" "$COMPUTE_NODE_FQDN" >>  $name-userdata.yaml
95         printf "\n" >>  $name-userdata.yaml
96     fi
97     printf "disable_root: false\n" >> $name-userdata.yaml
98     printf "ssh_authorized_keys:\n  - " >> $name-userdata.yaml
99
100     if [ ! -f $HOME/.ssh/id_rsa.pub ]; then
101         yes y | ssh-keygen -t rsa -N "" -f $HOME/.ssh/id_rsa
102     fi
103
104     cat $HOME/.ssh/id_rsa.pub >> $name-userdata.yaml
105     network_config_files >> $name-userdata.yaml
106     printf "\n" >> $name-userdata.yaml
107 }
108
109 apply_userdata_credential() {
110     name="$1"
111     cat <<EOF > ./$name-user-data-credential.yaml
112 apiVersion: v1
113 data:
114   userData: $(base64 -w 0 $name-userdata.yaml)
115 kind: Secret
116 metadata:
117   name: $name-user-data
118   namespace: metal3
119 type: Opaque
120 EOF
121     kubectl apply -n metal3 -f $name-user-data-credential.yaml
122 }
123
124 function make_bm_hosts {
125     while IFS=',' read -r name address user password mac; do
126         create_userdata $name
127         apply_userdata_credential $name
128         go run "${BMOPATH}"/cmd/make-bm-worker/main.go \
129            -address "$address" \
130            -password "$password" \
131            -user "$user" \
132            -boot-mac "$mac" \
133            "$name" > $name-bm-node.yaml
134         printf "  image:" >> $name-bm-node.yaml
135         printf "\n    url: ""%s" "${IMAGE_URL}" >> $name-bm-node.yaml
136         printf "\n    checksum: ""%s" "${IMAGE_CHECKSUM}" >> $name-bm-node.yaml
137         printf "\n  userData:" >> $name-bm-node.yaml
138         printf "\n    name: ""%s" "$name""-user-data" >> $name-bm-node.yaml
139         printf "\n    namespace: metal3\n" >> $name-bm-node.yaml
140         kubectl apply -f $name-bm-node.yaml -n metal3
141     done
142 }
143
144 function apply_bm_hosts {
145     list_nodes | make_bm_hosts
146 }
147
148 clone_repos
149 launch_baremetal_operator
150 apply_bm_hosts