Skip to content
Browse files

Add commands.run_script

  • Loading branch information...
1 parent 8db1e0b commit a3856d458643ca3747552e067ccc39173effbe9d @bitprophet bitprophet committed Sep 19, 2013
Showing with 36 additions and 0 deletions.
  1. +2 −0 docs/index.rst
  2. +34 −0 patchwork/commands.py
View
2 docs/index.rst
@@ -37,6 +37,8 @@ API description
* Including "download this URL directly to the remote end", etc
* `auth`: Create/manage users, groups, `sudoers` etc
* `vcs`: Manage version control checkouts
+* `commands`: Execute complicated tasks combining run/sudo/get/put and so
+ forth, such as uploading/running/removing script files.
Stuff that might want to live in some sort of plugin/ext system?)
View
34 patchwork/commands.py
@@ -0,0 +1,34 @@
+"""
+Command execution "super-tasks" - combining put/get/run/sudo, etc.
+"""
+from __future__ import with_statement
+
+import os
+
+from fabric.api import run, put, settings, cd
+
+
+def run_script(source, cwd, binary='bash', runner=run):
+ """
+ Invoke local script file ``source`` in remote directory ``cwd``.
+
+ Works by copying the file remotely, executing it, & removing it. Removal
+ will always occur even if execution fails.
+
+ :param source: Local path to script file; passed directly to ``fabric.operations.get``.
+ :param cwd: Remote working directory to copy to & invoke script from.
+ :param binary:
+ Command to run remotely with the script as its argument. Defaults to
+ ``'bash'``, meaning invocation will be e.g. ``bash myscript.sh``.
+ :param runner:
+ Fabric function call to use when invoking. Defaults to
+ ``fabric.operations.run``.
+ :rtype: Return value of the execution call.
+ """
+ fname = os.path.basename(source)
+ put(source, cwd)
+ with settings(cd(cwd), warn_only=True):
+ result = runner("%s %s" % (binary, fname))
+ runner("rm %s" % fname)
+ return result
+

0 comments on commit a3856d4

Please sign in to comment.
Something went wrong with that request. Please try again.