rameshiyer27 | a1954d4 | 2022-11-14 06:00:12 +0000 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # ============LICENSE_START==================================================== |
rameshiyer27 | 93fd998 | 2023-02-23 10:42:55 +0000 | [diff] [blame^] | 4 | # Copyright (C) 2022-2023 Nordix Foundation. |
rameshiyer27 | a1954d4 | 2022-11-14 06:00:12 +0000 | [diff] [blame] | 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 | # |
| 18 | # SPDX-License-Identifier: Apache-2.0 |
| 19 | # ============LICENSE_END====================================================== |
| 20 | |
| 21 | # This script spins up kubernetes cluster in Microk8s for deploying policy helm charts. |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 22 | # Runs CSITs in kubernetes. |
| 23 | |
| 24 | CSIT_SCRIPT="run-test.sh" |
| 25 | ROBOT_DOCKER_IMAGE="policy-csit-robot" |
| 26 | POLICY_CLAMP_ROBOT="policy-clamp-test.robot" |
| 27 | POLICY_API_ROBOT="api-test.robot" |
| 28 | POLICY_PAP_ROBOT="pap-test.robot" |
| 29 | POLICY_APEX_PDP_ROBOT="apex-pdp-test.robot" |
| 30 | POLICY_XACML_PDP_ROBOT="xacml-pdp-test.robot" |
| 31 | POLICY_DROOLS_PDP_ROBOT="drools-pdp-test.robot" |
| 32 | POLICY_API_CONTAINER="policy-api" |
| 33 | POLICY_PAP_CONTAINER="policy-pap" |
| 34 | POLICY_CLAMP_CONTAINER="policy-clamp-runtime-acm" |
| 35 | POLICY_APEX_CONTAINER="policy-apex-pdp" |
rameshiyer27 | 93fd998 | 2023-02-23 10:42:55 +0000 | [diff] [blame^] | 36 | POLICY_DROOLS_CONTAINER="policy-drools-pdp" |
| 37 | POLICY_XACML_CONTAINER="policy-xacml-pdp" |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 38 | |
| 39 | export PROJECT="" |
| 40 | export ROBOT_FILE="" |
rameshiyer27 | 6ebe5b6 | 2023-02-14 17:52:49 +0000 | [diff] [blame] | 41 | export ROBOT_LOG_DIR=${PWD}/archives |
rameshiyer27 | 62ccbbb | 2023-02-01 15:06:45 +0000 | [diff] [blame] | 42 | export READINESS_CONTAINERS=() |
rameshiyer27 | a1954d4 | 2022-11-14 06:00:12 +0000 | [diff] [blame] | 43 | |
| 44 | function spin_microk8s_cluster () { |
| 45 | echo "Verify if Microk8s cluster is running.." |
| 46 | microk8s version |
| 47 | exitcode="${?}" |
| 48 | |
| 49 | if [ "$exitcode" -ne 0 ]; then |
| 50 | echo "Microk8s cluster not available, Spinning up the cluster.." |
| 51 | sudo snap install microk8s --classic --channel=1.25/stable |
| 52 | |
| 53 | if [ "${?}" -ne 0 ]; then |
| 54 | echo "Failed to install kubernetes cluster. Aborting.." |
| 55 | return 1 |
| 56 | fi |
| 57 | echo "Microk8s cluster installed successfully" |
| 58 | sudo usermod -a -G microk8s $USER |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 59 | echo "Enabling DNS and helm3 plugins" |
rameshiyer27 | 6ebe5b6 | 2023-02-14 17:52:49 +0000 | [diff] [blame] | 60 | microk8s.enable dns helm3 hostpath-storage |
rameshiyer27 | a1954d4 | 2022-11-14 06:00:12 +0000 | [diff] [blame] | 61 | echo "Creating configuration file for Microk8s" |
| 62 | microk8s kubectl config view --raw > $HOME/.kube/config |
| 63 | chmod 600 $HOME/.kube/config |
| 64 | echo "K8s installation completed" |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 65 | echo "----------------------------------------" |
rameshiyer27 | a1954d4 | 2022-11-14 06:00:12 +0000 | [diff] [blame] | 66 | else |
| 67 | echo "K8s cluster is already running" |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 68 | echo "----------------------------------------" |
rameshiyer27 | a1954d4 | 2022-11-14 06:00:12 +0000 | [diff] [blame] | 69 | return 0 |
| 70 | fi |
| 71 | |
| 72 | } |
| 73 | |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 74 | |
rameshiyer27 | a1954d4 | 2022-11-14 06:00:12 +0000 | [diff] [blame] | 75 | function teardown_cluster () { |
| 76 | echo "Removing k8s cluster and k8s configuration file" |
| 77 | sudo snap remove microk8s;rm -rf $HOME/.kube/config |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 78 | sudo rm -rf /dockerdata-nfs/mariadb-galera/ |
rameshiyer27 | a1954d4 | 2022-11-14 06:00:12 +0000 | [diff] [blame] | 79 | echo "K8s Cluster removed" |
| 80 | } |
| 81 | |
| 82 | |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 83 | function build_robot_image () { |
| 84 | echo "Build docker image for robot framework" |
| 85 | cd ../helm; |
| 86 | clone_models |
| 87 | echo "Build robot framework docker image" |
| 88 | docker login -u docker -p docker nexus3.onap.org:10001 |
| 89 | docker build . --file Dockerfile --build-arg CSIT_SCRIPT="$CSIT_SCRIPT" --build-arg ROBOT_FILE="$ROBOT_FILE" --tag "${ROBOT_DOCKER_IMAGE}" --no-cache |
| 90 | echo "---------------------------------------------" |
| 91 | echo "Importing robot image in to microk8s registry" |
| 92 | docker save -o policy-csit-robot.tar ${ROBOT_DOCKER_IMAGE}:latest |
| 93 | microk8s ctr image import policy-csit-robot.tar |
| 94 | if [ "${?}" -eq 0 ]; then |
| 95 | rm -rf policy-csit-robot.tar |
| 96 | rm -rf tests/models/ |
| 97 | echo "---------------------------------------------" |
| 98 | echo "Installing Robot framework pod for running CSIT" |
rameshiyer27 | 6ebe5b6 | 2023-02-14 17:52:49 +0000 | [diff] [blame] | 99 | microk8s helm install csit-robot robot --set robot=$ROBOT_FILE --set "readiness={${READINESS_CONTAINERS[*]}}" --set robotLogDir=$ROBOT_LOG_DIR; |
rameshiyer27 | be22625 | 2023-02-09 14:46:18 +0000 | [diff] [blame] | 100 | print_robot_log |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 101 | fi |
| 102 | } |
| 103 | |
rameshiyer27 | be22625 | 2023-02-09 14:46:18 +0000 | [diff] [blame] | 104 | function print_robot_log () { |
| 105 | robotpod=$(microk8s kubectl get po | grep policy-csit) |
| 106 | podName=$(echo $robotpod | awk '{print $1}') |
| 107 | echo "The robot tests will begin once the policy components {${READINESS_CONTAINERS[*]}} are up and running..." |
| 108 | microk8s kubectl wait --for=condition=ready --timeout=180s pod/$podName |
| 109 | microk8s kubectl logs -f $podName |
| 110 | echo "Please check the logs of policy-csit-robot pod for the test execution results" |
| 111 | } |
| 112 | |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 113 | function clone_models () { |
| 114 | GIT_TOP=$(git rev-parse --show-toplevel) |
| 115 | GERRIT_BRANCH=$(awk -F= '$1 == "defaultbranch" { print $2 }' \ |
| 116 | "${GIT_TOP}"/.gitreview) |
| 117 | echo GERRIT_BRANCH="${GERRIT_BRANCH}" |
| 118 | # download models examples |
| 119 | git clone -b "${GERRIT_BRANCH}" --single-branch https://github.com/onap/policy-models.git tests/models |
rameshiyer27 | 62ccbbb | 2023-02-01 15:06:45 +0000 | [diff] [blame] | 120 | |
| 121 | # create a couple of variations of the policy definitions |
| 122 | sed -e 's!Measurement_vGMUX!ADifferentValue!' \ |
| 123 | tests/models/models-examples/src/main/resources/policies/vCPE.policy.monitoring.input.tosca.json \ |
| 124 | >tests/models/models-examples/src/main/resources/policies/vCPE.policy.monitoring.input.tosca.v1_2.json |
| 125 | |
| 126 | sed -e 's!"version": "1.0.0"!"version": "2.0.0"!' \ |
| 127 | -e 's!"policy-version": 1!"policy-version": 2!' \ |
| 128 | tests/models/models-examples/src/main/resources/policies/vCPE.policy.monitoring.input.tosca.json \ |
| 129 | >tests/models/models-examples/src/main/resources/policies/vCPE.policy.monitoring.input.tosca.v2.json |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | |
| 133 | function get_robot_file () { |
| 134 | case $PROJECT in |
| 135 | |
| 136 | clamp | policy-clamp) |
| 137 | export ROBOT_FILE=$POLICY_CLAMP_ROBOT |
rameshiyer27 | 62ccbbb | 2023-02-01 15:06:45 +0000 | [diff] [blame] | 138 | export READINESS_CONTAINERS=($POLICY_CLAMP_CONTAINER) |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 139 | ;; |
| 140 | |
| 141 | api | policy-api) |
| 142 | export ROBOT_FILE=$POLICY_API_ROBOT |
rameshiyer27 | 62ccbbb | 2023-02-01 15:06:45 +0000 | [diff] [blame] | 143 | export READINESS_CONTAINERS=($POLICY_API_CONTAINER) |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 144 | ;; |
| 145 | |
| 146 | pap | policy-pap) |
| 147 | export ROBOT_FILE=$POLICY_PAP_ROBOT |
rameshiyer27 | 62ccbbb | 2023-02-01 15:06:45 +0000 | [diff] [blame] | 148 | export READINESS_CONTAINERS=($POLICY_PAP_CONTAINER,$POLICY_API_CONTAINER) |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 149 | ;; |
| 150 | |
| 151 | apex-pdp | policy-apex-pdp) |
| 152 | export ROBOT_FILE=$POLICY_APEX_PDP_ROBOT |
rameshiyer27 | 62ccbbb | 2023-02-01 15:06:45 +0000 | [diff] [blame] | 153 | export READINESS_CONTAINERS=($POLICY_APEX_CONTAINER,$POLICY_API_CONTAINER,$POLICY_PAP_CONTAINER) |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 154 | ;; |
| 155 | |
| 156 | xacml-pdp | policy-xacml-pdp) |
rameshiyer27 | 62ccbbb | 2023-02-01 15:06:45 +0000 | [diff] [blame] | 157 | export ROBOT_FILE=($POLICY_XACML_PDP_ROBOT) |
rameshiyer27 | 93fd998 | 2023-02-23 10:42:55 +0000 | [diff] [blame^] | 158 | export READINESS_CONTAINERS=($POLICY_XACML_CONTAINER) |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 159 | ;; |
| 160 | |
| 161 | drools-pdp | policy-drools-pdp) |
rameshiyer27 | 62ccbbb | 2023-02-01 15:06:45 +0000 | [diff] [blame] | 162 | export ROBOT_FILE=($POLICY_DROOLS_PDP_ROBOT) |
rameshiyer27 | 93fd998 | 2023-02-23 10:42:55 +0000 | [diff] [blame^] | 163 | export READINESS_CONTAINERS=($POLICY_DROOLS_CONTAINER) |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 164 | ;; |
| 165 | |
| 166 | *) |
| 167 | echo "unknown project supplied" |
| 168 | ;; |
| 169 | esac |
| 170 | |
| 171 | } |
| 172 | |
| 173 | |
rameshiyer27 | a1954d4 | 2022-11-14 06:00:12 +0000 | [diff] [blame] | 174 | if [ $1 == "install" ]; then |
| 175 | spin_microk8s_cluster |
| 176 | if [ "${?}" -eq 0 ]; then |
| 177 | echo "Installing policy helm charts in the default namespace" |
rameshiyer27 | 62ccbbb | 2023-02-01 15:06:45 +0000 | [diff] [blame] | 178 | cd ../helm/;microk8s helm dependency build policy;microk8s helm install csit-policy policy; |
rameshiyer27 | a1954d4 | 2022-11-14 06:00:12 +0000 | [diff] [blame] | 179 | echo "Policy chart installation completed" |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 180 | echo "-------------------------------------------" |
| 181 | fi |
| 182 | |
| 183 | if [ "$2" ]; then |
| 184 | export PROJECT=$2 |
| 185 | get_robot_file |
| 186 | echo "CSIT will be invoked from $ROBOT_FILE" |
rameshiyer27 | 62ccbbb | 2023-02-01 15:06:45 +0000 | [diff] [blame] | 187 | echo "Readiness containers: ${READINESS_CONTAINERS[*]}" |
| 188 | echo "-------------------------------------------" |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 189 | build_robot_image |
| 190 | else |
| 191 | echo "No project supplied for running CSIT" |
rameshiyer27 | a1954d4 | 2022-11-14 06:00:12 +0000 | [diff] [blame] | 192 | fi |
| 193 | |
| 194 | elif [ $1 == "uninstall" ]; then |
| 195 | teardown_cluster |
| 196 | else |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 197 | echo "Invalid arguments provided. Usage: $0 [option..] {install {project} | uninstall}" |
rameshiyer27 | a1954d4 | 2022-11-14 06:00:12 +0000 | [diff] [blame] | 198 | fi |
| 199 | |