7b012a6334cfc09e3c6937dfd00d6972637abde5
[releng.git] / jjb / apex / apex-deploy.sh
1 #!/bin/bash
2 set -o errexit
3 set -o nounset
4 set -o pipefail
5
6 APEX_PKGS="common undercloud opendaylight-sfc onos"
7 IPV6_FLAG=False
8
9 # log info to console
10 echo "Starting the Apex virtual deployment."
11 echo "--------------------------------------------------------"
12 echo
13
14 if ! rpm -q wget > /dev/null; then
15   sudo yum -y install wget
16 fi
17
18 if [[ $BUILD_DIRECTORY == *verify* ]]; then
19     # Build is from a verify, use local build artifacts (not RPMs)
20     cd $WORKSPACE/../${BUILD_DIRECTORY}
21     WORKSPACE=$(pwd)
22     echo "WORKSPACE modified to $WORKSPACE"
23     cd $WORKSPACE/ci
24 elif [[ ! "$ARTIFACT_NAME" == "latest" ]]; then
25     # if artifact name is passed the pull a
26     # specific artifact from artifacts.opnfv.org
27     # artifact specified should be opnfv-apex-<version>.noarch.rpm
28     RPM_INSTALL_PATH=$GS_URL
29     RPM_LIST=$RPM_INSTALL_PATH/$ARTIFACT_NAME
30 else
31     # Use latest RPMS
32     if [[ $BUILD_DIRECTORY == *apex-build* ]]; then
33       # Triggered from a daily so RPMS should be in local directory
34       BUILD_DIRECTORY=$WORKSPACE/../$BUILD_DIRECTORY
35       echo "BUILD DIRECTORY modified to $BUILD_DIRECTORY"
36
37       if [[ -f ${BUILD_DIRECTORY}/../opnfv.properties ]]; then
38         # if opnfv.properties exists then use the
39         # local build. Source the file so we get local OPNFV vars
40         source ${BUILD_DIRECTORY}/../opnfv.properties
41         RPM_INSTALL_PATH=${BUILD_DIRECTORY}/noarch
42         RPM_LIST=$RPM_INSTALL_PATH/$(basename $OPNFV_RPM_URL)
43       else
44         echo "BUILD_DIRECTORY is from a daily job, so will not use latest from URL"
45         echo "Check that the slave has opnfv.properties in $BUILD_DIRECTORY"
46         exit 1
47       fi
48     else
49       # use the latest from artifacts.opnfv.org
50       # get the latest.properties to get the link to the latest artifact
51       if ! wget -O $WORKSPACE/opnfv.properties http://$GS_URL/latest.properties; then
52         echo "ERROR: Unable to find latest.properties at ${GS_URL}...exiting"
53         exit 1
54       fi
55       # source the file so we get OPNFV vars
56       source opnfv.properties
57       RPM_INSTALL_PATH=$(echo $OPNFV_RPM_URL | sed 's/'"$(basename $OPNFV_RPM_URL)"'//')
58       RPM_LIST=$RPM_INSTALL_PATH/$(basename $OPNFV_RPM_URL)
59     fi
60 fi
61
62 if [ -z "$DEPLOY_SCENARIO" ]; then
63   echo "Deploy scenario not set!"
64   exit 1
65 fi
66
67 # use local build for verify
68 if [[ "$BUILD_DIRECTORY" == *verify* ]]; then
69     if [ ! -e "${WORKSPACE}/build/lib" ]; then
70       ln -s ${WORKSPACE}/lib ${WORKSPACE}/build/lib
71     fi
72     DEPLOY_SETTINGS_DIR="${WORKSPACE}/config/deploy"
73     NETWORK_SETTINGS_DIR="${WORKSPACE}/config/network"
74     DEPLOY_CMD="$(pwd)/deploy.sh"
75     UTIL_CMD="$(pwd)/util.sh"
76     RESOURCES="${WORKSPACE}/build/images/"
77     CONFIG="${WORKSPACE}/build"
78     LIB="${WORKSPACE}/lib"
79     # Make sure python34 deps are installed
80     for dep_pkg in epel-release python34 python34-PyYAML python34-setuptools; 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     # Make sure jinja2 is installed
90     for python_pkg in jinja2; do
91       if ! python3.4 -c "import $python_pkg"; then
92         echo "$python_pkg package not found for python3.4, attempting to install..."
93         if ! sudo easy_install-3.4 $python_pkg; then
94           echo -e "Failed to install $python_pkg package for python3.4"
95           exit 1
96         fi
97       fi
98     done
99
100     # Make sure ipxe-roms-qemu package is updated to latest.
101     # This package is needed for multi virtio nic PXE boot in virtual environment.
102     sudo yum update -y ipxe-roms-qemu
103
104     if [ -z ${PYTHONPATH:-} ]; then
105         export PYTHONPATH=${WORKSPACE}/lib/python
106     else
107         export PYTHONPATH=$PYTHONPATH:${WORKSPACE}/lib/python
108     fi
109 # use RPMs
110 else
111     # find version of RPM
112     VERSION_EXTENSION=$(echo $(basename $RPM_LIST) | grep -Eo '[0-9]+\.[0-9]+-[0-9]{8}')
113     # build RPM List which already includes base Apex RPM
114     for pkg in ${APEX_PKGS}; do
115         RPM_LIST+=" ${RPM_INSTALL_PATH}/opnfv-apex-${pkg}-${VERSION_EXTENSION}.noarch.rpm"
116     done
117
118     # remove old / install new RPMs
119     if rpm -q opnfv-apex > /dev/null; then
120       INSTALLED_RPMS=$(rpm -qa | grep apex)
121       if [ -n "$INSTALLED_RPMS" ]; then
122         sudo yum remove -y ${INSTALLED_RPMS}
123       fi
124     fi
125
126     if ! sudo yum install -y $RPM_LIST; then
127       echo "Unable to install new RPMs: $RPM_LIST"
128       exit 1
129     fi
130
131     DEPLOY_CMD=opnfv-deploy
132     DEPLOY_SETTINGS_DIR="/etc/opnfv-apex/"
133     UTIL_CMD=opnfv-util
134     NETWORK_SETTINGS_DIR="/etc/opnfv-apex/"
135     RESOURCES="/var/opt/opnfv/images"
136     CONFIG="/var/opt/opnfv"
137     LIB="/var/opt/opnfv/lib"
138 fi
139
140 # set env vars to deploy cmd
141 DEPLOY_CMD="CONFIG=${CONFIG} RESOURCES=${RESOURCES} LIB=${LIB} ${DEPLOY_CMD}"
142
143 if [ "$OPNFV_CLEAN" == 'yes' ]; then
144   if [[ "$BUILD_DIRECTORY" == *verify* ]]; then
145     sudo CONFIG=${CONFIG} LIB=${LIB} ./clean.sh
146   else
147     sudo CONFIG=${CONFIG} LIB=${LIB} opnfv-clean
148   fi
149 fi
150
151 if echo ${DEPLOY_SCENARIO} | grep ipv6; then
152   IPV6_FLAG=True
153   DEPLOY_SCENARIO=$(echo ${DEPLOY_SCENARIO} |  sed 's/-ipv6//')
154   echo "INFO: IPV6 Enabled"
155 fi
156
157 echo "Deploy Scenario set to ${DEPLOY_SCENARIO}"
158 DEPLOY_FILE="${DEPLOY_SETTINGS_DIR}/${DEPLOY_SCENARIO}.yaml"
159
160 if [ ! -e "$DEPLOY_FILE" ]; then
161   echo "ERROR: Required settings file missing: Deploy settings file ${DEPLOY_FILE}"
162 fi
163
164 if [[ "$JOB_NAME" == *virtual* ]]; then
165   # settings for virtual deployment
166   if [ "$IPV6_FLAG" == "True" ]; then
167     NETWORK_FILE="${NETWORK_SETTINGS_DIR}/network_settings_v6.yaml"
168   else
169     NETWORK_FILE="${NETWORK_SETTINGS_DIR}/network_settings.yaml"
170   fi
171   DEPLOY_CMD="${DEPLOY_CMD} -v"
172 else
173   # settings for bare metal deployment
174   if [ "$IPV6_FLAG" == "True" ]; then
175     NETWORK_FILE="/root/network/network_settings_v6.yaml"
176   else
177     NETWORK_FILE="/root/network/network_settings.yaml"
178   fi
179   INVENTORY_FILE="/root/inventory/pod_settings.yaml"
180
181   if ! sudo test -e "$INVENTORY_FILE"; then
182     echo "ERROR: Required settings file missing: Inventory settings file ${INVENTORY_FILE}"
183     exit 1
184   fi
185   # include inventory file for bare metal deployment
186   DEPLOY_CMD="${DEPLOY_CMD} -i ${INVENTORY_FILE}"
187 fi
188
189 # Check that network settings file exists
190 if ! sudo test -e "$NETWORK_FILE"; then
191   echo "ERROR: Required settings file missing: Network Settings file ${NETWORK_FILE}"
192   exit 1
193 fi
194
195 # mock detached state for deploy
196 sudo $UTIL_CMD mock-detached on
197 # start deployment
198 sudo ${DEPLOY_CMD} -d ${DEPLOY_FILE} -n ${NETWORK_FILE} --debug
199 # turn of mock detached state
200 sudo $UTIL_CMD mock-detached off
201
202 echo
203 echo "--------------------------------------------------------"
204 echo "Done!"