0d81e5ae40c924eaea38e9be98610171d7d13448
[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 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         rm -rf /etc/cni/net.d/87-podman-bridge.conflist
30     fi
31
32     if [ "$1" == "offline" ]; then
33         cp $BUILD_DIR/87-podman-bridge.conflist /etc/cni/net.d/
34         return
35     fi
36
37     if !(wget $PODMAN_CNI_CONFLIST -P /etc/cni/net.d/); then
38         exit 1
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}; do
60         check_node=$(kubectl get node -o \
61             jsonpath='{.items[0].status.conditions[?(@.reason == "KubeletReady")].status}')
62         if [ $check_node != "" ]; then
63             node_status=${check_node}
64         fi
65
66         if [ $node_status == "True" ]; then
67             break
68         fi
69
70         sleep 3
71     done
72
73     if [ $node_status != "True" ]; then
74         echo "bootstrap cluster single node status is not ready"
75         exit 1
76     fi
77 }
78
79 function install_ironic_container {
80     # set password for mariadb
81     mariadb_password=$(echo $(date;hostname)|sha256sum |cut -c-20)
82
83     # Start image downloader container
84     docker run -d --net host --privileged --name ipa-downloader \
85         --env-file "${PWD}/ironic.env" \
86         -v "$IRONIC_DATA_DIR:/shared" "${IPA_DOWNLOADER_IMAGE}" /usr/local/bin/get-resource.sh
87
88     docker wait ipa-downloader
89
90     # Start dnsmasq, http, mariadb, and ironic containers using same image
91     # See this file for env vars you can set, like IP, DHCP_RANGE, INTERFACE
92     docker run -d --net host --privileged --name dnsmasq \
93         --env-file "${PWD}/ironic.env" \
94         -v "$IRONIC_DATA_DIR:/shared" --entrypoint /bin/rundnsmasq "${IRONIC_IMAGE}"
95
96     # For available env vars, see:
97     docker run -d --net host --privileged --name httpd \
98         --env-file "${PWD}/ironic.env" \
99         -v "$IRONIC_DATA_DIR:/shared" --entrypoint /bin/runhttpd "${IRONIC_IMAGE}"
100
101     # https://github.com/metal3-io/ironic/blob/master/runmariadb.sh
102     docker run -d --net host --privileged --name mariadb \
103         --env-file "${PWD}/ironic.env" \
104         -v "$IRONIC_DATA_DIR:/shared" --entrypoint /bin/runmariadb \
105         --env "MARIADB_PASSWORD=$mariadb_password" "${IRONIC_IMAGE}"
106
107     # See this file for additional env vars you may want to pass, like IP and INTERFACE
108     docker run -d --net host --privileged --name ironic \
109         --env-file "${PWD}/ironic.env" \
110         --env "MARIADB_PASSWORD=$mariadb_password" \
111         -v "$IRONIC_DATA_DIR:/shared" "${IRONIC_IMAGE}"
112
113     # Start Ironic Inspector
114     docker run -d --net host --privileged --name ironic-inspector \
115         --env-file "${PWD}/ironic.env" \
116         -v "$IRONIC_DATA_DIR:/shared" "${IRONIC_INSPECTOR_IMAGE}"
117 }
118
119 function remove_k8s_noschedule_taint {
120     #Bootstrap cluster is a single node
121     nodename=$(kubectl get node -o jsonpath='{.items[0].metadata.name}')
122     if !(kubectl taint node $nodename node-role.kubernetes.io/master:NoSchedule-); then
123         exit 1
124     fi
125 }
126
127 function install_k8s_single_node {
128     get_default_inteface_ipaddress apiserver_advertise_addr
129     kubeadm_init="kubeadm init --kubernetes-version=$KUBE_VERSION \
130         --pod-network-cidr=$POD_NETWORK_CIDR \
131         --apiserver-advertise-address=$apiserver_advertise_addr"
132     if !(${kubeadm_init}); then
133         exit 1
134     fi
135 }
136
137 function install_dhcp {
138     if [ ! -d $BS_DHCP_DIR ]; then
139         mkdir -p $BS_DHCP_DIR
140     fi
141
142     #make sure the dhcp conf sample are configured
143     if [ ! -f $BS_DHCP_DIR/dhcpd.conf ]; then
144         cp $PWD/05_dhcp.conf.sample $BS_DHCP_DIR/dhcpd.conf
145     fi
146
147     kubectl create -f $PWD/04_dhcp.yaml
148 }
149
150 function reset_dhcp {
151     kubectl delete -f $PWD/04_dhcp.yaml
152     if [ -d $BS_DHCP_DIR ]; then
153         rm -rf $BS_DHCP_DIR
154     fi
155 }
156
157 function create_ironic_env {
158     cat <<EOF > ${PWD}/ironic.env
159 PROVISIONING_INTERFACE=provisioning
160 DHCP_RANGE=172.22.0.10,172.22.0.100
161 DEPLOY_KERNEL_URL=http://172.22.0.1/images/ironic-python-agent.kernel
162 DEPLOY_RAMDISK_URL=http://172.22.0.1/images/ironic-python-agent.initramfs
163 IRONIC_ENDPOINT=http://172.22.0.1:6385/v1/
164 IRONIC_INSPECTOR_ENDPOINT=http://172.22.0.1:5050/v1/
165 CACHEURL=http://172.22.0.1/images
166 IRONIC_FAST_TRACK=false
167 EOF
168 }
169
170 function install {
171     #Kubeadm usage is deprecated in v1,0,0 version
172     #install_kubernetes
173     #install_k8s_single_node
174     #check_cni_network $1
175     #create_k8s_regular_user
176     #check_k8s_node_status
177     #remove_k8s_noschedule_taint
178
179     #install_podman
180     #Todo - error handling mechanism
181     create_ironic_env
182     install_ironic_container
183 }
184
185 if [ "$1" == "-o" ]; then
186     install offline
187     exit 0
188 fi
189
190 if [ "$1" == "--dhcp-start" ]; then
191     install_dhcp
192     echo "wait for 320s for nodes to be assigned"
193     sleep 6m
194     exit 0
195 fi
196
197 if [ "$1" == "--dhcp-reset" ]; then
198     reset_dhcp
199     echo "wait for 320s for nodes to be re-assigned"
200     sleep 6m
201     exit 0
202 fi
203
204 install