mcp/config: Move scenario defs to separate dir
[fuel.git] / ci / deploy.sh
1 #!/bin/bash
2 # shellcheck disable=SC2034,SC2154,SC1091
3 set -ex
4 ##############################################################################
5 # Copyright (c) 2017 Ericsson AB, Mirantis Inc., Enea AB and others.
6 # jonas.bjurel@ericsson.com
7 # All rights reserved. This program and the accompanying materials
8 # are made available under the terms of the Apache License, Version 2.0
9 # which accompanies this distribution, and is available at
10 # http://www.apache.org/licenses/LICENSE-2.0
11 ##############################################################################
12
13 ##############################################################################
14 # BEGIN of Exit handlers
15 #
16 do_exit () {
17     clean
18     echo "Exiting ..."
19 }
20 #
21 # End of Exit handlers
22 ##############################################################################
23
24 ##############################################################################
25 # BEGIN of usage description
26 #
27 usage ()
28 {
29 cat << EOF
30 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
31 $(notify "$(basename "$0"): Deploy the Fuel@OPNFV MCP stack" 3)
32
33 $(notify "USAGE:" 2)
34   $(basename "$0") -b base-uri -l lab-name -p pod-name -s deploy-scenario \\
35     [-B PXE Bridge [-B Mgmt Bridge [-B Internal Bridge [-B Public Bridge]]]]
36
37 $(notify "OPTIONS:" 2)
38   -b  Base-uri for the stack-configuration structure
39   -B  Bridge(s): 1st usage = PXE, 2nd = Mgmt, 3rd = Internal, 4th = Public
40   -h  Print this message and exit
41   -l  Lab-name
42   -p  Pod-name
43   -s  Deploy-scenario short-name
44
45 $(notify "DISABLED OPTIONS (not yet supported with MCP):" 3)
46   -d  (disabled) Dry-run
47   -e  (disabled) Do not launch environment deployment
48   -f  (disabled) Deploy on existing Salt master
49   -F  (disabled) Do only create a Salt master
50   -i  (disabled) iso url
51   -L  (disabled) Deployment log path and file name
52   -S  (disabled) Storage dir for VM images
53   -T  (disabled) Timeout, in minutes, for the deploy.
54
55 $(notify "Description:" 2)
56 Deploys the Fuel@OPNFV stack on the indicated lab resource.
57
58 This script provides the Fuel@OPNFV deployment abstraction.
59 It depends on the OPNFV official configuration directory/file structure
60 and provides a fairly simple mechanism to execute a deployment.
61
62 $(notify "Input parameters to the build script are:" 2)
63 -b Base URI to the configuration directory (needs to be provided in a URI
64    style, it can be a local resource: file:// or a remote resource http(s)://)
65 -B Bridges to be used by deploy script. It can be specified several times,
66    or as a comma separated list of bridges, or both: -B br1 -B br2,br3
67    First occurence sets PXE Brige, next Mgmt, then Internal and Public.
68    For an empty value, the deploy script will use virsh to create the default
69    expected network (e.g. -B pxe,,,public will use existing "pxe" and "public"
70    bridges, respectively create "mgmt" and "internal").
71    The default is pxebr.
72 -h Print this message and exit
73 -l Lab name as defined in the configuration directory, e.g. lf
74 -p POD name as defined in the configuration directory, e.g. pod-1
75 -s Deployment-scenario, this points to a short deployment scenario name, which
76    has to be defined in config directory (e.g. os-odl_l2-nofeature-noha).
77
78 $(notify "Disabled input parameters (not yet supported with MCP):" 3)
79 -d (disabled) Dry-run - Produce deploy config files, but do not execute deploy
80 -f (disabled) Deploy on existing Salt master
81 -e (disabled) Do not launch environment deployment
82 -F (disabled) Do only create a Salt master
83 -L (disabled) Deployment log path and name, eg. -L /home/jenkins/job.log.tar.gz
84 -S (disabled) Storage dir for VM images, default is fuel/deploy/images
85 -T (disabled) Timeout, in minutes, for the deploy.
86    It defaults to using the DEPLOY_TIMEOUT environment variable when defined.
87 -i (disabled) .iso image to be deployed (needs to be provided in a URI
88    style, it can be a local resource: file:// or a remote resource http(s)://)
89
90 $(notify "[NOTE] Root priviledges are needed for this script to run" 3)
91
92 Example:
93
94 $(notify "sudo $(basename "$0") \\
95   -b file:///home/jenkins/lab-config \\
96   -l lf -p pod1 \\
97   -s os-odl_l2-nofeature-noha" 2)
98 EOF
99 }
100
101 #
102 # END of usage description
103 ##############################################################################
104
105 ##############################################################################
106 # BEGIN of colored notification wrapper
107 #
108 notify() {
109     tput setaf "${2:-1}" || true
110     echo -en "${1:-"[WARN] Unsupported opt arg: $3\\n"}"
111     tput sgr0
112 }
113 #
114 # END of colored notification wrapper
115 ##############################################################################
116
117 ##############################################################################
118 # BEGIN of deployment clean-up
119 #
120 clean() {
121     echo "Cleaning up deploy tmp directories"
122 }
123 #
124 # END of deployment clean-up
125 ##############################################################################
126
127 ##############################################################################
128 # BEGIN of variables to customize
129 #
130 SCRIPT_PATH=$(readlink -f "$(dirname "${BASH_SOURCE[0]}")")
131 DEPLOY_DIR=$(cd "${SCRIPT_PATH}/../mcp/scripts"; pwd)
132 DEPLOY_TYPE='baremetal'
133 OPNFV_BRIDGES=('pxe' 'mgmt' 'internal' 'public')
134 URI_REGEXP='(file|https?|ftp)://.*'
135
136 export SSH_KEY=${SSH_KEY:-mcp.rsa}
137 export SALT_MASTER=${SALT_MASTER_IP:-192.168.10.100}
138 export SSH_OPTS="-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ${SSH_KEY}"
139
140 # Variables below are disabled for now, to be re-introduced or removed later
141 set +x
142 USE_EXISTING_FUEL=''
143 FUEL_CREATION_ONLY=''
144 NO_DEPLOY_ENVIRONMENT=''
145 STORAGE_DIR=''
146 DRY_RUN=0
147 if ! [ -z "${DEPLOY_TIMEOUT}" ]; then
148     DEPLOY_TIMEOUT="-dt ${DEPLOY_TIMEOUT}"
149 else
150     DEPLOY_TIMEOUT=""
151 fi
152 set -x
153 #
154 # END of variables to customize
155 ##############################################################################
156
157 ##############################################################################
158 # BEGIN of main
159 #
160 set +x
161 OPNFV_BRIDGE_IDX=0
162 while getopts "b:B:dfFl:L:p:s:S:T:i:he" OPTION
163 do
164     case $OPTION in
165         b)
166             BASE_CONFIG_URI=${OPTARG}
167             if [[ ! $BASE_CONFIG_URI =~ ${URI_REGEXP} ]]; then
168                 notify "[ERROR] -b $BASE_CONFIG_URI - invalid URI\n"
169                 usage
170                 exit 1
171             fi
172             ;;
173         B)
174             OIFS=${IFS}
175             IFS=','
176             OPT_BRIDGES=($OPTARG)
177             for bridge in "${OPT_BRIDGES[@]}"; do
178                 if [ -n "${bridge}" ]; then
179                     OPNFV_BRIDGES[${OPNFV_BRIDGE_IDX}]="${bridge}"
180                 fi
181                 OPNFV_BRIDGE_IDX=$[OPNFV_BRIDGE_IDX + 1]
182             done
183             IFS=${OIFS}
184             ;;
185         d)
186             notify '' 3 "${OPTION}"; continue
187             DRY_RUN=1
188             ;;
189         f)
190             notify '' 3 "${OPTION}"; continue
191             USE_EXISTING_FUEL='-nf'
192             ;;
193         F)
194             notify '' 3 "${OPTION}"; continue
195             FUEL_CREATION_ONLY='-fo'
196             ;;
197         e)
198             notify '' 3 "${OPTION}"; continue
199             NO_DEPLOY_ENVIRONMENT='-nde'
200             ;;
201         l)
202             TARGET_LAB=${OPTARG}
203             ;;
204         L)
205             notify '' 3 "${OPTION}"; continue
206             DEPLOY_LOG="-log ${OPTARG}"
207             ;;
208         p)
209             TARGET_POD=${OPTARG}
210             if [[ "${TARGET_POD}" =~ "virtual" ]]; then
211                 DEPLOY_TYPE='virtual'
212             fi
213             ;;
214         s)
215             DEPLOY_SCENARIO=${OPTARG}
216             ;;
217         S)
218             notify '' 3 "${OPTION}"; continue
219             if [[ ${OPTARG} ]]; then
220                 STORAGE_DIR="-s ${OPTARG}"
221             fi
222             ;;
223         T)
224             notify '' 3 "${OPTION}"; continue
225             DEPLOY_TIMEOUT="-dt ${OPTARG}"
226             ;;
227         i)
228             notify '' 3 "${OPTION}"; continue
229             ISO=${OPTARG}
230             if [[ ! $ISO =~ ${URI_REGEXP} ]]; then
231                 notify "[ERROR] -i $ISO - invalid URI\n"
232                 usage
233                 exit 1
234             fi
235             ;;
236         h)
237             usage
238             exit 0
239             ;;
240         *)
241             notify "[ERROR] Arguments not according to new argument style\n"
242             exit 1
243             ;;
244     esac
245 done
246
247 if [[ $EUID -ne 0 ]]; then
248     notify "[ERROR] This script must be run as root\n" 1>&2
249     exit 1
250 fi
251
252 # Validate mandatory arguments are set
253 # FIXME(armband): Bring back support for BASE_CONFIG_URI
254 if [ -z "${TARGET_LAB}" ] || [ -z "${TARGET_POD}" ] || \
255    [ -z "${DEPLOY_SCENARIO}" ]; then
256     notify "[ERROR] At least one of the mandatory args is missing!\n" 1>&2
257     usage
258     exit 1
259 fi
260
261 set -x
262
263 # Enable the automatic exit trap
264 trap do_exit SIGINT SIGTERM EXIT
265
266 # Set no restrictive umask so that Jenkins can removeeee any residuals
267 umask 0000
268
269 clean
270
271 pushd "${DEPLOY_DIR}" > /dev/null
272 # Prepare the deploy config files based on lab/pod information, deployment
273 # scenario, etc.
274
275 # Install required packages
276 [ -n "$(command -v apt-get)" ] && apt-get install -y \
277   git make rsync mkisofs curl virtinst cpu-checker qemu-kvm
278 [ -n "$(command -v yum)" ] && yum install -y \
279   git make rsync genisoimage curl virt-install qemu-kvm
280
281 # Check scenario file existence
282 if [[ ! -f  ../config/scenario/${DEPLOY_TYPE}/${DEPLOY_SCENARIO}.yaml ]]; then
283     notify "[WARN] ${DEPLOY_SCENARIO}.yaml not found! \
284             Setting simplest scenario (os-nosdn-nofeature-noha)\n" 3
285     DEPLOY_SCENARIO='os-nosdn-nofeature-noha'
286 fi
287
288 # Get required infra deployment data
289 source lib.sh
290 eval "$(parse_yaml "../config/scenario/${DEPLOY_TYPE}/defaults.yaml")"
291 eval "$(parse_yaml "../config/scenario/${DEPLOY_TYPE}/${DEPLOY_SCENARIO}.yaml")"
292
293 export CLUSTER_DOMAIN=${cluster_domain}
294
295 declare -A virtual_nodes_ram virtual_nodes_vcpus
296 for node in "${virtual_nodes[@]}"; do
297     virtual_custom_ram="virtual_${node}_ram"
298     virtual_custom_vcpus="virtual_${node}_vcpus"
299     virtual_nodes_ram[$node]=${!virtual_custom_ram:-$virtual_default_ram}
300     virtual_nodes_vcpus[$node]=${!virtual_custom_vcpus:-$virtual_default_vcpus}
301 done
302
303 # Infra setup
304 generate_ssh_key
305 prepare_vms virtual_nodes "${base_image}"
306 create_networks OPNFV_BRIDGES
307 create_vms virtual_nodes virtual_nodes_ram virtual_nodes_vcpus OPNFV_BRIDGES
308 update_pxe_network OPNFV_BRIDGES
309 start_vms virtual_nodes
310 check_connection
311
312 ./salt.sh
313
314 # Openstack cluster setup
315 for state in "${cluster_states[@]}"; do
316     notify "STATE: ${state}\n" 2
317     # shellcheck disable=SC2086,2029
318     ssh ${SSH_OPTS} "ubuntu@${SALT_MASTER}" \
319         sudo "/root/fuel/mcp/config/states/${state}"
320 done
321
322 popd > /dev/null
323
324 #
325 # END of main
326 ##############################################################################