From 0c401d4afe52a3e65f94ef49db25206c7dd45260 Mon Sep 17 00:00:00 2001
From: James Nairn <jwrn3@cam.ac.uk>
Date: Tue, 19 May 2020 16:24:13 +0100
Subject: [PATCH] Tweaks and systemd config

---
 canto_weekly.service | 11 ++++++
 canto_weekly.timer   |  5 +++
 jwrn3_test.sh        |  4 ++
 notes.txt            | 12 ++++++
 test.sh              | 66 +++++++++++++++++++++++++++++++++
 weekly_routine.sh    | 88 ++++++++++++++++++++++++++++++++++++++++----
 6 files changed, 179 insertions(+), 7 deletions(-)
 create mode 100644 canto_weekly.service
 create mode 100644 canto_weekly.timer
 create mode 100644 jwrn3_test.sh
 create mode 100644 notes.txt
 create mode 100755 test.sh

diff --git a/canto_weekly.service b/canto_weekly.service
new file mode 100644
index 0000000..b8541b7
--- /dev/null
+++ b/canto_weekly.service
@@ -0,0 +1,11 @@
+[Unit]
+Description=Runs Canto weekly_routine.sh script
+After=network-online.target
+Wants=network-online.target
+
+[Service]
+WorkingDirectory=/data/export/canto/canto
+ExecStart=/data/export/canto/canto/jwrn3_test.sh
+
+[Install]
+WantedBy=default.target
\ No newline at end of file
diff --git a/canto_weekly.timer b/canto_weekly.timer
new file mode 100644
index 0000000..3b89319
--- /dev/null
+++ b/canto_weekly.timer
@@ -0,0 +1,5 @@
+[Unit]
+Description=Run canto_weekly.service Monday morning at 1am.
+
+[Timer]
+OnCalendar=01:00:00
\ No newline at end of file
diff --git a/jwrn3_test.sh b/jwrn3_test.sh
new file mode 100644
index 0000000..877fe0a
--- /dev/null
+++ b/jwrn3_test.sh
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+# JWRN test script from within docker root
+echo "jwrn3_test" > ./jwrn3_test_result
\ No newline at end of file
diff --git a/notes.txt b/notes.txt
new file mode 100644
index 0000000..6499555
--- /dev/null
+++ b/notes.txt
@@ -0,0 +1,12 @@
+Notes
+
+canto-space = /data/export/canto/canto
+
+
+OnCalendar=Mon 01:00:00
+
+Copy files to /etc/systemd/system/
+sudo systemctl start canto_weekly.timer
+
+Is ssh passwordless set up for root?
+Shuold weekly run as root or another user?
diff --git a/test.sh b/test.sh
new file mode 100755
index 0000000..63f87bf
--- /dev/null
+++ b/test.sh
@@ -0,0 +1,66 @@
+#!/bin/sh
+
+# Called if total failure happens
+function fail {
+    # Echo output to stderr
+    echo "${1}" >&2
+
+    # Exit with non-zero code
+    exit 1
+}
+
+function retry {
+    # Define local vars
+
+    # Starting number
+    local n=1
+
+    # Max number of attempts
+    local max=5
+
+    # Sleep period in seconds until retry
+    local delay=2
+
+    # Loop 
+    while true; do
+
+    # Run array of commands passed in and break loop if successful
+    "${@}" && break || {
+
+        # Else loop while attempt no is less than max
+        if [[ "${n}" -lt "${max}" ]]; then
+
+            # Increment attempt counter
+            ((n++))
+
+            # echo status
+            echo "Command failed. Attempt ${n}/${max}"
+
+            # Sleep for period
+            sleep ${delay}
+        else
+            # Hit max attempts and still failed so giving up
+            fail "The command has failed after ${n} attempts."
+        fi
+    }
+    done
+}
+
+# Grab file from Daneb
+FILENAME="marker_file.txt"
+
+retry scp jwrn3@ent.csi.cam.ac.uk:"${FILENAME}" /tmp/
+
+# Get DBNAME from downloaded file
+if [[ -e "/tmp/${FILENAME}" ]]; then
+    DBNAME=$(cat "/tmp/${FILENAME}")
+fi
+
+# Check DBNAME is not blank
+if [[ -z ${BDNAME }]]; then
+    echo "${DBNAME} is blank, cannot continue"
+    exit 1
+fi
+
+# Transform $DBNAME 
+sed -E "s/(^[[:space:]]+\-[[:space:]]\"dbi\:Pg\:dbname=)[[:alpha:]]+(\;[[:space:]]host=deneb\.pdn\.cam\.ac\.uk\")/\1${DBNAME}\2/" canto_deployment.yaml
\ No newline at end of file
diff --git a/weekly_routine.sh b/weekly_routine.sh
index 592a5a3..672b980 100644
--- a/weekly_routine.sh
+++ b/weekly_routine.sh
@@ -1,5 +1,7 @@
 #!/bin/sh
 #weekly routine on Sunday pm /Monday am. This script should be run from /canto-space/
+# JWRN
+# /canto-space/ == /data/export/canto/canto
 
 # copy/update of ontologies from /data/export/curfiles/ontologies/trunk/ into 'canto-space/import_export'
 ## gm comment: I don't really know how rsync works so I didn't know which options to choose (so didn't try to!) and the syntax may not be correct, but the first path should be OK if Canto is installed on the current vm.
@@ -32,6 +34,8 @@ done
 
 #replace merged ontology and reload all ontologies to Canto
 ##vt comment: Ideally, add the following 'if' routine . If hard to implement, remove the 'if' routine and make the three commands run by default
+## JWRN comment: how do we know an ontology has been changed? Is there a piece of information we can write out and read back in?
+## JWRN comment: may be easier to just update whatever and improve in time
 
 if <any ontology has been changed>, then
 
@@ -44,14 +48,84 @@ if <any ontology has been changed>, then
 	
 fi
 
+# JWRN additions
 
-#update of database name in canto_deploy.yaml
-##vt comment: need lots of help here (as per point 6 in https://docs.google.com/document/d/19C-J8sJmZb_OSluxyzBWJxUkdR_N4sIpgjHI7u5pp0I/edit)
-	##	query SELECT name FROM chado WHERE type='production' AND current='t'; > $
-	## 	to replace the value of dbi:Pg:dbname (i.e. 'flybase') in: 
-	##	Model::ChadoModel:
-	##		connect_info:
-	##			"dbi:Pg:dbname=flybase; host=deneb.pdn.cam.ac.uk"
+# Function called by retry function if total failure happens
+function fail {
+    # Echo output to stderr
+    echo "${1}" >&2
+
+    # Exit with non-zero code
+    exit 1
+}
+
+# Function to retry command until sucessful with max number of attempts
+function retry {
+
+    # Starting number
+    local n=1
+
+    # Max number of attempts
+    local max=5
+
+    # Sleep period in seconds until retry
+    local delay=2
+
+    # Loop 
+    while true; do
+
+		# Run array of commands passed in and break loop if successful
+		"${@}" && break || {
+
+			# Else loop while attempt no is less than max
+			if [[ "${n}" -lt "${max}" ]]; then
+
+				# Increment attempt counter
+				((n++))
+
+				# echo status
+				echo "Command failed. Attempt ${n}/${max}"
+
+				# Sleep for period
+				sleep ${delay}
+			else
+				# Hit max attempts and still failed so giving up
+				fail "The command has failed after ${n} attempts."
+			fi
+		}
+    done
+}
+
+# Set filename of file to pull from 
+MARKERFILE="./canto_done"
+
+# Use retry function to pull marker file from deneb
+# JWRN comment: requires ssh keys be setup for root to fbadmin
+retry /usr/bin/scp fbadmin@deneb.pdb.cam.ac.uk:instance/canto_done "${MARKERFILE}"
+
+# Get DBNAME from downloaded file
+if [[ -e "${MARKERFILE}" ]]; then
+    DBNAME=$(cat "${MARKERFILE}")
+else
+	echo "${MARKERFILE} does not exist, cannot continue"
+	exit 1
+fi
+
+# Check DBNAME is not blank
+if [[ -z ${BDNAME }]]; then
+    echo "${DBNAME} is blank, cannot continue"
+    exit 1
+fi
+
+# Transform $DBNAME 
+CANTO_CONFIG="./canto_deployment.yaml"
+
+if [[ -e "${CANTO_CONFIG}" ]]; then
+	sed -i.bak -E "s/(^[[:space:]]+\-[[:space:]]\"dbi\:Pg\:dbname=)[[:alpha:]]+(\;[[:space:]]host=deneb\.pdn\.cam\.ac\.uk\")/\1${DBNAME}\2/" "${CANTO_CONFIG}"
+else
+	echo "${CANTO_CONFIG} does not exist, cannot continue"
+	exit 1
+fi
 
 
 #data import (using Gillian's scripts in the vm - see point 7.d in https://docs.google.com/document/d/19C-J8sJmZb_OSluxyzBWJxUkdR_N4sIpgjHI7u5pp0I/edit)
-- 
GitLab