inital Makefile struct with bm metal3
[icn.git] / env / metal3 / 03_launch_prereq.sh
1 #!/bin/bash
2 set -xe
3
4 LIBDIR="$(dirname "$PWD")"
5
6 source $LIBDIR/lib/logging.sh
7 source $LIBDIR/lib/common.sh
8
9 if [[ $EUID -ne 0 ]]; then
10     echo "launch script must be run as root"
11     exit 1
12 fi
13
14 function get_default_inteface_ipaddress() {
15         local _ip=$1
16         local _default_interface=$(awk '$2 == 00000000 { print $1 }' /proc/net/route)
17         local _ipv4address=$(ip addr show dev $_default_interface | awk '$1 == "inet" { sub("/.*", "", $2); print $2 }')
18         eval $_ip="'$_ipv4address'"
19 }
20
21
22
23 function check_cni_network() {
24         #since bootstrap cluster is a single node cluster,
25         #podman and bootstap cluster have same network configuration to avoid the cni network conf conflicts
26         if [ ! -d "/etc/cni/net.d" ]; then
27                 mkdir -p "/etc/cni/net.d"
28         fi
29
30         if [ ! -f "/etc/cni/net.d/87-podman-bridge.conflist" ]; then
31                 if [ "$1" == "offline" ]; then
32                         cp $BUILD_DIR/87-podman-bridge.conflist /etc/cni/net.d/
33                         return
34         fi
35
36                 if !(wget $PODMAN_CNI_CONFLIST -P /etc/cni/net.d/); then
37                         exit 1
38                 fi
39         fi
40 }
41
42 function create_k8s_regular_user() {
43         if [ ! -d "$HOME/.kube" ]; then
44                 mkdir -p $HOME/.kube
45         fi
46
47         if [ ! -f /etc/kubernetes/admin.conf]; then
48                 exit 1
49         fi
50
51         cp -rf /etc/kubernetes/admin.conf $HOME/.kube/config
52         chown $(id -u):$(id -g) $HOME/.kube/config
53 }
54
55 function check_k8s_node_status(){
56         echo 'checking bootstrap cluster single node status'
57         node_status="False"
58
59         for i in {1..5}
60                 do
61                         check_node=$(kubectl get node -o \
62                                                 jsonpath='{.items[0].status.conditions[?(@.reason == "KubeletReady")].status}')
63                         if [ $check_node != "" ]; then
64                                 node_status=${check_node}
65                         fi
66
67                         if [ $node_status == "True" ]; then
68                                 break
69                         fi
70
71                         sleep 3
72                 done
73
74         if [ $node_status != "True" ]; then
75                 echo "bootstrap cluster single node status is not ready"
76                 exit 1
77         fi
78 }
79
80 function install_podman() {
81         # set password for mariadb
82         mariadb_password=$(echo $(date;hostname)|sha256sum |cut -c-20)
83
84         # Create pod
85         podman pod create -n ironic-pod
86
87         # Start dnsmasq, http, mariadb, and ironic containers using same image
88         podman run -d --net host --privileged --name dnsmasq  --pod ironic-pod \
89                 -v $IRONIC_DATA_DIR:/shared --entrypoint /bin/rundnsmasq ${IRONIC_IMAGE}
90
91         podman run -d --net host --privileged --name httpd --pod ironic-pod \
92         -v $IRONIC_DATA_DIR:/shared --entrypoint /bin/runhttpd ${IRONIC_IMAGE}
93
94         podman run -d --net host --privileged --name mariadb --pod ironic-pod \
95         -v $IRONIC_DATA_DIR:/shared --entrypoint /bin/runmariadb \
96         --env MARIADB_PASSWORD=$mariadb_password ${IRONIC_IMAGE}
97
98         podman run -d --net host --privileged --name ironic --pod ironic-pod \
99         --env MARIADB_PASSWORD=$mariadb_password \
100         -v $IRONIC_DATA_DIR:/shared ${IRONIC_IMAGE}
101
102         # Start Ironic Inspector
103         podman run -d --net host --privileged --name ironic-inspector \
104                 --pod ironic-pod "${IRONIC_INSPECTOR_IMAGE}"
105 }
106
107 function remove_k8s_noschedule_taint() {
108         #Bootstrap cluster is a single node
109         nodename=$(kubectl get node -o jsonpath='{.items[0].metadata.name}')
110         if !(kubectl taint node $nodename node-role.kubernetes.io/master:NoSchedule-); then
111                 exit 1
112         fi
113 }
114
115 function install_k8s_single_node() {
116         get_default_inteface_ipaddress apiserver_advertise_addr
117         kubeadm_init="kubeadm init --kubernetes-version=$KUBE_VERSION \
118                                         --pod-network-cidr=$POD_NETWORK_CIDR \
119                                         --apiserver-advertise-address=$apiserver_advertise_addr"
120         if !(${kubeadm_init}); then
121                 exit 1
122         fi
123 }
124
125 function install() {
126         #install_kubernetes
127         install_k8s_single_node
128         check_cni_network $1
129         create_k8s_regular_user
130         check_k8s_node_status
131         remove_k8s_noschedule_taint
132
133         #install_podman
134         #Todo - error handling mechanism
135         install_podman
136 }
137
138 if [ "$1" == "-o" ]; then
139     install offline
140     exit 0
141 fi
142
143 install