"Fossies" - the Fresh Open Source Software Archive 
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Bash source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 #!/bin/bash
2
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14
15 # TODO(Dobroslaw): move this script to monasca-common/docker folder
16 # and leave here small script to download it and execute using env variables
17 # to minimize code duplication.
18
19 set -x # Print each script step.
20 set -eo pipefail # Exit the script if any statement returns error.
21
22 # This script is used for building Docker image with proper labels
23 # and proper version of monasca-common.
24 #
25 # Example usage:
26 # $ ./build_image.sh <repository_version> <upper_constains_branch> <common_version>
27 #
28 # Everything after `./build_image.sh` is optional and by default configured
29 # to get versions from `Dockerfile`.
30 #
31 # To build from master branch (default):
32 # $ ./build_image.sh
33 # To build specific version run this script in the following way:
34 # $ ./build_image.sh stable/queens
35 # Building from specific commit:
36 # $ ./build_image.sh cb7f226
37 # When building from a tag monasca-common will be used in version available
38 # in upper constraint file:
39 # $ ./build_image.sh 2.5.0
40 # To build image from Gerrit patch sets that is targeting branch stable/queens:
41 # $ ./build_image.sh refs/changes/51/558751/1 stable/queens
42 #
43 # If you want to build image with custom monasca-common version you need
44 # to provide it as in the following example:
45 # $ ./build_image.sh master master refs/changes/19/595719/3
46
47 # Go to folder with Docker files.
48 REAL_PATH=$(python -c "import os,sys; print(os.path.realpath('$0'))")
49 cd "$(dirname "$REAL_PATH")/../docker/"
50
51 [ -z "$DOCKER_IMAGE" ] && \
52 DOCKER_IMAGE=$(\grep DOCKER_IMAGE Dockerfile | cut -f2 -d"=")
53
54 : "${REPO_VERSION:=$1}"
55 [ -z "$REPO_VERSION" ] && \
56 REPO_VERSION=$(\grep REPO_VERSION Dockerfile | cut -f2 -d"=")
57 # Let's stick to more readable version and disable SC2001 here.
58 # shellcheck disable=SC2001
59 REPO_VERSION_CLEAN=$(echo "$REPO_VERSION" | sed 's|/|-|g')
60
61 [ -z "$APP_REPO" ] && APP_REPO=$(\grep APP_REPO Dockerfile | cut -f2 -d"=")
62 GITHUB_REPO=$(echo "$APP_REPO" | sed 's/git.openstack.org/github.com/' | \
63 sed 's/ssh:/https:/')
64
65 if [ -z "$CONSTRAINTS_FILE" ]; then
66 CONSTRAINTS_FILE=$(\grep CONSTRAINTS_FILE Dockerfile | cut -f2 -d"=") || true
67 : "${CONSTRAINTS_FILE:=http://git.openstack.org/cgit/openstack/requirements/plain/upper-constraints.txt}"
68 fi
69
70 : "${CONSTRAINTS_BRANCH:=$2}"
71 [ -z "$CONSTRAINTS_BRANCH" ] && \
72 CONSTRAINTS_BRANCH=$(\grep CONSTRAINTS_BRANCH Dockerfile | cut -f2 -d"=")
73
74 # When using stable version of repository use same stable constraints file.
75 case "$REPO_VERSION" in
76 *stable*)
77 CONSTRAINTS_BRANCH_CLEAN="$REPO_VERSION"
78 # Get monasca-common version from stable upper constraints file.
79 CONSTRAINTS_TMP_FILE=$(mktemp)
80 wget --output-document "$CONSTRAINTS_TMP_FILE" \
81 "$CONSTRAINTS_FILE"?h="$CONSTRAINTS_BRANCH_CLEAN"
82 UPPER_COMMON=$(\grep 'monasca-common' "$CONSTRAINTS_TMP_FILE")
83 # Get only version part from monasca-common.
84 UPPER_COMMON_VERSION="${UPPER_COMMON##*===}"
85 rm -rf "$CONSTRAINTS_TMP_FILE"
86 ;;
87 *)
88 CONSTRAINTS_BRANCH_CLEAN="$CONSTRAINTS_BRANCH"
89 ;;
90 esac
91
92 # Monasca-common variables.
93 if [ -z "$COMMON_REPO" ]; then
94 COMMON_REPO=$(\grep COMMON_REPO Dockerfile | cut -f2 -d"=") || true
95 : "${COMMON_REPO:=https://git.openstack.org/openstack/monasca-common}"
96 fi
97
98 : "${COMMON_VERSION:=$3}"
99 if [ -z "$COMMON_VERSION" ]; then
100 COMMON_VERSION=$(\grep COMMON_VERSION Dockerfile | cut -f2 -d"=") || true
101 if [ "$UPPER_COMMON_VERSION" ]; then
102 # Common from upper constraints file.
103 COMMON_VERSION="$UPPER_COMMON_VERSION"
104 fi
105 fi
106
107 # Clone project to temporary directory for getting proper commit number from
108 # branches and tags. We need this for setting proper image labels.
109 # Docker does not allow to get any data from inside of system when building
110 # image.
111 TMP_DIR=$(mktemp -d)
112 (
113 cd "$TMP_DIR"
114 # This many steps are needed to support gerrit patch sets.
115 git init
116 git remote add origin "$APP_REPO"
117 git fetch origin "$REPO_VERSION"
118 git reset --hard FETCH_HEAD
119 )
120 GIT_COMMIT=$(git -C "$TMP_DIR" rev-parse HEAD)
121 [ -z "${GIT_COMMIT}" ] && echo "No git commit hash found" && exit 1
122 rm -rf "$TMP_DIR"
123
124 # Do the same for monasca-common.
125 COMMON_TMP_DIR=$(mktemp -d)
126 (
127 cd "$COMMON_TMP_DIR"
128 # This many steps are needed to support gerrit patch sets.
129 git init
130 git remote add origin "$COMMON_REPO"
131 git fetch origin "$COMMON_VERSION"
132 git reset --hard FETCH_HEAD
133 )
134 COMMON_GIT_COMMIT=$(git -C "$COMMON_TMP_DIR" rev-parse HEAD)
135 [ -z "${COMMON_GIT_COMMIT}" ] && echo "No git commit hash found" && exit 1
136 rm -rf "$COMMON_TMP_DIR"
137
138 CREATION_TIME=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
139
140 docker build --no-cache \
141 --build-arg CREATION_TIME="$CREATION_TIME" \
142 --build-arg GITHUB_REPO="$GITHUB_REPO" \
143 --build-arg APP_REPO="$APP_REPO" \
144 --build-arg REPO_VERSION="$REPO_VERSION" \
145 --build-arg GIT_COMMIT="$GIT_COMMIT" \
146 --build-arg CONSTRAINTS_FILE="$CONSTRAINTS_FILE" \
147 --build-arg CONSTRAINTS_BRANCH="$CONSTRAINTS_BRANCH_CLEAN" \
148 --build-arg COMMON_REPO="$COMMON_REPO" \
149 --build-arg COMMON_VERSION="$COMMON_VERSION" \
150 --build-arg COMMON_GIT_COMMIT="$COMMON_GIT_COMMIT" \
151 --tag "$DOCKER_IMAGE":"$REPO_VERSION_CLEAN" .