6c826371a15174dc49f69965fed604d32510e640
[icn.git] / deploy / metal3-vm / lib / common.sh
1 #!/usr/bin/env bash
2 set -eu -o pipefail
3
4 eval "$(go env)"
5
6 SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
7 USER=`whoami`
8
9 # Get variables from the config file
10 if [ -z "${CONFIG:-}" ]; then
11     # See if there's a config_$USER.sh in the SCRIPTDIR
12     if [ ! -f ${SCRIPTDIR}/../config_${USER}.sh ]; then
13         cp ${SCRIPTDIR}/../config_example.sh ${SCRIPTDIR}/../config_${USER}.sh
14         echo "Automatically created config_${USER}.sh with default contents."
15     fi
16     CONFIG="${SCRIPTDIR}/../config_${USER}.sh"
17 fi
18 source $CONFIG
19
20 # Set variables
21 # Additional DNS
22 ADDN_DNS=${ADDN_DNS:-}
23 # External interface for routing traffic through the host
24 EXT_IF=${EXT_IF:-}
25 # Provisioning interface
26 PRO_IF=${PRO_IF:-}
27 # Does libvirt manage the baremetal bridge (including DNS and DHCP)
28 MANAGE_BR_BRIDGE=${MANAGE_BR_BRIDGE:-y}
29 # Only manage bridges if is set
30 MANAGE_PRO_BRIDGE=${MANAGE_PRO_BRIDGE:-y}
31 MANAGE_INT_BRIDGE=${MANAGE_INT_BRIDGE:-y}
32 # Internal interface, to bridge virbr0
33 INT_IF=${INT_IF:-}
34 #Root disk to deploy coreOS - use /dev/sda on BM
35 ROOT_DISK_NAME=${ROOT_DISK_NAME-"/dev/sda"}
36 #Container runtime
37 CONTAINER_RUNTIME=${CONTAINER_RUNTIME:-"docker"}
38
39 export EXTERNAL_SUBNET="192.168.111.0/24"
40 #Ironic data directory
41 IRONIC_DATA_DIR=${IRONIC_DATA_DIR:-"/opt/ironic"}
42 export SSH_PUB_KEY=~/.ssh/id_rsa.pub
43
44 FILESYSTEM=${FILESYSTEM:="/"}
45
46 WORKING_DIR=${WORKING_DIR:-"/opt/metal3-vm"}
47 NODES_FILE=${NODES_FILE:-"${WORKING_DIR}/ironic_nodes.json"}
48 NODES_PLATFORM=${NODES_PLATFORM:-"libvirt"}
49
50 export NUM_MASTERS=${NUM_MASTERS:-"1"}
51 export NUM_WORKERS=${NUM_WORKERS:-"1"}
52 export VM_EXTRADISKS=${VM_EXTRADISKS:-"false"}
53
54 # Ironic vars
55 export IRONIC_IMAGE=${IRONIC_IMAGE:-"integratedcloudnative/ironic:v1.0-icn"}
56 export IRONIC_INSPECTOR_IMAGE=${IRONIC_INSPECTOR_IMAGE:-"integratedcloudnative/ironic-inspector:v1.0-icn"}
57 export IRONIC_BAREMETAL_IMAGE=${IRONIC_BAREMETAL_IMAGE:-"integratedcloudnative/baremetal-operator:v1.0-icn"}
58 export IPA_DOWNLOADER_IMAGE=${IPA_DOWNLOADER_IMAGE:-"integratedcloudnative/ironic-ipa-downloader:v1.0-icn"}
59
60 # Verify requisites/permissions
61 # Connect to system libvirt
62 export LIBVIRT_DEFAULT_URI=qemu:///system
63 if [ "$USER" != "root" -a "${XDG_RUNTIME_DIR:-}" == "/run/user/0" ] ; then
64     echo "Please use a non-root user, WITH a login shell (e.g. su - USER)"
65     exit 1
66 fi
67
68 # Check if sudo privileges without password
69 if ! sudo -n uptime &> /dev/null ; then
70   echo "sudo without password is required"
71   exit 1
72 fi
73
74 # Check OS
75 OS=$(awk -F= '/^ID=/ { print $2 }' /etc/os-release | tr -d '"')
76 if [[ ! $OS =~ ^(centos|rhel|ubuntu)$ ]]; then
77   echo "Unsupported OS"
78   exit 1
79 fi
80
81 # Check CentOS version
82 os_version=$(awk -F= '/^VERSION_ID=/ { print $2 }' /etc/os-release | tr -d '"' | cut -f1 -d'.')
83 if [[ ${os_version} -ne 7 ]] && [[ ${os_version} -ne 18 ]]; then
84   echo "Required CentOS 7 or RHEL 7 or Ubuntu 18.04"
85   exit 1
86 fi
87
88 # Check d_type support
89 FSTYPE=$(df ${FILESYSTEM} --output=fstype | grep -v Type)
90
91 case ${FSTYPE} in
92   'ext4'|'btrfs')
93   ;;
94   'xfs')
95     if [[ $(xfs_info ${FILESYSTEM} | grep -q "ftype=1") ]]; then
96       echo "Filesystem not supported"
97       exit 1
98     fi
99   ;;
100   *)
101     echo "Filesystem not supported"
102     exit 1
103   ;;
104 esac
105
106 if [ ! -d "$WORKING_DIR" ]; then
107   echo "Creating Working Dir"
108   sudo mkdir "$WORKING_DIR"
109   sudo chown "${USER}:${USER}" "$WORKING_DIR"
110   chmod 755 "$WORKING_DIR"
111 fi
112
113 function list_nodes {
114     # Includes -machine and -machine-namespace
115     cat $NODES_FILE | \
116         jq '.nodes[] | {
117            name,
118            driver,
119            address:.driver_info.ipmi_address,
120            port:.driver_info.ipmi_port,
121            user:.driver_info.ipmi_username,
122            password:.driver_info.ipmi_password,
123            mac: .ports[0].address
124            } |
125            .name + " " +
126            .driver + "://" + .address + (if .port then ":" + .port else "" end)  + " " +
127            .user + " " + .password + " " + .mac' \
128        | sed 's/"//g'
129 }