Apex: Fix OS_VERSION detection
[releng.git] / jjb / apex / apex-deploy.sh
1 #!/bin/bash
2 set -o errexit
3 set -o nounset
4 set -o pipefail
5
6 IPV6_FLAG=False
7 ALLINONE_FLAG=False
8 CSIT_ENV_FLAG=False
9 FUNCTEST_ENV_FLAG=False
10
11 # log info to console
12 echo "Starting the Apex deployment."
13 echo "--------------------------------------------------------"
14 echo
15
16 if [ -z "$DEPLOY_SCENARIO" ]; then
17   echo "Deploy scenario not set!"
18   exit 1
19 else
20   echo "Deploy scenario: ${DEPLOY_SCENARIO}"
21 fi
22
23 # Dev or RPM/ISO build
24 if [[ "$ARTIFACT_VERSION" =~ dev ]]; then
25   # Settings for deploying from git workspace
26   DEPLOY_SETTINGS_DIR="${WORKSPACE}/config/deploy"
27   NETWORK_SETTINGS_DIR="${WORKSPACE}/config/network"
28   CLEAN_CMD="opnfv-clean"
29   # if we are using master, then we are downloading/caching upstream images
30   # we want to use that built in mechanism to avoid re-downloading every job
31   # so we use a dedicated folder to hold the upstream cache
32   UPSTREAM_CACHE=$HOME/upstream_cache
33   if [[ "$BRANCH" != 'stable/fraser' ]]; then
34     mkdir -p ${UPSTREAM_CACHE}
35     RESOURCES=$UPSTREAM_CACHE
36   else
37     RESOURCES="${WORKSPACE}/.build/"
38   fi
39   CONFIG="${WORKSPACE}/build"
40   BASE=$CONFIG
41   IMAGES=$RESOURCES
42   LIB="${WORKSPACE}/lib"
43   DEPLOY_CMD="opnfv-deploy --image-dir ${RESOURCES}"
44   # Ensure artifacts were downloaded and extracted correctly
45   # TODO(trozet) add verification here
46
47   # Install dev build
48   sudo rm -rf /tmp/.build
49   mv -f .build /tmp/
50   sudo pip3 install --upgrade --force-reinstall .
51   mv -f /tmp/.build ${WORKSPACE}/
52 else
53   DEPLOY_SETTINGS_DIR="/etc/opnfv-apex/"
54   NETWORK_SETTINGS_DIR="/etc/opnfv-apex/"
55   CLEAN_CMD="opnfv-clean"
56   # set to use different directory here because upon RPM removal this
57   # directory will be wiped in daily
58   UPSTREAM_CACHE=$HOME/upstream_cache
59   if [[ "$BRANCH" != 'stable/fraser' ]]; then
60     mkdir -p ${UPSTREAM_CACHE}
61     RESOURCES=$UPSTREAM_CACHE
62   else
63     RESOURCES="/var/opt/opnfv/images"
64   fi
65   DEPLOY_CMD="opnfv-deploy --image-dir ${RESOURCES}"
66   CONFIG="/var/opt/opnfv"
67   BASE=$CONFIG
68   IMAGES=$RESOURCES
69   LIB="/var/opt/opnfv/lib"
70   sudo mkdir -p /var/log/apex
71   sudo chmod 777 /var/log/apex
72   cd /var/log/apex
73 fi
74
75 # Install Dependencies
76 # Make sure python34 dependencies are installed
77 dependencies="epel-release python34 python34-devel libvirt-devel python34-pip \
78 ansible python34-PyYAML python34-jinja2 python34-setuptools python-tox ansible"
79
80 for dep_pkg in $dependencies; do
81   if ! rpm -q ${dep_pkg} > /dev/null; then
82     if ! sudo yum install -y ${dep_pkg}; then
83       echo "Failed to install ${dep_pkg}"
84       exit 1
85     fi
86   fi
87 done
88
89 if [[ "$JOB_NAME" =~ "virtual" ]]; then
90   # Make sure ipxe-roms-qemu package is updated to latest.
91   # This package is needed for multi virtio nic PXE boot in virtual environment.
92   sudo yum update -y ipxe-roms-qemu
93 fi
94
95 if [ "$OPNFV_CLEAN" == 'yes' ]; then
96   if sudo test -e '/root/inventory/pod_settings.yaml'; then
97     clean_opts='-i /root/inventory/pod_settings.yaml'
98   else
99     clean_opts=''
100   fi
101
102   sudo ${CLEAN_CMD} ${clean_opts}
103 fi
104
105 # These are add-ons to regular scenarios where you can do like
106 # os-nosdn-nofeature-noha-ipv6, or os-nosdn-nofeature-noha-allinone
107 if echo ${DEPLOY_SCENARIO} | grep ipv6; then
108   IPV6_FLAG=True
109   DEPLOY_SCENARIO=$(echo ${DEPLOY_SCENARIO} |  sed 's/-ipv6//')
110   echo "INFO: IPV6 Enabled"
111 fi
112
113 if echo ${DEPLOY_SCENARIO} | grep allinone; then
114   ALLINONE_FLAG=True
115   DEPLOY_SCENARIO=$(echo ${DEPLOY_SCENARIO} |  sed 's/-allinone//')
116   echo "INFO: All in one deployment detected"
117 fi
118
119 if echo ${DEPLOY_SCENARIO} | grep csit; then
120   CSIT_ENV_FLAG=True
121   DEPLOY_SCENARIO=$(echo ${DEPLOY_SCENARIO} |  sed 's/-csit//')
122   echo "INFO: CSIT env requested in deploy scenario"
123 elif echo ${DEPLOY_SCENARIO} | grep functest; then
124   FUNCTEST_ENV_FLAG=True
125   DEPLOY_SCENARIO=$(echo ${DEPLOY_SCENARIO} |  sed 's/-functest//')
126   echo "INFO: Functest env requested in deploy scenario"
127 fi
128
129 echo "Deploy Scenario set to ${DEPLOY_SCENARIO}"
130 DEPLOY_FILE="${DEPLOY_SETTINGS_DIR}/${DEPLOY_SCENARIO}.yaml"
131
132 if [ ! -e "$DEPLOY_FILE" ]; then
133   echo "ERROR: Required settings file missing: Deploy settings file ${DEPLOY_FILE}"
134 fi
135
136 if [[ "$JOB_NAME" =~ "virtual" ]]; then
137   # settings for virtual deployment
138   DEPLOY_CMD="${DEPLOY_CMD} -v"
139   if [[ "${DEPLOY_SCENARIO}" =~ fdio|ovs ]]; then
140     DEPLOY_CMD="${DEPLOY_CMD} --virtual-default-ram 12 --virtual-compute-ram 7"
141   fi
142   if [[ "$ALLINONE_FLAG" == "True" ]]; then
143     DEPLOY_CMD="${DEPLOY_CMD} --virtual-computes 0"
144   elif [[ "$PROMOTE" == "True" ]]; then
145     DEPLOY_CMD="${DEPLOY_CMD} --virtual-computes 2"
146   fi
147
148   if [[ "$FUNCTEST_ENV_FLAG" == "True"  || "$CSIT_ENV_FLAG" == "True" ]]; then
149     if [[ "$CSIT_ENV_FLAG" == "True" ]]; then
150       ENV_TYPE="csit"
151     else
152       ENV_TYPE="functest"
153     fi
154     if [ -z "$OS_VERSION" ]; then
155       echo "INFO: OS_VERSION not passed to deploy, detecting based on branch and scenario"
156       case $BRANCH in
157         master)
158           if [[ "$DEPLOY_SCENARIO" =~ "rocky" ]]; then
159             OS_VERSION=rocky
160           else
161             OS_VERSION=master
162           fi
163           ;;
164         *gambia)
165           OS_VERSION=queens
166           ;;
167         *)
168           echo "Unable to detection OS_VERSION, aborting"
169           exit 1
170           ;;
171       esac
172     fi
173     if [[ "$OS_VERSION" != "master" ]]; then
174       SNAP_ENV="${ENV_TYPE}-${OS_VERSION}-environment.yaml"
175     else
176       SNAP_ENV="${ENV_TYPE}-environment.yaml"
177     fi
178     DEPLOY_CMD="${DEPLOY_CMD} -e ${SNAP_ENV}"
179   fi
180 else
181   # settings for bare metal deployment
182   NETWORK_SETTINGS_DIR="/root/network"
183   INVENTORY_FILE="/root/inventory/pod_settings.yaml"
184
185   if ! sudo test -e "$INVENTORY_FILE"; then
186     echo "ERROR: Required settings file missing: Inventory settings file ${INVENTORY_FILE}"
187     exit 1
188   fi
189   # include inventory file for bare metal deployment
190   DEPLOY_CMD="${DEPLOY_CMD} -i ${INVENTORY_FILE}"
191 fi
192
193 if [ "$IPV6_FLAG" == "True" ]; then
194   NETWORK_FILE="${NETWORK_SETTINGS_DIR}/network_settings_v6.yaml"
195 elif [[ "$CSIT_ENV_FLAG" == "True"  || "$FUNCTEST_ENV_FLAG" == "True" ]]; then
196   # We use csit network settings which is single network for snapshots
197   NETWORK_FILE="${NETWORK_SETTINGS_DIR}/network_settings_csit.yaml"
198 else
199   NETWORK_FILE="${NETWORK_SETTINGS_DIR}/network_settings.yaml"
200 fi
201
202 # Check that network settings file exists
203 if ! sudo test -e "$NETWORK_FILE"; then
204   echo "ERROR: Required settings file missing: Network Settings file ${NETWORK_FILE}"
205   exit 1
206 fi
207
208 # start deployment
209 sudo ${DEPLOY_CMD} -d ${DEPLOY_FILE} -n ${NETWORK_FILE} --debug
210
211 echo
212 echo "--------------------------------------------------------"
213 echo "Done!"