3 # -----------------------------------------------------------------------------
4 # Copyright © 2018 AT&T USA
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
18 # -----------------------------------------------------------------------------
21 # This script generates docker-compose volume overlay files from the oom
22 # kubernetes configuration files. Multiple environments can be supported. By
23 # default the environment is the 'local' environment and the docker-compose
24 # file is named docker-compose-local.yml. This script only generates the
25 # overlay files, not the docker-compose file.
27 # Overlay files contain the springboot configuration for each SO container.
29 # The idea here is that docker-compose is lighter weight than kubernetes with
30 # rancher, and people will find it easier to use in a development environment.
31 # Whenever the overlay files in oom are modified, this script can be used to
32 # (re)build overlay files for the docker-compose environment.
34 # Reasonably up-to-date overlay files for the docker-compose environment may
35 # be checked into the docker-config repository as a convenience to those who
36 # don't want to (or can't) run this script. This script will refresh those.
40 # 1) build SO software and docker images -or- pull SO docker images from nexus3
42 # 2) Create configuration for 'local' environment:
43 # ./create-configs.sh -e local -i -u rd472p -b master
45 # 3) Start the environment:
46 # docker-compose -f docker-compose-local.yml up
51 echo "usage: $(basename $0) [-e env] [-i] [-u oom-git-user] [-b oom-git-branch]"
52 echo "where -e specifies the environment name (default: 'local')"
53 echo " -i re-initializes the staging directory (default: no)"
54 echo " -u specifies the git user for cloning oom (default: current user)"
55 echo " -b specifies the oom branch (default: current docker-config branch)"
59 # Purpose: prompts for a yes/no (or equivalent) answer
61 # Return Code: 0 for yes, 1 for no
62 # Usage: askConfirm prompt
69 echo -n "$ask_prompt" >> /dev/tty
72 ask_reply=${ask_reply##+([[:space:]])};
73 ask_reply=${ask_reply%%+([[:space:]])}
87 # Purpose: performs a literal string replacement (no regexs)
88 # Output: none, but modifies the specified file
89 # Return Code: 0 for success, 1 for failure
90 # Usage: replace source replacement file
98 if ! type xxd > /dev/null 2>&1
100 echo "xxd: command not found" 1>&2
104 local xsrc=$(echo -n "$src" | xxd -p | tr -d '\n')
105 local xrpl=$(echo -n "$rpl" | xxd -p | tr -d '\n')
107 xxd -p $file | tr -d '\n' | sed "s/$xsrc/$xrpl/g" | xxd -p -r > $file.replace || return 1
108 mv $file.replace $file || return 1
113 # Purpose: converts a camel-case variable name to snake (upper) case
114 # openStackUserName -> OPEN_STACK_USER_NAME
115 # Output: the converted variable name
116 # Usage: toSnake name
120 echo "$1" | sed -r 's/([A-Z])/_\L\1/g' | sed 's/^_//' | tr '[a-z]' '[A-Z]'
124 # Purpose: lists all the environment variables in the specified docker
125 # compose yaml for the specified container, e.g.:
129 # Output: the list of variable mappings
130 # Usage: listEnv composeFile containerName
137 local inContainer=FALSE
138 local inEnvironment=FALSE
142 if [[ $line == "$container:" ]]
145 elif [[ $inContainer == TRUE && $line == "environment:" ]]
149 if [[ $inEnvironment == TRUE ]]
151 if [[ ${line:0:1} == "-" ]]
159 done < $composeFile | sed -e "s/^- '//" -e "s/'$//"
163 # Purpose: tests if the specified environment variable is defined in the
164 # docker compose yaml for the specified container.
166 # Return Code: 0 if the variable exists, 1 if it doesn't
167 # Usage: varExists composeFile containerName variableName
175 listEnv $composeFile $container | grep "^$var=" > /dev/null
180 # Purpose: returns the value of the specified environment variable
181 # from the compose yaml for the specified container.
182 # Output: the variable value (empty if it isn't defined)
183 # Usage: varValue composeFile containerName variableName
191 listEnv $composeFile $container | grep "^$var=" | cut -d= -f2
194 ### MAIN CODE STARTS HERE
196 if [[ ! -d volumes/so ]]
198 echo "You must run this command in the docker-config directory" 1>&2
205 GITBRANCH=$(git rev-parse --abbrev-ref HEAD)
207 while getopts :e:iu:b: c
229 shift $(($OPTIND - 1))
237 if [[ ! -f docker-compose-$ENV.yml ]]
239 echo "No such file: docker-compose-$ENV.yml" 1>&2
245 if [[ -d staging && $INIT == TRUE ]]
247 if ! askConfirm "Delete existing staging directory? "
252 rm -fr staging || exit 1
255 mkdir -p staging || exit 1
257 # Get the oom repository from gerrit.
259 if [[ -d staging/oom ]]
261 cd staging/oom || exit 1
263 branch=$(git rev-parse --abbrev-ref HEAD)
265 if [[ $branch != $GITBRANCH ]]
267 echo "staging/oom is not on branch '$GITBRANCH'" 1>&2
274 echo "Cloning into staging/oom"
275 git clone https://$GITUSER@gerrit.onap.org/r/a/oom || exit 1
278 branch=$(git rev-parse --abbrev-ref HEAD)
280 if [[ $branch != $GITBRANCH ]]
282 if [[ $(git ls-remote origin $GITBRANCH | wc -l) == 0 ]]
284 echo "staging/oom has no '$GITBRANCH' branch" 1>&2
288 echo "Switching staging/oom to '$GITBRANCH' branch"
289 git checkout -b $GITBRANCH remotes/origin/$GITBRANCH || exit 1
295 kso=staging/oom/kubernetes/so
299 echo "No such directory: $kso" 1>&2
303 if [[ ! -d staging/volumes/so/config ]]
305 mkdir -p staging/volumes/so/config
310 for override in $(cd $kso && find * -name 'override.yaml')
312 subdir=${override%%/*}
314 if [[ $subdir == resources ]]
316 container=so-api-handler-infra
317 elif [[ $subdir == charts ]]
319 container=${override#charts/}
320 container=${container%%/*}
323 if [[ $container != so-monitoring ]]
325 container=${container#so-}
328 target=staging/volumes/so/config/$container/$ENV/override.yaml
330 if [[ ! -f $target ]]
332 mkdir -p $(dirname $target) || exit 1
333 cp $kso/$override $target.tmp || exit 1
335 if ! varExists docker-compose-$ENV.yml $container COMMON_NAMESPACE
337 echo "ERROR: no COMMON_NAMESPACE variable is defined in docker-compose-$ENV.yml for container $container" 1>&2
341 replace '{{ include "common.namespace" . }}' '${COMMON_NAMESPACE}' $target.tmp || exit 1
343 if ! varExists docker-compose-$ENV.yml $container CONTAINER_PORT
345 echo "ERROR: no CONTAINER_PORT variable is defined in docker-compose-$ENV.yml for container $container" 1>&2
349 replace '{{ index .Values.containerPort }}' '${CONTAINER_PORT}' $target.tmp || exit 1
351 for configValue in $(grep "{{ \.Values\.config\..*}}" $target.tmp | cut -d'{' -f3 | cut -d'}' -f1 | tr -d ' ' | sed 's/.Values.config.//' | sort -u)
353 var=$(toSnake $configValue)
355 if ! varExists docker-compose-$ENV.yml $container $var
357 echo "ERROR: no $var variable is defined in docker-compose-$ENV.yml for container $container" 1>&2
361 if [[ ${var:0:11} == "OPEN_STACK_" ]]
363 # Workaround for variables in the openstack
364 # adapter cloud configuration. The override
365 # yaml is read by a migration program that
366 # doesn't expand variables, so we need to
367 # substitute the variable values here.
369 value=$(varValue docker-compose-$ENV.yml $container $var)
370 replace "{{ .Values.config.$configValue }}" "$value" $target.tmp || exit 1
372 replace "{{ .Values.config.$configValue }}" '${'$var'}' $target.tmp || exit 1
376 if grep "{{" $target.tmp > /dev/null
378 echo "ERROR: Unresolved placeholders in $kso/$override:" 1>&2
379 grep "{{.*}}" $target.tmp | cut -d'{' -f3 | cut -d'}' -f1 | sed -e 's/^/ {{/' -e 's/$/}}/' | sort -u 1>&2
383 mv $target.tmp $target || exit 1
384 echo "Created $target"
386 stagedTargets="$stagedTargets target"
390 for override in $(cd staging && find volumes -name 'override.yaml')
392 dir=$(dirname $override)
393 mkdir -p $dir || exit 1
394 cp staging/$override $override || exit 1
395 echo "Installed $override"