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