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 vnodes=$1; shift
66 local err_br_not_found='Linux bridge not found!'
67 local err_br_virsh_net='is a virtual network, Linux bridge expected!'
68 local warn_br_endpoint="Endpoints might be inaccessible from external hosts!"
69 # MaaS requires a Linux bridge for PXE/admin
70 if [[ "${vnodes}" =~ mas01 ]]; then
71 if ! brctl showmacs "${br[0]}" >/dev/null 2>&1; then
72 notify_e "[ERROR] PXE/admin (${br[0]}) ${err_br_not_found}"
74 # Assume virsh network name matches bridge name (true if created by us)
75 if ${VIRSH} net-info "${br[0]}" >/dev/null 2>&1; then
76 notify_e "[ERROR] ${br[0]} ${err_br_virsh_net}"
79 # If virtual nodes are present, public should be a Linux bridge
80 if [ "$(echo "${vnodes}" | wc -w)" -gt 2 ]; then
81 if ! brctl showmacs "${br[3]}" >/dev/null 2>&1; then
82 if [[ "${vnodes}" =~ mas01 ]]; then
83 # Baremetal nodes *require* a proper public network
84 notify_e "[ERROR] Public (${br[3]}) ${err_br_not_found}"
86 notify_n "[WARN] Public (${br[3]}) ${err_br_not_found}" 3
87 notify_n "[WARN] ${warn_br_endpoint}" 3
90 if ${VIRSH} net-info "${br[3]}" >/dev/null 2>&1; then
91 if [[ "${vnodes}" =~ mas01 ]]; then
92 notify_e "[ERROR] ${br[3]} ${err_br_virsh_net}"
94 notify_n "[WARN] ${br[3]} ${err_br_virsh_net}" 3
95 notify_n "[WARN] ${warn_br_endpoint}" 3
101 function docker_install {
103 # Mininum effort attempt at installing Docker if missing
104 if ! docker --version; then
105 curl -fsSL https://get.docker.com -o get-docker.sh
106 sudo sh get-docker.sh
108 # On RHEL distros, the Docker service should be explicitly started
109 sudo systemctl start docker
111 DOCKER_VER=$(docker version --format '{{.Server.Version}}')
112 if [ "${DOCKER_VER%%.*}" -lt 2 ]; then
113 notify_e "[ERROR] Docker version ${DOCKER_VER} is too old, please upgrade it."
116 # Distro-provided docker-compose might be simply broken (Ubuntu 16.04, CentOS 7)
117 if ! docker-compose --version > /dev/null 2>&1; then
118 COMPOSE_BIN="${image_dir}/docker-compose"
119 COMPOSE_VERSION='1.22.0'
120 notify_n "[WARN] Using docker-compose ${COMPOSE_VERSION} in ${COMPOSE_BIN}" 3
121 if [ ! -e "${COMPOSE_BIN}" ]; then
122 COMPOSE_URL="https://github.com/docker/compose/releases/download/${COMPOSE_VERSION}"
123 sudo curl -L "${COMPOSE_URL}/docker-compose-$(uname -s)-$(uname -m)" -o "${COMPOSE_BIN}"
124 sudo chmod +x "${COMPOSE_BIN}"
129 function virtinst_install {
131 VIRT_VER=$(virt-install --version 2>&1)
132 if [ "${VIRT_VER//./}" -lt 140 ]; then
133 VIRT_TGZ="${image_dir}/virt-manager.tar.gz"
135 VIRT_URL="https://github.com/virt-manager/virt-manager/archive/v${VIRT_VER}.tar.gz"
136 notify_n "[WARN] Using virt-install ${VIRT_VER} from ${VIRT_TGZ}" 3
137 if [ ! -e "${VIRT_TGZ}" ]; then
138 curl -L "${VIRT_URL}" -o "${VIRT_TGZ}"
139 mkdir -p "${image_dir}/virt-manager"
140 tar xzf "${VIRT_TGZ}" -C "${image_dir}/virt-manager" --strip-components=1
145 function do_udev_cfg {
146 local _conf='/etc/udev/rules.d/99-opnfv-fuel-vnet-mtu.rules'
147 # http://linuxaleph.blogspot.com/2013/01/how-to-network-jumbo-frames-to-kvm-guest.html
148 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}"
149 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}"
150 sudo udevadm control --reload
154 function do_sysctl_cfg {
155 local _conf='/etc/sysctl.d/99-opnfv-fuel-bridge.conf'
156 # https://wiki.libvirt.org/page/Net.bridge.bridge-nf-call_and_sysctl.conf
157 if modprobe br_netfilter bridge; then
158 echo 'net.bridge.bridge-nf-call-arptables = 0' |& sudo tee "${_conf}"
159 echo 'net.bridge.bridge-nf-call-iptables = 0' |& sudo tee -a "${_conf}"
160 echo 'net.bridge.bridge-nf-call-ip6tables = 0' |& sudo tee -a "${_conf}"
161 # Some distros / sysadmins explicitly blacklist br_netfilter
162 sudo sysctl -q -p "${_conf}" || true
166 function generate_ssh_key {
167 # shellcheck disable=SC2155
168 local mcp_ssh_key=$(basename "${SSH_KEY}")
170 if [ -n "${SUDO_USER}" ] && [ "${SUDO_USER}" != 'root' ]; then
174 if [ -f "${SSH_KEY}" ]; then
176 ssh-keygen -f "${mcp_ssh_key}" -y > "${mcp_ssh_key}.pub"
179 [ -f "${mcp_ssh_key}" ] || ssh-keygen -f "${mcp_ssh_key}" -N ''
180 sudo install -D -o "${user}" -m 0600 "${mcp_ssh_key}" "${SSH_KEY}"