blob: 0e4f121374a2043b7260468c0fcdc8bc514032e1 [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 '{}' \;
53 echo "tar file ${tar_name} created in target dir"
54}
55
56function add_metadata {
57 local metafile="$1"
58
59 echo "Project name: ${PROJECT_NAME}" >> "${metafile}"
60 echo "Project version: ${PROJECT_VERSION}" >> "${metafile}"
61 echo "Package date: ${TIMESTAMP}" >> "${metafile}"
62}
63
64function add_additions {
65 local source="$1"
66 local target="$2"
67
68 if [ -d "${source}" ]; then
69 mkdir -p "${target}/$(basename $source)"
70 cp -r "${source}" "${target}"
71 echo "Adding directory ... $(basename $source)"
72 else
73 if [ -f "${source}" ]; then
74 cp "${source}" "${target}"
75 echo "Adding file ... $(basename $source)"
76 else
77 crash 4 "Invalid source specified for packaging: $1"
78 fi
79 fi
80}
81
Samuli Silvius3fb890c2019-01-19 14:27:58 +020082function build_sw_artifacts {
Tomáš Levoraade405a2019-01-29 14:25:04 +010083 cd ../ansible/docker
Samuli Silvius3fb890c2019-01-19 14:27:58 +020084 ./build_ansible_image.sh
85 if [ $? -ne 0 ]; then
86 crash 5 "Building of ansible runner image failed."
87 fi
88 cd -
89}
90
Petr Ospalý170d94b2018-12-20 16:40:58 +010091function create_sw_package {
Samuli Silvius730dc192019-02-04 14:26:27 +020092 local pkg_root="${PACKAGING_TARGET_DIR}/sw"
Petr Ospalý170d94b2018-12-20 16:40:58 +010093
94 # Create tar package
95 echo "[Creating software package]"
96
97 # Create directory structure of the sw package
98 mkdir -p "${pkg_root}"
99 cp -r ansible "${pkg_root}"
100
101 # Add additional files/dirs into package based on package.conf
102 for item in "${SW_PACKAGE_ADDONS[@]}";do
103 # all SW package addons are expected within ./ansible/application folder
104 add_additions "${item}" "${pkg_root}/ansible/application"
105 done
106
107 # Helm charts handling
108 echo "Helm charts handling"
109 # Copy charts available for ansible playbook to use/move them to target server/dir
110 mkdir -p "${pkg_root}"/ansible/application/helm_charts
111 cp -r "${HELM_CHARTS_DIR}"/* "${pkg_root}"/ansible/application/helm_charts
112
113 # Add metadata to the package
114 add_metadata "${pkg_root}"/package.info
115
116 # Create sw tar package
117 echo "Creating tar file ..."
118 PACKAGE_BASE_NAME="${SOFTWARE_PACKAGE_BASENAME}"
119 create_tar "${pkg_root}" ${PACKAGE_BASE_NAME}-${PROJECT_NAME}-${PROJECT_VERSION}-sw.tar
120 rm -rf "${pkg_root}"
121}
122
123function create_resource_package {
124 local pkg_root="${PACKAGING_TARGET_DIR}/resources"
125
126 # Create resource tar package
127 echo "[Creating resource package]"
128
129 # Create directory structure of the resource package
130 mkdir -p "${pkg_root}"
131
132 # Add artifacts into resource packagee based on package.conf config
133 for item in "${EXTERNAL_BINARIES_PACKAGE_ADDONS[@]}";do
134 if [ "$(basename $item)" == "resources" ]; then
135 echo "Note: Packaging all resources at once"
136 add_additions "${item}" "${PACKAGING_TARGET_DIR}"
137 else
138 add_additions "${item}" "${pkg_root}"
139 fi
140 done
141
142 # tar file with nexus_data is expected, we should find and untar it
143 # before resource.tar is created
144 for i in `ls -1 ${pkg_root} | grep tar`; do
145 tar tvf "${pkg_root}/${i}" | grep nexus_data &> /dev/null
146 if [ $? -eq 0 ]; then
147 echo "Debug: tar file with nexus blobs detected ${pkg_root}/${i}. Start unarchive ..."
148 tar xf "${pkg_root}/${i}" -C "${pkg_root}" &> /dev/null
149 echo "Debug: unarchive finished. Removing original file"
150 rm -f "${pkg_root}/${i}"
151 fi
152 done
153
154 echo "Creating tar file ..."
155 PACKAGE_BASE_NAME="${SOFTWARE_PACKAGE_BASENAME}"
156 create_tar "${pkg_root}" "${PACKAGE_BASE_NAME}-${PROJECT_NAME}-${PROJECT_VERSION}-resources.tar"
157 rm -rf "${pkg_root}"
158}
159
160function create_aux_package {
161 local pkg_root="${PACKAGING_TARGET_DIR}/aux"
162
163 # Create aux resource tar package
164 echo "Creating aux resource package"
165
166 # Create directory structure of the aux resource package
167 mkdir -p "${pkg_root}"
168
169 # Add artifacts into resource packagee based on package.conf config
170 for item in "${AUX_BINARIES_PACKAGE_ADDONS[@]}";do
171 add_additions "${item}" "${pkg_root}"
172 done
173
174 echo "Creating tar file ..."
175 PACKAGE_BASE_NAME="${SOFTWARE_PACKAGE_BASENAME}"
176 create_tar "${pkg_root}" "${PACKAGE_BASE_NAME}-${PROJECT_NAME}-${PROJECT_VERSION}-aux-resources.tar"
177 rm -rf "${pkg_root}"
178}
179
180#
181# =================== Main ===================
182#
183
184PROJECT_NAME="$1"
185PROJECT_VERSION="$2"
186PACKAGING_TARGET_DIR="$3"
187
188TIMESTAMP=$(date -u +%Y%m%dT%H%M%S)
Petr Ospalý170d94b2018-12-20 16:40:58 +0100189SCRIPT_DIR=$(dirname "${0}")
190LOCAL_PATH=$(readlink -f "$SCRIPT_DIR")
191
Petr Ospalý170d94b2018-12-20 16:40:58 +0100192if [ "$#" -lt 3 ]; then
193 echo "Missing some mandatory parameter!"
194 usage
195 exit 1
196fi
197
Samuli Silvius730dc192019-02-04 14:26:27 +0200198CONF_FILE=""
199for arg in "$@"; do
200 shift
201 case "$arg" in
202 -c|--conf)
203 CONF_FILE="$1" ;;
204 *)
205 set -- "$@" "$arg"
206 esac
207done
208
209if [ -z ${CONF_FILE} ]; then
210 CONF_FILE=${LOCAL_PATH}/package.conf # Fall to default conf file
Petr Ospalý170d94b2018-12-20 16:40:58 +0100211fi
212
Samuli Silvius730dc192019-02-04 14:26:27 +0200213if [ ! -f ${CONF_FILE} ]; then
214 crash 2 "Mandatory config file missing! Provide it with --conf option or ${LOCAL_PATH}/package.conf"
215fi
216
217source ${CONF_FILE}
218pushd ${LOCAL_PATH}
219
Petr Ospalý170d94b2018-12-20 16:40:58 +0100220# checking bash capability of parsing arrays
221whotest[0]='test' || (crash 3 "Arrays not supported in this version of bash.")
222
223
224# Prepare output directory for our packaging and create all tars
225
226rm -rf ${PACKAGING_TARGET_DIR}
Samuli Silvius3fb890c2019-01-19 14:27:58 +0200227build_sw_artifacts
Petr Ospalý170d94b2018-12-20 16:40:58 +0100228create_sw_package
229create_resource_package
230
231# This part will create aux package which consists of
232# artifacts which can be added into offline nexus during runtime
233if [ "${PREPARE_AUX_PACKAGE}" == "true" ]; then
234 create_aux_package
235else
236 echo "AUX package won't be created"
237fi
238
239popd