summaryrefslogtreecommitdiffstats
path: root/package.sh
blob: 3a4d12bc3f03629e025d3ee7e34b2abe63115994 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#! /usr/bin/env bash

#   COPYRIGHT NOTICE STARTS HERE
#
#   Copyright 2018 © Samsung Electronics Co., Ltd.
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
#   COPYRIGHT NOTICE ENDS HERE


# Scope of this packaging script is to generate tarfiles for offline installation
# Build of any additional artifacts is out of scope for this script

crash () {
    local exit_code="$1"
    local cause="$2"
    echo "Packaging script finished prematuraly"
    echo "Cause: $2"
    exit "${exit_code}"
}

usage () {
    echo "Usage:"
    echo "   ./$(basename $0) <project_name> <version>  <packaging_target_dir>"
    echo "Example: ./$(basename $0) onap-me 1.0.1  /tmp/package_onap-me_1.0.0"
    echo "packaging_target_dir will be created if does not exist. All tars will be produced into it."
}

function create_tar {
    local tar_dir="$1"
    local tar_name="$2"

    cd ${tar_dir}
    touch ${tar_name} # Trick to avoid sporadic "tar: .: file changed as we read it" warning message
    tar --exclude=${tar_name} -cf ../${tar_name} .
    cd - &> /dev/null # Trick to avoid printing new dir on stdout

    # Remove packaged folders
    find ${tar_dir}/* -maxdepth 0 -type d -exec rm -rf '{}' \;
    # Remove packaged files
    find ${tar_dir}/* ! -name ${tar_name} -exec rm '{}' \;
    echo "tar file ${tar_name} created in target dir"
}

function add_metadata {
    local metafile="$1"

    echo "Project name: ${PROJECT_NAME}" >> "${metafile}"
    echo "Project version: ${PROJECT_VERSION}" >> "${metafile}"
    echo "Package date: ${TIMESTAMP}" >> "${metafile}"
}

function add_additions {
    local source="$1"
    local target="$2"

    if [ -d "${source}" ]; then
        mkdir -p "${target}/$(basename $source)"
        cp -r "${source}" "${target}"
        echo "Adding directory  ... $(basename $source)"
    else
        if [ -f "${source}" ]; then
             cp "${source}" "${target}"
             echo "Adding file       ... $(basename $source)"
        else
             crash 4 "Invalid source specified for packaging: $1"
        fi
    fi
}

function create_sw_package {
    local pkg_root="${PACKAGING_TARGET_DIR}/onap"

    # Create tar package
    echo "[Creating software package]"

    # Create directory structure of the sw package
    mkdir -p "${pkg_root}"
    cp -r ansible "${pkg_root}"

    # Add additional files/dirs into package based on package.conf
    for item in "${SW_PACKAGE_ADDONS[@]}";do
        # all SW package addons are expected within ./ansible/application folder
        add_additions "${item}" "${pkg_root}/ansible/application"
    done

    # Helm charts handling
    echo "Helm charts handling"
    # Copy charts available for ansible playbook to use/move them to target server/dir
    mkdir -p "${pkg_root}"/ansible/application/helm_charts
    cp -r "${HELM_CHARTS_DIR}"/* "${pkg_root}"/ansible/application/helm_charts

    # Add metadata to the package
    add_metadata "${pkg_root}"/package.info

    # Create sw tar package
    echo "Creating tar file ..."
    PACKAGE_BASE_NAME="${SOFTWARE_PACKAGE_BASENAME}"
    create_tar "${pkg_root}" ${PACKAGE_BASE_NAME}-${PROJECT_NAME}-${PROJECT_VERSION}-sw.tar
    rm -rf "${pkg_root}"
}

function create_resource_package {
    local pkg_root="${PACKAGING_TARGET_DIR}/resources"

    # Create resource tar package
    echo "[Creating resource package]"

    # Create directory structure of the resource package
    mkdir -p "${pkg_root}"

    # Add artifacts into resource packagee based on package.conf config
    for item in "${EXTERNAL_BINARIES_PACKAGE_ADDONS[@]}";do
        if [ "$(basename $item)" == "resources" ]; then
            echo "Note: Packaging all resources at once"
            add_additions "${item}" "${PACKAGING_TARGET_DIR}"
        else
            add_additions "${item}" "${pkg_root}"
        fi
    done

    # tar file with nexus_data is expected, we should find and untar it
    # before resource.tar is created
    for i in `ls -1 ${pkg_root} | grep tar`; do
    tar tvf "${pkg_root}/${i}" | grep nexus_data &> /dev/null
    if [ $? -eq 0 ]; then
        echo "Debug: tar file with nexus blobs detected ${pkg_root}/${i}. Start unarchive ..."
        tar xf "${pkg_root}/${i}" -C "${pkg_root}" &> /dev/null
        echo "Debug: unarchive finished. Removing original file"
        rm -f "${pkg_root}/${i}"
    fi
    done

    echo "Creating tar file ..."
    PACKAGE_BASE_NAME="${SOFTWARE_PACKAGE_BASENAME}"
    create_tar "${pkg_root}" "${PACKAGE_BASE_NAME}-${PROJECT_NAME}-${PROJECT_VERSION}-resources.tar"
    rm -rf "${pkg_root}"
}

function create_aux_package {
    local pkg_root="${PACKAGING_TARGET_DIR}/aux"

    # Create aux resource tar package
    echo "Creating aux resource package"

    # Create directory structure of the aux resource package
    mkdir -p "${pkg_root}"

    # Add artifacts into resource packagee based on package.conf config
    for item in "${AUX_BINARIES_PACKAGE_ADDONS[@]}";do
        add_additions "${item}" "${pkg_root}"
    done

    echo "Creating tar file ..."
    PACKAGE_BASE_NAME="${SOFTWARE_PACKAGE_BASENAME}"
    create_tar "${pkg_root}" "${PACKAGE_BASE_NAME}-${PROJECT_NAME}-${PROJECT_VERSION}-aux-resources.tar"
    rm -rf "${pkg_root}"
}

#
# =================== Main ===================
#

PROJECT_NAME="$1"
PROJECT_VERSION="$2"
PACKAGING_TARGET_DIR="$3"

TIMESTAMP=$(date -u +%Y%m%dT%H%M%S)

# ensure that package.conf is sourced even when package.sh executed from another place
SCRIPT_DIR=$(dirname "${0}")
LOCAL_PATH=$(readlink -f "$SCRIPT_DIR")

# lets start from script directory as some path in script are relative
pushd "${LOCAL_PATH}"
source ./package.conf


if [ "$#" -lt 3 ]; then
    echo "Missing some mandatory parameter!"
    usage
    exit 1
fi

if [ ! -f "./package.conf" ]; then
    crash 2 "Mandatory config file ./package.conf missing!"
fi

# checking bash capability of parsing arrays
whotest[0]='test' || (crash 3 "Arrays not supported in this version of bash.")


# Prepare output directory for our packaging and create all tars

rm -rf ${PACKAGING_TARGET_DIR}
create_sw_package
create_resource_package

# This part will create aux package which consists of
# artifacts which can be added into offline nexus during runtime
if [ "${PREPARE_AUX_PACKAGE}" == "true" ]; then
    create_aux_package
else
    echo "AUX package won't be created"
fi

popd