refactoring metal3 code
[icn.git] / env / metal3 / 02_configure.sh
1 #!/usr/bin/env bash
2 set -xe
3 LIBDIR="$(dirname "$PWD")"
4
5 source $LIBDIR/lib/logging.sh
6 source $LIBDIR/lib/common.sh
7
8 if [[ $EUID -ne 0 ]]; then
9     echo "confgiure script must be run as root"
10     exit 1
11 fi
12
13 function check_inteface_ip {
14     local interface=$1
15     local ipaddr=$2
16
17     if [ ! $(ip addr show dev $interface) ]; then
18         exit 1
19     fi
20
21     local ipv4address=$(ip addr show dev $interface | awk '$1 == "inet" { sub("/.*", "", $2); print $2 }')
22     if [ "$ipv4address" != "$ipaddr" ]; then
23         exit 1
24     fi
25 }
26
27 function configure_dhcp_bridge {
28     brctl addbr dhcp0
29     ip link set dhcp0 up
30     brctl addif dhcp0 $BS_DHCP_INTERFACE
31     ip addr add dev dhcp0 $BS_DHCP_INTERFACE_IP
32 }
33
34 function configure_ironic_bridge {
35     brctl addbr provisioning
36     ip link set provisioning up
37     brctl addif provisioning $IRONIC_INTERFACE
38     ip addr add dev provisioning 172.22.0.1/24
39 }
40
41 function configure_kubelet {
42     swapoff -a
43     #Todo addition kubelet configuration
44 }
45
46 function configure_kubeadm {
47     #Todo error handing
48     if [ "$1" == "offline" ]; then
49         for images in kube-apiserver kube-controller-manager kube-scheduler kube-proxy; do
50             docker load --input $CONTAINER_IMAGES_DIR/$images.tar;
51         done
52
53         docker load --input $CONTAINER_IMAGES_DIR/pause.tar
54         docker load --input $CONTAINER_IMAGES_DIR/etcd.tar
55         docker load --input $CONTAINER_IMAGES_DIR/coredns.tar
56         return
57     fi
58     kubeadm config images pull --kubernetes-version=$KUBE_VERSION
59 }
60
61 function configure_ironic_interfaces {
62     #Todo later to change the CNI networking for podman networking
63     # Add firewall rules to ensure the IPA ramdisk can reach httpd, Ironic and the Inspector API on the host
64     if [ "$IRONIC_PROVISIONING_INTERFACE" ]; then
65         check_inteface_ip $IRONIC_PROVISIONING_INTERFACE $IRONIC_PROVISIONING_INTERFACE_IP
66     else
67         exit 1
68     fi
69
70     if [ "$IRONIC_IPMI_INTERFACE" ]; then
71         check_inteface_ip $IRONIC_IPMI_INTERFACE $IRONIC_IPMI_INTERFACE_IP
72     else
73         exit 1
74     fi
75
76     for port in 80 5050 6385 ; do
77         if ! sudo iptables -C INPUT -i $IRONIC_PROVISIONING_INTERFACE -p tcp -m tcp --dport $port -j ACCEPT > /dev/null 2>&1; then
78             sudo iptables -I INPUT -i $IRONIC_PROVISIONING_INTERFACE -p tcp -m tcp --dport $port -j ACCEPT
79         fi
80     done
81
82     # Allow ipmi to the bmc processes
83     if ! sudo iptables -C INPUT -i $IRONIC_IPMI_INTERFACE -p udp -m udp --dport 6230:6235 -j ACCEPT 2>/dev/null ; then
84         sudo iptables -I INPUT -i $IRONIC_IPMI_INTERFACE -p udp -m udp --dport 6230:6235 -j ACCEPT
85     fi
86
87     #Allow access to dhcp and tftp server for pxeboot
88     for port in 67 69 ; do
89         if ! sudo iptables -C INPUT -i $IRONIC_PROVISIONING_INTERFACE -p udp --dport $port -j ACCEPT 2>/dev/null ; then
90             sudo iptables -I INPUT -i $IRONIC_PROVISIONING_INTERFACE -p udp --dport $port -j ACCEPT
91         fi
92     done
93 }
94
95 function configure_ironic_offline {
96     if [ ! -d $CONTAINER_IMAGES_DIR ] && [ ! -d $BUILD_DIR ]; then
97         exit 1
98     fi
99
100     for image in ironic-inspector-image ironic-image podman-pause \
101         baremetal-operator socat; do
102         if [ ! -f "$CONTAINER_IMAGES_DIR/$image" ]; then
103             exit 1
104         fi
105     done
106
107     if [ ! -f "$BUILD_DIR/ironic-python-agent.initramfs"] && [ ! -f \
108         "$BUILD_DIR/ironic-python-agent.kernel" ] && [ ! -f
109         "$BUILD_DIR/$BM_IMAGE" ]; then
110         exit 1
111     fi
112
113     podman load --input $CONTAINER_IMAGES_DIR/ironic-inspector-image.tar
114     podman load --input $CONTAINER_IMAGES_DIR/ironic-image.tar
115     podman load --input $CONTAINER_IMAGES_DIR/podman-pause.tar
116
117     docker load --input $CONTAINER_IMAGES_DIR/baremetal-operator.tar
118     docker load --input $CONTAINER_IMAGES_DIR/socat.tar
119
120     mkdir -p "$IRONIC_DATA_DIR/html/images"
121
122     cp $BUILD_DIR/ironic-python-agent.initramfs $IRONIC_DATA_DIR/html/images/
123     cp $BUILD_DIR/ironic-python-agent.kernel $IRONIC_DATA_DIR/html/images/
124     cp $BUILD_DIR/$BM_IMAGE $IRONIC_DATA_DIR/html/images/
125     md5sum $BUILD_DIR/$BM_IMAGE | awk '{print $1}' > $BUILD_DIR/${BM_IMAGE}.md5sum
126 }
127
128 function configure_ironic {
129     if [ "$1" == "offline" ]; then
130         configure_ironic_offline
131         return
132     fi
133
134     #Podman usage is deprecated for v1.0.0 release
135     #podman pull $IRONIC_IMAGE
136     docker pull $IRONIC_IMAGE
137     #podman pull $IRONIC_INSPECTOR_IMAGE
138     docker pull $IRONIC_INSPECTOR_IMAGE
139
140     mkdir -p "$IRONIC_DATA_DIR/html/images"
141     pushd $IRONIC_DATA_DIR/html/images
142
143     if [ ! -f ironic-python-agent.initramfs ]; then
144         curl --insecure --compressed -L https://images.rdoproject.org/master/rdo_trunk/current-tripleo-rdo/ironic-python-agent.tar | tar -xf -
145     fi
146
147     if [[ "$BM_IMAGE_URL" && "$BM_IMAGE" ]]; then
148         curl -o ${BM_IMAGE} --insecure --compressed -O -L ${BM_IMAGE_URL}
149         md5sum ${BM_IMAGE} | awk '{print $1}' > ${BM_IMAGE}.md5sum
150     fi
151     popd
152 }
153
154 function configure {
155     #Kubeadm usage deprecated for v1.0.0 release
156     #configure_kubeadm $1
157     #configure_kubelet
158     configure_ironic $1
159     configure_dhcp_bridge
160     configure_ironic_bridge
161     configure_ironic_interfaces
162 }
163
164 if [ "$1" == "-o" ]; then
165     configure offline
166     exit 0
167 fi
168
169 configure