BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 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 managemnt functions for Http Proxy |
| 21 | |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 22 | ################ Test engine functions ################ |
| 23 | |
| 24 | # Create the image var used during the test |
| 25 | # arg: <image-tag-suffix> (selects staging, snapshot, release etc) |
| 26 | # <image-tag-suffix> is present only for images with staging, snapshot,release tags |
| 27 | __HTTPPROXY_imagesetup() { |
BjornMagnussonXA | 674793d | 2021-05-06 19:49:17 +0200 | [diff] [blame] | 28 | __check_and_create_image_var HTTPPROXY "HTTP_PROXY_IMAGE" "HTTP_PROXY_IMAGE_BASE" "HTTP_PROXY_IMAGE_TAG" LOCAL "$HTTP_PROXY_DISPLAY_NAME" |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | # Pull image from remote repo or use locally built image |
| 32 | # arg: <pull-policy-override> <pull-policy-original> |
| 33 | # <pull-policy-override> Shall be used for images allowing overriding. For example use a local image when test is started to use released images |
| 34 | # <pull-policy-original> Shall be used for images that does not allow overriding |
| 35 | # Both var may contain: 'remote', 'remote-remove' or 'local' |
| 36 | __HTTPPROXY_imagepull() { |
BjornMagnussonXA | 483ee33 | 2021-04-08 01:35:24 +0200 | [diff] [blame] | 37 | __check_and_pull_image $2 "$HTTP_PROXY_DISPLAY_NAME" $HTTP_PROXY_APP_NAME HTTP_PROXY_IMAGE |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | # Build image (only for simulator or interfaces stubs owned by the test environment) |
| 41 | # arg: <image-tag-suffix> (selects staging, snapshot, release etc) |
| 42 | # <image-tag-suffix> is present only for images with staging, snapshot,release tags |
| 43 | __HTTPPROXY_imagebuild() { |
BjornMagnussonXA | 674793d | 2021-05-06 19:49:17 +0200 | [diff] [blame] | 44 | cd ../$HTTP_PROXY_BUILD_DIR # Note: Reusing same impl as for kube proxy |
| 45 | echo " Building HTTPPROXY - $HTTP_PROXY_DISPLAY_NAME - image: $HTTP_PROXY_IMAGE" |
| 46 | docker build --build-arg NEXUS_PROXY_REPO=$NEXUS_PROXY_REPO -t $HTTP_PROXY_IMAGE . &> .dockererr |
| 47 | if [ $? -eq 0 ]; then |
| 48 | echo -e $GREEN" Build Ok"$EGREEN |
| 49 | __retag_and_push_image HTTP_PROXY_IMAGE |
| 50 | if [ $? -ne 0 ]; then |
| 51 | exit 1 |
| 52 | fi |
| 53 | else |
| 54 | echo -e $RED" Build Failed"$ERED |
| 55 | ((RES_CONF_FAIL++)) |
| 56 | cat .dockererr |
| 57 | echo -e $RED"Exiting...."$ERED |
| 58 | exit 1 |
| 59 | fi |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | # Generate a string for each included image using the app display name and a docker images format string |
BjornMagnussonXA | 483ee33 | 2021-04-08 01:35:24 +0200 | [diff] [blame] | 63 | # If a custom image repo is used then also the source image from the local repo is listed |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 64 | # arg: <docker-images-format-string> <file-to-append> |
| 65 | __HTTPPROXY_image_data() { |
| 66 | echo -e "$HTTP_PROXY_DISPLAY_NAME\t$(docker images --format $1 $HTTP_PROXY_IMAGE)" >> $2 |
BjornMagnussonXA | 483ee33 | 2021-04-08 01:35:24 +0200 | [diff] [blame] | 67 | if [ ! -z "$HTTP_PROXY_IMAGE_SOURCE" ]; then |
| 68 | echo -e "-- source image --\t$(docker images --format $1 $HTTP_PROXY_IMAGE_SOURCE)" >> $2 |
| 69 | fi |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | # Scale kubernetes resources to zero |
| 73 | # All resources shall be ordered to be scaled to 0, if relevant. If not relevant to scale, then do no action. |
| 74 | # This function is called for apps fully managed by the test script |
| 75 | __HTTPPROXY_kube_scale_zero() { |
| 76 | __kube_scale_all_resources $KUBE_SIM_NAMESPACE autotest HTTPPROXY |
| 77 | } |
| 78 | |
| 79 | # Scale kubernetes resources to zero and wait until this has been accomplished, if relevant. If not relevant to scale, then do no action. |
| 80 | # This function is called for prestarted apps not managed by the test script. |
| 81 | __HTTPPROXY_kube_scale_zero_and_wait() { |
BjornMagnussonXA | 05bfe48 | 2021-12-02 08:47:41 +0100 | [diff] [blame] | 82 | echo -e $RED" HTTPPROXY app is not scaled in this state"$ERED |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | # Delete all kube resouces for the app |
| 86 | # This function is called for apps managed by the test script. |
| 87 | __HTTPPROXY_kube_delete_all() { |
| 88 | __kube_delete_all_resources $KUBE_SIM_NAMESPACE autotest HTTPPROXY |
| 89 | } |
| 90 | |
| 91 | # Store docker logs |
| 92 | # This function is called for apps managed by the test script. |
| 93 | # args: <log-dir> <file-prexix> |
| 94 | __HTTPPROXY_store_docker_logs() { |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 95 | if [ $RUNMODE == "KUBE" ]; then |
BjornMagnussonXA | cb6113e | 2022-02-17 15:01:28 +0100 | [diff] [blame^] | 96 | kubectl $KUBECONF logs -l "autotest=HTTPPROXY" -n $KUBE_SIM_NAMESPACE --tail=-1 > $1$2_httpproxy.log 2>&1 |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 97 | else |
| 98 | docker logs $HTTP_PROXY_APP_NAME > $1$2_httpproxy.log 2>&1 |
| 99 | fi |
| 100 | } |
| 101 | |
| 102 | # Initial setup of protocol, host and ports |
| 103 | # This function is called for apps managed by the test script. |
| 104 | # args: - |
| 105 | __HTTPPROXY_initial_setup() { |
BjornMagnussonXA | 05bfe48 | 2021-12-02 08:47:41 +0100 | [diff] [blame] | 106 | use_http_proxy_http |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 107 | } |
| 108 | |
BjornMagnussonXA | 6fc58fd | 2021-11-18 08:19:45 +0100 | [diff] [blame] | 109 | # Set app short-name, app name and namespace for logging runtime statistics of kubernets pods or docker containers |
| 110 | # For docker, the namespace shall be excluded |
| 111 | # This function is called for apps managed by the test script as well as for prestarted apps. |
| 112 | # args: - |
| 113 | __HTTPPROXY_statisics_setup() { |
| 114 | if [ $RUNMODE == "KUBE" ]; then |
| 115 | echo "HTTPPROXY $HTTP_PROXY_APP_NAME $KUBE_SIM_NAMESPACE" |
| 116 | else |
| 117 | echo "HTTPPROXY $HTTP_PROXY_APP_NAME" |
| 118 | fi |
| 119 | } |
| 120 | |
BjornMagnussonXA | e60d04e | 2021-12-27 13:38:01 +0100 | [diff] [blame] | 121 | # Check application requirements, e.g. helm, the the test needs. Exit 1 if req not satisfied |
| 122 | # args: - |
| 123 | __HTTPPROXY_test_requirements() { |
| 124 | : |
| 125 | } |
| 126 | |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 127 | ####################################################### |
| 128 | |
BjornMagnussonXA | 05bfe48 | 2021-12-02 08:47:41 +0100 | [diff] [blame] | 129 | # Set http as the protocol to use for all communication to the http proxy |
BjornMagnussonXA | 674793d | 2021-05-06 19:49:17 +0200 | [diff] [blame] | 130 | # args: - |
| 131 | # (Function for test scripts) |
| 132 | use_http_proxy_http() { |
BjornMagnussonXA | 05bfe48 | 2021-12-02 08:47:41 +0100 | [diff] [blame] | 133 | __http_proxy_set_protocoll "http" $HTTP_PROXY_INTERNAL_PORT $HTTP_PROXY_EXTERNAL_PORT |
| 134 | } |
| 135 | |
| 136 | # Set https as the protocol to use for all communication to the http proxy |
| 137 | # args: - |
| 138 | # (Function for test scripts) |
| 139 | use_http_proxy_https() { |
| 140 | __http_proxy_set_protocoll "https" $HTTP_PROXY_INTERNAL_SECURE_PORT $HTTP_PROXY_EXTERNAL_SECURE_PORT |
| 141 | } |
| 142 | |
| 143 | # Setup paths to svc/container for internal and external access |
| 144 | # args: <protocol> <internal-port> <external-port> |
| 145 | __http_proxy_set_protocoll() { |
BjornMagnussonXA | 674793d | 2021-05-06 19:49:17 +0200 | [diff] [blame] | 146 | echo -e $BOLD"$HTTP_PROXY_DISPLAY_NAME protocol setting"$EBOLD |
BjornMagnussonXA | 05bfe48 | 2021-12-02 08:47:41 +0100 | [diff] [blame] | 147 | echo -e " Using $BOLD $1 $EBOLD towards $HTTP_PROXY_DISPLAY_NAME" |
| 148 | |
| 149 | ## Access to http proxy |
| 150 | ## HTTP_PROXY_CONFIG_HOST_NAME and HTTP_PROXY_CONFIG_PORT used by apps as config for proxy host and port |
| 151 | |
| 152 | HTTP_PROXY_SERVICE_PATH=$1"://"$HTTP_PROXY_APP_NAME":"$2 # docker access, container->container and script->container via proxy |
| 153 | HTTP_PROXY_CONFIG_HOST_NAME=$HTTP_PROXY_APP_NAME |
| 154 | HTTP_PROXY_CONFIG_PORT=$2 |
| 155 | if [ $RUNMODE == "KUBE" ]; then |
| 156 | HTTP_PROXY_CONFIG_HOST_NAME=$HTTP_PROXY_APP_NAME"."$KUBE_SIM_NAMESPACE |
| 157 | HTTP_PROXY_CONFIG_PORT=$3 |
| 158 | HTTP_PROXY_SERVICE_PATH=$1"://"$HTTP_PROXY_APP_NAME.$KUBE_SIM_NAMESPACE":"$3 # kube access, pod->svc and script->svc via proxy |
| 159 | fi |
BjornMagnussonXA | 674793d | 2021-05-06 19:49:17 +0200 | [diff] [blame] | 160 | |
| 161 | echo "" |
| 162 | } |
| 163 | |
BjornMagnussonXA | 05bfe48 | 2021-12-02 08:47:41 +0100 | [diff] [blame] | 164 | # Export env vars for config files, docker compose and kube resources |
| 165 | # args: |
| 166 | __http_proxy_export_vars() { |
BjornMagnussonXA | 674793d | 2021-05-06 19:49:17 +0200 | [diff] [blame] | 167 | |
BjornMagnussonXA | 05bfe48 | 2021-12-02 08:47:41 +0100 | [diff] [blame] | 168 | export HTTP_PROXY_APP_NAME |
| 169 | export HTTP_PROXY_DISPLAY_NAME |
| 170 | |
| 171 | export HTTP_PROXY_WEB_EXTERNAL_PORT |
| 172 | export HTTP_PROXY_WEB_INTERNAL_PORT |
| 173 | export HTTP_PROXY_EXTERNAL_PORT |
| 174 | export HTTP_PROXY_INTERNAL_PORT |
| 175 | |
| 176 | export HTTP_PROXY_WEB_EXTERNAL_SECURE_PORT |
| 177 | export HTTP_PROXY_WEB_INTERNAL_SECURE_PORT |
| 178 | export HTTP_PROXY_EXTERNAL_SECURE_PORT |
| 179 | export HTTP_PROXY_INTERNAL_SECURE_PORT |
| 180 | |
| 181 | export KUBE_SIM_NAMESPACE |
| 182 | export DOCKER_SIM_NWNAME |
| 183 | export HTTP_PROXY_IMAGE |
BjornMagnussonXA | 674793d | 2021-05-06 19:49:17 +0200 | [diff] [blame] | 184 | } |
| 185 | |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 186 | # Start the Http Proxy in the simulator group |
| 187 | # args: - |
| 188 | # (Function for test scripts) |
| 189 | start_http_proxy() { |
| 190 | |
| 191 | echo -e $BOLD"Starting $HTTP_PROXY_DISPLAY_NAME"$EBOLD |
| 192 | |
| 193 | if [ $RUNMODE == "KUBE" ]; then |
| 194 | |
| 195 | # Check if app shall be fully managed by the test script |
| 196 | __check_included_image "HTTPPROXY" |
| 197 | retcode_i=$? |
| 198 | |
| 199 | # Check if app shall only be used by the testscipt |
| 200 | __check_prestarted_image "HTTPPROXY" |
| 201 | retcode_p=$? |
| 202 | |
| 203 | if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then |
| 204 | echo -e $RED"The $HTTP_PROXY_APP_NAME app is not included as managed nor prestarted in this test script"$ERED |
| 205 | echo -e $RED"The $HTTP_PROXY_APP_NAME will not be started"$ERED |
| 206 | exit |
| 207 | fi |
| 208 | if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then |
| 209 | echo -e $RED"The $HTTP_PROXY_APP_NAME app is included both as managed and prestarted in this test script"$ERED |
| 210 | echo -e $RED"The $HTTP_PROXY_APP_NAME will not be started"$ERED |
| 211 | exit |
| 212 | fi |
| 213 | |
| 214 | # Check if app shall be used - not managed - by the test script |
| 215 | if [ $retcode_p -eq 0 ]; then |
| 216 | echo -e " Using existing $HTTP_PROXY_APP_NAME deployment and service" |
| 217 | echo " Setting HTTPPROXY replicas=1" |
| 218 | __kube_scale deployment $HTTP_PROXY_APP_NAME $KUBE_SIM_NAMESPACE 1 |
| 219 | fi |
| 220 | |
| 221 | if [ $retcode_i -eq 0 ]; then |
| 222 | echo -e " Creating $HTTP_PROXY_APP_NAME deployment and service" |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 223 | |
| 224 | __kube_create_namespace $KUBE_SIM_NAMESPACE |
| 225 | |
BjornMagnussonXA | 05bfe48 | 2021-12-02 08:47:41 +0100 | [diff] [blame] | 226 | __http_proxy_export_vars |
| 227 | |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 228 | # Create service |
| 229 | input_yaml=$SIM_GROUP"/"$HTTP_PROXY_COMPOSE_DIR"/"svc.yaml |
| 230 | output_yaml=$PWD/tmp/proxy_svc.yaml |
| 231 | __kube_create_instance service $HTTP_PROXY_APP_NAME $input_yaml $output_yaml |
| 232 | |
| 233 | # Create app |
| 234 | input_yaml=$SIM_GROUP"/"$HTTP_PROXY_COMPOSE_DIR"/"app.yaml |
| 235 | output_yaml=$PWD/tmp/proxy_app.yaml |
| 236 | __kube_create_instance app $HTTP_PROXY_APP_NAME $input_yaml $output_yaml |
| 237 | |
| 238 | fi |
| 239 | |
BjornMagnussonXA | 05bfe48 | 2021-12-02 08:47:41 +0100 | [diff] [blame] | 240 | __check_service_start $HTTP_PROXY_APP_NAME $HTTP_PROXY_SERVICE_PATH$HTTP_PROXY_ALIVE_URL |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 241 | |
| 242 | else |
| 243 | # Check if docker app shall be fully managed by the test script |
| 244 | __check_included_image 'HTTPPROXY' |
| 245 | if [ $? -eq 1 ]; then |
| 246 | echo -e $RED"The Http Proxy app is not included in this test script"$ERED |
| 247 | echo -e $RED"The Http Proxy will not be started"$ERED |
| 248 | exit |
| 249 | fi |
| 250 | |
BjornMagnussonXA | 05bfe48 | 2021-12-02 08:47:41 +0100 | [diff] [blame] | 251 | __http_proxy_export_vars |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 252 | |
| 253 | __start_container $HTTP_PROXY_COMPOSE_DIR "" NODOCKERARGS 1 $HTTP_PROXY_APP_NAME |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 254 | |
BjornMagnussonXA | 05bfe48 | 2021-12-02 08:47:41 +0100 | [diff] [blame] | 255 | __check_service_start $HTTP_PROXY_APP_NAME $HTTP_PROXY_SERVICE_PATH$HTTP_PROXY_ALIVE_URL |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 256 | fi |
| 257 | echo "" |
| 258 | } |