Upgraded test env with Kubernetes support
[nonrtric.git] / test / common / control_panel_api_functions.sh
1 #!/bin/bash
2
3 #  ============LICENSE_START===============================================
4 #  Copyright (C) 2020 Nordix Foundation. All rights reserved.
5 #  ========================================================================
6 #  Licensed under the Apache License, Version 2.0 (the "License");
7 #  you may not use this file except in compliance with the License.
8 #  You may obtain a copy of the License at
9 #
10 #       http://www.apache.org/licenses/LICENSE-2.0
11 #
12 #  Unless required by applicable law or agreed to in writing, software
13 #  distributed under the License is distributed on an "AS IS" BASIS,
14 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 #  See the License for the specific language governing permissions and
16 #  limitations under the License.
17 #  ============LICENSE_END=================================================
18 #
19
20 # This is a script that contains container/service management function
21 # and test functions for Control Panel
22
23 ## Access to control panel
24 # Host name may be changed if app started by kube
25 # Direct access from script
26 CP_HTTPX="http"
27 CP_HOST_NAME=$LOCALHOST_NAME
28 CP_PATH=$CP_HTTPX"://"$CP_HOST_NAME":"$CONTROL_PANEL_EXTERNAL_PORT
29
30 ###########################
31 ### Control Panel functions
32 ###########################
33
34 # Set http as the protocol to use for all communication to the Control Panel
35 # args: -
36 # (Function for test scripts)
37 use_control_panel_http() {
38         echo -e $BOLD"Control Panel, CP, protocol setting"$EBOLD
39         echo -e " Using $BOLD http $EBOLD towards CP"
40         CP_HTTPX="http"
41         CP_PATH=$CP_HTTPX"://"$CP_HOST_NAME":"$CONTROL_PANEL_EXTERNAL_PORT
42         echo ""
43 }
44
45 # Set https as the protocol to use for all communication to the Control Panel
46 # args: -
47 # (Function for test scripts)
48 use_control_panel_https() {
49         echo -e $BOLD"Control Panel, CP, protocol setting"$EBOLD
50         echo -e " Using $BOLD https $EBOLD towards CP"
51         CP_HTTPX="https"
52         CP_PATH=$CP_HTTPX"://"$CP_HOST_NAME":"$CONTROL_PANEL_EXTERNAL_SECURE_PORT
53         echo ""
54 }
55
56 # Start the Control Panel container
57 # args: -
58 # (Function for test scripts)
59 start_control_panel() {
60
61         echo -e $BOLD"Starting $CONTROL_PANEL_DISPLAY_NAME"$EBOLD
62
63         if [ $RUNMODE == "KUBE" ]; then
64
65                 # Check if app shall be fully managed by the test script
66                 __check_included_image "CP"
67                 retcode_i=$?
68
69                 # Check if app shall only be used by the testscipt
70                 __check_prestarted_image "CP"
71                 retcode_p=$?
72
73                 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
74                         echo -e $RED"The $CONTROL_PANEL_APP_NAME app is not included as managed nor prestarted in this test script"$ERED
75                         echo -e $RED"The $CONTROL_PANEL_APP_NAME will not be started"$ERED
76                         exit
77                 fi
78                 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
79                         echo -e $RED"The $CONTROL_PANEL_APP_NAME app is included both as managed and prestarted in this test script"$ERED
80                         echo -e $RED"The $CONTROL_PANEL_APP_NAME will not be started"$ERED
81                         exit
82                 fi
83
84                 # Check if app shall be used - not managed - by the test script
85                 __check_prestarted_image "CP"
86                 if [ $? -eq 0 ]; then
87                         echo -e " Using existing $CONTROL_PANEL_APP_NAME deployment and service"
88                         echo " Setting CP replicas=1"
89                         __kube_scale deployment $CONTROL_PANEL_APP_NAME $KUBE_NONRTRIC_NAMESPACE 1
90                 fi
91
92                 if [ $retcode_i -eq 0 ]; then
93
94                         echo -e " Creating $CP_APP_NAME app and expose service"
95
96                         #Export all vars needed for service and deployment
97                         export CONTROL_PANEL_APP_NAME
98                         export KUBE_NONRTRIC_NAMESPACE
99                         export CONTROL_PANEL_IMAGE
100                         export CONTROL_PANEL_INTERNAL_PORT
101                         export CONTROL_PANEL_INTERNAL_SECURE_PORT
102                         export CONTROL_PANEL_EXTERNAL_PORT
103                         export CONTROL_PANEL_EXTERNAL_SECURE_PORT
104                         export CONTROL_PANEL_CONFIG_MOUNT_PATH
105                         export CONTROL_PANEL_CONFIG_FILE
106                         export CP_CONFIG_CONFIGMAP_NAME=$CONTROL_PANEL_APP_NAME"-config"
107
108                         export POLICY_AGENT_EXTERNAL_SECURE_PORT
109                         export ECS_EXTERNAL_SECURE_PORT
110                         export POLICY_AGENT_DOMAIN_NAME=$POLICY_AGENT_APP_NAME.$KUBE_NONRTRIC_NAMESPACE
111                         export ECS_DOMAIN_NAME=$ECS_APP_NAME.$KUBE_NONRTRIC_NAMESPACE
112
113                         #Check if nonrtric namespace exists, if not create it
114                         __kube_create_namespace $KUBE_NONRTRIC_NAMESPACE
115
116                         # Create config map for config
117                         datafile=$PWD/tmp/$CONTROL_PANEL_CONFIG_FILE
118                         #Add config to properties file
119                         envsubst < $1 > $datafile
120                         output_yaml=$PWD/tmp/cp_cfc.yaml
121                         __kube_create_configmap $CP_CONFIG_CONFIGMAP_NAME $KUBE_NONRTRIC_NAMESPACE autotest CP $datafile $output_yaml
122
123                         # Create service
124                         input_yaml=$SIM_GROUP"/"$CONTROL_PANEL_COMPOSE_DIR"/"svc.yaml
125                         output_yaml=$PWD/tmp/cp_svc.yaml
126                         __kube_create_instance service $CONTROL_PANEL_APP_NAME $input_yaml $output_yaml
127
128                         # Create app
129                         input_yaml=$SIM_GROUP"/"$CONTROL_PANEL_COMPOSE_DIR"/"app.yaml
130                         output_yaml=$PWD/tmp/cp_app.yaml
131                         __kube_create_instance app $CONTROL_PANEL_APP_NAME $input_yaml $output_yaml
132
133                 fi
134
135                 echo " Retrieving host and ports for service..."
136                 CP_HOST_NAME=$(__kube_get_service_host $CONTROL_PANEL_APP_NAME $KUBE_NONRTRIC_NAMESPACE)
137
138                 CONTROL_PANEL_EXTERNAL_PORT=$(__kube_get_service_port $CONTROL_PANEL_APP_NAME $KUBE_NONRTRIC_NAMESPACE "http")
139                 CONTROL_PANEL_EXTERNAL_SECURE_PORT=$(__kube_get_service_port $CONTROL_PANEL_APP_NAME $KUBE_NONRTRIC_NAMESPACE "https")
140
141                 echo " Host IP, http port, https port: $CP_HOST_NAME $CONTROL_PANEL_EXTERNAL_PORT $CONTROL_PANEL_EXTERNAL_SECURE_PORT"
142                 if [ $CP_HTTPX == "http" ]; then
143                         CP_PATH=$CP_HTTPX"://"$CP_HOST_NAME":"$CONTROL_PANEL_EXTERNAL_PORT
144                 else
145                         CP_PATH=$CP_HTTPX"://"$CP_HOST_NAME":"$CONTROL_PANEL_EXTERNAL_SECURE_PORT
146                 fi
147
148                 __check_service_start $CONTROL_PANEL_APP_NAME $CP_PATH$CONTROL_PANEL_ALIVE_URL
149         else
150                 # Check if docker app shall be fully managed by the test script
151                 __check_included_image 'CP'
152                 if [ $? -eq 1 ]; then
153                         echo -e $RED"The Control Panel app is not included in this test script"$ERED
154                         echo -e $RED"The Control Panel will not be started"$ERED
155                         exit
156                 fi
157
158                 # Export needed vars for docker compose
159         export CONTROL_PANEL_APP_NAME
160         export CONTROL_PANEL_INTERNAL_PORT
161         export CONTROL_PANEL_EXTERNAL_PORT
162         export CONTROL_PANEL_INTERNAL_SECURE_PORT
163         export CONTROL_PANEL_EXTERNAL_SECURE_PORT
164         export DOCKER_SIM_NWNAME
165
166                 __start_container $CONTROL_PANEL_COMPOSE_DIR NODOCKERARGS 1 $CONTROL_PANEL_APP_NAME
167
168                 __check_service_start $CONTROL_PANEL_APP_NAME $CP_PATH$CONTROL_PANEL_ALIVE_URL
169         fi
170         echo ""
171 }
172