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