From 10622d4d5fb0fd1b89bdfd7a12c58d9f8bce200b Mon Sep 17 00:00:00 2001
From: Neil-Jocelyn Schark <neil-jocelyn.schark@stud.h-da.de>
Date: Thu, 24 Mar 2022 13:36:13 +0000
Subject: [PATCH] switch to new gitlab-runner server

<!--- Provide a general summary of your changes in the Title above -->

## Description
<!--- Describe your changes in detail -->

## Related Issue
<!--- This project only accepts merge requests related to open issues -->
<!--- If suggesting a new feature or change, please discuss it in an issue first -->
<!--- If fixing a bug, there should be an issue describing it with steps to reproduce -->
<!--- Please link to the issue here: -->

## Motivation and Context
<!--- Why is this change required? What problem does it solve? -->

## How Has This Been Tested?
<!--- Please describe in detail how you tested your changes. -->
<!--- Include details of your testing environment, and the tests you ran to -->
<!--- see how your change affects other areas of the code, etc. -->

## Screenshots (if appropriate):

## Types of changes
<!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: -->
- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)

## Checklist:
<!--- Go over all the following points, and put an `x` in all the boxes that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! -->
- [ ] My code follows the code style of this project.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [ ] I have read the **CONTRIBUTING** document.
- [ ] I have added tests to cover my changes.
- [ ] All new and existing tests passed.
---
 .gitlab-ci.yml                                |   3 +-
 .gitlab/ci/.containerlab-ci.yml               | 129 ------------------
 .gitlab/ci/.integration-test-containerlab.yml |  84 ++++++++++++
 .gitlab/ci/.integration-test.yml              |  14 +-
 .gitlab/ci/scripts/docker_host_port           |   0
 .gitlab/ci/scripts/generate_octet.py          |  19 +++
 .gitlab/ci/scripts/generate_octet.sh          |  12 --
 controller/api/initialise_test.go             |   2 +-
 .../integration/nucleusIntegration_test.go    |   2 +-
 9 files changed, 115 insertions(+), 150 deletions(-)
 delete mode 100644 .gitlab/ci/.containerlab-ci.yml
 create mode 100644 .gitlab/ci/.integration-test-containerlab.yml
 mode change 100644 => 100755 .gitlab/ci/scripts/docker_host_port
 create mode 100755 .gitlab/ci/scripts/generate_octet.py
 delete mode 100755 .gitlab/ci/scripts/generate_octet.sh

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 7f9bc1738..fed5f03c6 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -8,7 +8,6 @@ stages:
     - build
     - test
     - analyze
-    - apply
     - integration-test
     - build-release
     - .post
@@ -18,7 +17,7 @@ include:
     - local: "/.gitlab/ci/.code-quality-ci.yml"
     - local: "/.gitlab/ci/.security-and-compliance-ci.yml"
     - local: "/.gitlab/ci/.test.yml"
-    - local: "/.gitlab/ci/.containerlab-ci.yml"
+    - local: "/.gitlab/ci/.integration-test-containerlab.yml"
     - local: "/.gitlab/ci/.integration-test.yml"
     - local: "/.gitlab/ci/.uml-autogen-ci.yml"
     - local: "/.gitlab/ci/.build-release.yml"
diff --git a/.gitlab/ci/.containerlab-ci.yml b/.gitlab/ci/.containerlab-ci.yml
deleted file mode 100644
index 3b0c201e4..000000000
--- a/.gitlab/ci/.containerlab-ci.yml
+++ /dev/null
@@ -1,129 +0,0 @@
-variables:
-    CLAB_DIR: "/mnt"
-    CLAB_NAME: "clab${CI_PIPELINE_IID}"
-
-# Templates for Job Types
-.containerlab_deploy: &containerlab_deploy
-    stage: apply
-    rules:
-        - if: '$CI_PIPELINE_SOURCE != "merge_request_event"'
-          when: never
-        - when: on_success
-    tags:
-        - shell
-    before_script:
-        - cd ${CLAB_DIR}
-        - export PATH="${PATH}:${CI_PROJECT_DIR}/.gitlab/ci/scripts"
-        - echo "$CI_REGISTRY_PASSWORD" | docker login -u $CI_REGISTRY_USER --password-stdin $CI_REGISTRY
-        - echo ${GOSDN_IMAGE}
-        - docker pull ${GOSDN_IMAGE}
-        - docker pull ${CEOS_IMAGE}
-
-.containerlab_template: &containerlab_template
-    image: ${CI_DEPENDENCY_PROXY_GROUP_IMAGE_PREFIX}/alpine:latest
-    stage: build
-    rules:
-        - if: '$CI_PIPELINE_SOURCE != "merge_request_event"'
-          when: never
-        - when: on_success
-    before_script:
-        - export PATH="${PATH}:${CI_PROJECT_DIR}/.gitlab/ci/scripts"
-        - firstOctet=$(generate_octet.sh $CI_COMMIT_SHA)
-        - secondOctet=$(generate_octet.sh $CI_PIPELINE_ID)
-        - export CLAB_MGMT_SUBNET="172.$firstOctet.$secondOctet.0/24"
-    script:
-        - |
-            sed -e "s|@@CEOS_CONTAINER_IMAGE@@|${CEOS_IMAGE}|g" \
-                -e "s|@@GOSDN_CONTAINER_IMAGE@@|${GOSDN_IMAGE}|g" \
-                -e "s|@@CLAB_NAME@@|${CLAB_NAME}|g" \
-                -e "s|@@CLAB_MGMT_SUBNET@@|${CLAB_MGMT_SUBNET}|g" \
-                ${CLAB_TEMPLATE} > ${CLAB_NAME}.clab.yml
-        - cat ${CLAB_NAME}.clab.yml
-    artifacts:
-        name: ${CLAB_NAME}
-        paths:
-            - ${CLAB_NAME}.clab.yml
-
-# JOBS
-containerlab:template:integration:
-    extends: .containerlab_template
-    variables:
-        CLAB_TEMPLATE: "${CI_PROJECT_DIR}/controller/test/containerlab/int01.clab.tmpl.yml"
-
-containerlab:deploy:integration:
-    extends: .containerlab_deploy
-    needs: ["containerlab:template:integration", "build-image"]
-    script:
-        - sudo containerlab deploy --topo ${CI_PROJECT_DIR}/${CLAB_NAME}.clab.yml --reconfigure
-        - |
-            echo -e "\
-            GOSDN_HTTP_PORT=$(docker_host_port 8080 clab-${CLAB_NAME}-gosdn)\n\
-            GOSDN_GRPC_PORT=$(docker_host_port 55055 clab-${CLAB_NAME}-gosdn)\n\
-            CEOS1_PORT=$(docker_host_port 6030 clab-${CLAB_NAME}-ceos1)" \
-            > ${CI_PROJECT_DIR}/build.env
-        - cat ${CI_PROJECT_DIR}/build.env
-    artifacts:
-        reports:
-            dotenv: ${CI_PROJECT_DIR}/build.env
-
-containerlab:destroy:
-    rules:
-        - if: '$CI_PIPELINE_SOURCE != "merge_request_event"'
-          when: never
-        - when: always
-    stage: .post
-    tags:
-        - shell
-    needs:
-        [
-            "containerlab:deploy:integration",
-            "integration-test:nucleus",
-            "integration-test:api",
-            "containerlab:template:integration",
-        ]
-    before_script:
-        - cd ${CLAB_DIR}
-    script:
-        - sudo containerlab destroy --topo ${CI_PROJECT_DIR}/${CLAB_NAME}.clab.yml
-        - docker volume rm -f ${CLAB_NAME}-volume
-        - docker image rm -f ${GOSDN_IMAGE}
-    allow_failure: true
-
-#containerlab:template:develop:
-#  extends: .containerlab_template
-#  variables:
-#    CLAB_NAME: "dev"
-#    CLAB_TEMPLATE: "${CI_PROJECT_DIR}/test/containerlab/dev.clab.tmpl.yml"
-#    GOSDN_CONTAINER_IMAGE: "$CI_REGISTRY_IMAGE:develop"
-#  rules:
-#    - if: $CI_COMMIT_BRANCH == 'develop' && $CI_NIGHTLY == null
-#
-#
-#containerlab:deploy:develop:
-#  extends:
-#    - .containerlab_deploy
-#  needs: ["containerlab:template:develop", "build-image"]
-#  variables:
-#    CLAB_NAME: "dev"
-#    GOSDN_CONTAINER_IMAGE: "$CI_REGISTRY_IMAGE:develop"
-#  script:
-#    - sudo containerlab deploy --topo ${CI_PROJECT_DIR}/${CLAB_NAME}.clab.yml --reconfigure
-#  rules:
-#    - if: $CI_COMMIT_BRANCH == 'develop' && $CI_NIGHTLY == null
-
-containerlab:clean:
-    stage: .post
-    tags:
-        - shell
-    before_script:
-        - cd ${CLAB_DIR}
-    script:
-        - docker kill $(docker ps -q) || true
-        - docker rm $(docker ps -a -q) || true
-        - docker rmi $(docker images | grep 'registry.code.fbi.h-da.de/danet/gosdn ' | awk '{print $3}') || true
-        - sudo rm -rf ${CLAB_DIR}/clab-* || true
-        - sudo sed -i 's|.*clab.*||g' /etc/hosts
-    allow_failure: true
-    rules:
-        - if: $CI_SCHEDULE_CLEAN
-          when: always
diff --git a/.gitlab/ci/.integration-test-containerlab.yml b/.gitlab/ci/.integration-test-containerlab.yml
new file mode 100644
index 000000000..000af8c0f
--- /dev/null
+++ b/.gitlab/ci/.integration-test-containerlab.yml
@@ -0,0 +1,84 @@
+variables:
+    CLAB_TEMPLATE: "${CI_PROJECT_DIR}/controller/test/containerlab/int01.clab.tmpl.yml"
+    CLAB_DIR: "/home/gitlab-runner/clab"
+    CLAB_NAME: "clab${CI_PIPELINE_IID}"
+
+containerlab-deploy:
+    stage: integration-test
+    rules:
+        - if: '$CI_PIPELINE_SOURCE != "merge_request_event"'
+          when: never
+        - when: on_success
+    tags:
+        - shell-containerlab
+    needs: ["build-image", "build-testing-image"]
+    script:
+        - firstOctet=$(python3 ${CI_PROJECT_DIR}/.gitlab/ci/scripts/generate_octet.py $CI_COMMIT_SHA)
+        - secondOctet=$(python3 ${CI_PROJECT_DIR}/.gitlab/ci/scripts/generate_octet.py $CI_PIPELINE_ID)
+        - export CLAB_MGMT_SUBNET="172.$firstOctet.$secondOctet.0/24"
+        - cd ${CLAB_DIR}
+        - ls -la
+        - |
+            sed -e "s|@@CEOS_CONTAINER_IMAGE@@|${CEOS_IMAGE}|g" \
+                -e "s|@@GOSDN_CONTAINER_IMAGE@@|${GOSDN_IMAGE}|g" \
+                -e "s|@@CLAB_NAME@@|${CLAB_NAME}|g" \
+                -e "s|@@CLAB_MGMT_SUBNET@@|${CLAB_MGMT_SUBNET}|g" \
+                ${CLAB_TEMPLATE} > ${CLAB_NAME}.clab.yml
+        - cat ${CLAB_NAME}.clab.yml
+        - echo "$CI_REGISTRY_PASSWORD" | docker login -u $CI_REGISTRY_USER --password-stdin $CI_REGISTRY
+        - echo ${GOSDN_IMAGE}
+        - docker pull ${GOSDN_IMAGE}
+        - docker pull ${CEOS_IMAGE}
+        - sudo containerlab deploy --topo ${CLAB_DIR}/${CLAB_NAME}.clab.yml --reconfigure
+        - |
+            echo -e "\
+            GOSDN_HTTP_PORT=$(${CI_PROJECT_DIR}/.gitlab/ci/scripts/docker_host_port 8080 clab-${CLAB_NAME}-gosdn)\n\
+            GOSDN_GRPC_PORT=$(${CI_PROJECT_DIR}/.gitlab/ci/scripts/docker_host_port 55055 clab-${CLAB_NAME}-gosdn)\n\
+            CEOS1_PORT=$(${CI_PROJECT_DIR}/.gitlab/ci/scripts/docker_host_port 6030 clab-${CLAB_NAME}-ceos1)" \
+            > ${CI_PROJECT_DIR}/build.env
+        - cat ${CI_PROJECT_DIR}/build.env
+    artifacts:
+        reports:
+            dotenv: ${CI_PROJECT_DIR}/build.env
+
+
+containerlab-destroy:
+    rules:
+        - if: '$CI_PIPELINE_SOURCE != "merge_request_event"'
+          when: never
+        - when: always
+    stage: integration-test
+    tags:
+        - shell-containerlab
+    needs:
+        [
+            "containerlab-deploy",
+            "integration-test:nucleus",
+            "integration-test:api"
+
+        ]
+    before_script:
+        - cd ${CLAB_DIR}
+    script:
+        - sudo containerlab destroy --topo ${CLAB_DIR}/${CLAB_NAME}.clab.yml
+        - docker volume rm -f ${CLAB_NAME}-volume
+        - rm ${CLAB_DIR}/${CLAB_NAME}.clab.yml
+    allow_failure: true
+
+
+#containerlab:clean:
+#    stage: .post
+#    tags:
+#        - shell-containerlab
+#    before_script:
+#        - cd ${CLAB_DIR}
+#    script:
+#        - docker kill $(docker ps -q) || true
+#        - docker rm $(docker ps -a -q) || true
+#        - docker rmi $(docker images | grep 'registry.code.fbi.h-da.de/danet/gosdn ' | awk '{print $3}') || true
+#        - sudo rm -rf ${CLAB_DIR}/clab-* || true
+#        - sudo sed -i 's|.*clab.*||g' /etc/hosts
+#    allow_failure: true
+#    rules:
+#        - if: $CI_SCHEDULE_CLEAN
+#          when: always
diff --git a/.gitlab/ci/.integration-test.yml b/.gitlab/ci/.integration-test.yml
index 1069f78aa..2f6ee1b73 100644
--- a/.gitlab/ci/.integration-test.yml
+++ b/.gitlab/ci/.integration-test.yml
@@ -6,13 +6,11 @@
           when: never
         - when: on_success
     needs:
-        - job: "containerlab:deploy:integration"
-    tags:
-        - integration-test-docker
+        ["containerlab-deploy"]
     variables:
         GOSDN_LOG: "nolog"
-        GOSDN_TEST_API_ENDPOINT: "${CI_SERVER_IP}:${GOSDN_GRPC_PORT}"
-        CEOS_TEST_ENDPOINT: "${CI_SERVER_IP}:${CEOS1_PORT}"
+        GOSDN_TEST_API_ENDPOINT: "10.254.254.105:${GOSDN_GRPC_PORT}"
+        CEOS_TEST_ENDPOINT: "10.254.254.105:${CEOS1_PORT}"
         GOSDN_TEST_USER: "admin"
         GOSDN_TEST_PASSWORD: "admin"
 
@@ -30,3 +28,9 @@ integration-test:api:
     script:
         - cd controller/
         - make integration-test-api
+
+integration-test:cli:
+    <<: *integration-test
+    script:
+        - cd cli/
+        - echo "Your tests here"
diff --git a/.gitlab/ci/scripts/docker_host_port b/.gitlab/ci/scripts/docker_host_port
old mode 100644
new mode 100755
diff --git a/.gitlab/ci/scripts/generate_octet.py b/.gitlab/ci/scripts/generate_octet.py
new file mode 100755
index 000000000..a0005706c
--- /dev/null
+++ b/.gitlab/ci/scripts/generate_octet.py
@@ -0,0 +1,19 @@
+import sys
+from hashlib import sha256
+
+input_data = sys.argv[1]
+
+# Hash input and store it as string
+hashed_string = sha256(bytes(input_data, encoding='utf8')).hexdigest()
+
+final_string = ""
+
+# Iterate over hash and take out all chars that are a number
+for character in hashed_string:
+    if character.isdigit():
+        final_string += character
+
+final_int = int(final_string)
+
+# Print number mod 256 to get a number between 0 and 255 for ip adress
+print(final_int % 256)
diff --git a/.gitlab/ci/scripts/generate_octet.sh b/.gitlab/ci/scripts/generate_octet.sh
deleted file mode 100755
index c45a36839..000000000
--- a/.gitlab/ci/scripts/generate_octet.sh
+++ /dev/null
@@ -1,12 +0,0 @@
-#!/bin/sh
-
-input=$1
-
-hashed_value=$(echo $input | sha256sum | awk '{print $1}' )
-
-decimal_value=$((16#$hashed_value))
-decimal_value=${decimal_value/-/}
-
-octet=$(((decimal_value % 255)))
-
-echo -n $octet
diff --git a/controller/api/initialise_test.go b/controller/api/initialise_test.go
index abd0d518d..499cb033e 100644
--- a/controller/api/initialise_test.go
+++ b/controller/api/initialise_test.go
@@ -118,7 +118,7 @@ func bufDialer(context.Context, string) (net.Conn, error) {
 
 const testPath = "/system/config/hostname"
 
-var testAddress = "141.100.70.170:6030"
+var testAddress = "10.254.254.105:6030"
 var testAPIEndpoint = "gosdn-latest.apps.ocp.fbi.h-da.de"
 var testUsername = "admin"
 var testPassword = "arista"
diff --git a/controller/test/integration/nucleusIntegration_test.go b/controller/test/integration/nucleusIntegration_test.go
index 689c02c30..402c2c94e 100644
--- a/controller/test/integration/nucleusIntegration_test.go
+++ b/controller/test/integration/nucleusIntegration_test.go
@@ -29,7 +29,7 @@ const unreachable = "203.0.113.10:6030"
 const testPath = "/system/config/hostname"
 
 var modifiedHostname = "ceos3000"
-var testAddress = "141.100.70.170:6030"
+var testAddress = "10.254.254.105:6030"
 var testUsername = "admin"
 var testPassword = "arista"
 var opt *tpb.TransportOption
-- 
GitLab