|
| 1 | +#!/bin/bash |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +# or more contributor license agreements. See the NOTICE file |
| 4 | +# distributed with this work for additional information |
| 5 | +# regarding copyright ownership. The ASF licenses this file |
| 6 | +# to you under the Apache License, Version 2.0 (the |
| 7 | +# "License"); you may not use this file except in compliance |
| 8 | +# with the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, |
| 13 | +# software distributed under the License is distributed on an |
| 14 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +# KIND, either express or implied. See the License for the |
| 16 | +# specific language governing permissions and limitations |
| 17 | +# under the License. |
| 18 | + |
| 19 | +set -e |
| 20 | + |
| 21 | +# Flag to show help text |
| 22 | +show_help=false |
| 23 | + |
| 24 | +# Workspace path |
| 25 | +workspace_path="" |
| 26 | + |
| 27 | +# Using remote git repository variables |
| 28 | +use_remote=false |
| 29 | +git_remote="" |
| 30 | +git_ref="" |
| 31 | +remove_first=false |
| 32 | + |
| 33 | +# packaging flags to be sent to script |
| 34 | +PKG_ARGS="" |
| 35 | +HELP_ARG="" |
| 36 | + |
| 37 | +while [ -n "$1" ]; do |
| 38 | + case "$1" in |
| 39 | + --git-remote) |
| 40 | + if [ -n "$git_remote" ]; then |
| 41 | + echo "Error: you have already entered value for --git-remote" |
| 42 | + exit 1 |
| 43 | + else |
| 44 | + git_remote=$2 |
| 45 | + use_remote=true |
| 46 | + shift 2 |
| 47 | + fi |
| 48 | + ;; |
| 49 | + |
| 50 | + --git-ref) |
| 51 | + if [ -n "$git_ref" ]; then |
| 52 | + echo "Error: you have already entered value for --git-ref" |
| 53 | + exit 1 |
| 54 | + else |
| 55 | + git_ref=$2 |
| 56 | + use_remote=true |
| 57 | + shift 2 |
| 58 | + fi |
| 59 | + ;; |
| 60 | + |
| 61 | + --remove-first) |
| 62 | + if [ $remove_first = true ]; then |
| 63 | + echo "Error: you have already entered --remove_first" |
| 64 | + exit 1 |
| 65 | + else |
| 66 | + remove_first=true |
| 67 | + shift 1 |
| 68 | + fi |
| 69 | + ;; |
| 70 | + |
| 71 | + --workspace-path) |
| 72 | + if [ -n "$workspace_path" ]; then |
| 73 | + echo "Error: you have already entered value for --workspace-path" |
| 74 | + exit 1 |
| 75 | + else |
| 76 | + workspace_path=$2 |
| 77 | + shift 2 |
| 78 | + fi |
| 79 | + ;; |
| 80 | + |
| 81 | + -h | --help) |
| 82 | + if [ $show_help = true ]; then |
| 83 | + echo "Error: you have already entered -h, --help" |
| 84 | + exit 1 |
| 85 | + else |
| 86 | + show_help=true |
| 87 | + HELP_ARG="$1" |
| 88 | + shift 1 |
| 89 | + fi |
| 90 | + ;; |
| 91 | + |
| 92 | + -* | --* | *) |
| 93 | + PKG_ARGS="$PKG_ARGS $1" |
| 94 | + shift 1 |
| 95 | + ;; |
| 96 | + esac |
| 97 | +done |
| 98 | + |
| 99 | +set -- $PKG_ARGS |
| 100 | + |
| 101 | +# use '/mnt/build/cloudstack' as default workspace path |
| 102 | +if [ -z "$workspace_path" ]; then |
| 103 | + workspace_path="/mnt/build/cloudstack" |
| 104 | +fi |
| 105 | + |
| 106 | +# Both of --git-remote AND --git-ref must be specified at the same time |
| 107 | +if [ $use_remote = true ]; then |
| 108 | + if [ -z "$git_remote" -o -z "$git_ref" ]; then |
| 109 | + echo "Error: you must specify --git-remote and --git-ref at the same time" |
| 110 | + exit 1 |
| 111 | + fi |
| 112 | +fi |
| 113 | + |
| 114 | +# Check if cloudstack directory exists or not. Options are either: |
| 115 | +# |
| 116 | +# 1) cloudstack directory is provided through the host's volume |
| 117 | +# 2) cloudstack directory is NOT provided and git remote and ref are provided |
| 118 | +# |
| 119 | +# Any combination of the above situations is invalid. |
| 120 | +if [ -d "${workspace_path}" ]; then |
| 121 | + if [ $use_remote = true ]; then |
| 122 | + if [ $remove_first = false ]; then |
| 123 | + echo "Error: Could not clone remote git repository, '${workspace_path}' exists" |
| 124 | + exit 1 |
| 125 | + else |
| 126 | + echo "Removing ${workspace_path} ..." |
| 127 | + rm -rf ${workspace_path} |
| 128 | + echo -e "\n--------\n" |
| 129 | + fi |
| 130 | + fi |
| 131 | +else |
| 132 | + if [ $use_remote = false ]; then |
| 133 | + echo "Could not find '${workspace_path}'" |
| 134 | + exit 1 |
| 135 | + fi |
| 136 | +fi |
| 137 | + |
| 138 | +# Print out some environment information |
| 139 | +echo -e "System information:" |
| 140 | +cat /etc/*-release |
| 141 | + |
| 142 | +echo -e "\nGit version:" |
| 143 | +git --version |
| 144 | + |
| 145 | +echo -e "\nJava version:" |
| 146 | +java -version |
| 147 | + |
| 148 | +echo -e "\nMaven version:" |
| 149 | +mvn --version |
| 150 | + |
| 151 | +echo -e "\nPython version:" |
| 152 | +python --version |
| 153 | + |
| 154 | +echo -e "\ndpkg version:" |
| 155 | +dpkg --version |
| 156 | + |
| 157 | +echo -e "\ndevscripts version:" |
| 158 | +dpkg -s devscripts | grep "Version:" | awk '{print $2}' |
| 159 | + |
| 160 | +echo -e "\ndebhelper version:" |
| 161 | +dpkg -s debhelper | grep "Version:" | awk '{print $2}' |
| 162 | + |
| 163 | +echo -e "\ngenisoimage version:" |
| 164 | +genisoimage --version |
| 165 | + |
| 166 | +echo -e "\nlsb-release version:" |
| 167 | +dpkg -s lsb-release | grep "Version:" | awk '{print $2}' |
| 168 | + |
| 169 | +echo -e "\nbuild-essential version:" |
| 170 | +dpkg -s build-essential | grep "Version:" | awk '{print $2}' |
| 171 | + |
| 172 | +echo -e "\n--------\n" |
| 173 | + |
| 174 | +# Clone the remote provided git repo and ref |
| 175 | +if [ $use_remote = true ]; then |
| 176 | + echo "Cloning $git_remote ..." |
| 177 | + git clone --quiet --depth=50 $git_remote ${workspace_path} |
| 178 | + |
| 179 | + cd ${workspace_path} |
| 180 | + |
| 181 | + echo "Fetching $git_ref ..." |
| 182 | + git fetch --quiet origin +$git_ref: |
| 183 | + |
| 184 | + echo "Checking out $git_ref ..." |
| 185 | + git checkout --quiet --force FETCH_HEAD |
| 186 | + |
| 187 | + echo -e "\n--------\n" |
| 188 | +fi |
| 189 | + |
| 190 | +# Make sure build-deb.sh script exists before going any further |
| 191 | +if [ ! -f "${workspace_path}/packaging/build-deb.sh" ]; then |
| 192 | + echo "Could not find '${workspace_path}/packaging/build-deb.sh'" |
| 193 | + exit 1 |
| 194 | +fi |
| 195 | + |
| 196 | +# convert LONG flags to SHORT flags for anything prior 4.12.x.x |
| 197 | +echo "Detecting CloudStack version ..." |
| 198 | +pom_version=$(cd ${workspace_path}; mvn -q -Dexec.executable="echo" -Dexec.args='${project.version}' --non-recursive org.codehaus.mojo:exec-maven-plugin:1.3.1:exec) |
| 199 | +echo "${pom_version}" |
| 200 | +major_version=$(echo ${pom_version} | cut -d. -f1) |
| 201 | +minor_version=$(echo ${pom_version} | cut -d. -f2) |
| 202 | +echo -e "\n--------\n" |
| 203 | + |
| 204 | +if [ $major_version -lt 4 ] || [ $major_version -eq 4 -a $minor_version -lt 12 ]; then |
| 205 | + if [ $show_help = true ]; then |
| 206 | + HELP_ARG="" |
| 207 | + fi |
| 208 | +fi |
| 209 | + |
| 210 | +# Show help, both from docker-entrypoint.sh and ${workspace_path}/packaging/build-deb.sh |
| 211 | +if [ $show_help = true ]; then |
| 212 | + if [ -n "$HELP_ARG" ]; then |
| 213 | + help=$(cd ${workspace_path}/packaging; bash -x ./build-deb.sh $HELP_ARG) |
| 214 | + else |
| 215 | + help="" |
| 216 | + fi |
| 217 | + cat << USAGE |
| 218 | +Usage: docker run ... khos2ow/cloudstack-deb-builder [DOCKER_OPTIONS] ... [PACKAGING_OPTIONS]... |
| 219 | +CloudStack DEB builder which acts as a wrapper for CloudStack package script. Optionally |
| 220 | +you can specify remote git repository and ref to be cloned and checked out and run the |
| 221 | +packaging script on in. |
| 222 | +
|
| 223 | +Optional arguments: |
| 224 | + --git-remote string Set the git remote repository to clone (must be set together with \`--git-ref\`) (default: none) |
| 225 | + --git-ref string Set the ref from remote repository to check out (must be set together with \`--git-remote\`) (default: none) |
| 226 | + --remove-first Remove existing \`${workspace_path}\` directory before cloning (default: false) |
| 227 | + --workspace-path string Set the directory path of workspace to work with (default: \`/mnt/build/cloudstack\`) |
| 228 | +
|
| 229 | +Other arguments: |
| 230 | + -h, --help Display this help message and exit |
| 231 | +
|
| 232 | +Examples: |
| 233 | + docker run ... khos2ow/cloudstack-deb-builder [PACKAGING_OPTIONS] ... |
| 234 | + docker run ... khos2ow/cloudstack-deb-builder --git-remote https://path.to.repo/cloudstack.git --git-ref foo-branch [PACKAGING_OPTIONS] ... |
| 235 | +
|
| 236 | +-------- |
| 237 | +
|
| 238 | +$help |
| 239 | +
|
| 240 | +USAGE |
| 241 | + exit 0 |
| 242 | +fi |
| 243 | + |
| 244 | +# Adjust user and group provided by host |
| 245 | +function adjust_owner() { |
| 246 | + # if both set then change the owner |
| 247 | + if [ -n "${USER_ID}" -a -z "${USER_GID}" ]; then |
| 248 | + chown -R ${USER_ID} ${workspace_path} |
| 249 | + elif [ -n "${USER_ID}" -a -n "${USER_GID}" ]; then |
| 250 | + chown -R ${USER_ID}:${USER_GID} ${workspace_path} |
| 251 | + fi |
| 252 | +} |
| 253 | + |
| 254 | +{ |
| 255 | + cd ${workspace_path}/packaging |
| 256 | + |
| 257 | + # do the packaging |
| 258 | + bash -x ./build-deb.sh $@ && { |
| 259 | + mkdir -p ${workspace_path}/dist/debbuild/DEBS |
| 260 | + |
| 261 | + cp ${workspace_path}/../cloudstack-*.deb ${workspace_path}/dist/debbuild/DEBS |
| 262 | + cp ${workspace_path}/../cloudstack_*.changes ${workspace_path}/dist/debbuild/DEBS |
| 263 | + |
| 264 | + adjust_owner |
| 265 | + } |
| 266 | +} || { |
| 267 | + status=$? |
| 268 | + |
| 269 | + adjust_owner |
| 270 | + echo "Packaging DEB failed" |
| 271 | + exit $status |
| 272 | +} |
0 commit comments