Adding support for helm plugins
[oom/offline-installer.git] / package.sh
1 #! /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
25 crash () {
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
33 usage () {
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
40 function 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
56 function 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
64 function 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
82 function create_sw_package {
83     local pkg_root="${PACKAGING_TARGET_DIR}/onap"
84
85     # Create tar package
86     echo "[Creating software package]"
87
88     # Create directory structure of the sw package
89     mkdir -p "${pkg_root}"
90     cp -r ansible "${pkg_root}"
91
92     # Add additional files/dirs into package based on package.conf
93     for item in "${SW_PACKAGE_ADDONS[@]}";do
94         # all SW package addons are expected within ./ansible/application folder
95         add_additions "${item}" "${pkg_root}/ansible/application"
96     done
97
98     # Helm charts handling
99     echo "Helm charts handling"
100     # Copy charts available for ansible playbook to use/move them to target server/dir
101     mkdir -p "${pkg_root}"/ansible/application/helm_charts
102     cp -r "${HELM_CHARTS_DIR}"/* "${pkg_root}"/ansible/application/helm_charts
103
104     # Add metadata to the package
105     add_metadata "${pkg_root}"/package.info
106
107     # Create sw tar package
108     echo "Creating tar file ..."
109     PACKAGE_BASE_NAME="${SOFTWARE_PACKAGE_BASENAME}"
110     create_tar "${pkg_root}" ${PACKAGE_BASE_NAME}-${PROJECT_NAME}-${PROJECT_VERSION}-sw.tar
111     rm -rf "${pkg_root}"
112 }
113
114 function create_resource_package {
115     local pkg_root="${PACKAGING_TARGET_DIR}/resources"
116
117     # Create resource tar package
118     echo "[Creating resource package]"
119
120     # Create directory structure of the resource package
121     mkdir -p "${pkg_root}"
122
123     # Add artifacts into resource packagee based on package.conf config
124     for item in "${EXTERNAL_BINARIES_PACKAGE_ADDONS[@]}";do
125         if [ "$(basename $item)" == "resources" ]; then
126             echo "Note: Packaging all resources at once"
127             add_additions "${item}" "${PACKAGING_TARGET_DIR}"
128         else
129             add_additions "${item}" "${pkg_root}"
130         fi
131     done
132
133     # tar file with nexus_data is expected, we should find and untar it
134     # before resource.tar is created
135     for i in `ls -1 ${pkg_root} | grep tar`; do
136     tar tvf "${pkg_root}/${i}" | grep nexus_data &> /dev/null
137     if [ $? -eq 0 ]; then
138         echo "Debug: tar file with nexus blobs detected ${pkg_root}/${i}. Start unarchive ..."
139         tar xf "${pkg_root}/${i}" -C "${pkg_root}" &> /dev/null
140         echo "Debug: unarchive finished. Removing original file"
141         rm -f "${pkg_root}/${i}"
142     fi
143     done
144
145     echo "Creating tar file ..."
146     PACKAGE_BASE_NAME="${SOFTWARE_PACKAGE_BASENAME}"
147     create_tar "${pkg_root}" "${PACKAGE_BASE_NAME}-${PROJECT_NAME}-${PROJECT_VERSION}-resources.tar"
148     rm -rf "${pkg_root}"
149 }
150
151 function create_aux_package {
152     local pkg_root="${PACKAGING_TARGET_DIR}/aux"
153
154     # Create aux resource tar package
155     echo "Creating aux resource package"
156
157     # Create directory structure of the aux resource package
158     mkdir -p "${pkg_root}"
159
160     # Add artifacts into resource packagee based on package.conf config
161     for item in "${AUX_BINARIES_PACKAGE_ADDONS[@]}";do
162         add_additions "${item}" "${pkg_root}"
163     done
164
165     echo "Creating tar file ..."
166     PACKAGE_BASE_NAME="${SOFTWARE_PACKAGE_BASENAME}"
167     create_tar "${pkg_root}" "${PACKAGE_BASE_NAME}-${PROJECT_NAME}-${PROJECT_VERSION}-aux-resources.tar"
168     rm -rf "${pkg_root}"
169 }
170
171 #
172 # =================== Main ===================
173 #
174
175 PROJECT_NAME="$1"
176 PROJECT_VERSION="$2"
177 PACKAGING_TARGET_DIR="$3"
178
179 TIMESTAMP=$(date -u +%Y%m%dT%H%M%S)
180
181 # ensure that package.conf is sourced even when package.sh executed from another place
182 SCRIPT_DIR=$(dirname "${0}")
183 LOCAL_PATH=$(readlink -f "$SCRIPT_DIR")
184
185 # lets start from script directory as some path in script are relative
186 pushd "${LOCAL_PATH}"
187 source ./package.conf
188
189
190 if [ "$#" -lt 3 ]; then
191     echo "Missing some mandatory parameter!"
192     usage
193     exit 1
194 fi
195
196 if [ ! -f "./package.conf" ]; then
197     crash 2 "Mandatory config file ./package.conf missing!"
198 fi
199
200 # checking bash capability of parsing arrays
201 whotest[0]='test' || (crash 3 "Arrays not supported in this version of bash.")
202
203
204 # Prepare output directory for our packaging and create all tars
205
206 rm -rf ${PACKAGING_TARGET_DIR}
207 create_sw_package
208 create_resource_package
209
210 # This part will create aux package which consists of
211 # artifacts which can be added into offline nexus during runtime
212 if [ "${PREPARE_AUX_PACKAGE}" == "true" ]; then
213     create_aux_package
214 else
215     echo "AUX package won't be created"
216 fi
217
218 popd