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