blob: c527db2e30318af2e1b8fe556556ed20e5fbdb7b [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
34usage () {
35 echo "Usage:"
Bartek Grzybowskibf9f9ef2019-03-14 15:34:51 +010036 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ý170d94b2018-12-20 16:40:58 +010045 echo "packaging_target_dir will be created if does not exist. All tars will be produced into it."
46}
47
48function 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 Silvius426e6c02019-02-06 11:25:01 +020061 echo "Tar file created to $(dirname ${tar_dir})/${tar_name}"
62}
63
64function 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ý170d94b2018-12-20 16:40:58 +010069}
70
71function add_metadata {
72 local metafile="$1"
Petr Ospalý170d94b2018-12-20 16:40:58 +010073 echo "Project name: ${PROJECT_NAME}" >> "${metafile}"
74 echo "Project version: ${PROJECT_VERSION}" >> "${metafile}"
75 echo "Package date: ${TIMESTAMP}" >> "${metafile}"
76}
77
78function add_additions {
79 local source="$1"
80 local target="$2"
Petr Ospalý170d94b2018-12-20 16:40:58 +010081 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 Silvius3fb890c2019-01-19 14:27:58 +020095function build_sw_artifacts {
Samuli Silvius426e6c02019-02-06 11:25:01 +020096 cd ${LOCAL_PATH}/../ansible/docker
Samuli Silvius3fb890c2019-01-19 14:27:58 +020097 ./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ý170d94b2018-12-20 16:40:58 +0100104function create_sw_package {
Samuli Silvius426e6c02019-02-06 11:25:01 +0200105 PKG_ROOT="${PACKAGING_TARGET_DIR}/sw"
Petr Ospalý170d94b2018-12-20 16:40:58 +0100106
107 # Create directory structure of the sw package
Samuli Silvius426e6c02019-02-06 11:25:01 +0200108 mkdir -p "${PKG_ROOT}"
109 cp -r ${LOCAL_PATH}/../ansible "${PKG_ROOT}"
Petr Ospalý170d94b2018-12-20 16:40:58 +0100110
Samuli Silvius426e6c02019-02-06 11:25:01 +0200111 # Add application additional files/dirs into package based on package.conf
112 for item in "${APP_CONFIGURATION[@]}";do
Petr Ospalý170d94b2018-12-20 16:40:58 +0100113 # all SW package addons are expected within ./ansible/application folder
Samuli Silvius426e6c02019-02-06 11:25:01 +0200114 add_additions "${item}" "${PKG_ROOT}/${APPLICATION_FILES_IN_PACKAGE}"
Petr Ospalý170d94b2018-12-20 16:40:58 +0100115 done
116
Samuli Silvius426e6c02019-02-06 11:25:01 +0200117 # 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 Silvius6e5b45a2019-02-04 12:52:20 +0200120 if [ ! -z "${HELM_CHARTS_DIR}" ];
121 then
Samuli Silvius426e6c02019-02-06 11:25:01 +0200122 echo "Add application Helm charts"
Samuli Silvius6e5b45a2019-02-04 12:52:20 +0200123 # Copy charts available for ansible playbook to use/move them to target server/dir
Samuli Silvius426e6c02019-02-06 11:25:01 +0200124 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 Silvius6e5b45a2019-02-04 12:52:20 +0200127 fi
Petr Ospalý170d94b2018-12-20 16:40:58 +0100128
129 # Add metadata to the package
Samuli Silvius426e6c02019-02-06 11:25:01 +0200130 add_metadata "${PKG_ROOT}"/package.info
Petr Ospalý170d94b2018-12-20 16:40:58 +0100131
132 # Create sw tar package
Samuli Silvius426e6c02019-02-06 11:25:01 +0200133 create_pkg sw
Petr Ospalý170d94b2018-12-20 16:40:58 +0100134}
135
136function create_resource_package {
Samuli Silvius426e6c02019-02-06 11:25:01 +0200137 PKG_ROOT="${PACKAGING_TARGET_DIR}/resources"
Petr Ospalý170d94b2018-12-20 16:40:58 +0100138
139 # Create directory structure of the resource package
Samuli Silvius426e6c02019-02-06 11:25:01 +0200140 mkdir -p "${PKG_ROOT}"
Petr Ospalý170d94b2018-12-20 16:40:58 +0100141
Samuli Silvius426e6c02019-02-06 11:25:01 +0200142 # 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ý170d94b2018-12-20 16:40:58 +0100146
147 # tar file with nexus_data is expected, we should find and untar it
148 # before resource.tar is created
Samuli Silvius426e6c02019-02-06 11:25:01 +0200149 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ý170d94b2018-12-20 16:40:58 +0100157 done
158
Samuli Silvius426e6c02019-02-06 11:25:01 +0200159 create_pkg resources
Petr Ospalý170d94b2018-12-20 16:40:58 +0100160}
161
162function create_aux_package {
Samuli Silvius426e6c02019-02-06 11:25:01 +0200163 PKG_ROOT="${PACKAGING_TARGET_DIR}/aux"
Petr Ospalý170d94b2018-12-20 16:40:58 +0100164
165 # Create directory structure of the aux resource package
Samuli Silvius426e6c02019-02-06 11:25:01 +0200166 mkdir -p "${PKG_ROOT}"
Petr Ospalý170d94b2018-12-20 16:40:58 +0100167
168 # Add artifacts into resource packagee based on package.conf config
Samuli Silvius426e6c02019-02-06 11:25:01 +0200169 for item in "${APP_AUX_BINARIES[@]}";do
170 add_additions "${item}" "${PKG_ROOT}"
Petr Ospalý170d94b2018-12-20 16:40:58 +0100171 done
172
Samuli Silvius426e6c02019-02-06 11:25:01 +0200173 create_pkg aux-resources
Petr Ospalý170d94b2018-12-20 16:40:58 +0100174}
175
176#
177# =================== Main ===================
178#
179
180PROJECT_NAME="$1"
181PROJECT_VERSION="$2"
182PACKAGING_TARGET_DIR="$3"
183
184TIMESTAMP=$(date -u +%Y%m%dT%H%M%S)
Petr Ospalý170d94b2018-12-20 16:40:58 +0100185SCRIPT_DIR=$(dirname "${0}")
186LOCAL_PATH=$(readlink -f "$SCRIPT_DIR")
187
Samuli Silvius426e6c02019-02-06 11:25:01 +0200188# Relative location inside the package for application related files.
189# Application means Kubernetes application installed by Helm charts on ready cluster (e.g. onap).
190APPLICATION_FILES_IN_PACKAGE="ansible/application"
191
Samuli Silviusfe111112019-02-05 09:45:24 +0200192# 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 Silvius426e6c02019-02-06 11:25:01 +0200201HELM_CHARTS_DIR_IN_PACKAGE="${APPLICATION_FILES_IN_PACKAGE}/helm_charts"
Samuli Silviusfe111112019-02-05 09:45:24 +0200202
Petr Ospalý170d94b2018-12-20 16:40:58 +0100203if [ "$#" -lt 3 ]; then
Bartek Grzybowskibf9f9ef2019-03-14 15:34:51 +0100204 echo "Missing some mandatory arguments!"
Petr Ospalý170d94b2018-12-20 16:40:58 +0100205 usage
206 exit 1
207fi
208
Samuli Silvius730dc192019-02-04 14:26:27 +0200209CONF_FILE=""
Bartek Grzybowskibf9f9ef2019-03-14 15:34:51 +0100210FORCE_REMOVE=0
Samuli Silvius730dc192019-02-04 14:26:27 +0200211for arg in "$@"; do
212 shift
213 case "$arg" in
214 -c|--conf)
215 CONF_FILE="$1" ;;
Bartek Grzybowskibf9f9ef2019-03-14 15:34:51 +0100216 --force)
217 FORCE_REMOVE=1 ;;
Samuli Silvius730dc192019-02-04 14:26:27 +0200218 *)
219 set -- "$@" "$arg"
220 esac
221done
222
223if [ -z ${CONF_FILE} ]; then
224 CONF_FILE=${LOCAL_PATH}/package.conf # Fall to default conf file
Petr Ospalý170d94b2018-12-20 16:40:58 +0100225fi
226
Samuli Silvius730dc192019-02-04 14:26:27 +0200227if [ ! -f ${CONF_FILE} ]; then
228 crash 2 "Mandatory config file missing! Provide it with --conf option or ${LOCAL_PATH}/package.conf"
229fi
230
231source ${CONF_FILE}
232pushd ${LOCAL_PATH}
233
Petr Ospalý170d94b2018-12-20 16:40:58 +0100234# checking bash capability of parsing arrays
235whotest[0]='test' || (crash 3 "Arrays not supported in this version of bash.")
236
Bartek Grzybowskibf9f9ef2019-03-14 15:34:51 +0100237# Prepare output directory for our packaging
238# Check target dir exists and is not empty
239if [ -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
245fi
Petr Ospalý170d94b2018-12-20 16:40:58 +0100246
Bartek Grzybowskibf9f9ef2019-03-14 15:34:51 +0100247# Create all tars
Samuli Silvius3fb890c2019-01-19 14:27:58 +0200248build_sw_artifacts
Petr Ospalý170d94b2018-12-20 16:40:58 +0100249create_sw_package
250create_resource_package
Samuli Silvius426e6c02019-02-06 11:25:01 +0200251create_aux_package
Petr Ospalý170d94b2018-12-20 16:40:58 +0100252
253popd