summaryrefslogtreecommitdiffstats
path: root/policy-management/src/main/server-gen/bin/deploy-artifact
blob: b25aaf9cdaf46cc64b1347a83ef92fa3d3334ea9 (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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
#!/usr/bin/env bash

# ============LICENSE_START=======================================================
# ONAP
# ================================================================================
# Copyright (C) 2018-2020 AT&T Intellectual Property. All rights reserved.
# Modifications Copyright (C) 2020 Bell Canada.
# ================================================================================
# 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.
# ============LICENSE_END=========================================================

source ${POLICY_HOME}/etc/profile.d/env.sh

##############################################################################
# Usage: usage
##############################################################################

function usage() {
    echo
    echo -e "syntax: $(basename "$0") "
    echo -e "\t [-f|-l|-d]"
    echo -e "\t -s <custom-settings> "
    echo -e "\t -a <artifact> "
    echo
    echo -e "Options:"
    echo -e "\t -f|--file-repo: deploy in the file repository"
    echo -e "\t -l|--local-repo: install in the local repository"
    echo -e "\t -d|--dependencies: install dependencies in the local repository"
    echo -e "\t -s|--settings: custom settings.xml"
    echo -e "\t -a|--artifact: file artifact (jar or pom) to deploy and/or install"
    echo
    echo
}

##############################################################################
# Usage: init <artifact>
#
# If the artifact is a jar, this function extracts the maven metadata for
# consumption in this script.
#
# As a result the global variables will be set:
# WORKING_DIR: working directory with extracted files.
# WORKING_POM: pom file location
# WORKING_POM_PROPERTIES: pom properties file location
##############################################################################

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

    local artifact="${1}"
    if [[ ! -f ${artifact} ]]; then
        echo "${artifact}: artifact does not exist"
        return 1
    fi

    if [[ ${artifact} != *.jar ]]; then
        return 0
    fi

    local dir=$(mktemp -d)
    local jar="${artifact##*/}"

    WORKING_DIR=$(realpath "${dir}")

    cp -p "${artifact}" "${WORKING_DIR}/${jar}"
    pushd "${dir}"

    local rc=0

    # determine name of 'pom' file within JAR
    local pom=$(jar tf "${jar}" META-INF | grep '/pom\.xml$' | head -1)
    if [[ -n ${pom} ]] ; then
        jar xf "${jar}" "${pom}"
        WORKING_POM=$(realpath "${pom}")
    else
        echo "${artifact}: pom not found"
    fi

    local pomProperties=$(jar tf "${jar}" META-INF | grep '/pom\.properties$' | head -1)
    if [[ -n ${pomProperties} ]]; then
        jar xf "${jar}" "${pomProperties}"
        WORKING_POM_PROPERTIES=$(realpath ${pomProperties})
        sed -i 's/\r$//' "${WORKING_POM_PROPERTIES}"
        source "${WORKING_POM_PROPERTIES}"
        echo "${artifact}: sourcing in ${WORKING_POM_PROPERTIES}"
    else
        echo "${artifact}: pom.properties not found"
        if [[ -n ${WORKING_POM} ]]; then
            if ! getPomAttributes "${WORKING_POM}" artifactId groupId version ; then
                echo "${WORKING_POM}: cannot extract maven coordinates"
                rc=1
            fi
        else
            echo "${artifact}: cannot extract maven coordinates"
            rc=1
        fi
    fi

    if [[ -z ${version} ]] || [[ -z ${groupId} ]] || [[ -z ${artifactId} ]]; then
        echo "${artifact}: some coordinates cannot be extracted"
        rc=1
    fi

    echo "${artifact}: coordinates ${groupId}:${artifactId}:${version}"
    popd

    return ${rc}
}

##############################################################################
# Usage: cleanup
#
# Clean up temporary resources.
##############################################################################

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

    if [[ -n ${WORKING_DIR} ]]; then
        rm -rf "${WORKING_DIR}"
    fi
}

##############################################################################
# Usage: getPomAttributes <pom-file> <attribute> ...
#
# This function performs simplistic parsing of a 'pom.xml' file, extracting
# the specified attributes (e.g. 'groupId', 'artifactId', 'version'). The
# attributes are returned as environment variables with the associated name
##############################################################################

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

    local file="$1"
    if [[ ! -f "${file}" ]]; then
        echo "${file}: file does not exist"
        return 1
    fi

    local tab=$'\t' rval=0 attr value
    shift

    for attr in "$@" ; do
        # Try to fetch the parameter associated with the 'pom.xml' file.
        # Initially, the 'parent' element is excluded. If the desired
        # parameter is not found, the 'parent' element is included in the
        # second attempt.
        value=$(sed -n \
            -e '/<parent>/,/<\/parent>/d' \
            -e '/<dependencies>/,/<\/dependencies>/d' \
            -e '/<build>/,/<\/build>/d' \
            -e '/<profiles>/,/<\/profiles>/d' \
            -e '/<description>/,/<\/description>/d' \
            -e '/<packaging>/,/<\/packaging>/d' \
            -e '/<modelVersion>/,/<\/modelVersion>/d' \
            -e '/<properties>/,/<\/properties>/d' \
            -e "/^[ ${tab}]*<${attr}>\([^<]*\)<\/${attr}>.*/{s//\1/p;}" \
            <"${file}")

        if [[ "${value}" == "" ]]; then
            # need to check parent for parameter
            value=$(sed -n \
                -e '/<dependencies>/,/<\/dependencies>/d' \
                -e '/<build>/,/<\/build>/d' \
                -e '/<profiles>/,/<\/profiles>/d' \
                -e '/<description>/,/<\/description>/d' \
                -e '/<packaging>/,/<\/packaging>/d' \
                -e '/<modelVersion>/,/<\/modelVersion>/d' \
                -e '/<properties>/,/<\/properties>/d' \
                -e "/^[ ${tab}]*<${attr}>\([^<]*\)<\/${attr}>.*/{s//\1/p;}" \
                <"${file}")

            if [[ "${value}" == "" ]] ; then
                echo "${file}: Can't determine ${attr}"
                rval=1
            fi
        fi

        # the following sets an environment variable with the name referred
        # to by ${attr}
        read "${attr}" <<<"${value}"
    done
    return ${rval}
}

##############################################################################
# Usage: setMavenProxyArgs
#
# This function performs parsing of http proxy environment variable if provided,
# extracting the attributes such as proxy host, port, username and password.
# These proxy attributes are set into the global variable for maven proxy
# settings to be used as build arguments with maven commands.
##############################################################################

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

    if [[ -z ${http_proxy} ]]; then
        return 0
    fi

    local proxy_creds="${http_proxy#*//}"
    local proxy="${proxy_creds#*@}"
    local host="${proxy%:*}"
    local port="${proxy#*:}"
    MVN_PROXY_SETTINGS="-DproxyHost=${host} -DproxyPort=${port}"

    if [[ "$proxy_creds" == *"@"* ]]; then
        local creds="${proxy_creds%%@*}"
        local username="${creds%:*}"
        local password="${creds#*:}"
        MVN_PROXY_SETTINGS+=" -DproxyUsername=${username} -DproxyPassword=${password}"
    fi
}

##############################################################################
# Usage: deployJar <jar-file>
#
# This function deploys a JAR file in a repository, as well as
# the 'pom.xml' member it contains.
#################################################################

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

    local file="${1}"

    if [[ ! -f ${file} ]]; then
        return 1
    fi

    local repoId repoUrl
    if [[ "${version}" =~ SNAPSHOT ]] ; then
        repoId=${SNAPSHOT_REPOSITORY_ID}
        repoUrl=${SNAPSHOT_REPOSITORY_URL}
    else
        repoId=${RELEASE_REPOSITORY_ID}
        repoUrl=${RELEASE_REPOSITORY_URL}
    fi

    if [[ -z ${repoUrl} ]] || [[ -z ${repoId} ]]; then
        echo "{file}: no repository id/url to deploy jar"
        return 1
    fi

    echo "${file}: deploying jar artifact to repository ${repoId}: ${repoUrl}"
    echo "${file}: coordinates ${groupId} ${artifactId} ${version}"

    mvn ${CUSTOM_SETTINGS} ${MVN_PROXY_SETTINGS} deploy:deploy-file \
        -Dfile="${file}" \
        -Dversion="${version}" \
        -Dpackaging=jar \
        -DgeneratePom=false \
        -DpomFile="${WORKING_POM}" \
        -DrepositoryId="${repoId}" \
        -Durl="${repoUrl}" \
        -DupdateReleaseInfo=true

    return ${?}
}

##############################################################################
# Usage: deployPom <pom-file>
#
# This function deploys a 'pom.xml' file in the local repository
##############################################################################

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

    local file="${1}"

    if [[ ! -f ${file} ]]; then
        return 1
    fi

    if ! getPomAttributes "${file}" artifactId groupId version ; then
        echo "${file}: cannot deploy pom due to missing attributes"
        return 1
    fi

    local repoId repoUrl
    if [[ "${version}" =~ SNAPSHOT ]] ; then
        repoId=${SNAPSHOT_REPOSITORY_ID}
        repoUrl=${SNAPSHOT_REPOSITORY_URL}
    else
        repoId=${RELEASE_REPOSITORY_ID}
        repoUrl=${RELEASE_REPOSITORY_URL}
    fi

    echo "${file}: deploying pom artifact to repository ${repoId}: ${repoUrl}"
    echo "${file}: coordinates ${groupId} ${artifactId} ${version}"

    mvn ${CUSTOM_SETTINGS} ${MVN_PROXY_SETTINGS} deploy:deploy-file \
        -Dfile="${file}" \
        -Dpackaging=pom \
        -DgeneratePom=false \
        -DgroupId="${groupId}" \
        -DartifactId="${artifactId}" \
        -Dversion="${version}" \
        -DrepositoryId="${repoId}" \
        -Durl="${repoUrl}" \
        -DupdateReleaseInfo=true

    return ${?}
}

##############################################################################
# Usage: deployArtifact
#
# This function deploys a maven artifact in a repository
##############################################################################

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

    local file="${1}"
    if [[ -z "${file}" ]]; then
        echo "${file}: artifact file not provided"
        return 1
    fi

    if [[ ! -f "${file}" ]]; then
        echo "${file}: artifact file does not exist"
        return 1
    fi

    case "${file}" in
        *pom.xml|*.pom)
            deployPom "${file}"
            return ${?}
            ;;
        *.jar)
            deployJar "${file}"
            return ${?}
            ;;
        *)  echo "${file}: Don't know how to deploy artifact"
            return 1
            ;;
    esac
}

##############################################################################
# Usage: installJar <artifact-file>
#
# This function installs a jar packaged artifact in the local repository
##############################################################################

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

    local file="${1}"

    if [[ ! -f ${file} ]]; then
        return 1
    fi

    mvn ${CUSTOM_SETTINGS} ${MVN_PROXY_SETTINGS} \
    org.apache.maven.plugins:maven-install-plugin:3.0.0-M1:install-file -Dfile="${file}"

    return $?
}

##############################################################################
# Usage: installPom <pom-file>
#
# This function installs a pom file in the local repository
##############################################################################

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

    local file="${1}"

    if [[ ! -f ${file} ]]; then
        return 1
    fi

    mvn ${CUSTOM_SETTINGS} ${MVN_PROXY_SETTINGS} \
        org.apache.maven.plugins:maven-install-plugin:3.0.0-M1:install-file \
        -Dpackaging=pom \
        -Dfile="${file}" \
        -DpomFile="${file}"

    return $?
}

##############################################################################
# Usage: installArtifact
#
# This function installs a maven artifacts in the local repository
##############################################################################

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

    local file="${1}"
    if [[ -z "${file}" ]]; then
        echo "${file}: artifact file not provided"
        return 1
    fi

    if [[ ! -f "${file}" ]]; then
        echo "${file}: artifact file does not exist"
        return 1
    fi

    case "${file}" in
        *pom.xml|*.pom)
            installPom "${file}"
            return ${?}
            ;;
        *.jar)
            installJar "${file}"
            return ${?}
            ;;
        *)  echo "${file}: Don't know how to install the artifact"
            return 1
            ;;
    esac
}

##############################################################################
# Usage: installDependencies <pom-file>
#
# This function installs the dependencies of an artifact in the file or
# local repository
##############################################################################

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

    local file="${1}"

    if [[ ! -f ${file} ]]; then
        return 1
    fi

    if [[ -z ${DEPENDENCY_REPO_URL} ]]; then
        echo "${file}: no repo url to install dependencies"
        return 1
    fi

    echo "${file}: deploying dependencies from repository ${DEPENDENCY_REPO_URL}"
    echo "${file}: coordinates ${groupId} ${artifactId} ${version}"

    mvn ${CUSTOM_SETTINGS} ${MVN_PROXY_SETTINGS} \
        org.apache.maven.plugins:maven-dependency-plugin:3.1.1:get \
        -DartifactId="${artifactId}" \
        -DgroupId="${groupId}" \
        -Dversion="${version}" \
        -DremoteRepositories="${DEPENDENCY_REPO_URL}"

    return ${?}
}

##############################################################################
# MAIN
##############################################################################

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

# reset globals

unset ARTIFACT_FILE
unset LOCAL_INSTALL
unset INSTALL_DEPS
unset FILE_REPO_INSTALL
unset WORKING_DIR
unset WORKING_POM
unset WORKING_POM_PROPERTIES
unset DEPENDENCY_REPO_URL
unset SETTINGS_FILE
unset CUSTOM_SETTINGS
unset MVN_PROXY_SETTINGS

# process input

until [[ -z "$1" ]]; do
    case $1 in
        -a|--artifact)     shift
                           ARTIFACT_FILE=$1
                           ;;
        -s|--settings)     shift
                           SETTINGS_FILE=$1
                           ;;
        -l|--local-repo)   LOCAL_INSTALL="true"
                           ;;
        -d|--dependencies) INSTALL_DEPS="true"
                           ;;
        -f|--file-repo)    FILE_REPO_INSTALL="true"
                           ;;
        *)                 usage
                           exit 1
                           ;;
    esac
    shift
done

if [[ -z ${ARTIFACT_FILE} ]]; then
    echo "No artifact file provided: $*"
    usage
    exit 1
fi

if [[ -n ${SETTINGS_FILE} ]]; then
    CUSTOM_SETTINGS="--settings=${SETTINGS_FILE}"
fi

# Set proxy attributes into MVN_PROXY_SETTINGS variable
setMavenProxyArgs

# retval has the count of failed operations

retval=0

# initialize

init "${ARTIFACT_FILE}"
retval=$?
if [[ ${retval} != 0 ]]; then
    cleanup
    exit ${retval}
fi

# remote repo deploy operation
#
# SNAPSHOT_REPOSITORY_URL and RELEASE_REPOSITORY_URL
# are pre-existing environmental variables (base.conf)

if [[ -n ${SNAPSHOT_REPOSITORY_URL} ]] || [[ -n ${RELEASE_REPOSITORY_URL} ]]; then
    deployArtifact "${ARTIFACT_FILE}"
    retval=$(( retval + ${?} ))
fi

# deploy in file repository

if [[ -n ${FILE_REPO_INSTALL} ]]; then
    FILE_REPO_ID="file-repository"
    FILE_REPO_URL="file:${HOME}/.m2/file-repository"

    SNAPSHOT_REPOSITORY_ID="${FILE_REPO_ID}"
    SNAPSHOT_REPOSITORY_URL="${FILE_REPO_URL}"
    RELEASE_REPOSITORY_ID="${FILE_REPO_ID}"
    RELEASE_REPOSITORY_URL="${FILE_REPO_URL}"

    mkdir -p "${FILE_REPO_URL#file:}" 2> /dev/null
    deployArtifact "${ARTIFACT_FILE}"
    retval=$(( retval + ${?} ))
fi

# install in local repository

if [[ -n ${LOCAL_INSTALL} ]]; then
    installArtifact "${ARTIFACT_FILE}"
    retval=$(( retval + ${?} ))
fi

# install dependencies in local and/or file repositories

if [[ -n ${INSTALL_DEPS} ]]; then
    if [[ -n ${FILE_REPO_INSTALL} ]]; then
        DEPENDENCY_REPO_URL="${FILE_REPO_URL}"
        installDependencies "${ARTIFACT_FILE}"
        retval=$(( retval + ${?} ))
    fi

    if [[ -n ${LOCAL_INSTALL} ]]; then
        DEPENDENCY_REPO_URL="file:${HOME}/.m2/repository"
        installDependencies "${ARTIFACT_FILE}"
        retval=$(( retval + ${?} ))
    fi
fi

cleanup

exit ${retval}