From 29868f8787b4c0dcbec99037945769b5d26eb8e6 Mon Sep 17 00:00:00 2001
From: lukken <lukken@astron.nl>
Date: Wed, 26 Oct 2022 11:43:32 +0000
Subject: [PATCH] L2SS-983: Partial Ansible Deploy

---
 .gitignore         |  3 +++
 .gitlab-ci.yml     | 18 ++++++++++++++++--
 deploy/ansible.cfg |  3 +++
 deploy/deploy.yml  | 47 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 69 insertions(+), 2 deletions(-)
 create mode 100644 deploy/ansible.cfg
 create mode 100644 deploy/deploy.yml

diff --git a/.gitignore b/.gitignore
index f77736405..247edda4a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -27,5 +27,8 @@ tangostationcontrol/docs/build
 **/pending_log_messages.db
 **/.eggs
 
+deploy/*.retry
+deploy/hosts
+
 docker-compose/alerta-web/alerta-secrets.json
 docker-compose/tmp
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index d6f41a10f..15263eb7d 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -668,14 +668,28 @@ stages:
   rules:
 #    - if: ($CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH) && $CI_COMMIT_TAG
   - if: $CI_COMMIT_TAG
+  before_script:
+    # Use Gitlab protected variable to provide key
+    - echo "$DEPLOY_KEY" > id_rsa
+    - chmod 400 id_rsa
+    - ssh-keygen -y -f id_rsa > id_rsa.pub
+    # Add ssh key to agent
+    - eval $(ssh-agent)
+    - ssh-add id_rsa
+    # Prevent error of ansible being run in world writeable directory
+    - chmod o-w .
   script:
-    - echo "start"
-    - echo "ssh $DEPLOY_USER @ $DEPLOY_HOST"
+    - echo "Deploying version $CI_COMMIT_TAG"
+    - cd deploy || exit 1
+    - echo "[all]" > hosts
+    - echo "stat ansible_host=$DEPLOY_HOST ansible_user=$DEPLOY_USER" >> hosts
+    - ansible-playbook deploy.yml --extra-vars station_version=$CI_COMMIT_TAG
 .deploy_l2ts_base:
   extends: .base_deploy
   variables:
     DEPLOY_USER: $L2TS_USERNAME
     DEPLOY_HOST: $L2TS_HOSTNAME
+    DEPLOY_KEY: $L2TS_DEPLOY_KEY
 deploy_l2ts_stop:
   extends: .deploy_l2ts_base
   environment:
diff --git a/deploy/ansible.cfg b/deploy/ansible.cfg
new file mode 100644
index 000000000..8da5e9439
--- /dev/null
+++ b/deploy/ansible.cfg
@@ -0,0 +1,3 @@
+[defaults]
+host_key_checking = False
+inventory = hosts
diff --git a/deploy/deploy.yml b/deploy/deploy.yml
new file mode 100644
index 000000000..e89ae6cf6
--- /dev/null
+++ b/deploy/deploy.yml
@@ -0,0 +1,47 @@
+---
+- name: StationControl Early Deployment
+  hosts: all
+  tasks:
+     - name: Register git installation status
+       command: which git
+       changed_when: false
+       failed_when: git_installed.rc not in [0,1]
+       register: git_installed
+     - name: Register tango directory status
+       shell: cd ~/tango
+       args:
+         chdir: ~
+       changed_when: false
+       failed_when: tango_directory.rc not in [0,1]
+       register: tango_directory
+     - name: Register pending changes
+       changed_when: false
+       failed_when: pending_changes.rc not in [0,1]
+       shell: |
+         git status | grep Changes
+         ret=$?
+         echo $ret
+         if [ $ret -eq 0 ]; then
+           exit 1
+         fi
+         exit 0
+       args:
+         chdir: ~/tango
+       register: pending_changes
+     - name: Check git installation status
+       fail:
+         msg: "Git does not appear to be installed!"
+       when: git_installed.rc not in [0]
+     - name: Check tango directory status
+       fail:
+         msg: "Tango directory appears to be missing!"
+       when: tango_directory.rc not in [0]
+     - name: Check pending changes
+       fail:
+         msg: "Deployment repository seems to have pending changes!"
+       when: pending_changes not in [0]
+     - name: Update sources
+       changed_when: false
+       shell: "git checkout v{{ station_version }}"
+       args:
+         chdir: ~/tango
\ No newline at end of file
-- 
GitLab