Capture creation of BMH resources into Helm chart
[icn.git] / deploy / metal3 / scripts / 01_metal3.sh
1 #!/usr/bin/env bash
2 set -eu -o pipefail
3
4 SCRIPTDIR="$(readlink -f $(dirname ${BASH_SOURCE[0]}))"
5 LIBDIR="$(dirname $(dirname $(dirname ${SCRIPTDIR})))/env/lib"
6
7 eval "$(go env)"
8
9 source $LIBDIR/common.sh
10
11 if [[ $EUID -ne 0 ]]; then
12     echo "This script must be run as root"
13     exit 1
14 fi
15
16 function deprovision_compute_node {
17     name="$1"
18     if kubectl get baremetalhost $name -n metal3 &>/dev/null; then
19         kubectl patch baremetalhost $name -n metal3 --type merge \
20         -p '{"spec":{"image":{"url":"","checksum":""}}}'
21     fi
22 }
23
24 function create_userdata {
25     name="$1"
26     username="$2"
27     password="$3"
28     COMPUTE_NODE_FQDN="$name.akraino.icn.org"
29
30     # validate that the user isn't expecting the deprecated
31     # COMPUTE_NODE_PASSWORD to be used
32     if [ "$password" != "${COMPUTE_NODE_PASSWORD:-$password}" ]; then
33         cat <<EOF
34 COMPUTE_NODE_PASSWORD "$COMPUTE_NODE_PASSWORD" not equal to nodes.json $name password "$password".
35 Unset COMPUTE_NODE_PASSWORD and retry.
36 EOF
37         exit 1
38     fi
39
40     printf "    userData:\n" >>${SCRIPTDIR}/machines-values.yaml
41     if [ -n "$username" ]; then
42         printf "      name: ${username}\n" >>${SCRIPTDIR}/machines-values.yaml
43     fi
44     if [ -n "$password" ]; then
45         passwd=$(mkpasswd --method=SHA-512 --rounds 4096 "$password")
46         printf "      hashedPassword: ${passwd}\n" >>${SCRIPTDIR}/machines-values.yaml
47     fi
48
49     if [ -n "$COMPUTE_NODE_FQDN" ]; then
50         printf "      fqdn: ${COMPUTE_NODE_FQDN}\n" >>${SCRIPTDIR}/machines-values.yaml
51     fi
52
53     if [ ! -f $HOME/.ssh/id_rsa.pub ]; then
54         yes y | ssh-keygen -t rsa -N "" -f $HOME/.ssh/id_rsa
55     fi
56
57     printf "      sshAuthorizedKey: $(cat $HOME/.ssh/id_rsa.pub)\n" >>${SCRIPTDIR}/machines-values.yaml
58 }
59
60 create_networkdata() {
61     name="$1"
62     node_networkdata $name >>${SCRIPTDIR}/machines-values.yaml
63 }
64
65 function make_bm_hosts {
66     while IFS=',' read -r name ipmi_username ipmi_password ipmi_address boot_mac os_username os_password os_image_name; do
67         printf "  ${name}:\n" >>${SCRIPTDIR}/machines-values.yaml
68         printf "    bmcUsername: ${ipmi_username}\n" >>${SCRIPTDIR}/machines-values.yaml
69         printf "    bmcPassword: ${ipmi_password}\n" >>${SCRIPTDIR}/machines-values.yaml
70         printf "    bmcAddress: ipmi://${ipmi_address}\n" >>${SCRIPTDIR}/machines-values.yaml
71         if [[ ! -z ${boot_mac} ]]; then
72             printf "    bootMACAddress: ${boot_mac}\n" >>${SCRIPTDIR}/machines-values.yaml
73         fi
74         printf "    imageName: ${BM_IMAGE}\n" >>${SCRIPTDIR}/machines-values.yaml
75         create_userdata $name $os_username $os_password
76         create_networkdata $name
77     done
78 }
79
80 function configure_nodes {
81     if [ ! -d $IRONIC_DATA_DIR ]; then
82         mkdir -p $IRONIC_DATA_DIR
83     fi
84
85     #make sure nodes.json file in /opt/ironic/ are configured
86     if [ ! -f $IRONIC_DATA_DIR/nodes.json ]; then
87         cp ${SCRIPTDIR}/nodes.json.sample $IRONIC_DATA_DIR/nodes.json
88     fi
89 }
90
91 function deprovision_bm_hosts {
92     while IFS=',' read -r name ipmi_username ipmi_password ipmi_address boot_mac os_username os_password os_image_name; do
93         deprovision_compute_node $name
94     done
95 }
96
97 function clean_all {
98     helm -n metal3 uninstall machines
99     rm -f ${SCRIPTDIR}/machines-values.yaml
100     if [ -f $IRONIC_DATA_DIR/nodes.json ]; then
101         rm -rf $IRONIC_DATA_DIR/nodes.json
102     fi
103 }
104
105 function apply_bm_hosts {
106     printf "machines:\n" >${SCRIPTDIR}/machines-values.yaml
107     list_nodes | make_bm_hosts
108     helm -n metal3 install machines ${SCRIPTDIR}/../../machines --create-namespace -f ${SCRIPTDIR}/machines-values.yaml
109 }
110
111 function deprovision_all_hosts {
112     list_nodes | deprovision_bm_hosts
113 }
114
115 if [ "$1" == "deprovision" ]; then
116     configure_nodes
117     deprovision_all_hosts
118     exit 0
119 fi
120
121 if [ "$1" == "provision" ]; then
122     configure_nodes
123     apply_bm_hosts
124     exit 0
125 fi
126
127 if [ "$1" == "clean" ]; then
128     configure_nodes
129     clean_all
130     exit 0
131 fi
132
133 echo "Usage: metal3.sh"
134 echo "provision   - provision baremetal node as specified in common.sh"
135 echo "deprovision - deprovision baremetal node as specified in common.sh"
136 echo "clean       - clean all the bmh resources"
137 exit 1