From 0dbbb1056cb3e74982c0678339c8ec3c093659bc Mon Sep 17 00:00:00 2001
From: dzogove <vehbo.dzogovic@ext.ec.europa.eu>
Date: Thu, 25 May 2023 11:18:07 +0200
Subject: [PATCH] add docker images builder

---
 .gitlab-ci.yml                  | 21 ++++++++++++--
 Dockerfile                      |  5 ++--
 ansible/build.yml               | 50 +++++++++++++++++++++++++++++++++
 ansible/deliver.yml             | 39 +++++++++++++++++++++++++
 ansible/start.yml               | 19 +++++++++++++
 ansible/stop.yml                | 10 +++++++
 netbox_configuration/plugins.py |  1 +
 requirements.txt                |  2 ++
 8 files changed, 142 insertions(+), 5 deletions(-)
 create mode 100644 ansible/build.yml
 create mode 100644 ansible/deliver.yml
 create mode 100644 ansible/start.yml
 create mode 100644 ansible/stop.yml
 create mode 100644 netbox_configuration/plugins.py

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 26a969c..5f91aa1 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -4,9 +4,26 @@ default:
     - shell
 
 stages:
+  - default_validate
   - build
+  - deliver
 
-build-job:
+include:
+  - project: 'digit-c4/digitc4-template-cicd'
+    file: 'gitlab-ci-ansible.yml'
+    ref: main
+
+build-job:       # This job runs in the build stage, which runs first.
   stage: build
   script:
-    - echo "Build..."
+    - ansible-playbook ansible/build.yml
+
+deliver-latest-job:
+  stage: deliver
+  variables:
+    IMAGE_TAG: "latest"
+  script:
+    - ansible-playbook ansible/deliver.yml
+  only:
+    - main
+    - master
diff --git a/Dockerfile b/Dockerfile
index 50269fd..616d97f 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -11,7 +11,6 @@ RUN apt-get update && \
     rm -rf /var/lib/apt/lists/*
 
 COPY . /opt/netbox/netbox/plugins/netbox_rps_plugin
+COPY netbox_configuration/plugins.py /etc/netbox/config/plugins.py
 
-RUN pip install /opt/netbox/netbox/plugins/netbox_rps_plugin
-
-RUN echo "PLUGINS = ['netbox_rps_plugin']" >> /opt/netbox/netbox/netbox/configuration.py
+RUN pip install -r /opt/netbox/netbox/plugins/netbox_rps_plugin/requirements.txt
diff --git a/ansible/build.yml b/ansible/build.yml
new file mode 100644
index 0000000..eb79cf2
--- /dev/null
+++ b/ansible/build.yml
@@ -0,0 +1,50 @@
+- name: Build testing image
+  hosts: localhost
+
+  tasks:
+    - name: Log into private registry
+      docker_login:
+        registry: "{{ lookup('ansible.builtin.env','CI_REGISTRY') }}"
+        username: "{{ lookup('ansible.builtin.env','CI_REGISTRY_USER') }}"
+        password: "{{ lookup('ansible.builtin.env','CI_REGISTRY_PASSWORD') }}"
+        reauthorize: true
+
+    - name: Remove image
+      community.docker.docker_image:
+        state: absent
+        name: "{{ lookup('ansible.builtin.env','CI_PROJECT_NAME') }}:{{ lookup('ansible.builtin.env','CI_COMMIT_SHORT_SHA') }}"
+        force_absent: true
+
+    - name: Building image
+      community.docker.docker_image:
+        build:
+          path: ../
+          args:
+            http_proxy: "{{ lookup('ansible.builtin.env', 'HTTP_PROXY') }}"
+            https_proxy: "{{ lookup('ansible.builtin.env', 'HTTPS_PROXY') }}"
+        name: "{{ lookup('ansible.builtin.env','CI_PROJECT_NAME') }}:{{ lookup('ansible.builtin.env','CI_COMMIT_SHORT_SHA') }}"
+        repository: "{{ lookup('ansible.builtin.env','CI_REGISTRY_IMAGE') }}:{{ lookup('ansible.builtin.env','CI_COMMIT_SHORT_SHA') }}"
+        push: true
+        force_source: true
+        force_tag: true
+        source: build
+
+    - name: Remove local image
+      community.docker.docker_image:
+        state: absent
+        name: "{{ lookup('ansible.builtin.env','CI_PROJECT_NAME') }}:{{ lookup('ansible.builtin.env','CI_COMMIT_SHORT_SHA') }}"
+        force_absent: true
+
+    - name: Remove local image
+      community.docker.docker_image:
+        state: absent
+        name: "{{ lookup('ansible.builtin.env','CI_REGISTRY_IMAGE') }}:{{ lookup('ansible.builtin.env','CI_COMMIT_SHORT_SHA') }}"
+        force_absent: true
+
+    - name: Log out of registry
+      community.docker.docker_login:
+        state: absent
+
+    - name: debug
+      debug:
+        msg: "{{ lookup('ansible.builtin.env','CI_REGISTRY_IMAGE') }}:{{ lookup('ansible.builtin.env','CI_COMMIT_SHORT_SHA') }}"
diff --git a/ansible/deliver.yml b/ansible/deliver.yml
new file mode 100644
index 0000000..ad0fedb
--- /dev/null
+++ b/ansible/deliver.yml
@@ -0,0 +1,39 @@
+- name: Push to Registry
+  hosts: localhost
+
+  tasks:
+    - name: Log into private registry
+      docker_login:
+        registry: "{{ lookup('ansible.builtin.env','CI_REGISTRY') }}"
+        username: "{{ lookup('ansible.builtin.env','CI_REGISTRY_USER') }}"
+        password: "{{ lookup('ansible.builtin.env','CI_REGISTRY_PASSWORD') }}"
+        reauthorize: true
+
+    - name: Get image to deliver
+      community.docker.docker_image:
+        name: "{{ lookup('ansible.builtin.env','CI_REGISTRY_IMAGE') }}:{{ lookup('ansible.builtin.env','CI_COMMIT_SHORT_SHA') }}"
+        source: pull
+
+    - name: Push tag image
+      community.docker.docker_image:
+        name: "{{ lookup('ansible.builtin.env','CI_REGISTRY_IMAGE') }}:{{ lookup('ansible.builtin.env','CI_COMMIT_SHORT_SHA') }}"
+        repository: "{{ lookup('ansible.builtin.env','CI_REGISTRY_IMAGE') }}:{{ lookup('ansible.builtin.env','IMAGE_TAG') }}"
+        push: true
+        force_tag: true
+        source: local
+
+    - name: Remove local image
+      community.docker.docker_image:
+        state: absent
+        name: "{{ lookup('ansible.builtin.env','CI_REGISTRY_IMAGE') }}:{{ lookup('ansible.builtin.env','CI_COMMIT_SHORT_SHA') }}"
+        force_absent: true
+
+    - name: Remove local image
+      community.docker.docker_image:
+        state: absent
+        name: "{{ lookup('ansible.builtin.env','CI_REGISTRY_IMAGE') }}:{{ lookup('ansible.builtin.env','IMAGE_TAG') }}"
+        force_absent: true
+
+    - name: Log out of registry
+      community.docker.docker_login:
+        state: absent
diff --git a/ansible/start.yml b/ansible/start.yml
new file mode 100644
index 0000000..66b165c
--- /dev/null
+++ b/ansible/start.yml
@@ -0,0 +1,19 @@
+- name: Start services
+  hosts: localhost
+
+  tasks:
+    - name: Log into private registry
+      docker_login:
+        registry: "{{ lookup('ansible.builtin.env','CI_REGISTRY') }}"
+        username: "{{ lookup('ansible.builtin.env','CI_REGISTRY_USER') }}"
+        password: "{{ lookup('ansible.builtin.env','CI_REGISTRY_PASSWORD') }}"
+        reauthorize: true
+
+    - name: Start up services
+      community.docker.docker_compose:
+        project_src: ../
+        files:
+          - docker-compose.yml
+          - docker-compose.testci.yml
+        state: present
+        remove_orphans: true
diff --git a/ansible/stop.yml b/ansible/stop.yml
new file mode 100644
index 0000000..8ad54cd
--- /dev/null
+++ b/ansible/stop.yml
@@ -0,0 +1,10 @@
+- name: Stop Services
+  hosts: localhost
+  gather_facts: false
+  tasks:
+    - name: Tear down existing services
+      community.docker.docker_compose:
+        project_src: ./
+        state: absent
+        remove_volumes: true
+        remove_orphans: true
diff --git a/netbox_configuration/plugins.py b/netbox_configuration/plugins.py
new file mode 100644
index 0000000..209d3ac
--- /dev/null
+++ b/netbox_configuration/plugins.py
@@ -0,0 +1 @@
+PLUGINS = ['netbox_dns', 'netbox_rps_plugin']
\ No newline at end of file
diff --git a/requirements.txt b/requirements.txt
index e69de29..c27f299 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -0,0 +1,2 @@
+/opt/netbox/netbox/plugins/netbox_rps_plugin
+netbox-plugin-dns
\ No newline at end of file
-- 
GitLab