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