From 23076d54b9218a810b9dff2d89f2be23d1c8a099 Mon Sep 17 00:00:00 2001
From: Anders Harrisson <anders.harrisson@ess.eu>
Date: Sat, 16 Jul 2022 07:17:07 +0200
Subject: [PATCH] Revert "Use a submodule based repository for OPIs"

This reverts commit 3590dd6cccc186d625c529a8ee2ff67e6d191667.
---
 README.md                              | 39 ++++++++++++---
 defaults/main.yml                      |  3 +-
 molecule/default/molecule.yml          |  9 ++++
 molecule/default/prepare.yml           | 21 +++++++-
 molecule/default/tests/test_default.py | 14 +++++-
 tasks/main.yml                         | 66 ++++++++++----------------
 6 files changed, 99 insertions(+), 53 deletions(-)

diff --git a/README.md b/README.md
index 6064ac3..8a3eaaf 100644
--- a/README.md
+++ b/README.md
@@ -18,21 +18,15 @@ phoebus_options: "-logging {{ phoebus_home }}/logging.properties"
 phoebus_ess_templates_version: 1.0.4
 phoebus_ess_symbols_version: 1.0.5
 phoebus_ess_opis_version: master
-phoebus_opis_repo: https://gitlab.esss.lu.se/opis/directory.git
-phoebus_opis_repo_version: main
+phoebus_opis_repos: []
 phoebus_git_folders_base: /usr/local/share/cs-studio
 phoebus_opis_path: "{{ phoebus_git_folders_base }}/opis"
 phoebus_ess_symbols_path: "{{ phoebus_git_folders_base }}/ess-symbols"
 phoebus_ess_templates_path: "{{ phoebus_git_folders_base }}/ess-templates"
-phoebus_layouts_path: "{{ phoebus_git_folders_base }}/layouts"
-phoebus_layouts_repo: "https://gitlab.esss.lu.se/accop/shared-cs-studio-phoebus-layouts.git"
-phoebus_layouts_version: master
 # Set to [] to not create a python environment
 phoebus_python_packages:
-  - epics-base=7.0.3.1=h68659b9_4
   - python=3.7
   - py4j
-  - pvapy
   - connect2j
   - numpy=1.17
   - scipy=1.3
@@ -41,6 +35,37 @@ phoebus_python_packages:
 
 The `-server 4918` option can be added by overriding the `phoebus_options` variable.
 
+The `phoebus_opis_repos` variable is a list of dictionaries with the following keys:
+
+- url: URL to the git repository (mandatory)
+- version: version of the repo [default: HEAD]
+- dest: directory name where to clone the repo [default: name of the git repo]
+
+Note that `dest` is the relative path under `phoebus_opis_path`.
+The following variables:
+
+```yaml
+phoebus_opis_path: /data/opis-pbi
+phoebus_opis_repos:
+  - url: https://gitlab.esss.lu.se/beam-diagnostics/opis-pbi/opis-pbi-engineer.git
+    dest: "10-Engineer/10-ACC/99-PBI"
+  - url: https://gitlab.esss.lu.se/beam-diagnostics/opis-pbi/opis-pbi-operator.git
+    dest: "30-Operator/10-ACC/99-PBI"
+```
+
+Would create:
+
+```bash
+/data/
+`-- opis-pbi
+    |-- 10-Engineer
+    |   `-- 10-ACC
+    |       `-- 99-PBI
+    `-- 30-Operator
+        `-- 10-ACC
+            `-- 99-PBI
+```
+
 ## Example Playbook
 
 ```yaml
diff --git a/defaults/main.yml b/defaults/main.yml
index 0fb55c6..d3b0986 100644
--- a/defaults/main.yml
+++ b/defaults/main.yml
@@ -11,8 +11,7 @@ phoebus_options: "-logging {{ phoebus_home }}/logging.properties"
 phoebus_ess_templates_version: 1.0.4
 phoebus_ess_symbols_version: 1.0.5
 phoebus_ess_opis_version: master
-phoebus_opis_repo: https://gitlab.esss.lu.se/opis/directory.git
-phoebus_opis_repo_version: main
+phoebus_opis_repos: []
 phoebus_git_folders_base: /usr/local/share/cs-studio
 phoebus_opis_path: "{{ phoebus_git_folders_base }}/opis"
 phoebus_ess_symbols_path: "{{ phoebus_git_folders_base }}/ess-symbols"
diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml
index 1de4831..fabfaaf 100644
--- a/molecule/default/molecule.yml
+++ b/molecule/default/molecule.yml
@@ -15,6 +15,15 @@ provisioner:
     group_vars:
       molecule_group:
         phoebus_opis_path: /data/opis-pbi
+        phoebus_opis_repos:
+          - url: https://gitlab.esss.lu.se/beam-diagnostics/opis-pbi/opis-pbi-systemexpert.git
+            version: "1d34327ea88a12fec5e64c38839e038361e0a660"
+          - url: https://gitlab.esss.lu.se/beam-diagnostics/opis-pbi/opis-pbi-engineer.git
+            dest: "10-Engineer/10-ACC/99-PBI"
+          - url: https://gitlab.esss.lu.se/beam-diagnostics/opis-pbi/opis-pbi-operator.git
+            dest: "30-Operator/10-ACC/99-PBI"
+          - url: https://gitlab.esss.lu.se/opis/acc/wtrc/operator.git
+            dest: 30-Operator/10-ACC/999-WtrC
         phoebus_settings:
           org.phoebus.pv.ca/addr_list: 127.0.0.1
 scenario:
diff --git a/molecule/default/prepare.yml b/molecule/default/prepare.yml
index 5358b3b..9cd3634 100644
--- a/molecule/default/prepare.yml
+++ b/molecule/default/prepare.yml
@@ -2,4 +2,23 @@
 - name: Prepare
   hosts: all
   gather_facts: false
-  tasks: []
+  tasks:
+
+    - name: install required dependencies
+      yum:
+        name:
+          - tar
+          - gzip
+          - git
+        state: present
+
+    - name: install undefined opi repositories  # noqa 401
+      git:
+        repo: "{{ item.url }}"
+        dest: "{{ phoebus_opis_path }}/{{ item.dest | default(item.url | basename | replace('.git', '')) }}"
+        force: true
+      loop:
+        - url: https://gitlab.esss.lu.se/opis/acc/ts2/rf/engineer.git
+          dest: 10-Engineer/10-ACC/999-TS2/99-RF
+        - url: https://gitlab.esss.lu.se/opis/acc/ts2/rf/systemexpert.git
+          dest: 20-SystemExpert/10-ACC/999-TS2/99-RF
diff --git a/molecule/default/tests/test_default.py b/molecule/default/tests/test_default.py
index a70fe6e..7e5579b 100644
--- a/molecule/default/tests/test_default.py
+++ b/molecule/default/tests/test_default.py
@@ -33,6 +33,18 @@ def test_settings(host):
 def test_opis_repositories(host):
     for filename in (
         "/usr/local/share/cs-studio/ess-opis/README.md",
-        "/data/opis-pbi/.git/HEAD"
+        "/data/opis-pbi/opis-pbi-systemexpert/README.md",
+        "/data/opis-pbi/10-Engineer/10-ACC/99-PBI/README.md",
+        "/data/opis-pbi/30-Operator/10-ACC/99-PBI/README.md",
     ):
         assert host.file(filename).exists
+    cmd = host.run("cd /data/opis-pbi/opis-pbi-systemexpert;git rev-parse HEAD")
+    assert cmd.stdout.strip() == "1d34327ea88a12fec5e64c38839e038361e0a660"
+
+
+def test_opis_repositories_are_deleted(host):
+    for filename in (
+        "/data/opis-pbi/20-SystemExpert/10-ACC/999-TS2/99-RF/README.md",
+        "/data/opis-pbi/10-Engineer/10-ACC/999-TS2/99-RF/README.md",
+    ):
+        assert host.file(filename).exists is False
diff --git a/tasks/main.yml b/tasks/main.yml
index 8d20465..ff37333 100644
--- a/tasks/main.yml
+++ b/tasks/main.yml
@@ -148,53 +148,17 @@
     - phoebus-ess-opis
     - phoebus-git-folders
 
-- name: Check CS-Studio OPI repository location
-  stat:
-    path: "{{ phoebus_opis_path }}/.git"
-  register: phoebus_opis_path_git
-  tags:
-    - phoebus-ess-opis
-    - phoebus-git-folders
-
-- name: Remove old CS-Studio repository location
-  file:
-    path: "{{ phoebus_opis_path }}"
-    state: absent
-  when: not phoebus_opis_path_git.stat.exists
-  tags:
-    - phoebus-ess-opis
-    - phoebus-git-folders
-
-- name: install CS-Studio OPI repository
+- name: install CS-Studio OPIs repositories
   git:
-    repo: "{{ phoebus_opis_repo }}"
-    dest: "{{ phoebus_opis_path }}"
-    version: main
-    recursive: true
+    repo: "{{ item.url }}"
+    dest: "{{ phoebus_opis_path }}/{{ item.dest | default(item.url | basename | replace('.git', '')) }}"
+    version: "{{ item.version | default(omit) }}"
     force: true
+  loop: "{{ phoebus_opis_repos }}"
   tags:
     - phoebus-ess-opis
     - phoebus-git-folders
-
-- name: Check if OPI repository has untracked files
-  command:
-    cmd: git status -u --porcelain  # noqa 303
-    chdir: "{{ phoebus_opis_path }}"
-  register: phoebus_opis_untracked_files
-  failed_when: false
-  changed_when: false
-  tags:
-    - phoebus-ess-opis
-    - phoebus-git-folders
-
-- name: Clean any untracked files in OPI repository
-  command:
-    cmd: git clean -ffd  # noqa 303
-    chdir: "{{ phoebus_opis_path }}"
-  when: phoebus_opis_untracked_files.stdout
-  tags:
-    - phoebus-ess-opis
-    - phoebus-git-folders
+  register: phoebus_git_data
 
 - name: install CS-Studio layouts folder
   git:
@@ -204,3 +168,21 @@
   tags:
     - phoebus-layouts
     - phoebus-git-folders
+
+- name: Find opi repo folders  # Searches the opi path for repos with included gitlab-ci.yml file
+  find:
+    paths: "{{ phoebus_opis_path }}"
+    file_type: directory
+    hidden: true
+    recurse: true
+    patterns:
+      - '.git'
+  register: phoebus_opis_found_repos
+
+
+- name: Remove repositories not defined in phoebus_opis_repos  # parses through found repos and compare them to defined opi repos.
+  file:
+    path: "{{ item.path | dirname }}"
+    state: absent
+  when: item.path | dirname not in phoebus_git_data.results | map(attribute='invocation.module_args.dest') | list
+  loop: "{{ phoebus_opis_found_repos.files }}"
-- 
GitLab