Merge "Adapt push_logs after CI refactoring"
[releng.git] / jjb / apex / apex-deploy-virtual.sh
1 #!/bin/bash
2 set -o errexit
3 set -o nounset
4 set -o pipefail
5
6 # log info to console
7 echo "Starting the Apex virtual deployment."
8 echo "--------------------------------------------------------"
9 echo
10
11 if [[ $BUILD_DIRECTORY == *verify-master* ]]; then
12     cd $WORKSPACE/../${BUILD_DIRECTORY/build_output/}
13     WORKSPACE=$(pwd)
14     echo "WORKSPACE modified to $WORKSPACE"
15     cd $WORKSPACE/ci
16 elif [[ ! "$ARTIFACT_NAME" == "latest" ]]; then
17     # if artifact name is passed the pull a
18     # specific artifact from artifacts.opnfv.org
19     RPM_INSTALL_PATH=$GS_URL
20     RPM_LIST=$RPM_INSTALL_PATH/$ARTIFACT_NAME
21 else
22     if [[ $BUILD_DIRECTORY == *verify* ]]; then
23       BUILD_DIRECTORY=$WORKSPACE/../$BUILD_DIRECTORY
24       echo "BUILD DIRECTORY modified to $BUILD_DIRECTORY"
25     elif [[ $BUILD_DIRECTORY == *apex-build* ]]; then
26       BUILD_DIRECTORY=$WORKSPACE/../$BUILD_DIRECTORY
27       echo "BUILD DIRECTORY modified to $BUILD_DIRECTORY"
28     fi
29
30     if [[ -f ${BUILD_DIRECTORY}/../opnfv.properties ]]; then
31         # if opnfv.properties exists then use the
32         # local build. Source the file so we get local OPNFV vars
33         source ${BUILD_DIRECTORY}/../opnfv.properties
34         RPM_INSTALL_PATH=${BUILD_DIRECTORY}/noarch
35         RPM_LIST=$RPM_INSTALL_PATH/$(basename $OPNFV_RPM_URL)
36     else
37         if [[ $BUILD_DIRECTORY == *verify* ]]; then
38           echo "BUILD_DIRECTORY is from a verify job, so will not use latest from URL"
39           echo "Check that the slave has opnfv.properties in $BUILD_DIRECTORY"
40           exit 1
41         elif [[ $BUILD_DIRECTORY == *apex-build* ]]; then
42           echo "BUILD_DIRECTORY is from a daily job, so will not use latest from URL"
43           echo "Check that the slave has opnfv.properties in $BUILD_DIRECTORY"
44           exit 1
45         fi
46         # no opnfv.properties means use the latest from artifacts.opnfv.org
47         # get the latest.properties to get the link to the latest artifact
48         curl -s -o $WORKSPACE/opnfv.properties http://$GS_URL/latest.properties
49         [[ -f opnfv.properties ]] || exit 1
50         # source the file so we get OPNFV vars
51         source opnfv.properties
52         RPM_INSTALL_PATH=$(echo $OPNFV_RPM_URL | sed 's/'"$(basename $OPNFV_RPM_URL)"'//')
53         RPM_LIST=$RPM_INSTALL_PATH/$(basename $OPNFV_RPM_URL)
54     fi
55 fi
56
57 if [ -z "$DEPLOY_SCENARIO" ]; then
58   echo "Deploy scenario not set!"
59   exit 1
60 fi
61
62 # use local build for verify
63 if [[ $BUILD_DIRECTORY == *verify-master* ]]; then
64     if [ ! -e "${WORKSPACE}/build/lib" ]; then ln -s ${WORKSPACE}/lib ${WORKSPACE}/build/lib; fi
65     DEPLOY_CMD="CONFIG=${WORKSPACE}/build RESOURCES=${WORKSPACE}/build/images/ ./deploy.sh -c ${WORKSPACE}/build -r ${WORKSPACE}/build/images/"
66     DEPLOY_FILE="${WORKSPACE}/config/deploy/${DEPLOY_SCENARIO}.yaml"
67     NETWORK_FILE="${WORKSPACE}/config/network/network_settings.yaml"
68     # Make sure python34 is installed
69     if ! rpm -q python34 > /dev/null; then
70         sudo yum install -y epel-release
71         if ! sudo yum install -y python34; then
72             echo "Failed to install python34"
73             exit 1
74         fi
75     fi
76     if ! rpm -q python34-PyYAML > /dev/null; then
77         sudo yum install -y epel-release
78         if ! sudo yum install -y python34-PyYAML; then
79             echo "Failed to install python34-PyYAML"
80             exit 1
81         fi
82     fi
83     if ! rpm -q python34-setuptools > /dev/null; then
84         if ! sudo yum install -y python34-setuptools; then
85             echo "Failed to install python34-setuptools"
86             exit 1
87         fi
88     fi
89     if [ -z ${PYTHONPATH:-} ]; then
90         export PYTHONPATH=${WORKSPACE}/lib/python
91     else
92         export PYTHONPATH=$PYTHONPATH:${WORKSPACE}/lib/python
93     fi
94 else
95     VERSION_EXTENSION=$(echo $(basename $RPM_LIST) | grep -Eo '[0-9]+\.[0-9]+-[0-9]{8}')
96     for pkg in common undercloud opendaylight-sfc onos; do
97         RPM_LIST+=" ${RPM_INSTALL_PATH}/opnfv-apex-${pkg}-${VERSION_EXTENSION}.noarch.rpm"
98     done
99
100     # update / install the new rpm
101     if rpm -q opnfv-apex > /dev/null; then
102        INSTALLED_RPMS=$(rpm -qa | grep apex)
103        for x in $INSTALLED_RPMS; do
104          INSTALLED_RPM_VER=$(echo $x | grep -Eo '[0-9]+\.[0-9]+-[0-9]{8}')
105          # Does each RPM's version match the version required for deployment
106          if [ "$INSTALLED_RPM_VER" == "$VERSION_EXTENSION" ]; then
107            echo "RPM $x is already installed"
108          else
109            echo "RPM $x does not match version $VERSION_EXTENSION"
110            echo "Will upgrade/downgrade RPMs..."
111            # Try to upgrade/downgrade RPMS
112            if sudo yum update -y $RPM_LIST | grep "does not update installed package"; then
113              if ! sudo yum downgrade -y $RPM_LIST; then
114                sudo yum remove -y opnfv-apex-undercloud opnfv-apex-common opnfv-apex-opendaylight-sfc opnfv-apex-onos
115                if ! sudo yum downgrade -y $RPM_LIST; then
116                  echo "Unable to downgrade RPMs: $RPM_LIST"
117                  exit 1
118                fi
119              fi
120            fi
121            break
122          fi
123        done
124     else
125        sudo yum install -y $RPM_LIST;
126     fi
127     DEPLOY_CMD=opnfv-deploy
128     DEPLOY_FILE="/etc/opnfv-apex/${DEPLOY_SCENARIO}.yaml"
129     NETWORK_FILE="/etc/opnfv-apex/network_settings.yaml"
130     export RESOURCES="/var/opt/opnfv/images"
131     export CONFIG="/var/opt/opnfv"
132 fi
133
134 if [ "$OPNFV_CLEAN" == 'yes' ]; then
135     if [[ $BUILD_DIRECTORY == *verify-master* ]]; then
136         sudo CONFIG=${WORKSPACE}/build ./clean.sh
137     else
138         sudo opnfv-clean
139     fi
140 fi
141 # initiate virtual deployment
142 echo "Deploy Scenario set to ${DEPLOY_SCENARIO}"
143 if [ -e $DEPLOY_FILE ]; then
144   sudo $DEPLOY_CMD -v -d ${DEPLOY_FILE} -n $NETWORK_FILE --debug
145 else
146   echo "File does not exist /etc/opnfv-apex/${DEPLOY_SCENARIO}.yaml"
147   exit 1
148 fi
149 echo
150 echo "--------------------------------------------------------"
151 echo "Done!"