9fa52123e98185222bb5740dd55b683616f85b10
[oom.git] / kubernetes / policy / charts / drools / resources / config / opt / policy / config / drools / apps-install.sh
1 #!/bin/bash
2 #
3 # ============LICENSE_START=======================================================
4 # ONAP
5 # ================================================================================
6 # Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
7 # Modifications Copyright © 2018 Amdocs, Bell Canada
8 # ================================================================================
9 # Licensed under the Apache License, Version 2.0 (the "License");
10 # you may not use this file except in compliance with the License.
11 # You may obtain a copy of the License at
12 #
13 #      http://www.apache.org/licenses/LICENSE-2.0
14 #
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS,
17 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 # See the License for the specific language governing permissions and
19 # limitations under the License.
20 # ============LICENSE_END=========================================================
21 #
22
23 # #################################
24 # Usage
25 # #################################
26
27 function usage {
28     echo
29     echo "Usage: $(basename $0) <application-name> <zipped-application-version> <download-directory>"
30     echo "Example: $(basename $0) controlloop 1.2.0 /opt/policy/config/drools"
31     echo
32 }
33
34 # #################################
35 # snapshot url computation
36 # #################################
37
38 function url_snapshot {
39     if [[ $DEBUG == y ]]; then
40         echo "-- ${FUNCNAME[0]} --"
41         set -x
42     fi
43
44     APP_URL="${APP_URL}/snapshots/org/onap/policy/drools-applications/${APP_NAME}/packages/apps-${APP_NAME}/${APP_VERSION}"
45
46     local APP_METADATA_URL="${APP_URL}/maven-metadata.xml"
47     local APP_SNAPSHOT_VERSION=$(curl --silent "${APP_METADATA_URL}" | grep -Po "(?<=<value>).*(?=</value>)" | sort -V | tail -1)
48
49     if [[ -z ${APP_SNAPSHOT_VERSION} ]]; then
50         echo "ERROR: cannot compute SNAPSHOT version"
51         usage
52         exit 1
53     fi
54
55     APP_URL="${APP_URL}/apps-${APP_NAME}-${APP_SNAPSHOT_VERSION}.zip"
56 }
57
58 # #################################
59 # release url computation
60 # #################################
61
62 function url_release {
63     if [[ $DEBUG == y ]]; then
64         echo "-- ${FUNCNAME[0]} --"
65         set -x
66     fi
67
68     APP_URL="${APP_URL}/releases/org/onap/policy/drools-applications/${APP_NAME}/packages/apps-${APP_NAME}/${APP_VERSION}/apps-${APP_NAME}-${APP_VERSION}.zip"
69 }
70
71 # #################################
72 # Main
73 # #################################
74
75 if [[ $DEBUG == y ]]; then
76     set -x
77 fi
78
79 APP_NAME=$1
80 if [[ -z ${APP_NAME} ]]; then
81     echo "ERROR: no APPLICATION NAME provided (ie. controlloop)"
82     usage
83     exit 1
84 fi
85
86 APP_VERSION=$2
87 if [[ -z ${APP_VERSION} ]]; then
88     echo "ERROR: no APPLICATION VERSION provided"
89     usage
90     exit 1
91 fi
92
93 DOWNLOAD_DIR=$3
94 if [[ -z ${DOWNLOAD_DIR} ]]; then
95     echo "ERROR: no DOWNLOAD DIRECTORY provided"
96     usage
97     exit 1
98 fi
99
100 if [[ ! -d ${DOWNLOAD_DIR} ]]; then
101     echo "ERROR: ${DOWNLOAD_DIR} is not a directory"
102     usage
103     exit 1
104 fi
105
106 APP_GROUP_ID="org.onap.policy.drools-applications.${APP_NAME}.packages"
107 APP_ARTIFACT_ID="apps-${APP_NAME}"
108 APP_BASE_URL="https://nexus.onap.org/content/repositories"
109
110 APP_URL="${APP_BASE_URL}"
111
112 if [[ ${APP_VERSION} =~ \-SNAPSHOT$ ]]; then
113     url_snapshot
114 else
115     url_release
116 fi
117
118 wget "${APP_URL}" -O "${DOWNLOAD_DIR}"/apps-"${APP_NAME}".zip
119 if [[ $? != 0 ]]; then
120     echo "ERROR: cannot download ${DOWNLOAD_DIR}/apps-${APP_NAME}.zip"
121     exit 1
122 fi
123
124 echo "APP ${APP_NAME} stored at ${DOWNLOAD_DIR}/apps-${APP_NAME}.zip"
125 ls -l "${DOWNLOAD_DIR}"/apps-"${APP_NAME}".zip