Petr Ospalý | 170d94b | 2018-12-20 16:40:58 +0100 | [diff] [blame] | 1 | #! /usr/bin/env bash |
| 2 | |
| 3 | # COPYRIGHT NOTICE STARTS HERE |
| 4 | # |
| 5 | # Copyright 2018 © Samsung Electronics Co., Ltd. |
| 6 | # |
| 7 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | # you may not use this file except in compliance with the License. |
| 9 | # You may obtain a copy of the License at |
| 10 | # |
| 11 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | # |
| 13 | # Unless required by applicable law or agreed to in writing, software |
| 14 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | # See the License for the specific language governing permissions and |
| 17 | # limitations under the License. |
| 18 | # |
| 19 | # COPYRIGHT NOTICE ENDS HERE |
| 20 | |
| 21 | |
| 22 | # Scope of this packaging script is to generate tarfiles for offline installation |
| 23 | # Build of any additional artifacts is out of scope for this script |
| 24 | |
| 25 | crash () { |
| 26 | local exit_code="$1" |
| 27 | local cause="$2" |
| 28 | echo "Packaging script finished prematuraly" |
| 29 | echo "Cause: $2" |
| 30 | exit "${exit_code}" |
| 31 | } |
| 32 | |
| 33 | usage () { |
| 34 | echo "Usage:" |
Samuli Silvius | 730dc19 | 2019-02-04 14:26:27 +0200 | [diff] [blame] | 35 | echo " ./$(basename $0) <project_name> <version> <packaging_target_dir> [--conf <file>]" |
| 36 | echo "Example: ./$(basename $0) myproject 1.0.1 /tmp/package --conf ~/myproject.conf" |
Petr Ospalý | 170d94b | 2018-12-20 16:40:58 +0100 | [diff] [blame] | 37 | echo "packaging_target_dir will be created if does not exist. All tars will be produced into it." |
| 38 | } |
| 39 | |
| 40 | function create_tar { |
| 41 | local tar_dir="$1" |
| 42 | local tar_name="$2" |
| 43 | |
| 44 | cd ${tar_dir} |
| 45 | touch ${tar_name} # Trick to avoid sporadic "tar: .: file changed as we read it" warning message |
| 46 | tar --exclude=${tar_name} -cf ../${tar_name} . |
| 47 | cd - &> /dev/null # Trick to avoid printing new dir on stdout |
| 48 | |
| 49 | # Remove packaged folders |
| 50 | find ${tar_dir}/* -maxdepth 0 -type d -exec rm -rf '{}' \; |
| 51 | # Remove packaged files |
| 52 | find ${tar_dir}/* ! -name ${tar_name} -exec rm '{}' \; |
| 53 | echo "tar file ${tar_name} created in target dir" |
| 54 | } |
| 55 | |
| 56 | function add_metadata { |
| 57 | local metafile="$1" |
| 58 | |
| 59 | echo "Project name: ${PROJECT_NAME}" >> "${metafile}" |
| 60 | echo "Project version: ${PROJECT_VERSION}" >> "${metafile}" |
| 61 | echo "Package date: ${TIMESTAMP}" >> "${metafile}" |
| 62 | } |
| 63 | |
| 64 | function add_additions { |
| 65 | local source="$1" |
| 66 | local target="$2" |
| 67 | |
| 68 | if [ -d "${source}" ]; then |
| 69 | mkdir -p "${target}/$(basename $source)" |
| 70 | cp -r "${source}" "${target}" |
| 71 | echo "Adding directory ... $(basename $source)" |
| 72 | else |
| 73 | if [ -f "${source}" ]; then |
| 74 | cp "${source}" "${target}" |
| 75 | echo "Adding file ... $(basename $source)" |
| 76 | else |
| 77 | crash 4 "Invalid source specified for packaging: $1" |
| 78 | fi |
| 79 | fi |
| 80 | } |
| 81 | |
Samuli Silvius | 3fb890c | 2019-01-19 14:27:58 +0200 | [diff] [blame] | 82 | function build_sw_artifacts { |
Tomáš Levora | ade405a | 2019-01-29 14:25:04 +0100 | [diff] [blame] | 83 | cd ../ansible/docker |
Samuli Silvius | 3fb890c | 2019-01-19 14:27:58 +0200 | [diff] [blame] | 84 | ./build_ansible_image.sh |
| 85 | if [ $? -ne 0 ]; then |
| 86 | crash 5 "Building of ansible runner image failed." |
| 87 | fi |
| 88 | cd - |
| 89 | } |
| 90 | |
Petr Ospalý | 170d94b | 2018-12-20 16:40:58 +0100 | [diff] [blame] | 91 | function create_sw_package { |
Samuli Silvius | 730dc19 | 2019-02-04 14:26:27 +0200 | [diff] [blame] | 92 | local pkg_root="${PACKAGING_TARGET_DIR}/sw" |
Petr Ospalý | 170d94b | 2018-12-20 16:40:58 +0100 | [diff] [blame] | 93 | |
| 94 | # Create tar package |
| 95 | echo "[Creating software package]" |
| 96 | |
| 97 | # Create directory structure of the sw package |
| 98 | mkdir -p "${pkg_root}" |
| 99 | cp -r ansible "${pkg_root}" |
| 100 | |
| 101 | # Add additional files/dirs into package based on package.conf |
| 102 | for item in "${SW_PACKAGE_ADDONS[@]}";do |
| 103 | # all SW package addons are expected within ./ansible/application folder |
| 104 | add_additions "${item}" "${pkg_root}/ansible/application" |
| 105 | done |
| 106 | |
| 107 | # Helm charts handling |
Samuli Silvius | 6e5b45a | 2019-02-04 12:52:20 +0200 | [diff] [blame] | 108 | if [ ! -z "${HELM_CHARTS_DIR}" ]; |
| 109 | then |
| 110 | echo "Helm charts handling" |
| 111 | # Copy charts available for ansible playbook to use/move them to target server/dir |
Samuli Silvius | fe11111 | 2019-02-05 09:45:24 +0200 | [diff] [blame^] | 112 | mkdir -p ${pkg_root}/${HELM_CHARTS_DIR_IN_PACKAGE} |
| 113 | cp -r "${HELM_CHARTS_DIR}"/* ${pkg_root}/${HELM_CHARTS_DIR_IN_PACKAGE} |
Samuli Silvius | 6e5b45a | 2019-02-04 12:52:20 +0200 | [diff] [blame] | 114 | fi |
Petr Ospalý | 170d94b | 2018-12-20 16:40:58 +0100 | [diff] [blame] | 115 | |
| 116 | # Add metadata to the package |
| 117 | add_metadata "${pkg_root}"/package.info |
| 118 | |
| 119 | # Create sw tar package |
| 120 | echo "Creating tar file ..." |
| 121 | PACKAGE_BASE_NAME="${SOFTWARE_PACKAGE_BASENAME}" |
| 122 | create_tar "${pkg_root}" ${PACKAGE_BASE_NAME}-${PROJECT_NAME}-${PROJECT_VERSION}-sw.tar |
| 123 | rm -rf "${pkg_root}" |
| 124 | } |
| 125 | |
| 126 | function create_resource_package { |
| 127 | local pkg_root="${PACKAGING_TARGET_DIR}/resources" |
| 128 | |
| 129 | # Create resource tar package |
| 130 | echo "[Creating resource package]" |
| 131 | |
| 132 | # Create directory structure of the resource package |
| 133 | mkdir -p "${pkg_root}" |
| 134 | |
| 135 | # Add artifacts into resource packagee based on package.conf config |
| 136 | for item in "${EXTERNAL_BINARIES_PACKAGE_ADDONS[@]}";do |
| 137 | if [ "$(basename $item)" == "resources" ]; then |
| 138 | echo "Note: Packaging all resources at once" |
| 139 | add_additions "${item}" "${PACKAGING_TARGET_DIR}" |
| 140 | else |
| 141 | add_additions "${item}" "${pkg_root}" |
| 142 | fi |
| 143 | done |
| 144 | |
| 145 | # tar file with nexus_data is expected, we should find and untar it |
| 146 | # before resource.tar is created |
| 147 | for i in `ls -1 ${pkg_root} | grep tar`; do |
| 148 | tar tvf "${pkg_root}/${i}" | grep nexus_data &> /dev/null |
| 149 | if [ $? -eq 0 ]; then |
| 150 | echo "Debug: tar file with nexus blobs detected ${pkg_root}/${i}. Start unarchive ..." |
| 151 | tar xf "${pkg_root}/${i}" -C "${pkg_root}" &> /dev/null |
| 152 | echo "Debug: unarchive finished. Removing original file" |
| 153 | rm -f "${pkg_root}/${i}" |
| 154 | fi |
| 155 | done |
| 156 | |
| 157 | echo "Creating tar file ..." |
| 158 | PACKAGE_BASE_NAME="${SOFTWARE_PACKAGE_BASENAME}" |
| 159 | create_tar "${pkg_root}" "${PACKAGE_BASE_NAME}-${PROJECT_NAME}-${PROJECT_VERSION}-resources.tar" |
| 160 | rm -rf "${pkg_root}" |
| 161 | } |
| 162 | |
| 163 | function create_aux_package { |
| 164 | local pkg_root="${PACKAGING_TARGET_DIR}/aux" |
| 165 | |
| 166 | # Create aux resource tar package |
| 167 | echo "Creating aux resource package" |
| 168 | |
| 169 | # Create directory structure of the aux resource package |
| 170 | mkdir -p "${pkg_root}" |
| 171 | |
| 172 | # Add artifacts into resource packagee based on package.conf config |
| 173 | for item in "${AUX_BINARIES_PACKAGE_ADDONS[@]}";do |
| 174 | add_additions "${item}" "${pkg_root}" |
| 175 | done |
| 176 | |
| 177 | echo "Creating tar file ..." |
| 178 | PACKAGE_BASE_NAME="${SOFTWARE_PACKAGE_BASENAME}" |
| 179 | create_tar "${pkg_root}" "${PACKAGE_BASE_NAME}-${PROJECT_NAME}-${PROJECT_VERSION}-aux-resources.tar" |
| 180 | rm -rf "${pkg_root}" |
| 181 | } |
| 182 | |
| 183 | # |
| 184 | # =================== Main =================== |
| 185 | # |
| 186 | |
| 187 | PROJECT_NAME="$1" |
| 188 | PROJECT_VERSION="$2" |
| 189 | PACKAGING_TARGET_DIR="$3" |
| 190 | |
| 191 | TIMESTAMP=$(date -u +%Y%m%dT%H%M%S) |
Petr Ospalý | 170d94b | 2018-12-20 16:40:58 +0100 | [diff] [blame] | 192 | SCRIPT_DIR=$(dirname "${0}") |
| 193 | LOCAL_PATH=$(readlink -f "$SCRIPT_DIR") |
| 194 | |
Samuli Silvius | fe11111 | 2019-02-05 09:45:24 +0200 | [diff] [blame^] | 195 | # Relative location inside the package to place Helm charts to be available for |
| 196 | # Ansible process to transfer them into machine (infra node) running Helm repository. |
| 197 | # NOTE: This is quite hardcoded place to put them and agreement with Ansible code |
| 198 | # is done in ansible/group_vars/all.yml with variable "app_helm_charts_install_directory" |
| 199 | # whihc value must match to value of this variable (with exception of slash '/' |
| 200 | # prepended so that ansible docker/chroot process can see the dir). |
| 201 | # This variable can be of course changed in package.conf if really needed if |
| 202 | # corresponding ansible variable "app_helm_charts_install_directory" value |
| 203 | # adjusted accordingly. |
| 204 | HELM_CHARTS_DIR_IN_PACKAGE="ansible/application/helm_charts" |
| 205 | |
Petr Ospalý | 170d94b | 2018-12-20 16:40:58 +0100 | [diff] [blame] | 206 | if [ "$#" -lt 3 ]; then |
| 207 | echo "Missing some mandatory parameter!" |
| 208 | usage |
| 209 | exit 1 |
| 210 | fi |
| 211 | |
Samuli Silvius | 730dc19 | 2019-02-04 14:26:27 +0200 | [diff] [blame] | 212 | CONF_FILE="" |
| 213 | for arg in "$@"; do |
| 214 | shift |
| 215 | case "$arg" in |
| 216 | -c|--conf) |
| 217 | CONF_FILE="$1" ;; |
| 218 | *) |
| 219 | set -- "$@" "$arg" |
| 220 | esac |
| 221 | done |
| 222 | |
| 223 | if [ -z ${CONF_FILE} ]; then |
| 224 | CONF_FILE=${LOCAL_PATH}/package.conf # Fall to default conf file |
Petr Ospalý | 170d94b | 2018-12-20 16:40:58 +0100 | [diff] [blame] | 225 | fi |
| 226 | |
Samuli Silvius | 730dc19 | 2019-02-04 14:26:27 +0200 | [diff] [blame] | 227 | if [ ! -f ${CONF_FILE} ]; then |
| 228 | crash 2 "Mandatory config file missing! Provide it with --conf option or ${LOCAL_PATH}/package.conf" |
| 229 | fi |
| 230 | |
| 231 | source ${CONF_FILE} |
| 232 | pushd ${LOCAL_PATH} |
| 233 | |
Petr Ospalý | 170d94b | 2018-12-20 16:40:58 +0100 | [diff] [blame] | 234 | # checking bash capability of parsing arrays |
| 235 | whotest[0]='test' || (crash 3 "Arrays not supported in this version of bash.") |
| 236 | |
| 237 | |
| 238 | # Prepare output directory for our packaging and create all tars |
| 239 | |
| 240 | rm -rf ${PACKAGING_TARGET_DIR} |
Samuli Silvius | 3fb890c | 2019-01-19 14:27:58 +0200 | [diff] [blame] | 241 | build_sw_artifacts |
Petr Ospalý | 170d94b | 2018-12-20 16:40:58 +0100 | [diff] [blame] | 242 | create_sw_package |
| 243 | create_resource_package |
| 244 | |
| 245 | # This part will create aux package which consists of |
| 246 | # artifacts which can be added into offline nexus during runtime |
| 247 | if [ "${PREPARE_AUX_PACKAGE}" == "true" ]; then |
| 248 | create_aux_package |
| 249 | else |
| 250 | echo "AUX package won't be created" |
| 251 | fi |
| 252 | |
| 253 | popd |