summaryrefslogtreecommitdiff
path: root/actions
diff options
authormattyw <lp@mattyw.net>2015-10-07 16:11:12 +0100
committermattyw <lp@mattyw.net>2015-10-07 16:11:12 +0100
commit6d6471484cd0d24b73bb991a73e001c050255d87 (patch)
tree364efa62042c655d40c5f3cb620970a350eea56c /actions
parent07d5dcf52132328240c1b105b27dfd361f3b4429 (diff)
action/backup: Use the action functions from charmhelpers
Diffstat (limited to 'actions')
-rw-r--r--actions/backup.py43
-rw-r--r--actions/backup_test.py14
2 files changed, 18 insertions, 39 deletions
diff --git a/actions/backup.py b/actions/backup.py
index 01eb968..a4aed2e 100644
--- a/actions/backup.py
+++ b/actions/backup.py
@@ -1,6 +1,12 @@
import subprocess
import os
from distutils.spawn import find_executable
+try:
+ from charmhelpers.core.hookenv import action_get, action_set, action_fail
+except ImportError:
+ subprocess.check_call(['apt-get', 'install', '-y', 'python-pip'])
+ subprocess.check_call(['pip', 'install', 'charmhelpers'])
+ from charmhelpers.core.hookenv import action_get, action_set, action_fail
def mkdir(dir):
@@ -14,29 +20,6 @@ def execute(command, current_working_directory):
)
-def action_set(key, val):
- action_cmd = ['action-set']
- if isinstance(val, dict):
- for k, v in val.iteritems():
- action_set('%s.%s' % (key, k), v)
- return
-
- action_cmd.append('%s=%s' % (key, val))
- subprocess.check_call(action_cmd)
-
-
-def action_get(key):
- if find_executable('action-get'):
- return subprocess.check_output(['action-get', key]).strip()
- return None
-
-
-def action_fail(message):
- if find_executable('action-fail'):
- return subprocess.check_call(['action-fail', message])
- return None
-
-
def dump():
args = str(action_get("args")).strip()
dir = str(action_get("workingDir"))
@@ -55,22 +38,20 @@ def backup(cmd, args, dir):
except OSError, e:
pass # Ignoring, the directory already exists
except Exception, e:
- action_set("directory creation exception", e)
+ action_set({"directory creation exception": e})
action_fail(e)
return
command = cmd + " " + args
command = command.strip()
- action_set("command", command)
- action_set("working-dir", dir)
+ action_set({"command": command, "working-dir": dir})
try:
output = execute(command, dir)
- action_set("output", output)
+ action_set({"output": output})
except subprocess.CalledProcessError, e:
- action_set("error_code", e.returncode)
- action_set("exception", e)
- action_set("output", e.output)
+ action_set({"error_code": e.returncode,
+ "exception": e, "output": e.output})
action_fail(e)
except Exception, e:
- action_set("exception", e)
+ action_set({"exception": e})
action_fail(e)
diff --git a/actions/backup_test.py b/actions/backup_test.py
index ec739ef..76f9ac0 100644
--- a/actions/backup_test.py
+++ b/actions/backup_test.py
@@ -25,10 +25,9 @@ class TestBackups(unittest.TestCase):
mkdir.assert_called_once_with("this/dir")
execute.assert_called_once_with("mongodump -arg1 -arg2", "this/dir")
action_set.assert_has_calls([
- call("command", "mongodump -arg1 -arg2"),
- call("working-dir", "this/dir"),
- call("output", "output"),
- ])
+ call({"command": "mongodump -arg1 -arg2",
+ "working-dir": "this/dir"}),
+ call({"output": "output"})])
def test_restore(self):
mkdir = create_autospec(backup.mkdir, return_value=True)
@@ -45,10 +44,9 @@ class TestBackups(unittest.TestCase):
mkdir.assert_called_once_with("this/dir")
execute.assert_called_once_with("mongorestore -arg1 -arg2", "this/dir")
action_set.assert_has_calls([
- call("command", "mongorestore -arg1 -arg2"),
- call("working-dir", "this/dir"),
- call("output", "output"),
- ])
+ call({"command": "mongodump -arg1 -arg2",
+ "working-dir": "this/dir"}),
+ call({"output": "output"})])
if __name__ == '__main__':
unittest.main()