From 79ab5da6733e375e547257a8a82e336ebfd2f5c6 Mon Sep 17 00:00:00 2001
From: Varac <varac@varac.net>
Date: Tue, 24 Aug 2021 14:33:02 +0200
Subject: [PATCH] Fix CI on kubernetes executor

* Make CLUSTER_DIR relative
* Fix ansible inventory location
* Fix kubeconfig download location

Fixes: #937
---
 .gitlab-ci.yml                               | 26 +++++++++-----------
 ansible/roles/setup-kubernetes/tasks/k3s.yml |  2 +-
 openappstack/__main__.py                     |  2 +-
 openappstack/ansible.py                      |  4 +--
 openappstack/cluster.py                      |  2 +-
 test/pytest/test_resources.py                |  2 +-
 6 files changed, 16 insertions(+), 22 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 5c39702de..c8c9035c1 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -21,16 +21,12 @@ include:
     echo "HOSTNAME:                  $HOSTNAME"
     echo "IP_ADDRESS:                $IP_ADDRESS"
     echo "Uptime:                    $(uptime)"
-    echo "CLUSTER_DIR:               $CLUSTER_DIR"
     echo "ANSIBLE_HOST_KEY_CHECKING: $ANSIBLE_HOST_KEY_CHECKING"
     echo "KANIKO_BUILD_IMAGENAME:    $KANIKO_BUILD_IMAGENAME"
     echo "KANIKO build image ref:    ${CI_REGISTRY_IMAGE}/${KANIKO_BUILD_IMAGENAME}:${CI_CONTAINER_TAG}"
     echo "SSH_KEY_ID:                $SSH_KEY_ID"
     echo "SHELL                      $SHELL"
-    echo
-    [ -d $CLUSTER_DIR ] && find $CLUSTER_DIR || echo "directory ${CLUSTER_DIR} not found"
-    echo
-    echo
+    echo "CI_PROJECT_DIR:            $CI_PROJECT_DIR"
 
 # The dotenv report requires us to report the artifacts in every job that is
 # required with a `needs:` from another job.
@@ -184,7 +180,7 @@ variables:
   HOSTNAME: "${CI_COMMIT_REF_SLUG}"
   ANSIBLE_HOST_KEY_CHECKING: "False"
   KANIKO_BUILD_IMAGENAME: "openappstack-ci"
-  CLUSTER_DIR: "/builds/openappstack/openappstack/clusters/${CI_COMMIT_REF_SLUG}"
+  CLUSTER_DIR: "clusters/${CI_COMMIT_REF_SLUG}"
 
 default:
   image: "${CI_REGISTRY_IMAGE}/${KANIKO_BUILD_IMAGENAME}:${CI_CONTAINER_TAG}"
@@ -309,7 +305,7 @@ test-dns:
   script:
     - *debug_information
     - cd ansible/
-    - pytest -v -s -m 'dns' --connection=ansible --ansible-inventory=${CLUSTER_DIR}/inventory.yml --hosts='ansible://*'
+    - pytest -v -s -m 'dns' --connection=ansible --ansible-inventory=../${CLUSTER_DIR}/inventory.yml --hosts='ansible://*'
   extends:
     - .general_rules
   interruptible: true
@@ -354,7 +350,7 @@ setup-openappstack:
     - *debug_information
     - cd ansible/
     - export KUBECONFIG="${PWD}/../clusters/${HOSTNAME}/kube_config_cluster.yml"
-    - pytest -v -s -m 'kustomizations' --resource="$RESOURCE" --connection=ansible --ansible-inventory=${CLUSTER_DIR}/inventory.yml --hosts='ansible://*' --reruns 120 --reruns-delay 20
+    - pytest -v -s -m 'kustomizations' --resource="$RESOURCE" --connection=ansible --ansible-inventory=../${CLUSTER_DIR}/inventory.yml --hosts='ansible://*' --reruns 120 --reruns-delay 20
   extends:
     - .ssh_setup
     - .general_rules
@@ -390,7 +386,7 @@ openappstack-kustomizations-ready:
     - *debug_information
     - cd ansible/
     - export KUBECONFIG="${PWD}/../clusters/${HOSTNAME}/kube_config_cluster.yml"
-    - pytest -v -s -m 'helmreleases' --resource="$RESOURCE" --namespace="$NAMESPACE" --connection=ansible --ansible-inventory=${CLUSTER_DIR}/inventory.yml --hosts='ansible://*' --reruns 120 --reruns-delay 20
+    - pytest -v -s -m 'helmreleases' --resource="$RESOURCE" --namespace="$NAMESPACE" --connection=ansible --ansible-inventory=../${CLUSTER_DIR}/inventory.yml --hosts='ansible://*' --reruns 120 --reruns-delay 20
   extends:
     - .ssh_setup
   interruptible: true
@@ -595,7 +591,7 @@ wordpress-helm-release:
     - *debug_information
     - cd ansible/
     - export KUBECONFIG="${PWD}/../clusters/${HOSTNAME}/kube_config_cluster.yml"
-    - pytest -v -s -m 'deployments' --resource="$RESOURCE" --namespace="$NAMESPACE" --connection=ansible --ansible-inventory=${CLUSTER_DIR}/inventory.yml --hosts='ansible://*' --reruns 120 --reruns-delay 10
+    - pytest -v -s -m 'deployments' --resource="$RESOURCE" --namespace="$NAMESPACE" --connection=ansible --ansible-inventory=../${CLUSTER_DIR}/inventory.yml --hosts='ansible://*' --reruns 120 --reruns-delay 10
   extends:
     - .ssh_setup
   interruptible: true
@@ -606,7 +602,7 @@ wordpress-helm-release:
     - *debug_information
     - cd ansible/
     - export KUBECONFIG="${PWD}/../clusters/${HOSTNAME}/kube_config_cluster.yml"
-    - pytest -v -s -m 'statefulsets' --resource="$RESOURCE" --namespace="$NAMESPACE" --connection=ansible --ansible-inventory=${CLUSTER_DIR}/inventory.yml --hosts='ansible://*' --reruns 120 --reruns-delay 10
+    - pytest -v -s -m 'statefulsets' --resource="$RESOURCE" --namespace="$NAMESPACE" --connection=ansible --ansible-inventory=../${CLUSTER_DIR}/inventory.yml --hosts='ansible://*' --reruns 120 --reruns-delay 10
   extends:
     - .ssh_setup
   interruptible: true
@@ -617,7 +613,7 @@ wordpress-helm-release:
     - *debug_information
     - cd ansible/
     - export KUBECONFIG="${PWD}/../clusters/${HOSTNAME}/kube_config_cluster.yml"
-    - pytest -v -s -m 'daemonsets' --resource="$RESOURCE" --namespace="$NAMESPACE" --connection=ansible --ansible-inventory=${CLUSTER_DIR}/inventory.yml --hosts='ansible://*' --reruns 120 --reruns-delay 10
+    - pytest -v -s -m 'daemonsets' --resource="$RESOURCE" --namespace="$NAMESPACE" --connection=ansible --ansible-inventory=../${CLUSTER_DIR}/inventory.yml --hosts='ansible://*' --reruns 120 --reruns-delay 10
   extends:
     - .ssh_setup
   interruptible: true
@@ -753,7 +749,7 @@ wordpress-statefulset:
   script:
     - *debug_information
     - cd ansible/
-    - pytest -v -s -m 'certs' --resource="$RESOURCE" --connection=ansible --ansible-inventory=${CLUSTER_DIR}/inventory.yml --hosts='ansible://*' --reruns 120 --reruns-delay 10
+    - pytest -v -s -m 'certs' --resource="$RESOURCE" --connection=ansible --ansible-inventory=../${CLUSTER_DIR}/inventory.yml --hosts='ansible://*' --reruns 120 --reruns-delay 10
   extends:
     - .ssh_setup
   interruptible: true
@@ -829,7 +825,7 @@ testinfra:
   script:
     - *debug_information
     - cd ansible/
-    - pytest -v -s -m 'testinfra' --connection=ansible --ansible-inventory=${CLUSTER_DIR}/inventory.yml --hosts='ansible://*'
+    - pytest -v -s -m 'testinfra' --connection=ansible --ansible-inventory=../${CLUSTER_DIR}/inventory.yml --hosts='ansible://*'
   extends:
     - .ssh_setup
     - .general_rules
@@ -847,7 +843,7 @@ kube-prometheus-stack-alerts:
     - *debug_information
     - export BASIC_AUTH_PW=$(python3 -m openappstack $HOSTNAME secrets | grep oas-prometheus-basic-auth | cut -d'=' -f2)
     - cd test/
-    - bash ../.gitlab/ci_scripts/retry_cmd_until_success.sh 10 10 pytest -s -m 'prometheus' --connection=ansible --ansible-inventory=${CLUSTER_DIR}/inventory.yml --hosts='ansible://*'
+    - bash ../.gitlab/ci_scripts/retry_cmd_until_success.sh 10 10 pytest -s -m 'prometheus' --connection=ansible --ansible-inventory=../${CLUSTER_DIR}/inventory.yml --hosts='ansible://*'
   extends:
     - .ssh_setup
     - .kube_prometheus_stack_rules
diff --git a/ansible/roles/setup-kubernetes/tasks/k3s.yml b/ansible/roles/setup-kubernetes/tasks/k3s.yml
index 745b9dd8a..19e8f297f 100644
--- a/ansible/roles/setup-kubernetes/tasks/k3s.yml
+++ b/ansible/roles/setup-kubernetes/tasks/k3s.yml
@@ -71,4 +71,4 @@
     flat: yes
   loop:
     - src: "/etc/rancher/k3s/k3s.yaml"
-      dest: "{{ cluster_dir }}/kube_config_cluster.yml"
+      dest: "../{{ cluster_dir }}/kube_config_cluster.yml"
diff --git a/openappstack/__main__.py b/openappstack/__main__.py
index 8d17fb5f6..e8b2d4ddf 100755
--- a/openappstack/__main__.py
+++ b/openappstack/__main__.py
@@ -365,7 +365,6 @@ def test(clus, args):
     taiko_path = os.path.join(os.path.dirname(__file__), '..', 'test', 'taiko')
     # Run from the taiko directory so taiko automatically loads all the
     # necessary files
-    os.chdir(taiko_path)
     clus.load_data()
     command = ['taiko']
     if args.observe:
@@ -429,6 +428,7 @@ def test(clus, args):
 
     log.info('Running taiko command %s', command)
 
+    os.chdir(taiko_path)
     with Popen(command, stdout=PIPE, bufsize=1,
                universal_newlines=True) as taiko:
         for line in taiko.stdout:
diff --git a/openappstack/ansible.py b/openappstack/ansible.py
index e9797f7d9..1e02d60ad 100644
--- a/openappstack/ansible.py
+++ b/openappstack/ansible.py
@@ -8,7 +8,6 @@ import yaml
 
 log = logging.getLogger(__name__)  # pylint: disable=invalid-name
 
-ANSIBLE_INVENTORY = './clusters/{cluster_name}/inventory.yml'
 ANSIBLE_PATH = os.path.join(os.path.dirname(__file__),
                             '..', 'ansible')
 
@@ -37,8 +36,7 @@ def run_ansible(clus, playbook, ansible_params=None):
     ansible_playbook_command += \
         ['-e', 'cluster_dir=' + clus.cluster_dir]
 
-    ansible_playbook_command += \
-        ['-i', clus.inventory_file, playbook]
+    ansible_playbook_command += ['-i', '../' + clus.inventory_file, playbook]
 
     log.info('Running "%s" in ansible directory "%s"',
              ansible_playbook_command,
diff --git a/openappstack/cluster.py b/openappstack/cluster.py
index 7a36b0380..9a45a001b 100644
--- a/openappstack/cluster.py
+++ b/openappstack/cluster.py
@@ -11,7 +11,7 @@ from kubernetes import client, config
 
 from openappstack import ansible
 
-CLUSTER_PATH = os.path.join(os.getcwd(), 'clusters')
+CLUSTER_PATH = 'clusters'
 
 log = logging.getLogger(__name__)  # pylint: disable=invalid-name
 
diff --git a/test/pytest/test_resources.py b/test/pytest/test_resources.py
index 4d6b09097..e91ff21a8 100644
--- a/test/pytest/test_resources.py
+++ b/test/pytest/test_resources.py
@@ -134,7 +134,7 @@ def run_around_tests():
     Prepare kube config before running a test
     """
     cluster_dir = os.environ.get("CLUSTER_DIR")
-    kubeconfig = os.path.join(str(cluster_dir), 'kube_config_cluster.yml')
+    kubeconfig = os.path.join('..', str(cluster_dir), 'kube_config_cluster.yml')
     config.load_kube_config(config_file=kubeconfig)
     yield
 
-- 
GitLab