-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GHI-#11 Implement the core plugin "Shell(snowsaw.Plugin)"
- Loading branch information
1 parent
37bbe62
commit c634ad2
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import os | ||
import subprocess | ||
import snowsaw | ||
|
||
|
||
class Shell(snowsaw.Plugin): | ||
""" | ||
Core plugin to run arbitrary shell commands. | ||
""" | ||
_directive = "shell" | ||
|
||
def can_handle(self, directive): | ||
return directive == self._directive | ||
|
||
def handle(self, directive, data): | ||
if directive != self._directive: | ||
raise ValueError("Core plugin \"Shell\" cannot handle the directive \"{}\"".format(directive)) | ||
return self._process_commands(data) | ||
|
||
def _process_commands(self, data): | ||
""" | ||
Processes specified commands. | ||
:param data: The commands to process | ||
:return: True if the commands have been processed successfully, False otherwise | ||
""" | ||
success = True | ||
defaults = self._context.defaults().get("shell", {}) | ||
with open(os.devnull, "w") as devnull: | ||
for item in data: | ||
stdin = stdout = stderr = devnull | ||
if isinstance(item, dict): | ||
cmd = item["command"] | ||
msg = item.get("description", None) | ||
if item.get("stdin", defaults.get("stdin", False)) is True: | ||
stdin = None | ||
if item.get("stdout", defaults.get("stdout", False)) is True: | ||
stdout = None | ||
if item.get("stderr", defaults.get("stderr", False)) is True: | ||
stderr = None | ||
elif isinstance(item, list): | ||
cmd = item[0] | ||
msg = item[1] if len(item) > 1 else None | ||
else: | ||
cmd = item | ||
msg = None | ||
if msg is None: | ||
self._log.lowinfo(cmd) | ||
else: | ||
self._log.lowinfo('{} [{}]'.format(msg, cmd)) | ||
executable = os.environ.get("SHELL") | ||
ret = subprocess.call(cmd, shell=True, stdin=stdin, stdout=stdout, stderr=stderr, cwd=self._context.snowblock_dir(), | ||
executable=executable) | ||
if ret != 0: | ||
success = False | ||
self._log.warning("Command [{}] failed".format(cmd)) | ||
if success: | ||
self._log.info("=> All commands have been executed") | ||
else: | ||
self._log.error("Some commands were not successfully executed") | ||
return success |