blob: 755a89edf11c690687b5fc252830cbbe76bd5f1c [file] [log] [blame]
rameshiyer27a1954d42022-11-14 06:00:12 +00001#!/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.
rameshiyer2707004862023-01-23 11:50:01 +000022# Runs CSITs in kubernetes.
23
24CSIT_SCRIPT="run-test.sh"
25ROBOT_DOCKER_IMAGE="policy-csit-robot"
26POLICY_CLAMP_ROBOT="policy-clamp-test.robot"
27POLICY_API_ROBOT="api-test.robot"
28POLICY_PAP_ROBOT="pap-test.robot"
29POLICY_APEX_PDP_ROBOT="apex-pdp-test.robot"
30POLICY_XACML_PDP_ROBOT="xacml-pdp-test.robot"
31POLICY_DROOLS_PDP_ROBOT="drools-pdp-test.robot"
32POLICY_API_CONTAINER="policy-api"
33POLICY_PAP_CONTAINER="policy-pap"
34POLICY_CLAMP_CONTAINER="policy-clamp-runtime-acm"
35POLICY_APEX_CONTAINER="policy-apex-pdp"
36
37export PROJECT=""
38export ROBOT_FILE=""
39export READINESS_CONTAINER=""
rameshiyer27a1954d42022-11-14 06:00:12 +000040
41function 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
rameshiyer2707004862023-01-23 11:50:01 +000056 echo "Enabling DNS and helm3 plugins"
rameshiyer27a1954d42022-11-14 06:00:12 +000057 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"
rameshiyer2707004862023-01-23 11:50:01 +000062 echo "----------------------------------------"
rameshiyer27a1954d42022-11-14 06:00:12 +000063 else
64 echo "K8s cluster is already running"
rameshiyer2707004862023-01-23 11:50:01 +000065 echo "----------------------------------------"
rameshiyer27a1954d42022-11-14 06:00:12 +000066 return 0
67 fi
68
69}
70
rameshiyer2707004862023-01-23 11:50:01 +000071
rameshiyer27a1954d42022-11-14 06:00:12 +000072function teardown_cluster () {
73 echo "Removing k8s cluster and k8s configuration file"
74 sudo snap remove microk8s;rm -rf $HOME/.kube/config
rameshiyer2707004862023-01-23 11:50:01 +000075 sudo rm -rf /dockerdata-nfs/mariadb-galera/
rameshiyer27a1954d42022-11-14 06:00:12 +000076 echo "K8s Cluster removed"
77}
78
79
rameshiyer2707004862023-01-23 11:50:01 +000080function 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
101function 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
111function 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 ;;
145esac
146
147}
148
149
rameshiyer27a1954d42022-11-14 06:00:12 +0000150if [ $1 == "install" ]; then
151 spin_microk8s_cluster
152 if [ "${?}" -eq 0 ]; then
153 echo "Installing policy helm charts in the default namespace"
rameshiyer2707004862023-01-23 11:50:01 +0000154 cd ../helm/;helm dependency build policy;microk8s helm install csit-policy policy;
rameshiyer27a1954d42022-11-14 06:00:12 +0000155 echo "Policy chart installation completed"
rameshiyer2707004862023-01-23 11:50:01 +0000156 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"
rameshiyer27a1954d42022-11-14 06:00:12 +0000167 fi
168
169elif [ $1 == "uninstall" ]; then
170 teardown_cluster
171else
rameshiyer2707004862023-01-23 11:50:01 +0000172 echo "Invalid arguments provided. Usage: $0 [option..] {install {project} | uninstall}"
rameshiyer27a1954d42022-11-14 06:00:12 +0000173fi
174