summaryrefslogtreecommitdiffstats
path: root/cm-container/scripts/load-plugins.sh
blob: f4d1f66d4bb36490cc83960e3072d47a38a6b15b (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
#!/bin/bash
# ============LICENSE_START=======================================================
# Copyright (c) 2019-2020 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.
# ============LICENSE_END=========================================================
# Runs at deployment time to load the plugins/type files
# that were stored into the container file system at build time

PLUGINS_LOADED=/opt/manager/plugins-loaded
PLUGIN_DIR=/opt/plugins

# Set defaults for CM address, protocol, and port
CMADDR=${CMADDR:-dcae-cloudify-manager}
CMPROTO=${CMPROTO:-https}
CMPORT=${CMPORT:-443}

# Password is currently fixed at the default
# Eventually the password will be passed in as an environment variable
CMPASS=${CMPASS:-admin}

# Set up additional parameters for using HTTPS
CACERT="/opt/onap/certs/cacert.pem"
CURLTLS=""
if [ $CMPROTO = "https" ]
then
    CURLTLS="--cacert $CACERT"
fi

### FUNCTION DEFINITIONS ###

# cm_hasany: Query Cloudify Manager and return 0 (true) if there are any entities matching the query
# Used to see if something is already present on CM
# $1 -- query fragment, for instance "plugins?archive_name=xyz.wgn" to get
#  the number of plugins that came from the archive file "xyz.wgn"
function cm_hasany {
    # We use _include=id to limit the amount of data the CM sends back
    # We rely on the "metadata.pagination.total" field in the response
    # for the total number of matching entities
    COUNT=$(curl -Ss -H "Tenant: default_tenant" --user admin:${CMPASS} ${CURLTLS} "${CMPROTO}://${CMADDR}:${CMPORT}/api/v3.1/$1&_include=id" \
             | /bin/jq .metadata.pagination.total)
    if (( $COUNT > 0 ))
    then
        return 0
    else
        return 1
    fi
}

# Install plugin if it's not already installed
# $1 -- path to wagon file for plugin
# $2 -- path to type file for plugin
function install_plugin {
    ARCHIVE=$(basename $1)
    # See if it's already installed
    if cm_hasany "plugins?archive_name=$ARCHIVE"
    then
        echo plugin $1 already installed on ${CMADDR}
    else
        cfy plugin upload  -y $2 $1
    fi
}

### END FUNCTION DEFINTIONS ###

set -ex


# Wait for Cloudify Manager to come up
while ! /scripts/cloudify-ready.sh
do
    echo "Waiting for CM to come up"
    sleep 15
done

if [[ ! -f ${PLUGINS_LOADED} ]]
then

  # Each subdirectory of ${PLUGIN_DIR} contains a wagon (.wgn) and type file (.yaml)
  for p in ${PLUGIN_DIR}/*
  do
    # Expecting exactly 1 .wgn and 1 .yaml
    # But just in case, taking only the first of each
    # (If either is missing, will fail on install_plugin)
    wagons=($p/*.wgn)
    types=($p/*.yaml)
    install_plugin ${wagons[0]} ${types[0]}
  done

  touch ${PLUGINS_LOADED}
else
  echo "Plugins already loaded"
fi