blob: a3c1ded26f2b5276957a67a56ec537afb5a1abaf [file] [log] [blame]
Petr Ospalý170d94b2018-12-20 16:40:58 +01001#! /usr/bin/env bash
2
3# COPYRIGHT NOTICE STARTS HERE
4#
Samuli Silviuse0563452019-02-14 10:42:54 +02005# Copyright 2018-2019 © Samsung Electronics Co., Ltd.
Petr Ospalý170d94b2018-12-20 16:40:58 +01006#
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 Silviuse0563452019-02-14 10:42:54 +020024set -e
Petr Ospalý170d94b2018-12-20 16:40:58 +010025
26crash () {
27 local exit_code="$1"
28 local cause="$2"
Samuli Silviuse0563452019-02-14 10:42:54 +020029 echo "Packaging script finished prematurely"
Petr Ospalý170d94b2018-12-20 16:40:58 +010030 echo "Cause: $2"
31 exit "${exit_code}"
32}
33
Bartek Grzybowskifdf97982019-03-18 16:09:51 +010034crash_arguments () {
35 echo "Missing some mandatory arguments!"
36 usage
37 exit 1
38}
39
Petr Ospalý170d94b2018-12-20 16:40:58 +010040usage () {
41 echo "Usage:"
Bartek Grzybowskibf9f9ef2019-03-14 15:34:51 +010042 echo " ./$(basename $0) <project_name> <version> <packaging_target_dir> [--conf <file>] [--force]"
43 echo ""
44 echo "Options:"
45 echo " --force Remove packaging_target_dir if exists prior to script execution"
46 echo " --conf Custom configuration file path for script"
47 echo ""
48 echo "Example:"
49 echo " ./$(basename $0) myproject 1.0.1 /tmp/package --conf ~/myproject.conf"
50 echo ""
Petr Ospalý170d94b2018-12-20 16:40:58 +010051 echo "packaging_target_dir will be created if does not exist. All tars will be produced into it."
52}
53
54function create_tar {
55 local tar_dir="$1"
56 local tar_name="$2"
57
58 cd ${tar_dir}
59 touch ${tar_name} # Trick to avoid sporadic "tar: .: file changed as we read it" warning message
60 tar --exclude=${tar_name} -cf ../${tar_name} .
61 cd - &> /dev/null # Trick to avoid printing new dir on stdout
62
63 # Remove packaged folders
64 find ${tar_dir}/* -maxdepth 0 -type d -exec rm -rf '{}' \;
65 # Remove packaged files
66 find ${tar_dir}/* ! -name ${tar_name} -exec rm '{}' \;
Samuli Silvius426e6c02019-02-06 11:25:01 +020067 echo "Tar file created to $(dirname ${tar_dir})/${tar_name}"
68}
69
70function create_pkg {
71 local pkg_type="$1"
72 echo "[Creating ${pkg_type} package]"
73 create_tar "${PKG_ROOT}" offline-${PROJECT_NAME}-${PROJECT_VERSION}-${pkg_type}.tar
74 rm -rf "${PKG_ROOT}"
Petr Ospalý170d94b2018-12-20 16:40:58 +010075}
76
77function add_metadata {
78 local metafile="$1"
Petr Ospalý170d94b2018-12-20 16:40:58 +010079 echo "Project name: ${PROJECT_NAME}" >> "${metafile}"
80 echo "Project version: ${PROJECT_VERSION}" >> "${metafile}"
81 echo "Package date: ${TIMESTAMP}" >> "${metafile}"
82}
83
84function add_additions {
85 local source="$1"
86 local target="$2"
Petr Ospalý170d94b2018-12-20 16:40:58 +010087 if [ -d "${source}" ]; then
88 mkdir -p "${target}/$(basename $source)"
89 cp -r "${source}" "${target}"
90 echo "Adding directory ... $(basename $source)"
91 else
92 if [ -f "${source}" ]; then
93 cp "${source}" "${target}"
94 echo "Adding file ... $(basename $source)"
95 else
96 crash 4 "Invalid source specified for packaging: $1"
97 fi
98 fi
99}
100
Samuli Silvius3fb890c2019-01-19 14:27:58 +0200101function build_sw_artifacts {
Samuli Silvius426e6c02019-02-06 11:25:01 +0200102 cd ${LOCAL_PATH}/../ansible/docker
Samuli Silvius3fb890c2019-01-19 14:27:58 +0200103 ./build_ansible_image.sh
104 if [ $? -ne 0 ]; then
105 crash 5 "Building of ansible runner image failed."
106 fi
107 cd -
108}
109
Petr Ospalý170d94b2018-12-20 16:40:58 +0100110function create_sw_package {
Samuli Silvius426e6c02019-02-06 11:25:01 +0200111 PKG_ROOT="${PACKAGING_TARGET_DIR}/sw"
Petr Ospalý170d94b2018-12-20 16:40:58 +0100112
113 # Create directory structure of the sw package
Samuli Silvius426e6c02019-02-06 11:25:01 +0200114 mkdir -p "${PKG_ROOT}"
115 cp -r ${LOCAL_PATH}/../ansible "${PKG_ROOT}"
Petr Ospalý170d94b2018-12-20 16:40:58 +0100116
Samuli Silvius426e6c02019-02-06 11:25:01 +0200117 # Add application additional files/dirs into package based on package.conf
118 for item in "${APP_CONFIGURATION[@]}";do
Petr Ospalý170d94b2018-12-20 16:40:58 +0100119 # all SW package addons are expected within ./ansible/application folder
Samuli Silvius426e6c02019-02-06 11:25:01 +0200120 add_additions "${item}" "${PKG_ROOT}/${APPLICATION_FILES_IN_PACKAGE}"
Petr Ospalý170d94b2018-12-20 16:40:58 +0100121 done
122
Samuli Silvius426e6c02019-02-06 11:25:01 +0200123 # Application Helm charts
124 # To be consistent with resources and aux dir, create charts dir even if no charts provided.
125 mkdir -p ${PKG_ROOT}/${HELM_CHARTS_DIR_IN_PACKAGE}
Samuli Silvius6e5b45a2019-02-04 12:52:20 +0200126 if [ ! -z "${HELM_CHARTS_DIR}" ];
127 then
Samuli Silvius426e6c02019-02-06 11:25:01 +0200128 echo "Add application Helm charts"
Samuli Silvius6e5b45a2019-02-04 12:52:20 +0200129 # Copy charts available for ansible playbook to use/move them to target server/dir
Samuli Silvius426e6c02019-02-06 11:25:01 +0200130 cp -r "${HELM_CHARTS_DIR}"/* ${PKG_ROOT}/${HELM_CHARTS_DIR_IN_PACKAGE}
131 else
132 echo "No Helm charts defined, no application will be automatically installed by this package!"
Samuli Silvius6e5b45a2019-02-04 12:52:20 +0200133 fi
Petr Ospalý170d94b2018-12-20 16:40:58 +0100134
135 # Add metadata to the package
Samuli Silvius426e6c02019-02-06 11:25:01 +0200136 add_metadata "${PKG_ROOT}"/package.info
Petr Ospalý170d94b2018-12-20 16:40:58 +0100137
138 # Create sw tar package
Samuli Silvius426e6c02019-02-06 11:25:01 +0200139 create_pkg sw
Petr Ospalý170d94b2018-12-20 16:40:58 +0100140}
141
142function create_resource_package {
Samuli Silvius426e6c02019-02-06 11:25:01 +0200143 PKG_ROOT="${PACKAGING_TARGET_DIR}/resources"
Petr Ospalý170d94b2018-12-20 16:40:58 +0100144
145 # Create directory structure of the resource package
Samuli Silvius426e6c02019-02-06 11:25:01 +0200146 mkdir -p "${PKG_ROOT}"
Petr Ospalý170d94b2018-12-20 16:40:58 +0100147
Samuli Silvius426e6c02019-02-06 11:25:01 +0200148 # Add artifacts into resource package based on package.conf config
149 if [ ! -z ${APP_BINARY_RESOURCES_DIR} ]; then
150 cp -r ${APP_BINARY_RESOURCES_DIR}/* ${PKG_ROOT}
151 fi
Petr Ospalý170d94b2018-12-20 16:40:58 +0100152
153 # tar file with nexus_data is expected, we should find and untar it
154 # before resource.tar is created
Samuli Silvius426e6c02019-02-06 11:25:01 +0200155 for i in `ls -1 ${PKG_ROOT} | grep tar`; do
156 tar tvf "${PKG_ROOT}/${i}" | grep nexus_data &> /dev/null
157 if [ $? -eq 0 ]; then
158 echo "Debug: tar file with nexus blobs detected ${PKG_ROOT}/${i}. Start unarchive ..."
159 tar xf "${PKG_ROOT}/${i}" -C "${PKG_ROOT}" &> /dev/null
160 echo "Debug: unarchive finished. Removing original file"
161 rm -f "${PKG_ROOT}/${i}"
162 fi
Petr Ospalý170d94b2018-12-20 16:40:58 +0100163 done
164
Samuli Silvius426e6c02019-02-06 11:25:01 +0200165 create_pkg resources
Petr Ospalý170d94b2018-12-20 16:40:58 +0100166}
167
168function create_aux_package {
Samuli Silvius426e6c02019-02-06 11:25:01 +0200169 PKG_ROOT="${PACKAGING_TARGET_DIR}/aux"
Petr Ospalý170d94b2018-12-20 16:40:58 +0100170
171 # Create directory structure of the aux resource package
Samuli Silvius426e6c02019-02-06 11:25:01 +0200172 mkdir -p "${PKG_ROOT}"
Petr Ospalý170d94b2018-12-20 16:40:58 +0100173
174 # Add artifacts into resource packagee based on package.conf config
Samuli Silvius426e6c02019-02-06 11:25:01 +0200175 for item in "${APP_AUX_BINARIES[@]}";do
176 add_additions "${item}" "${PKG_ROOT}"
Petr Ospalý170d94b2018-12-20 16:40:58 +0100177 done
178
Samuli Silvius426e6c02019-02-06 11:25:01 +0200179 create_pkg aux-resources
Petr Ospalý170d94b2018-12-20 16:40:58 +0100180}
181
182#
183# =================== Main ===================
184#
185
186PROJECT_NAME="$1"
187PROJECT_VERSION="$2"
188PACKAGING_TARGET_DIR="$3"
189
190TIMESTAMP=$(date -u +%Y%m%dT%H%M%S)
Petr Ospalý170d94b2018-12-20 16:40:58 +0100191SCRIPT_DIR=$(dirname "${0}")
192LOCAL_PATH=$(readlink -f "$SCRIPT_DIR")
193
Samuli Silvius426e6c02019-02-06 11:25:01 +0200194# Relative location inside the package for application related files.
195# Application means Kubernetes application installed by Helm charts on ready cluster (e.g. onap).
196APPLICATION_FILES_IN_PACKAGE="ansible/application"
197
Samuli Silviusfe111112019-02-05 09:45:24 +0200198# Relative location inside the package to place Helm charts to be available for
199# Ansible process to transfer them into machine (infra node) running Helm repository.
200# NOTE: This is quite hardcoded place to put them and agreement with Ansible code
201# is done in ansible/group_vars/all.yml with variable "app_helm_charts_install_directory"
202# whihc value must match to value of this variable (with exception of slash '/'
203# prepended so that ansible docker/chroot process can see the dir).
204# This variable can be of course changed in package.conf if really needed if
205# corresponding ansible variable "app_helm_charts_install_directory" value
206# adjusted accordingly.
Samuli Silvius426e6c02019-02-06 11:25:01 +0200207HELM_CHARTS_DIR_IN_PACKAGE="${APPLICATION_FILES_IN_PACKAGE}/helm_charts"
Samuli Silviusfe111112019-02-05 09:45:24 +0200208
Bartek Grzybowskifdf97982019-03-18 16:09:51 +0100209if [ $# -eq 0 ]; then
210 crash_arguments
Petr Ospalý170d94b2018-12-20 16:40:58 +0100211fi
212
Samuli Silvius730dc192019-02-04 14:26:27 +0200213CONF_FILE=""
Bartek Grzybowskibf9f9ef2019-03-14 15:34:51 +0100214FORCE_REMOVE=0
Bartek Grzybowskifdf97982019-03-18 16:09:51 +0100215arg_ind=0
Samuli Silvius730dc192019-02-04 14:26:27 +0200216for arg in "$@"; do
217 shift
Bartek Grzybowskifdf97982019-03-18 16:09:51 +0100218 ((arg_ind+=1))
219 if [[ ${arg} =~ ^[-]{1,2}[a-zA-Z-]+$ && ${arg_ind} -lt 4 ]]; then
220 echo "Non-positional parameters should follow mandatory arguments!"
221 usage
222 exit 1
223 fi
Samuli Silvius730dc192019-02-04 14:26:27 +0200224 case "$arg" in
225 -c|--conf)
226 CONF_FILE="$1" ;;
Bartek Grzybowskibf9f9ef2019-03-14 15:34:51 +0100227 --force)
228 FORCE_REMOVE=1 ;;
Samuli Silvius730dc192019-02-04 14:26:27 +0200229 *)
230 set -- "$@" "$arg"
Bartek Grzybowskifdf97982019-03-18 16:09:51 +0100231 if [ "$#" -lt 3 ]; then
232 crash_arguments
233 fi ;;
Samuli Silvius730dc192019-02-04 14:26:27 +0200234 esac
235done
236
237if [ -z ${CONF_FILE} ]; then
238 CONF_FILE=${LOCAL_PATH}/package.conf # Fall to default conf file
Petr Ospalý170d94b2018-12-20 16:40:58 +0100239fi
240
Samuli Silvius730dc192019-02-04 14:26:27 +0200241if [ ! -f ${CONF_FILE} ]; then
242 crash 2 "Mandatory config file missing! Provide it with --conf option or ${LOCAL_PATH}/package.conf"
243fi
244
245source ${CONF_FILE}
246pushd ${LOCAL_PATH}
247
Petr Ospalý170d94b2018-12-20 16:40:58 +0100248# checking bash capability of parsing arrays
249whotest[0]='test' || (crash 3 "Arrays not supported in this version of bash.")
250
Bartek Grzybowskibf9f9ef2019-03-14 15:34:51 +0100251# Prepare output directory for our packaging
252# Check target dir exists and is not empty
253if [ -d ${PACKAGING_TARGET_DIR} ] && [ "$(ls -A ${PACKAGING_TARGET_DIR})" ]; then
254 if [ ${FORCE_REMOVE} -eq 0 ]; then
255 crash 1 "Target directory not empty. Use --force to overwrite it."
256 else
257 rm -rf ${PACKAGING_TARGET_DIR}
258 fi
259fi
Petr Ospalý170d94b2018-12-20 16:40:58 +0100260
Bartek Grzybowskibf9f9ef2019-03-14 15:34:51 +0100261# Create all tars
Samuli Silvius3fb890c2019-01-19 14:27:58 +0200262build_sw_artifacts
Petr Ospalý170d94b2018-12-20 16:40:58 +0100263create_sw_package
264create_resource_package
Samuli Silvius426e6c02019-02-06 11:25:01 +0200265create_aux_package
Petr Ospalý170d94b2018-12-20 16:40:58 +0100266
267popd