[baremetal] Containerize MaaS
[fuel.git] / mcp / scripts / lib_jump_common.sh
1 #!/bin/bash -e
2 ##############################################################################
3 # Copyright (c) 2018 Mirantis Inc., Enea AB and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9 #
10 # Library of shell functions used by build / deploy scripts on jumpserver:
11 # - distro package requirements installation (e.g. DEB, RPM);
12 # - other package requirements from custom sources (e.g. docker);
13 # - jumpserver prerequisites validation (e.g. network bridges);
14 # - distro configuration (e.g. udev, sysctl);
15 # etc.
16
17 ##############################################################################
18 # private helper functions
19 ##############################################################################
20
21 function __parse_yaml {
22   local prefix=$2
23   local s
24   local w
25   local fs
26   s='[[:space:]]*'
27   w='[a-zA-Z0-9_]*'
28   fs="$(echo @|tr @ '\034')"
29   sed -e 's|---||g' -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
30       -e "s|^\($s\)\($w\)$s[:-]$s\(.*\)$s\$|\1$fs\2$fs\3|p" "$1" |
31   awk -F"$fs" '{
32   indent = length($1)/2;
33   vname[indent] = $2;
34   for (i in vname) {if (i > indent) {delete vname[i]}}
35       if (length($3) > 0) {
36           vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
37           printf("%s%s%s=(\"%s\")\n", "'"$prefix"'",vn, $2, $3);
38       }
39   }' | sed 's/_=/+=/g'
40 }
41
42 ##############################################################################
43 # public functions
44 ##############################################################################
45
46 function jumpserver_pkg_install {
47   local req_type=$1
48   if [ -n "$(command -v apt-get)" ]; then
49     pkg_type='deb'; pkg_cmd='sudo apt-get install -y'
50   else
51     pkg_type='rpm'; pkg_cmd='sudo yum install -y --skip-broken'
52   fi
53   eval "$(__parse_yaml "./requirements_${pkg_type}.yaml")"
54   for section in 'common' "$(uname -i)"; do
55     section_var="${req_type}_${section}[*]"
56     pkg_list+=" ${!section_var}"
57   done
58   # shellcheck disable=SC2086
59   ${pkg_cmd} ${pkg_list}
60 }
61
62 function jumpserver_check_requirements {
63   # shellcheck disable=SC2178
64   local states=$1; shift
65   # shellcheck disable=SC2178
66   local vnodes=$1; shift
67   local br=("$@")
68   local err_br_not_found='Linux bridge not found!'
69   local err_br_virsh_net='is a virtual network, Linux bridge expected!'
70   local warn_br_endpoint="Endpoints might be inaccessible from external hosts!"
71   # MaaS requires a Linux bridge for PXE/admin
72   if [[ "${states}" =~ maas ]]; then
73     if ! brctl showmacs "${br[0]}" >/dev/null 2>&1; then
74       notify_e "[ERROR] PXE/admin (${br[0]}) ${err_br_not_found}"
75     fi
76     # Assume virsh network name matches bridge name (true if created by us)
77     if ${VIRSH} net-info "${br[0]}" >/dev/null 2>&1; then
78       notify_e "[ERROR] ${br[0]} ${err_br_virsh_net}"
79     fi
80   fi
81   # If virtual nodes are present, public should be a Linux bridge
82   if [ -n "${vnodes}" ]; then
83     if ! brctl showmacs "${br[3]}" >/dev/null 2>&1; then
84       if [[ "${states}" =~ maas ]]; then
85         # Baremetal nodes *require* a proper public network
86         notify_e "[ERROR] Public (${br[3]}) ${err_br_not_found}"
87       else
88         notify_n "[WARN] Public (${br[3]}) ${err_br_not_found}" 3
89         notify_n "[WARN] ${warn_br_endpoint}" 3
90       fi
91     fi
92     if ${VIRSH} net-info "${br[3]}" >/dev/null 2>&1; then
93       if [[ "${states}" =~ maas ]]; then
94         notify_e "[ERROR] ${br[3]} ${err_br_virsh_net}"
95       else
96         notify_n "[WARN] ${br[3]} ${err_br_virsh_net}" 3
97         notify_n "[WARN] ${warn_br_endpoint}" 3
98       fi
99     fi
100   fi
101 }
102
103 function docker_install {
104   local image_dir=$1
105   # Mininum effort attempt at installing Docker if missing
106   if ! docker --version; then
107     curl -fsSL https://get.docker.com -o get-docker.sh
108     sudo sh get-docker.sh
109     rm get-docker.sh
110     # On RHEL distros, the Docker service should be explicitly started
111     sudo systemctl start docker
112   else
113     DOCKER_VER=$(docker version --format '{{.Server.Version}}')
114     if [ "${DOCKER_VER%%.*}" -lt 2 ]; then
115       notify_e "[ERROR] Docker version ${DOCKER_VER} is too old, please upgrade it."
116     fi
117   fi
118   # Distro-provided docker-compose might be simply broken (Ubuntu 16.04, CentOS 7)
119   if ! docker-compose --version > /dev/null 2>&1 || \
120       [ "$(docker-compose version --short | tr -d '.')" -lt 1220 ] && \
121       [ "$(uname -m)" = 'x86_64' ]; then
122     COMPOSE_BIN="${image_dir}/docker-compose"
123     COMPOSE_VERSION='1.22.0'
124     notify_n "[WARN] Using docker-compose ${COMPOSE_VERSION} in ${COMPOSE_BIN}" 3
125     if [ ! -e "${COMPOSE_BIN}" ]; then
126       COMPOSE_URL="https://github.com/docker/compose/releases/download/${COMPOSE_VERSION}"
127       sudo curl -L "${COMPOSE_URL}/docker-compose-$(uname -s)-$(uname -m)" -o "${COMPOSE_BIN}"
128       sudo chmod +x "${COMPOSE_BIN}"
129     fi
130   fi
131 }
132
133 function virtinst_install {
134   local image_dir=$1
135   VIRT_VER=$(virt-install --version 2>&1)
136   if [ "${VIRT_VER//./}" -lt 140 ]; then
137     VIRT_TGZ="${image_dir}/virt-manager.tar.gz"
138     VIRT_VER='1.4.3'
139     VIRT_URL="https://github.com/virt-manager/virt-manager/archive/v${VIRT_VER}.tar.gz"
140     notify_n "[WARN] Using virt-install ${VIRT_VER} from ${VIRT_TGZ}" 3
141     if [ ! -e "${VIRT_TGZ}" ]; then
142       curl -L "${VIRT_URL}" -o "${VIRT_TGZ}"
143       mkdir -p "${image_dir}/virt-manager"
144       tar xzf "${VIRT_TGZ}" -C "${image_dir}/virt-manager" --strip-components=1
145     fi
146   fi
147 }
148
149 function do_udev_cfg {
150   local _conf='/etc/udev/rules.d/99-opnfv-fuel-vnet-mtu.rules'
151   # http://linuxaleph.blogspot.com/2013/01/how-to-network-jumbo-frames-to-kvm-guest.html
152   echo 'SUBSYSTEM=="net", ACTION=="add|change", KERNEL=="vnet*", RUN+="/bin/sh -c '"'/bin/sleep 1; /sbin/ip link set %k mtu 9000'\"" |& sudo tee "${_conf}"
153   echo 'SUBSYSTEM=="net", ACTION=="add|change", KERNEL=="*-nic", RUN+="/bin/sh -c '"'/bin/sleep 1; /sbin/ip link set %k mtu 9000'\"" |& sudo tee -a "${_conf}"
154   sudo udevadm control --reload
155   sudo udevadm trigger
156 }
157
158 function do_sysctl_cfg {
159   local _conf='/etc/sysctl.d/99-opnfv-fuel-bridge.conf'
160   # https://wiki.libvirt.org/page/Net.bridge.bridge-nf-call_and_sysctl.conf
161   if modprobe br_netfilter bridge; then
162     echo 'net.bridge.bridge-nf-call-arptables = 0' |& sudo tee "${_conf}"
163     echo 'net.bridge.bridge-nf-call-iptables = 0'  |& sudo tee -a "${_conf}"
164     echo 'net.bridge.bridge-nf-call-ip6tables = 0' |& sudo tee -a "${_conf}"
165     # Some distros / sysadmins explicitly blacklist br_netfilter
166     sudo sysctl -q -p "${_conf}" || true
167   fi
168 }
169
170 function generate_ssh_key {
171   # shellcheck disable=SC2155
172   local mcp_ssh_key=$(basename "${SSH_KEY}")
173   local user=${USER}
174   if [ -n "${SUDO_USER}" ] && [ "${SUDO_USER}" != 'root' ]; then
175     user=${SUDO_USER}
176   fi
177
178   if [ -f "${SSH_KEY}" ]; then
179     cp "${SSH_KEY}" .
180     ssh-keygen -f "${mcp_ssh_key}" -y > "${mcp_ssh_key}.pub"
181   fi
182
183   [ -f "${mcp_ssh_key}" ] || ssh-keygen -f "${mcp_ssh_key}" -N ''
184   sudo install -D -o "${user}" -m 0600 "${mcp_ssh_key}" "${SSH_KEY}"
185 }