blob: cb0782d2a17355caa92a2a7fc2feb5512d106b1f [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:"
35 echo " ./$(basename $0) <project_name> <version> <packaging_target_dir>"
36 echo "Example: ./$(basename $0) onap-me 1.0.1 /tmp/package_onap-me_1.0.0"
37 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 {
83 cd ansible/docker
84 ./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 {
92 local pkg_root="${PACKAGING_TARGET_DIR}/onap"
93
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)
189
190# ensure that package.conf is sourced even when package.sh executed from another place
191SCRIPT_DIR=$(dirname "${0}")
192LOCAL_PATH=$(readlink -f "$SCRIPT_DIR")
193
194# lets start from script directory as some path in script are relative
195pushd "${LOCAL_PATH}"
196source ./package.conf
197
198
199if [ "$#" -lt 3 ]; then
200 echo "Missing some mandatory parameter!"
201 usage
202 exit 1
203fi
204
205if [ ! -f "./package.conf" ]; then
206 crash 2 "Mandatory config file ./package.conf missing!"
207fi
208
209# checking bash capability of parsing arrays
210whotest[0]='test' || (crash 3 "Arrays not supported in this version of bash.")
211
212
213# Prepare output directory for our packaging and create all tars
214
215rm -rf ${PACKAGING_TARGET_DIR}
Samuli Silvius3fb890c2019-01-19 14:27:58 +0200216build_sw_artifacts
Petr Ospalý170d94b2018-12-20 16:40:58 +0100217create_sw_package
218create_resource_package
219
220# This part will create aux package which consists of
221# artifacts which can be added into offline nexus during runtime
222if [ "${PREPARE_AUX_PACKAGE}" == "true" ]; then
223 create_aux_package
224else
225 echo "AUX package won't be created"
226fi
227
228popd