From 160adb4c0af4ecf32b3da9c342f9cc56d6b4a5d4 Mon Sep 17 00:00:00 2001
From: Joris Luijsterburg <joris.luijsterburg@wur.nl>
Date: Wed, 27 Jul 2022 16:12:27 +0200
Subject: [PATCH] Added first POC of s3specific free space rule

---
 s3/bash/changerescfreespace          |  9 +++++++++
 s3/bash/s3diskusagefromresc          | 15 ++++++++++++++
 s3/interactiveRules/upds3freespace.r | 30 ++++++++++++++++++++++++++++
 3 files changed, 54 insertions(+)
 create mode 100644 s3/bash/changerescfreespace
 create mode 100644 s3/bash/s3diskusagefromresc
 create mode 100644 s3/interactiveRules/upds3freespace.r

diff --git a/s3/bash/changerescfreespace b/s3/bash/changerescfreespace
new file mode 100644
index 0000000..8867724
--- /dev/null
+++ b/s3/bash/changerescfreespace
@@ -0,0 +1,9 @@
+#!/bin/bash
+# modresc Name [name, type, host, path, status, comment, info, free_space, context, rebalance] Value
+#         (modify Resource)
+#iadmin modresc s3resc1 freespace 100MB
+
+
+name=$1
+value=$2
+iadmin modresc $name freespace $value
diff --git a/s3/bash/s3diskusagefromresc b/s3/bash/s3diskusagefromresc
new file mode 100644
index 0000000..d0e9ee3
--- /dev/null
+++ b/s3/bash/s3diskusagefromresc
@@ -0,0 +1,15 @@
+#!/bin/bash
+#remember to set the .s3cfg when excuting this, the irods user needs it as well!
+bucketname=$1
+totalbucketsize=$2
+
+s3result=$(s3cmd du s3:/$bucketname)
+returnvalue=$?
+
+s3resultarray=($s3result)
+s3duinb=${s3resultarray[0]}
+s3duinkb=`expr $s3duinb / 1024 `
+s3totalfree=`expr $totalbucketsize - $s3duinkb`
+
+echo $s3totalfree
+exit $returnvalue
diff --git a/s3/interactiveRules/upds3freespace.r b/s3/interactiveRules/upds3freespace.r
new file mode 100644
index 0000000..d030d14
--- /dev/null
+++ b/s3/interactiveRules/upds3freespace.r
@@ -0,0 +1,30 @@
+upds3freespace{
+ #first test if this is an s3resource or not, and get the bucketname at the same time
+  foreach(*resource in SELECT RESC_TYPE_NAME,RESC_VAULT_PATH where RESC_NAME = "*resc"){
+    *resourcetype=*resource.RESC_TYPE_NAME
+    *bucket=*resource.RESC_VAULT_PATH
+    if(*resourcetype!="s3"){
+      writeLine("stdout","This is not an s3 resource, the rule doesn't work with a non s3-resource")
+      failmsg(-1,"This is not an s3 resource, the rule doesn't work with a non s3-resource")
+    }
+  }
+
+  #get bucket free space in variable
+  writeLine("stdout","fetching disk usage of irods bucket with maximum of 10MB");
+  msiExecCmd("s3diskusagefromresc","*bucket 10240", "null", "null", "null", *OUTPUT);
+
+  #TODO: check if output is correct, the resource could be ill configured
+  #msiGetStderrInExecCmdOut(*OUTPUT,*ErrorOut);
+  #writeLine("stdout","Error message is *ErrorOut");
+
+  #printing messages
+  msiGetStdoutInExecCmdOut(*OUTPUT,*Out);
+  writeLine("stdout","Output message is *Out");
+
+
+
+  #TODO: update freespace with icommand iadmin
+
+}
+INPUT *resc="s3resc2"
+OUTPUT ruleExecOut
-- 
GitLab