a1f5e386bf9d3f781c47c3a19e2217b0689ef3ca
[fuel.git] / ci / deploy.sh
1 #!/bin/bash
2 set -ex
3 ##############################################################################
4 # Copyright (c) 2017 Ericsson AB, Mirantis Inc. and others.
5 # jonas.bjurel@ericsson.com
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
11
12 ############################################################################
13 # BEGIN of Exit handlers
14 #
15 do_exit () {
16     clean
17     echo "Exiting ..."
18 }
19 #
20 # End of Exit handlers
21 ############################################################################
22
23 ############################################################################
24 # BEGIN of usage description
25 #
26 usage ()
27 {
28 cat << EOF
29 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
30 `basename $0`: Deploys the Fuel@OPNFV stack
31
32 usage: `basename $0` -b base-uri [-B PXE Bridge] [-f] [-F] [-H] -l lab-name -p pod-name -s deploy-scenario [-S image-dir] [-T timeout] -i iso
33        -s deployment-scenario [-S optional Deploy-scenario path URI]
34        [-R optional local relen repo (containing deployment Scenarios]
35
36 OPTIONS:
37   -b  Base-uri for the stack-configuration structure
38   -B  PXE Bridge for booting of Fuel master
39   -d  Dry-run
40   -f  Deploy on existing Fuel master
41   -e  Do not launch environment deployment
42   -F  Do only create a Fuel master
43   -h  Print this message and exit
44   -H  No health check
45   -l  Lab-name
46   -L  Deployment log path and file name
47   -p  Pod-name
48   -s  Deploy-scenario short-name/base-file-name
49   -S  Storage dir for VM images
50   -T  Timeout, in minutes, for the deploy.
51   -i  iso url
52
53 Description:
54 Deploys the Fuel@OPNFV stack on the indicated lab resource
55
56 This script provides the Fuel@OPNFV deployment abstraction
57 It depends on the OPNFV official configuration directory/file structure
58 and provides a fairly simple mechanism to execute a deployment.
59 Input parameters to the build script is:
60 -b Base URI to the configuration directory (needs to be provided in a URI
61    style, it can be a local resource: file:// or a remote resource http(s)://)
62 -B PXE Bridge for booting of Fuel master. It can be specified several times,
63    or as a comma separated list of bridges, or both: -B br1 -B br2,br3
64    One NIC connected to each specified bridge will be created in the Fuel VM,
65    in the same order as provided in the command line. The default is pxebr.
66 -d Dry-run - Produces deploy config files (config/dea.yaml and
67    config/dha.yaml), but does not execute deploy
68 -f Deploy on existing Fuel master
69 -e Do not launch environment deployment
70 -F Do only create a Fuel master
71 -h Print this message and exit
72 -H Do not run fuel built in health-check after successfull deployment
73 -l Lab name as defined in the configuration directory, e.g. lf
74 -L Deployment log path and name, eg. -L /home/jenkins/logs/job888.log.tar.gz
75 -p POD name as defined in the configuration directory, e.g. pod-1
76 -s Deployment-scenario, this points to a deployment/test scenario file as
77    defined in the configuration directory:
78    e.g fuel-ocl-heat-ceilometer_scenario_0.0.1.yaml
79    or a deployment short-name as defined by scenario.yaml in the deployment
80    scenario path.
81 -S Storage dir for VM images, default is fuel/deploy/images
82 -T Timeout, in minutes, for the deploy. It defaults to using the DEPLOY_TIMEOUT
83    environment variable when defined, or to the default in deploy.py otherwise
84 -i .iso image to be deployed (needs to be provided in a URI
85    style, it can be a local resource: file:// or a remote resource http(s)://)
86
87 NOTE: Root priviledges are needed for this script to run
88
89
90 Examples:
91 sudo `basename $0` -b file:///home/jenkins/lab-config -l lf -p pod1 -s ha_odl-l3_heat_ceilometer -i file:///home/jenkins/myiso.iso
92 EOF
93 }
94
95 #
96 # END of usage description
97 ############################################################################
98
99 ############################################################################
100 # BEGIN of deployment clean-up
101 #
102 clean() {
103     echo "Cleaning up deploy tmp directories"
104     rm -rf ${SCRIPT_PATH}/ISO
105 }
106 #
107 # END of deployment clean-up
108 ############################################################################
109
110 ############################################################################
111 # BEGIN of shorthand variables for internal use
112 #
113 SCRIPT_PATH=$(readlink -f $(dirname ${BASH_SOURCE[0]}))
114 DEPLOY_DIR=$(cd ${SCRIPT_PATH}/../mcp/scripts; pwd)
115 PXE_BRIDGE=''
116 NO_HEALTH_CHECK=''
117 USE_EXISTING_FUEL=''
118 FUEL_CREATION_ONLY=''
119 NO_DEPLOY_ENVIRONMENT=''
120 STORAGE_DIR=''
121 DRY_RUN=0
122 if ! [ -z $DEPLOY_TIMEOUT ]; then
123     DEPLOY_TIMEOUT="-dt $DEPLOY_TIMEOUT"
124 else
125     DEPLOY_TIMEOUT=""
126 fi
127 #
128 # END of variables to customize
129 ############################################################################
130
131 ############################################################################
132 # BEGIN of main
133 #
134 while getopts "b:B:dfFHl:L:p:s:S:T:i:he" OPTION
135 do
136     case $OPTION in
137         b)
138             BASE_CONFIG_URI=${OPTARG}
139             if [[ ! $BASE_CONFIG_URI == file://* ]] && \
140                [[ ! $BASE_CONFIG_URI == http://* ]] && \
141                [[ ! $BASE_CONFIG_URI == https://* ]] && \
142                [[ ! $BASE_CONFIG_URI == ftp://* ]]; then
143                 echo "-b $BASE_CONFIG_URI - Not given in URI style"
144                 usage
145                 exit 1
146             fi
147             ;;
148         B)
149             for bridge in ${OPTARG//,/ }; do
150                 PXE_BRIDGE+=" -b $bridge"
151             done
152             ;;
153         d)
154             DRY_RUN=1
155             ;;
156         f)
157             USE_EXISTING_FUEL='-nf'
158             ;;
159         F)
160             FUEL_CREATION_ONLY='-fo'
161             ;;
162         e)
163             NO_DEPLOY_ENVIRONMENT='-nde'
164             ;;
165         H)
166             NO_HEALTH_CHECK='-nh'
167             ;;
168         l)
169             TARGET_LAB=${OPTARG}
170             ;;
171         L)
172             DEPLOY_LOG="-log ${OPTARG}"
173             ;;
174         p)
175             TARGET_POD=${OPTARG}
176             ;;
177         s)
178             DEPLOY_SCENARIO=${OPTARG}
179             ;;
180         S)
181             if [[ ${OPTARG} ]]; then
182                 STORAGE_DIR="-s ${OPTARG}"
183             fi
184             ;;
185         T)
186             DEPLOY_TIMEOUT="-dt ${OPTARG}"
187             ;;
188         i)
189             ISO=${OPTARG}
190             if [[ ! $ISO == file://* ]] && \
191                [[ ! $ISO == http://* ]] && \
192                [[ ! $ISO == https://* ]] && \
193                [[ ! $ISO == ftp://* ]]; then
194                 echo "-i $ISO - Not given in URI style"
195                 usage
196                 exit 1
197             fi
198             ;;
199         h)
200             usage
201             exit 0
202             ;;
203         *)
204             echo "${OPTION} is not a valid argument"
205             echo "Arguments not according to new argument style"
206             echo "Trying old-style compatibility mode"
207             pushd ${DEPLOY_DIR} > /dev/null
208             python deploy.py "$@"
209             popd > /dev/null
210             exit 0
211             ;;
212     esac
213 done
214
215 if [[ $EUID -ne 0 ]]; then
216     echo "This script must be run as root" 1>&2
217     exit 1
218 fi
219
220 # Enable the automatic exit trap
221 trap do_exit SIGINT SIGTERM EXIT
222
223 # Set no restrictive umask so that Jenkins can removeeee any residuals
224 umask 0000
225
226 clean
227
228 pushd ${DEPLOY_DIR} > /dev/null
229 # Prepare the deploy config files based on lab/pod information, deployment
230 # scenario, etc.
231
232 # Install required packages
233 [ -n "$(command -v apt-get)" ] && apt-get install -y mkisofs curl virtinst cpu-checker qemu-kvm
234 [ -n "$(command -v yum)" ] && yum install -y genisoimage curl virt-install qemu-kvm
235
236 # Check scenario file existence
237 if [[ ! -f  ../config/${DEPLOY_SCENARIO}.yaml ]]; then
238     echo "[WARN] ${DEPLOY_SCENARIO}.yaml not found, setting simplest scenario"
239     DEPLOY_SCENARIO='os-nosdn-nofeature-noha'
240 fi
241
242 # Get required infra deployment data
243 source lib.sh
244 eval $(parse_yaml ../config/defaults.yaml)
245 eval $(parse_yaml ../config/${DEPLOY_SCENARIO}.yaml)
246
247 declare -A virtual_nodes_ram virtual_nodes_vcpus
248 for node in "${virtual_nodes[@]}"; do
249     virtual_custom_ram="virtual_${node}_ram"
250     virtual_custom_vcpus="virtual_${node}_vcpus"
251     virtual_nodes_ram[$node]=${!virtual_custom_ram:-$virtual_default_ram}
252     virtual_nodes_vcpus[$node]=${!virtual_custom_vcpus:-$virtual_default_vcpus}
253 done
254
255 export CLUSTER_DOMAIN=$cluster_domain
256 export SSH_KEY=${SSH_KEY:-mcp.rsa}
257 export SALT_MASTER=${SALT_MASTER_IP:-192.168.10.100}
258 export SSH_OPTS="-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ${SSH_KEY}"
259
260 # Infra setup
261 generate_ssh_key
262 prepare_vms virtual_nodes $base_image
263 create_networks
264 create_vms virtual_nodes virtual_nodes_ram virtual_nodes_vcpus
265 update_pxe_network
266 start_vms virtual_nodes
267 check_connection
268
269 ./salt.sh
270
271 # Openstack cluster setup
272 for state in "${cluster_states[@]}"; do
273     echo "STATE: $state"
274     ssh ${SSH_OPTS} ubuntu@${SALT_MASTER} sudo /root/fuel/mcp/config/states/$state
275 done
276
277 ## Disable Fuel deployment engine
278 #
279 # echo "python deploy-config.py -dha ${BASE_CONFIG_URI}/labs/${TARGET_LAB}/${TARGET_POD}/fuel/config/dha.yaml -deab file://${DEPLOY_DIR}/config/dea_base.yaml -deao ${BASE_CONFIG_URI}/labs/${TARGET_LAB}/${TARGET_POD}/fuel/config/dea-pod-override.yaml -scenario-base-uri file://${DEPLOY_DIR}/scenario -scenario ${DEPLOY_SCENARIO} -plugins file://${DEPLOY_DIR}/config/plugins -output ${SCRIPT_PATH}/config"
280 #
281 # python deploy-config.py -dha ${BASE_CONFIG_URI}/labs/${TARGET_LAB}/${TARGET_POD}/fuel/config/dha.yaml -deab file://${DEPLOY_DIR}/config/dea_base.yaml -deao ${BASE_CONFIG_URI}/labs/${TARGET_LAB}/${TARGET_POD}/fuel/config/dea-pod-override.yaml -scenario-base-uri file://${DEPLOY_DIR}/scenario -scenario ${DEPLOY_SCENARIO} -plugins file://${DEPLOY_DIR}/config/plugins -output ${SCRIPT_PATH}/config
282 #
283 # if [ $DRY_RUN -eq 0 ]; then
284 #     # Download iso if it doesn't already exists locally
285 #     if [[ $ISO == file://* ]]; then
286 #         ISO=${ISO#file://}
287 #     else
288 #         mkdir -p ${SCRIPT_PATH}/ISO
289 #         curl -o ${SCRIPT_PATH}/ISO/image.iso $ISO
290 #         ISO=${SCRIPT_PATH}/ISO/image.iso
291 #     fi
292 #     # Start deployment
293 #     echo "python deploy.py $DEPLOY_LOG $STORAGE_DIR $PXE_BRIDGE $USE_EXISTING_FUEL $FUEL_CREATION_ONLY $NO_HEALTH_CHECK $NO_DEPLOY_ENVIRONMENT -dea ${SCRIPT_PATH}/config/dea.yaml -dha ${SCRIPT_PATH}/config/dha.yaml -iso $ISO $DEPLOY_TIMEOUT"
294 #     python deploy.py $DEPLOY_LOG $STORAGE_DIR $PXE_BRIDGE $USE_EXISTING_FUEL $FUEL_CREATION_ONLY $NO_HEALTH_CHECK $NO_DEPLOY_ENVIRONMENT -dea ${SCRIPT_PATH}/config/dea.yaml -dha ${SCRIPT_PATH}/config/dha.yaml -iso $ISO $DEPLOY_TIMEOUT
295 # fi
296 popd > /dev/null
297
298 #
299 # END of main
300 ############################################################################