Merge "Use newer bluval images"
[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 make_bm_hosts {
25     while IFS=',' read -r name ipmi_username ipmi_password ipmi_address boot_mac os_username os_password os_image_name; do
26         node_machine_values >${SCRIPTDIR}/${name}-values.yaml
27         helm -n metal3 install ${name} ${SCRIPTDIR}/../../machine --create-namespace -f ${SCRIPTDIR}/${name}-values.yaml
28
29     done
30 }
31
32 function configure_nodes {
33     if [ ! -d $IRONIC_DATA_DIR ]; then
34         mkdir -p $IRONIC_DATA_DIR
35     fi
36
37     #make sure nodes.json file in /opt/ironic/ are configured
38     if [ ! -f $NODES_FILE ]; then
39         cp ${SCRIPTDIR}/nodes.json.sample $NODES_FILE
40     fi
41 }
42
43 function deprovision_bm_hosts {
44     while IFS=',' read -r name ipmi_username ipmi_password ipmi_address boot_mac os_username os_password os_image_name; do
45         deprovision_compute_node $name
46     done
47 }
48
49 function clean_bm_hosts {
50     while IFS=',' read -r name ipmi_username ipmi_password ipmi_address boot_mac os_username os_password os_image_name; do
51         helm -n metal3 uninstall ${name}
52         rm -rf ${SCRIPTDIR}/${name}-values.yaml
53     done
54 }
55
56 function clean_all {
57     list_nodes | clean_bm_hosts
58     if [ -f $NODES_FILE ]; then
59         rm -rf $NODES_FILE
60     fi
61 }
62
63 function apply_bm_hosts {
64     list_nodes | make_bm_hosts
65 }
66
67 function deprovision_all_hosts {
68     list_nodes | deprovision_bm_hosts
69 }
70
71 if [ "$1" == "deprovision" ]; then
72     configure_nodes
73     deprovision_all_hosts
74     exit 0
75 fi
76
77 if [ "$1" == "provision" ]; then
78     configure_nodes
79     apply_bm_hosts
80     exit 0
81 fi
82
83 if [ "$1" == "clean" ]; then
84     configure_nodes
85     clean_all
86     exit 0
87 fi
88
89 echo "Usage: metal3.sh"
90 echo "provision   - provision baremetal node as specified in common.sh"
91 echo "deprovision - deprovision baremetal node as specified in common.sh"
92 echo "clean       - clean all the bmh resources"
93 exit 1