Petr Ospalý | 170d94b | 2018-12-20 16:40:58 +0100 | [diff] [blame] | 1 | #! /usr/bin/env bash |
| 2 | |
| 3 | # COPYRIGHT NOTICE STARTS HERE |
| 4 | # |
Samuli Silvius | e056345 | 2019-02-14 10:42:54 +0200 | [diff] [blame] | 5 | # Copyright 2018-2019 © Samsung Electronics Co., Ltd. |
Petr Ospalý | 170d94b | 2018-12-20 16:40:58 +0100 | [diff] [blame] | 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 |
Samuli Silvius | e056345 | 2019-02-14 10:42:54 +0200 | [diff] [blame] | 24 | set -e |
Petr Ospalý | 170d94b | 2018-12-20 16:40:58 +0100 | [diff] [blame] | 25 | |
| 26 | crash () { |
| 27 | local exit_code="$1" |
| 28 | local cause="$2" |
Samuli Silvius | e056345 | 2019-02-14 10:42:54 +0200 | [diff] [blame] | 29 | echo "Packaging script finished prematurely" |
Petr Ospalý | 170d94b | 2018-12-20 16:40:58 +0100 | [diff] [blame] | 30 | echo "Cause: $2" |
| 31 | exit "${exit_code}" |
| 32 | } |
| 33 | |
| 34 | usage () { |
| 35 | echo "Usage:" |
Bartek Grzybowski | bf9f9ef | 2019-03-14 15:34:51 +0100 | [diff] [blame^] | 36 | echo " ./$(basename $0) <project_name> <version> <packaging_target_dir> [--conf <file>] [--force]" |
| 37 | echo "" |
| 38 | echo "Options:" |
| 39 | echo " --force Remove packaging_target_dir if exists prior to script execution" |
| 40 | echo " --conf Custom configuration file path for script" |
| 41 | echo "" |
| 42 | echo "Example:" |
| 43 | echo " ./$(basename $0) myproject 1.0.1 /tmp/package --conf ~/myproject.conf" |
| 44 | echo "" |
Petr Ospalý | 170d94b | 2018-12-20 16:40:58 +0100 | [diff] [blame] | 45 | echo "packaging_target_dir will be created if does not exist. All tars will be produced into it." |
| 46 | } |
| 47 | |
| 48 | function create_tar { |
| 49 | local tar_dir="$1" |
| 50 | local tar_name="$2" |
| 51 | |
| 52 | cd ${tar_dir} |
| 53 | touch ${tar_name} # Trick to avoid sporadic "tar: .: file changed as we read it" warning message |
| 54 | tar --exclude=${tar_name} -cf ../${tar_name} . |
| 55 | cd - &> /dev/null # Trick to avoid printing new dir on stdout |
| 56 | |
| 57 | # Remove packaged folders |
| 58 | find ${tar_dir}/* -maxdepth 0 -type d -exec rm -rf '{}' \; |
| 59 | # Remove packaged files |
| 60 | find ${tar_dir}/* ! -name ${tar_name} -exec rm '{}' \; |
Samuli Silvius | 426e6c0 | 2019-02-06 11:25:01 +0200 | [diff] [blame] | 61 | echo "Tar file created to $(dirname ${tar_dir})/${tar_name}" |
| 62 | } |
| 63 | |
| 64 | function create_pkg { |
| 65 | local pkg_type="$1" |
| 66 | echo "[Creating ${pkg_type} package]" |
| 67 | create_tar "${PKG_ROOT}" offline-${PROJECT_NAME}-${PROJECT_VERSION}-${pkg_type}.tar |
| 68 | rm -rf "${PKG_ROOT}" |
Petr Ospalý | 170d94b | 2018-12-20 16:40:58 +0100 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | function add_metadata { |
| 72 | local metafile="$1" |
Petr Ospalý | 170d94b | 2018-12-20 16:40:58 +0100 | [diff] [blame] | 73 | echo "Project name: ${PROJECT_NAME}" >> "${metafile}" |
| 74 | echo "Project version: ${PROJECT_VERSION}" >> "${metafile}" |
| 75 | echo "Package date: ${TIMESTAMP}" >> "${metafile}" |
| 76 | } |
| 77 | |
| 78 | function add_additions { |
| 79 | local source="$1" |
| 80 | local target="$2" |
Petr Ospalý | 170d94b | 2018-12-20 16:40:58 +0100 | [diff] [blame] | 81 | if [ -d "${source}" ]; then |
| 82 | mkdir -p "${target}/$(basename $source)" |
| 83 | cp -r "${source}" "${target}" |
| 84 | echo "Adding directory ... $(basename $source)" |
| 85 | else |
| 86 | if [ -f "${source}" ]; then |
| 87 | cp "${source}" "${target}" |
| 88 | echo "Adding file ... $(basename $source)" |
| 89 | else |
| 90 | crash 4 "Invalid source specified for packaging: $1" |
| 91 | fi |
| 92 | fi |
| 93 | } |
| 94 | |
Samuli Silvius | 3fb890c | 2019-01-19 14:27:58 +0200 | [diff] [blame] | 95 | function build_sw_artifacts { |
Samuli Silvius | 426e6c0 | 2019-02-06 11:25:01 +0200 | [diff] [blame] | 96 | cd ${LOCAL_PATH}/../ansible/docker |
Samuli Silvius | 3fb890c | 2019-01-19 14:27:58 +0200 | [diff] [blame] | 97 | ./build_ansible_image.sh |
| 98 | if [ $? -ne 0 ]; then |
| 99 | crash 5 "Building of ansible runner image failed." |
| 100 | fi |
| 101 | cd - |
| 102 | } |
| 103 | |
Petr Ospalý | 170d94b | 2018-12-20 16:40:58 +0100 | [diff] [blame] | 104 | function create_sw_package { |
Samuli Silvius | 426e6c0 | 2019-02-06 11:25:01 +0200 | [diff] [blame] | 105 | PKG_ROOT="${PACKAGING_TARGET_DIR}/sw" |
Petr Ospalý | 170d94b | 2018-12-20 16:40:58 +0100 | [diff] [blame] | 106 | |
| 107 | # Create directory structure of the sw package |
Samuli Silvius | 426e6c0 | 2019-02-06 11:25:01 +0200 | [diff] [blame] | 108 | mkdir -p "${PKG_ROOT}" |
| 109 | cp -r ${LOCAL_PATH}/../ansible "${PKG_ROOT}" |
Petr Ospalý | 170d94b | 2018-12-20 16:40:58 +0100 | [diff] [blame] | 110 | |
Samuli Silvius | 426e6c0 | 2019-02-06 11:25:01 +0200 | [diff] [blame] | 111 | # Add application additional files/dirs into package based on package.conf |
| 112 | for item in "${APP_CONFIGURATION[@]}";do |
Petr Ospalý | 170d94b | 2018-12-20 16:40:58 +0100 | [diff] [blame] | 113 | # all SW package addons are expected within ./ansible/application folder |
Samuli Silvius | 426e6c0 | 2019-02-06 11:25:01 +0200 | [diff] [blame] | 114 | add_additions "${item}" "${PKG_ROOT}/${APPLICATION_FILES_IN_PACKAGE}" |
Petr Ospalý | 170d94b | 2018-12-20 16:40:58 +0100 | [diff] [blame] | 115 | done |
| 116 | |
Samuli Silvius | 426e6c0 | 2019-02-06 11:25:01 +0200 | [diff] [blame] | 117 | # Application Helm charts |
| 118 | # To be consistent with resources and aux dir, create charts dir even if no charts provided. |
| 119 | mkdir -p ${PKG_ROOT}/${HELM_CHARTS_DIR_IN_PACKAGE} |
Samuli Silvius | 6e5b45a | 2019-02-04 12:52:20 +0200 | [diff] [blame] | 120 | if [ ! -z "${HELM_CHARTS_DIR}" ]; |
| 121 | then |
Samuli Silvius | 426e6c0 | 2019-02-06 11:25:01 +0200 | [diff] [blame] | 122 | echo "Add application Helm charts" |
Samuli Silvius | 6e5b45a | 2019-02-04 12:52:20 +0200 | [diff] [blame] | 123 | # Copy charts available for ansible playbook to use/move them to target server/dir |
Samuli Silvius | 426e6c0 | 2019-02-06 11:25:01 +0200 | [diff] [blame] | 124 | cp -r "${HELM_CHARTS_DIR}"/* ${PKG_ROOT}/${HELM_CHARTS_DIR_IN_PACKAGE} |
| 125 | else |
| 126 | echo "No Helm charts defined, no application will be automatically installed by this package!" |
Samuli Silvius | 6e5b45a | 2019-02-04 12:52:20 +0200 | [diff] [blame] | 127 | fi |
Petr Ospalý | 170d94b | 2018-12-20 16:40:58 +0100 | [diff] [blame] | 128 | |
| 129 | # Add metadata to the package |
Samuli Silvius | 426e6c0 | 2019-02-06 11:25:01 +0200 | [diff] [blame] | 130 | add_metadata "${PKG_ROOT}"/package.info |
Petr Ospalý | 170d94b | 2018-12-20 16:40:58 +0100 | [diff] [blame] | 131 | |
| 132 | # Create sw tar package |
Samuli Silvius | 426e6c0 | 2019-02-06 11:25:01 +0200 | [diff] [blame] | 133 | create_pkg sw |
Petr Ospalý | 170d94b | 2018-12-20 16:40:58 +0100 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | function create_resource_package { |
Samuli Silvius | 426e6c0 | 2019-02-06 11:25:01 +0200 | [diff] [blame] | 137 | PKG_ROOT="${PACKAGING_TARGET_DIR}/resources" |
Petr Ospalý | 170d94b | 2018-12-20 16:40:58 +0100 | [diff] [blame] | 138 | |
| 139 | # Create directory structure of the resource package |
Samuli Silvius | 426e6c0 | 2019-02-06 11:25:01 +0200 | [diff] [blame] | 140 | mkdir -p "${PKG_ROOT}" |
Petr Ospalý | 170d94b | 2018-12-20 16:40:58 +0100 | [diff] [blame] | 141 | |
Samuli Silvius | 426e6c0 | 2019-02-06 11:25:01 +0200 | [diff] [blame] | 142 | # Add artifacts into resource package based on package.conf config |
| 143 | if [ ! -z ${APP_BINARY_RESOURCES_DIR} ]; then |
| 144 | cp -r ${APP_BINARY_RESOURCES_DIR}/* ${PKG_ROOT} |
| 145 | fi |
Petr Ospalý | 170d94b | 2018-12-20 16:40:58 +0100 | [diff] [blame] | 146 | |
| 147 | # tar file with nexus_data is expected, we should find and untar it |
| 148 | # before resource.tar is created |
Samuli Silvius | 426e6c0 | 2019-02-06 11:25:01 +0200 | [diff] [blame] | 149 | for i in `ls -1 ${PKG_ROOT} | grep tar`; do |
| 150 | tar tvf "${PKG_ROOT}/${i}" | grep nexus_data &> /dev/null |
| 151 | if [ $? -eq 0 ]; then |
| 152 | echo "Debug: tar file with nexus blobs detected ${PKG_ROOT}/${i}. Start unarchive ..." |
| 153 | tar xf "${PKG_ROOT}/${i}" -C "${PKG_ROOT}" &> /dev/null |
| 154 | echo "Debug: unarchive finished. Removing original file" |
| 155 | rm -f "${PKG_ROOT}/${i}" |
| 156 | fi |
Petr Ospalý | 170d94b | 2018-12-20 16:40:58 +0100 | [diff] [blame] | 157 | done |
| 158 | |
Samuli Silvius | 426e6c0 | 2019-02-06 11:25:01 +0200 | [diff] [blame] | 159 | create_pkg resources |
Petr Ospalý | 170d94b | 2018-12-20 16:40:58 +0100 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | function create_aux_package { |
Samuli Silvius | 426e6c0 | 2019-02-06 11:25:01 +0200 | [diff] [blame] | 163 | PKG_ROOT="${PACKAGING_TARGET_DIR}/aux" |
Petr Ospalý | 170d94b | 2018-12-20 16:40:58 +0100 | [diff] [blame] | 164 | |
| 165 | # Create directory structure of the aux resource package |
Samuli Silvius | 426e6c0 | 2019-02-06 11:25:01 +0200 | [diff] [blame] | 166 | mkdir -p "${PKG_ROOT}" |
Petr Ospalý | 170d94b | 2018-12-20 16:40:58 +0100 | [diff] [blame] | 167 | |
| 168 | # Add artifacts into resource packagee based on package.conf config |
Samuli Silvius | 426e6c0 | 2019-02-06 11:25:01 +0200 | [diff] [blame] | 169 | for item in "${APP_AUX_BINARIES[@]}";do |
| 170 | add_additions "${item}" "${PKG_ROOT}" |
Petr Ospalý | 170d94b | 2018-12-20 16:40:58 +0100 | [diff] [blame] | 171 | done |
| 172 | |
Samuli Silvius | 426e6c0 | 2019-02-06 11:25:01 +0200 | [diff] [blame] | 173 | create_pkg aux-resources |
Petr Ospalý | 170d94b | 2018-12-20 16:40:58 +0100 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | # |
| 177 | # =================== Main =================== |
| 178 | # |
| 179 | |
| 180 | PROJECT_NAME="$1" |
| 181 | PROJECT_VERSION="$2" |
| 182 | PACKAGING_TARGET_DIR="$3" |
| 183 | |
| 184 | TIMESTAMP=$(date -u +%Y%m%dT%H%M%S) |
Petr Ospalý | 170d94b | 2018-12-20 16:40:58 +0100 | [diff] [blame] | 185 | SCRIPT_DIR=$(dirname "${0}") |
| 186 | LOCAL_PATH=$(readlink -f "$SCRIPT_DIR") |
| 187 | |
Samuli Silvius | 426e6c0 | 2019-02-06 11:25:01 +0200 | [diff] [blame] | 188 | # Relative location inside the package for application related files. |
| 189 | # Application means Kubernetes application installed by Helm charts on ready cluster (e.g. onap). |
| 190 | APPLICATION_FILES_IN_PACKAGE="ansible/application" |
| 191 | |
Samuli Silvius | fe11111 | 2019-02-05 09:45:24 +0200 | [diff] [blame] | 192 | # Relative location inside the package to place Helm charts to be available for |
| 193 | # Ansible process to transfer them into machine (infra node) running Helm repository. |
| 194 | # NOTE: This is quite hardcoded place to put them and agreement with Ansible code |
| 195 | # is done in ansible/group_vars/all.yml with variable "app_helm_charts_install_directory" |
| 196 | # whihc value must match to value of this variable (with exception of slash '/' |
| 197 | # prepended so that ansible docker/chroot process can see the dir). |
| 198 | # This variable can be of course changed in package.conf if really needed if |
| 199 | # corresponding ansible variable "app_helm_charts_install_directory" value |
| 200 | # adjusted accordingly. |
Samuli Silvius | 426e6c0 | 2019-02-06 11:25:01 +0200 | [diff] [blame] | 201 | HELM_CHARTS_DIR_IN_PACKAGE="${APPLICATION_FILES_IN_PACKAGE}/helm_charts" |
Samuli Silvius | fe11111 | 2019-02-05 09:45:24 +0200 | [diff] [blame] | 202 | |
Petr Ospalý | 170d94b | 2018-12-20 16:40:58 +0100 | [diff] [blame] | 203 | if [ "$#" -lt 3 ]; then |
Bartek Grzybowski | bf9f9ef | 2019-03-14 15:34:51 +0100 | [diff] [blame^] | 204 | echo "Missing some mandatory arguments!" |
Petr Ospalý | 170d94b | 2018-12-20 16:40:58 +0100 | [diff] [blame] | 205 | usage |
| 206 | exit 1 |
| 207 | fi |
| 208 | |
Samuli Silvius | 730dc19 | 2019-02-04 14:26:27 +0200 | [diff] [blame] | 209 | CONF_FILE="" |
Bartek Grzybowski | bf9f9ef | 2019-03-14 15:34:51 +0100 | [diff] [blame^] | 210 | FORCE_REMOVE=0 |
Samuli Silvius | 730dc19 | 2019-02-04 14:26:27 +0200 | [diff] [blame] | 211 | for arg in "$@"; do |
| 212 | shift |
| 213 | case "$arg" in |
| 214 | -c|--conf) |
| 215 | CONF_FILE="$1" ;; |
Bartek Grzybowski | bf9f9ef | 2019-03-14 15:34:51 +0100 | [diff] [blame^] | 216 | --force) |
| 217 | FORCE_REMOVE=1 ;; |
Samuli Silvius | 730dc19 | 2019-02-04 14:26:27 +0200 | [diff] [blame] | 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 | |
Bartek Grzybowski | bf9f9ef | 2019-03-14 15:34:51 +0100 | [diff] [blame^] | 237 | # Prepare output directory for our packaging |
| 238 | # Check target dir exists and is not empty |
| 239 | if [ -d ${PACKAGING_TARGET_DIR} ] && [ "$(ls -A ${PACKAGING_TARGET_DIR})" ]; then |
| 240 | if [ ${FORCE_REMOVE} -eq 0 ]; then |
| 241 | crash 1 "Target directory not empty. Use --force to overwrite it." |
| 242 | else |
| 243 | rm -rf ${PACKAGING_TARGET_DIR} |
| 244 | fi |
| 245 | fi |
Petr Ospalý | 170d94b | 2018-12-20 16:40:58 +0100 | [diff] [blame] | 246 | |
Bartek Grzybowski | bf9f9ef | 2019-03-14 15:34:51 +0100 | [diff] [blame^] | 247 | # Create all tars |
Samuli Silvius | 3fb890c | 2019-01-19 14:27:58 +0200 | [diff] [blame] | 248 | build_sw_artifacts |
Petr Ospalý | 170d94b | 2018-12-20 16:40:58 +0100 | [diff] [blame] | 249 | create_sw_package |
| 250 | create_resource_package |
Samuli Silvius | 426e6c0 | 2019-02-06 11:25:01 +0200 | [diff] [blame] | 251 | create_aux_package |
Petr Ospalý | 170d94b | 2018-12-20 16:40:58 +0100 | [diff] [blame] | 252 | |
| 253 | popd |