aboutsummaryrefslogtreecommitdiffstats
path: root/controlloop/build/docker-cl
blob: 663b1f4b100f3069426f360283f61cca8737c907 (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/env bash

# ########################################################################
#
# Copyright 2019 AT&T Intellectual Property. All rights reserved
#
# 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.
# ########################################################################

function usage {
    if [[ ${DEBUG} == y ]]; then
        echo "-- $0 --"
        set -x
    fi

    echo
    echo "SYNTAX:"
    echo -e "\t$(basename "$0") "
    echo -e "\t\t[--help|-h] [--build|-b <repo:tag>] [--push|-p <repo:tag>]"
    echo -e "\t\t[--verify|-v] [--merge|-m] [--release|-r] "
    echo -e "\t\t[--run <volume-full-path>] [--cmd <volume-full-path> <cmd>]"
    echo
    echo
}

function build {
    if [[ ${DEBUG} == y ]]; then
        echo "-- ${FUNCNAME[0]} $* --"
        set -x
    fi

    local tag tags

    for tag in "$@"; do
        tags="${tags} --tag ${tag}"
    done

    (
    set -x;
    docker pull "${DOCKER_PULL_REPOSITORY}"/onap/policy-drools
    docker tag "${DOCKER_PULL_REPOSITORY}"/onap/policy-drools onap/policy-drools
    docker build ${BUILD_ARGS} ${tags} ${IMAGE_PATH}
    )

    if [[ $? != 0 ]]; then
        echo -e "\nERROR: docker build\n"
        return 1
    fi

    docker image ls | egrep "TAG|${IMAGE}"

    return 0
}

function push {
    if [[ ${DEBUG} == y ]]; then
        echo "-- ${FUNCNAME[0]} $* --"
        set -x
    fi

    local tag="${1}"

    if [[ -z ${tag} ]]; then
        echo -e "\nERROR: no <repo:tag> provided\n"
        return 1
    fi

    (set -x; docker push ${tag})

    if [[ $? != 0 ]]; then
        echo -e "\nERROR: docker push\n"
        return 2
    fi

    return 0
}

function release {
    if [[ ${DEBUG} == y ]]; then
        echo "-- ${FUNCNAME[0]} --"
        set -x
    fi

    build ${RELEASE_BUILD_TAGS} && push ${RELEASE_PUSH_TAGS}
    return $?
}

function merge {
    if [[ ${DEBUG} == y ]]; then
        echo "-- ${FUNCNAME[0]} --"
        set -x
    fi

    build ${MERGE_BUILD_TAGS} && push ${MERGE_PUSH_TAGS}
    return $?
}

function verify {
    if [[ ${DEBUG} == y ]]; then
        echo "-- ${FUNCNAME[0]} --"
        set -x
    fi

    build ${VERIFY_BUILD_TAGS}

    return $?
}

function run {
    local debugEnv

    if [[ ${DEBUG} == y ]]; then
        echo "-- ${FUNCNAME[0]} $* --"
        set -x
        debugEnv='-e "DEBUG=y"'
    fi

    local volume="$1"
    local cmd="$2"

    if [[ ! -d ${volume} ]]; then
        echo -e "\nERROR: an absolute path to a volume must be provided: ${volume}\n"
        return 1
    fi

    (
    set -x
    docker run --rm ${debugEnv} -it -v "${volume}":/tmp/policy-install/config -p 9696:9696 \
        --name pdpd-cl onap/"${IMAGE}" ${cmd}
    )

    return $?
}

(
if [[ ${DEBUG} == y ]]; then
    echo "-- $0 $* --"
    set -x
fi

VERSION_PATH="controlloop/packages/docker-controlloop/target/version"
if [[ ! -f "${VERSION_PATH}" ]]; then
    cd  "$(dirname "$0")"/../..
    if [[ ! -f "${VERSION_PATH}" ]]; then
        echo -e "\nERROR: ${VERSION_PATH} cannot be found\n"
        usage
        exit 1
    fi
fi

DOCKER_PULL_REPOSITORY="nexus3.onap.org:10001"
DOCKER_PUSH_REPOSITORY="nexus3.onap.org:10003"
IMAGE=policy-pdpd-cl
IMAGE_PATH="controlloop/packages/docker-controlloop/target/${IMAGE}"

VERSION="$(cat "${VERSION_PATH}")"
MAJOR_MINOR_VERSION="$(cut -f 1,2 -d . "${VERSION_PATH}")"
TIMESTAMP="$(date -u +%Y%m%dT%H%M%S)"

if [[ -z "${VERSION}" ]]; then
    echo -e "\nERROR: no version\n"
    usage
    exit 1
fi

if [[ -z "${MAJOR_MINOR_VERSION}" ]]; then
    echo "\nERROR: no major/minor version: ${VERSION}\n"
    usage
    exit 1
fi

if [[ ${VERSION} == *"SNAPSHOT"* ]]; then
    MAJOR_MINOR_VERSION="${MAJOR_MINOR_VERSION}-SNAPSHOT"
else
    MAJOR_MINOR_VERSION="${MAJOR_MINOR_VERSION}-STAGING"
fi

BUILD_ARGS="--build-arg BUILD_VERSION_APP_CL=${VERSION}"

echo
echo -e "BUILD INFO:"
echo -e "\timage: ${IMAGE}"
echo -e "\timage-path: ${IMAGE_PATH}"
echo -e "\timage: ${IMAGE}"
echo -e "\tregistry: ${DOCKER_PUSH_REPOSITORY}"
echo -e "\tpatch: ${VERSION}"
echo -e "\tversion: ${MAJOR_MINOR_VERSION}"
echo -e "\ttimestamp: ${TIMESTAMP}"
echo

TAG_LATEST="onap/${IMAGE}:latest"
TAG_REPO_LATEST="${DOCKER_PUSH_REPOSITORY}/onap/${IMAGE}:latest"
TAG_REPO_VERSION_LATEST="${DOCKER_PUSH_REPOSITORY}/onap/${IMAGE}:${MAJOR_MINOR_VERSION}-latest"
TAG_REPO_VERSION_TIMESTAMP="${DOCKER_PUSH_REPOSITORY}/onap/${IMAGE}:${VERSION}-${TIMESTAMP}Z"
TAG_REPO_VERSION_STAGING_TIMESTAMP="${DOCKER_PUSH_REPOSITORY}/onap/${IMAGE}:${VERSION}-STAGING-${TIMESTAMP}Z"

VERIFY_BUILD_TAGS="${TAG_LATEST} ${TAG_REPO_VERSION_LATEST} ${TAG_REPO_VERSION_TIMESTAMP}"

MERGE_BUILD_TAGS="${TAG_LATEST} ${TAG_REPO_VERSION_LATEST} ${TAG_REPO_VERSION_TIMESTAMP}"
RELEASE_BUILD_TAGS="${TAG_LATEST} ${TAG_REPO_LATEST} ${TAG_REPO_VERSION_LATEST} ${TAG_REPO_VERSION_STAGING_TIMESTAMP}"

MERGE_PUSH_TAGS="${TAG_REPO_VERSION_LATEST} ${TAG_REPO_VERSION_TIMESTAMP}"
RELEASE_PUSH_TAGS="${TAG_REPO_VERSION_LATEST} ${TAG_REPO_VERSION_STAGING_TIMESTAMP}"

echo
echo -e "TAGS:"
echo -e "\tBUILD:"
echo -e "\t\tverify:\n\t\t\t${VERIFY_BUILD_TAGS// /$'\n\t\t\t'}"
echo -e "\t\tmerge:\n\t\t\t${MERGE_BUILD_TAGS// /$'\n\t\t\t'}"
echo -e "\t\trelease:\n\t\t\t${RELEASE_BUILD_TAGS// /$'\n\t\t\t'}"
echo -e "\tPUSH:"
echo -e "\t\tverify: "
echo -e "\t\tmerge:\n\t\t\t${MERGE_PUSH_TAGS// /$'\n\t\t\t'}"
echo -e "\t\trelease:\n\t\t\t${RELEASE_PUSH_TAGS// /$'\n\t\t\t'}"
echo
echo

until [[ -z "$1" ]]; do
    case "$1" in
        -h|--help)      usage
                        ;;
        -b|--build)     shift
                        build "$1"
                        ;;
        -p|--push)      shift
                        push "$1"
                        ;;
        -r|--release)   release
                        ;;
        -m|--merge)     merge
                        ;;
        -v|--verify)    verify
                        ;;
        --run)          shift
                        volumeArg="$1"
                        run "${volumeArg}"
                        ;;
        --cmd)          shift
                        volumeArg="$1"
                        shift
                        cmdArg="$1"
                        run "${volumeArg}" "${cmdArg}"
                        ;;
        *)              usage
                        exit 1
                        ;;
    esac
    shift
done
)