Move plugin upload to CM container
[dcaegen2/deployments.git] / cm-container / scripts / load-plugins.sh
1 #!/bin/bash
2 # ============LICENSE_START=======================================================
3 # Copyright (c) 2019-2020 AT&T Intellectual Property. All rights reserved.
4 # ================================================================================
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #      http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 # ============LICENSE_END=========================================================
17 # Runs at deployment time to load the plugins/type files
18 # that were stored into the container file system at build time
19
20 PLUGINS_LOADED=/opt/manager/plugins-loaded
21 PLUGIN_DIR=/opt/plugins
22
23 # Set defaults for CM address, protocol, and port
24 CMADDR=${CMADDR:-dcae-cloudify-manager}
25 CMPROTO=${CMPROTO:-https}
26 CMPORT=${CMPORT:-443}
27
28 # Password is currently fixed at the default
29 # Eventually the password will be passed in as an environment variable
30 CMPASS=${CMPASS:-admin}
31
32 # Set up additional parameters for using HTTPS
33 CACERT="/opt/onap/certs/cacert.pem"
34 CURLTLS=""
35 if [ $CMPROTO = "https" ]
36 then
37     CURLTLS="--cacert $CACERT"
38 fi
39
40 ### FUNCTION DEFINITIONS ###
41
42 # cm_hasany: Query Cloudify Manager and return 0 (true) if there are any entities matching the query
43 # Used to see if something is already present on CM
44 # $1 -- query fragment, for instance "plugins?archive_name=xyz.wgn" to get
45 #  the number of plugins that came from the archive file "xyz.wgn"
46 function cm_hasany {
47     # We use _include=id to limit the amount of data the CM sends back
48     # We rely on the "metadata.pagination.total" field in the response
49     # for the total number of matching entities
50     COUNT=$(curl -Ss -H "Tenant: default_tenant" --user admin:${CMPASS} ${CURLTLS} "${CMPROTO}://${CMADDR}:${CMPORT}/api/v3.1/$1&_include=id" \
51              | /bin/jq .metadata.pagination.total)
52     if (( $COUNT > 0 ))
53     then
54         return 0
55     else
56         return 1
57     fi
58 }
59
60 # Install plugin if it's not already installed
61 # $1 -- path to wagon file for plugin
62 # $2 -- path to type file for plugin
63 function install_plugin {
64     ARCHIVE=$(basename $1)
65     # See if it's already installed
66     if cm_hasany "plugins?archive_name=$ARCHIVE"
67     then
68         echo plugin $1 already installed on ${CMADDR}
69     else
70         cfy plugin upload  -y $2 $1
71     fi
72 }
73
74 ### END FUNCTION DEFINTIONS ###
75
76 set -ex
77
78
79 # Wait for Cloudify Manager to come up
80 while ! /scripts/cloudify-ready.sh
81 do
82     echo "Waiting for CM to come up"
83     sleep 15
84 done
85
86 if [[ ! -f ${PLUGINS_LOADED} ]]
87 then
88
89   # Each subdirectory of ${PLUGIN_DIR} contains a wagon (.wgn) and type file (.yaml)
90   for p in ${PLUGIN_DIR}/*
91   do
92     # Expecting exactly 1 .wgn and 1 .yaml
93     # But just in case, taking only the first of each
94     # (If either is missing, will fail on install_plugin)
95     wagons=($p/*.wgn)
96     types=($p/*.yaml)
97     install_plugin ${wagons[0]} ${types[0]}
98   done
99
100   touch ${PLUGINS_LOADED}
101 else
102   echo "Plugins already loaded"
103 fi