blob: cd2531c4b1fae7802856cb410352af24dd16edbd [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:"
Samuli Silvius730dc192019-02-04 14:26:27 +020036 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ý170d94b2018-12-20 16:40:58 +010038 echo "packaging_target_dir will be created if does not exist. All tars will be produced into it."
39}
40
41function 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 Silvius426e6c02019-02-06 11:25:01 +020054 echo "Tar file created to $(dirname ${tar_dir})/${tar_name}"
55}
56
57function 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ý170d94b2018-12-20 16:40:58 +010062}
63
64function add_metadata {
65 local metafile="$1"
Petr Ospalý170d94b2018-12-20 16:40:58 +010066 echo "Project name: ${PROJECT_NAME}" >> "${metafile}"
67 echo "Project version: ${PROJECT_VERSION}" >> "${metafile}"
68 echo "Package date: ${TIMESTAMP}" >> "${metafile}"
69}
70
71function add_additions {
72 local source="$1"
73 local target="$2"
Petr Ospalý170d94b2018-12-20 16:40:58 +010074 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 Silvius3fb890c2019-01-19 14:27:58 +020088function build_sw_artifacts {
Samuli Silvius426e6c02019-02-06 11:25:01 +020089 cd ${LOCAL_PATH}/../ansible/docker
Samuli Silvius3fb890c2019-01-19 14:27:58 +020090 ./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ý170d94b2018-12-20 16:40:58 +010097function create_sw_package {
Samuli Silvius426e6c02019-02-06 11:25:01 +020098 PKG_ROOT="${PACKAGING_TARGET_DIR}/sw"
Petr Ospalý170d94b2018-12-20 16:40:58 +010099
100 # Create directory structure of the sw package
Samuli Silvius426e6c02019-02-06 11:25:01 +0200101 mkdir -p "${PKG_ROOT}"
102 cp -r ${LOCAL_PATH}/../ansible "${PKG_ROOT}"
Petr Ospalý170d94b2018-12-20 16:40:58 +0100103
Samuli Silvius426e6c02019-02-06 11:25:01 +0200104 # Add application additional files/dirs into package based on package.conf
105 for item in "${APP_CONFIGURATION[@]}";do
Petr Ospalý170d94b2018-12-20 16:40:58 +0100106 # all SW package addons are expected within ./ansible/application folder
Samuli Silvius426e6c02019-02-06 11:25:01 +0200107 add_additions "${item}" "${PKG_ROOT}/${APPLICATION_FILES_IN_PACKAGE}"
Petr Ospalý170d94b2018-12-20 16:40:58 +0100108 done
109
Samuli Silvius426e6c02019-02-06 11:25:01 +0200110 # 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 Silvius6e5b45a2019-02-04 12:52:20 +0200113 if [ ! -z "${HELM_CHARTS_DIR}" ];
114 then
Samuli Silvius426e6c02019-02-06 11:25:01 +0200115 echo "Add application Helm charts"
Samuli Silvius6e5b45a2019-02-04 12:52:20 +0200116 # Copy charts available for ansible playbook to use/move them to target server/dir
Samuli Silvius426e6c02019-02-06 11:25:01 +0200117 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 Silvius6e5b45a2019-02-04 12:52:20 +0200120 fi
Petr Ospalý170d94b2018-12-20 16:40:58 +0100121
122 # Add metadata to the package
Samuli Silvius426e6c02019-02-06 11:25:01 +0200123 add_metadata "${PKG_ROOT}"/package.info
Petr Ospalý170d94b2018-12-20 16:40:58 +0100124
125 # Create sw tar package
Samuli Silvius426e6c02019-02-06 11:25:01 +0200126 create_pkg sw
Petr Ospalý170d94b2018-12-20 16:40:58 +0100127}
128
129function create_resource_package {
Samuli Silvius426e6c02019-02-06 11:25:01 +0200130 PKG_ROOT="${PACKAGING_TARGET_DIR}/resources"
Petr Ospalý170d94b2018-12-20 16:40:58 +0100131
132 # Create directory structure of the resource package
Samuli Silvius426e6c02019-02-06 11:25:01 +0200133 mkdir -p "${PKG_ROOT}"
Petr Ospalý170d94b2018-12-20 16:40:58 +0100134
Samuli Silvius426e6c02019-02-06 11:25:01 +0200135 # 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ý170d94b2018-12-20 16:40:58 +0100139
140 # tar file with nexus_data is expected, we should find and untar it
141 # before resource.tar is created
Samuli Silvius426e6c02019-02-06 11:25:01 +0200142 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ý170d94b2018-12-20 16:40:58 +0100150 done
151
Samuli Silvius426e6c02019-02-06 11:25:01 +0200152 create_pkg resources
Petr Ospalý170d94b2018-12-20 16:40:58 +0100153}
154
155function create_aux_package {
Samuli Silvius426e6c02019-02-06 11:25:01 +0200156 PKG_ROOT="${PACKAGING_TARGET_DIR}/aux"
Petr Ospalý170d94b2018-12-20 16:40:58 +0100157
158 # Create directory structure of the aux resource package
Samuli Silvius426e6c02019-02-06 11:25:01 +0200159 mkdir -p "${PKG_ROOT}"
Petr Ospalý170d94b2018-12-20 16:40:58 +0100160
161 # Add artifacts into resource packagee based on package.conf config
Samuli Silvius426e6c02019-02-06 11:25:01 +0200162 for item in "${APP_AUX_BINARIES[@]}";do
163 add_additions "${item}" "${PKG_ROOT}"
Petr Ospalý170d94b2018-12-20 16:40:58 +0100164 done
165
Samuli Silvius426e6c02019-02-06 11:25:01 +0200166 create_pkg aux-resources
Petr Ospalý170d94b2018-12-20 16:40:58 +0100167}
168
169#
170# =================== Main ===================
171#
172
173PROJECT_NAME="$1"
174PROJECT_VERSION="$2"
175PACKAGING_TARGET_DIR="$3"
176
177TIMESTAMP=$(date -u +%Y%m%dT%H%M%S)
Petr Ospalý170d94b2018-12-20 16:40:58 +0100178SCRIPT_DIR=$(dirname "${0}")
179LOCAL_PATH=$(readlink -f "$SCRIPT_DIR")
180
Samuli Silvius426e6c02019-02-06 11:25:01 +0200181# Relative location inside the package for application related files.
182# Application means Kubernetes application installed by Helm charts on ready cluster (e.g. onap).
183APPLICATION_FILES_IN_PACKAGE="ansible/application"
184
Samuli Silviusfe111112019-02-05 09:45:24 +0200185# 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 Silvius426e6c02019-02-06 11:25:01 +0200194HELM_CHARTS_DIR_IN_PACKAGE="${APPLICATION_FILES_IN_PACKAGE}/helm_charts"
Samuli Silviusfe111112019-02-05 09:45:24 +0200195
Petr Ospalý170d94b2018-12-20 16:40:58 +0100196if [ "$#" -lt 3 ]; then
197 echo "Missing some mandatory parameter!"
198 usage
199 exit 1
200fi
201
Samuli Silvius730dc192019-02-04 14:26:27 +0200202CONF_FILE=""
203for arg in "$@"; do
204 shift
205 case "$arg" in
206 -c|--conf)
207 CONF_FILE="$1" ;;
208 *)
209 set -- "$@" "$arg"
210 esac
211done
212
213if [ -z ${CONF_FILE} ]; then
214 CONF_FILE=${LOCAL_PATH}/package.conf # Fall to default conf file
Petr Ospalý170d94b2018-12-20 16:40:58 +0100215fi
216
Samuli Silvius730dc192019-02-04 14:26:27 +0200217if [ ! -f ${CONF_FILE} ]; then
218 crash 2 "Mandatory config file missing! Provide it with --conf option or ${LOCAL_PATH}/package.conf"
219fi
220
221source ${CONF_FILE}
222pushd ${LOCAL_PATH}
223
Petr Ospalý170d94b2018-12-20 16:40:58 +0100224# checking bash capability of parsing arrays
225whotest[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
230rm -rf ${PACKAGING_TARGET_DIR}
Samuli Silvius3fb890c2019-01-19 14:27:58 +0200231build_sw_artifacts
Petr Ospalý170d94b2018-12-20 16:40:58 +0100232create_sw_package
233create_resource_package
Samuli Silvius426e6c02019-02-06 11:25:01 +0200234create_aux_package
Petr Ospalý170d94b2018-12-20 16:40:58 +0100235
236popd