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 ##############################################################################
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);
17 ##############################################################################
18 # private helper functions
19 ##############################################################################
21 function __parse_yaml {
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" |
32 indent = length($1)/2;
34 for (i in vname) {if (i > indent) {delete vname[i]}}
36 vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
37 printf("%s%s%s=(\"%s\")\n", "'"$prefix"'",vn, $2, $3);
42 ##############################################################################
44 ##############################################################################
46 function jumpserver_pkg_install {
48 if [ -n "$(command -v apt-get)" ]; then
49 pkg_type='deb'; pkg_cmd='sudo apt-get install -y'
51 pkg_type='rpm'; pkg_cmd='sudo yum install -y --skip-broken'
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}"
58 # shellcheck disable=SC2086
59 ${pkg_cmd} ${pkg_list}
62 function jumpserver_check_requirements {
63 # shellcheck disable=SC2178
64 local states=$1; shift
65 # shellcheck disable=SC2178
66 local vnodes=$1; shift
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}"
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}"
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}"
88 notify_n "[WARN] Public (${br[3]}) ${err_br_not_found}" 3
89 notify_n "[WARN] ${warn_br_endpoint}" 3
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}"
96 notify_n "[WARN] ${br[3]} ${err_br_virsh_net}" 3
97 notify_n "[WARN] ${warn_br_endpoint}" 3
100 # https://bugs.launchpad.net/ubuntu/+source/qemu/+bug/1797332
101 if lsb_release -d | grep -q -e 'Ubuntu 16.04'; then
102 if uname -r | grep -q -e '^4\.4\.'; then
103 notify_n "[WARN] Host kernel too old; nested virtualization issues!" 3
104 notify_n "[WARN] apt install linux-generic-hwe-16.04 && reboot" 3
105 notify_e "[ERROR] Please upgrade the kernel and reboot!"
111 function docker_install {
113 # Mininum effort attempt at installing Docker if missing
114 if ! docker --version; then
115 curl -fsSL https://get.docker.com -o get-docker.sh
116 sudo sh get-docker.sh
118 # On RHEL distros, the Docker service should be explicitly started
119 sudo systemctl start docker
121 DOCKER_VER=$(docker version --format '{{.Server.Version}}')
122 if [ "${DOCKER_VER%%.*}" -lt 2 ]; then
123 notify_e "[ERROR] Docker version ${DOCKER_VER} is too old, please upgrade it."
126 # Distro-provided docker-compose might be simply broken (Ubuntu 16.04, CentOS 7)
127 if ! docker-compose --version > /dev/null 2>&1 || \
128 [ "$(docker-compose version --short | tr -d '.')" -lt 1220 ] && \
129 [ "$(uname -m)" = 'x86_64' ]; then
130 COMPOSE_BIN="${image_dir}/docker-compose"
131 COMPOSE_VERSION='1.22.0'
132 notify_n "[WARN] Using docker-compose ${COMPOSE_VERSION} in ${COMPOSE_BIN}" 3
133 if [ ! -e "${COMPOSE_BIN}" ]; then
134 COMPOSE_URL="https://github.com/docker/compose/releases/download/${COMPOSE_VERSION}"
135 sudo curl -L "${COMPOSE_URL}/docker-compose-$(uname -s)-$(uname -m)" -o "${COMPOSE_BIN}"
136 sudo chmod +x "${COMPOSE_BIN}"
141 function virtinst_install {
143 VIRT_VER=$(virt-install --version 2>&1)
144 if [ "${VIRT_VER//./}" -lt 140 ]; then
145 VIRT_TGZ="${image_dir}/virt-manager.tar.gz"
147 VIRT_URL="https://github.com/virt-manager/virt-manager/archive/v${VIRT_VER}.tar.gz"
148 notify_n "[WARN] Using virt-install ${VIRT_VER} from ${VIRT_TGZ}" 3
149 if [ ! -e "${VIRT_TGZ}" ]; then
150 curl -L "${VIRT_URL}" -o "${VIRT_TGZ}"
151 mkdir -p "${image_dir}/virt-manager"
152 tar xzf "${VIRT_TGZ}" -C "${image_dir}/virt-manager" --strip-components=1
157 function do_udev_cfg {
158 local _conf='/etc/udev/rules.d/99-opnfv-fuel-vnet-mtu.rules'
159 # http://linuxaleph.blogspot.com/2013/01/how-to-network-jumbo-frames-to-kvm-guest.html
160 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}"
161 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}"
162 sudo udevadm control --reload
166 function do_sysctl_cfg {
167 local _conf='/etc/sysctl.d/99-opnfv-fuel-bridge.conf'
168 # https://wiki.libvirt.org/page/Net.bridge.bridge-nf-call_and_sysctl.conf
169 if modprobe br_netfilter bridge; then
170 echo 'net.bridge.bridge-nf-call-arptables = 0' |& sudo tee "${_conf}"
171 echo 'net.bridge.bridge-nf-call-iptables = 0' |& sudo tee -a "${_conf}"
172 echo 'net.bridge.bridge-nf-call-ip6tables = 0' |& sudo tee -a "${_conf}"
173 # Some distros / sysadmins explicitly blacklist br_netfilter
174 sudo sysctl -q -p "${_conf}" || true
178 function generate_ssh_key {
179 # shellcheck disable=SC2155
180 local mcp_ssh_key=$(basename "${SSH_KEY}")
182 if [ -n "${SUDO_USER}" ] && [ "${SUDO_USER}" != 'root' ]; then
186 if [ -f "${SSH_KEY}" ]; then
188 ssh-keygen -f "${mcp_ssh_key}" -y > "${mcp_ssh_key}.pub"
191 [ -f "${mcp_ssh_key}" ] || ssh-keygen -f "${mcp_ssh_key}" -N ''
192 sudo install -D -o "${user}" -m 0600 "${mcp_ssh_key}" "${SSH_KEY}"