blob: 63774e029645541d0d75a302d2047cba0ae2df4e [file] [log] [blame]
Petr Ospalý170d94b2018-12-20 16:40:58 +01001#! /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
25crash () {
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
33usage () {
34 echo "Usage:"
Samuli Silvius730dc192019-02-04 14:26:27 +020035 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ý170d94b2018-12-20 16:40:58 +010037 echo "packaging_target_dir will be created if does not exist. All tars will be produced into it."
38}
39
40function 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 Silvius426e6c02019-02-06 11:25:01 +020053 echo "Tar file created to $(dirname ${tar_dir})/${tar_name}"
54}
55
56function 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ý170d94b2018-12-20 16:40:58 +010061}
62
63function add_metadata {
64 local metafile="$1"
Petr Ospalý170d94b2018-12-20 16:40:58 +010065 echo "Project name: ${PROJECT_NAME}" >> "${metafile}"
66 echo "Project version: ${PROJECT_VERSION}" >> "${metafile}"
67 echo "Package date: ${TIMESTAMP}" >> "${metafile}"
68}
69
70function add_additions {
71 local source="$1"
72 local target="$2"
Petr Ospalý170d94b2018-12-20 16:40:58 +010073 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 Silvius3fb890c2019-01-19 14:27:58 +020087function build_sw_artifacts {
Samuli Silvius426e6c02019-02-06 11:25:01 +020088 cd ${LOCAL_PATH}/../ansible/docker
Samuli Silvius3fb890c2019-01-19 14:27:58 +020089 ./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ý170d94b2018-12-20 16:40:58 +010096function create_sw_package {
Samuli Silvius426e6c02019-02-06 11:25:01 +020097 PKG_ROOT="${PACKAGING_TARGET_DIR}/sw"
Petr Ospalý170d94b2018-12-20 16:40:58 +010098
99 # Create directory structure of the sw package
Samuli Silvius426e6c02019-02-06 11:25:01 +0200100 mkdir -p "${PKG_ROOT}"
101 cp -r ${LOCAL_PATH}/../ansible "${PKG_ROOT}"
Petr Ospalý170d94b2018-12-20 16:40:58 +0100102
Samuli Silvius426e6c02019-02-06 11:25:01 +0200103 # Add application additional files/dirs into package based on package.conf
104 for item in "${APP_CONFIGURATION[@]}";do
Petr Ospalý170d94b2018-12-20 16:40:58 +0100105 # all SW package addons are expected within ./ansible/application folder
Samuli Silvius426e6c02019-02-06 11:25:01 +0200106 add_additions "${item}" "${PKG_ROOT}/${APPLICATION_FILES_IN_PACKAGE}"
Petr Ospalý170d94b2018-12-20 16:40:58 +0100107 done
108
Samuli Silvius426e6c02019-02-06 11:25:01 +0200109 # 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 Silvius6e5b45a2019-02-04 12:52:20 +0200112 if [ ! -z "${HELM_CHARTS_DIR}" ];
113 then
Samuli Silvius426e6c02019-02-06 11:25:01 +0200114 echo "Add application Helm charts"
Samuli Silvius6e5b45a2019-02-04 12:52:20 +0200115 # Copy charts available for ansible playbook to use/move them to target server/dir
Samuli Silvius426e6c02019-02-06 11:25:01 +0200116 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 Silvius6e5b45a2019-02-04 12:52:20 +0200119 fi
Petr Ospalý170d94b2018-12-20 16:40:58 +0100120
121 # Add metadata to the package
Samuli Silvius426e6c02019-02-06 11:25:01 +0200122 add_metadata "${PKG_ROOT}"/package.info
Petr Ospalý170d94b2018-12-20 16:40:58 +0100123
124 # Create sw tar package
Samuli Silvius426e6c02019-02-06 11:25:01 +0200125 create_pkg sw
Petr Ospalý170d94b2018-12-20 16:40:58 +0100126}
127
128function create_resource_package {
Samuli Silvius426e6c02019-02-06 11:25:01 +0200129 PKG_ROOT="${PACKAGING_TARGET_DIR}/resources"
Petr Ospalý170d94b2018-12-20 16:40:58 +0100130
131 # Create directory structure of the resource package
Samuli Silvius426e6c02019-02-06 11:25:01 +0200132 mkdir -p "${PKG_ROOT}"
Petr Ospalý170d94b2018-12-20 16:40:58 +0100133
Samuli Silvius426e6c02019-02-06 11:25:01 +0200134 # 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ý170d94b2018-12-20 16:40:58 +0100138
139 # tar file with nexus_data is expected, we should find and untar it
140 # before resource.tar is created
Samuli Silvius426e6c02019-02-06 11:25:01 +0200141 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ý170d94b2018-12-20 16:40:58 +0100149 done
150
Samuli Silvius426e6c02019-02-06 11:25:01 +0200151 create_pkg resources
Petr Ospalý170d94b2018-12-20 16:40:58 +0100152}
153
154function create_aux_package {
Samuli Silvius426e6c02019-02-06 11:25:01 +0200155 PKG_ROOT="${PACKAGING_TARGET_DIR}/aux"
Petr Ospalý170d94b2018-12-20 16:40:58 +0100156
157 # Create directory structure of the aux resource package
Samuli Silvius426e6c02019-02-06 11:25:01 +0200158 mkdir -p "${PKG_ROOT}"
Petr Ospalý170d94b2018-12-20 16:40:58 +0100159
160 # Add artifacts into resource packagee based on package.conf config
Samuli Silvius426e6c02019-02-06 11:25:01 +0200161 for item in "${APP_AUX_BINARIES[@]}";do
162 add_additions "${item}" "${PKG_ROOT}"
Petr Ospalý170d94b2018-12-20 16:40:58 +0100163 done
164
Samuli Silvius426e6c02019-02-06 11:25:01 +0200165 create_pkg aux-resources
Petr Ospalý170d94b2018-12-20 16:40:58 +0100166}
167
168#
169# =================== Main ===================
170#
171
172PROJECT_NAME="$1"
173PROJECT_VERSION="$2"
174PACKAGING_TARGET_DIR="$3"
175
176TIMESTAMP=$(date -u +%Y%m%dT%H%M%S)
Petr Ospalý170d94b2018-12-20 16:40:58 +0100177SCRIPT_DIR=$(dirname "${0}")
178LOCAL_PATH=$(readlink -f "$SCRIPT_DIR")
179
Samuli Silvius426e6c02019-02-06 11:25:01 +0200180# Relative location inside the package for application related files.
181# Application means Kubernetes application installed by Helm charts on ready cluster (e.g. onap).
182APPLICATION_FILES_IN_PACKAGE="ansible/application"
183
Samuli Silviusfe111112019-02-05 09:45:24 +0200184# 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 Silvius426e6c02019-02-06 11:25:01 +0200193HELM_CHARTS_DIR_IN_PACKAGE="${APPLICATION_FILES_IN_PACKAGE}/helm_charts"
Samuli Silviusfe111112019-02-05 09:45:24 +0200194
Petr Ospalý170d94b2018-12-20 16:40:58 +0100195if [ "$#" -lt 3 ]; then
196 echo "Missing some mandatory parameter!"
197 usage
198 exit 1
199fi
200
Samuli Silvius730dc192019-02-04 14:26:27 +0200201CONF_FILE=""
202for arg in "$@"; do
203 shift
204 case "$arg" in
205 -c|--conf)
206 CONF_FILE="$1" ;;
207 *)
208 set -- "$@" "$arg"
209 esac
210done
211
212if [ -z ${CONF_FILE} ]; then
213 CONF_FILE=${LOCAL_PATH}/package.conf # Fall to default conf file
Petr Ospalý170d94b2018-12-20 16:40:58 +0100214fi
215
Samuli Silvius730dc192019-02-04 14:26:27 +0200216if [ ! -f ${CONF_FILE} ]; then
217 crash 2 "Mandatory config file missing! Provide it with --conf option or ${LOCAL_PATH}/package.conf"
218fi
219
220source ${CONF_FILE}
221pushd ${LOCAL_PATH}
222
Petr Ospalý170d94b2018-12-20 16:40:58 +0100223# checking bash capability of parsing arrays
224whotest[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
229rm -rf ${PACKAGING_TARGET_DIR}
Samuli Silvius3fb890c2019-01-19 14:27:58 +0200230build_sw_artifacts
Petr Ospalý170d94b2018-12-20 16:40:58 +0100231create_sw_package
232create_resource_package
Samuli Silvius426e6c02019-02-06 11:25:01 +0200233create_aux_package
Petr Ospalý170d94b2018-12-20 16:40:58 +0100234
235popd