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="" |
| 39 | export READINESS_CONTAINER="" |
rameshiyer27 | a1954d4 | 2022-11-14 06:00:12 +0000 | [diff] [blame] | 40 | |
| 41 | function spin_microk8s_cluster () { |
| 42 | echo "Verify if Microk8s cluster is running.." |
| 43 | microk8s version |
| 44 | exitcode="${?}" |
| 45 | |
| 46 | if [ "$exitcode" -ne 0 ]; then |
| 47 | echo "Microk8s cluster not available, Spinning up the cluster.." |
| 48 | sudo snap install microk8s --classic --channel=1.25/stable |
| 49 | |
| 50 | if [ "${?}" -ne 0 ]; then |
| 51 | echo "Failed to install kubernetes cluster. Aborting.." |
| 52 | return 1 |
| 53 | fi |
| 54 | echo "Microk8s cluster installed successfully" |
| 55 | sudo usermod -a -G microk8s $USER |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame^] | 56 | echo "Enabling DNS and helm3 plugins" |
rameshiyer27 | a1954d4 | 2022-11-14 06:00:12 +0000 | [diff] [blame] | 57 | microk8s.enable dns helm3 |
| 58 | echo "Creating configuration file for Microk8s" |
| 59 | microk8s kubectl config view --raw > $HOME/.kube/config |
| 60 | chmod 600 $HOME/.kube/config |
| 61 | echo "K8s installation completed" |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame^] | 62 | echo "----------------------------------------" |
rameshiyer27 | a1954d4 | 2022-11-14 06:00:12 +0000 | [diff] [blame] | 63 | else |
| 64 | echo "K8s cluster is already running" |
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 | return 0 |
| 67 | fi |
| 68 | |
| 69 | } |
| 70 | |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame^] | 71 | |
rameshiyer27 | a1954d4 | 2022-11-14 06:00:12 +0000 | [diff] [blame] | 72 | function teardown_cluster () { |
| 73 | echo "Removing k8s cluster and k8s configuration file" |
| 74 | sudo snap remove microk8s;rm -rf $HOME/.kube/config |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame^] | 75 | sudo rm -rf /dockerdata-nfs/mariadb-galera/ |
rameshiyer27 | a1954d4 | 2022-11-14 06:00:12 +0000 | [diff] [blame] | 76 | echo "K8s Cluster removed" |
| 77 | } |
| 78 | |
| 79 | |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame^] | 80 | function build_robot_image () { |
| 81 | echo "Build docker image for robot framework" |
| 82 | cd ../helm; |
| 83 | clone_models |
| 84 | echo "Build robot framework docker image" |
| 85 | docker login -u docker -p docker nexus3.onap.org:10001 |
| 86 | docker build . --file Dockerfile --build-arg CSIT_SCRIPT="$CSIT_SCRIPT" --build-arg ROBOT_FILE="$ROBOT_FILE" --tag "${ROBOT_DOCKER_IMAGE}" --no-cache |
| 87 | echo "---------------------------------------------" |
| 88 | echo "Importing robot image in to microk8s registry" |
| 89 | docker save -o policy-csit-robot.tar ${ROBOT_DOCKER_IMAGE}:latest |
| 90 | microk8s ctr image import policy-csit-robot.tar |
| 91 | if [ "${?}" -eq 0 ]; then |
| 92 | rm -rf policy-csit-robot.tar |
| 93 | rm -rf tests/models/ |
| 94 | echo "---------------------------------------------" |
| 95 | echo "Installing Robot framework pod for running CSIT" |
| 96 | helm install csit-robot robot --set robot=$ROBOT_FILE --set readiness=$READINESS_CONTAINER; |
| 97 | echo "Please check the logs of policy-csit-robot pod for the test execution results" |
| 98 | fi |
| 99 | } |
| 100 | |
| 101 | function clone_models () { |
| 102 | GIT_TOP=$(git rev-parse --show-toplevel) |
| 103 | GERRIT_BRANCH=$(awk -F= '$1 == "defaultbranch" { print $2 }' \ |
| 104 | "${GIT_TOP}"/.gitreview) |
| 105 | echo GERRIT_BRANCH="${GERRIT_BRANCH}" |
| 106 | # download models examples |
| 107 | git clone -b "${GERRIT_BRANCH}" --single-branch https://github.com/onap/policy-models.git tests/models |
| 108 | } |
| 109 | |
| 110 | |
| 111 | function get_robot_file () { |
| 112 | case $PROJECT in |
| 113 | |
| 114 | clamp | policy-clamp) |
| 115 | export ROBOT_FILE=$POLICY_CLAMP_ROBOT |
| 116 | export READINESS_CONTAINER=$POLICY_CLAMP_CONTAINER |
| 117 | ;; |
| 118 | |
| 119 | api | policy-api) |
| 120 | export ROBOT_FILE=$POLICY_API_ROBOT |
| 121 | export READINESS_CONTAINER=$POLICY_API_CONTAINER |
| 122 | ;; |
| 123 | |
| 124 | pap | policy-pap) |
| 125 | export ROBOT_FILE=$POLICY_PAP_ROBOT |
| 126 | export READINESS_CONTAINER=$POLICY_PAP_CONTAINER |
| 127 | ;; |
| 128 | |
| 129 | apex-pdp | policy-apex-pdp) |
| 130 | export ROBOT_FILE=$POLICY_APEX_PDP_ROBOT |
| 131 | export READINESS_CONTAINER=$POLICY_APEX_CONTAINER |
| 132 | ;; |
| 133 | |
| 134 | xacml-pdp | policy-xacml-pdp) |
| 135 | export ROBOT_FILE=$POLICY_XACML_PDP_ROBOT |
| 136 | ;; |
| 137 | |
| 138 | drools-pdp | policy-drools-pdp) |
| 139 | export ROBOT_FILE=$POLICY_DROOLS_PDP_ROBOT |
| 140 | ;; |
| 141 | |
| 142 | *) |
| 143 | echo "unknown project supplied" |
| 144 | ;; |
| 145 | esac |
| 146 | |
| 147 | } |
| 148 | |
| 149 | |
rameshiyer27 | a1954d4 | 2022-11-14 06:00:12 +0000 | [diff] [blame] | 150 | if [ $1 == "install" ]; then |
| 151 | spin_microk8s_cluster |
| 152 | if [ "${?}" -eq 0 ]; then |
| 153 | echo "Installing policy helm charts in the default namespace" |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame^] | 154 | cd ../helm/;helm dependency build policy;microk8s helm install csit-policy policy; |
rameshiyer27 | a1954d4 | 2022-11-14 06:00:12 +0000 | [diff] [blame] | 155 | echo "Policy chart installation completed" |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame^] | 156 | echo "-------------------------------------------" |
| 157 | fi |
| 158 | |
| 159 | if [ "$2" ]; then |
| 160 | export PROJECT=$2 |
| 161 | get_robot_file |
| 162 | echo "CSIT will be invoked from $ROBOT_FILE" |
| 163 | echo "Readiness container: $READINESS_CONTAINER" |
| 164 | build_robot_image |
| 165 | else |
| 166 | echo "No project supplied for running CSIT" |
rameshiyer27 | a1954d4 | 2022-11-14 06:00:12 +0000 | [diff] [blame] | 167 | fi |
| 168 | |
| 169 | elif [ $1 == "uninstall" ]; then |
| 170 | teardown_cluster |
| 171 | else |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame^] | 172 | echo "Invalid arguments provided. Usage: $0 [option..] {install {project} | uninstall}" |
rameshiyer27 | a1954d4 | 2022-11-14 06:00:12 +0000 | [diff] [blame] | 173 | fi |
| 174 | |