1
0
Fork 0
mirror of synced 2024-11-25 17:45:34 -05:00

Add more options for shell commands

This commit introduces an extended configuration syntax for shell
commands. This syntax allows for specifying whether the stdin, stdout,
and stderr streams are enabled.
This commit is contained in:
Anish Athalye 2015-02-03 11:53:05 -05:00
parent 0b4b79e422
commit 38c0f65801
2 changed files with 50 additions and 8 deletions

View file

@ -159,15 +159,28 @@ base directory (that is specified when running the installer).
#### Format #### Format
Shell commands are specified as an array of commands, where each command is a Shell commands can be specified in several different ways. The simplest way is
two element array containing the actual shell command as the first element and just to specify a command as a string containing the command to be run. Another
a human-readable description as the second element. way is to specify a two element array where the first element is the shell
command and the second is an optional human-readable description. Shell
commands support an extended syntax as well, which provides more fine-grained
control. A command can be specified as a dictionary that contains the command
to be run, a description, and whether stdin, stdout, and stderr are enabled. In
this syntax, all keys are optional except for the command itself.
##### Example (YAML) ##### Example (YAML)
```yaml ```yaml
- shell: - shell:
- mkdir -p ~/src
- [mkdir -p ~/downloads, Creating downloads directory] - [mkdir -p ~/downloads, Creating downloads directory]
-
command: read var && echo Your variable is $var
stdin: true
stdout: true
-
command: read fail
stderr: true
``` ```
##### Example (JSON) ##### Example (JSON)
@ -175,7 +188,17 @@ a human-readable description as the second element.
```json ```json
[{ [{
"shell": [ "shell": [
["mkdir -p ~/downloads", "Creating downloads directory"] "mkdir -p ~/src",
["mkdir -p ~/downloads", "Creating downloads directory"],
{
"command": "read var && echo Your variable is $var",
"stdin": true,
"stdout": true
},
{
"command": "read fail",
"stderr": true
}
] ]
}] }]
``` ```

View file

@ -20,10 +20,29 @@ class CommandRunner(Executor):
def _process_commands(self, data): def _process_commands(self, data):
success = True success = True
with open(os.devnull, 'w') as devnull: with open(os.devnull, 'w') as devnull:
for cmd, msg in data: for item in data:
self._log.lowinfo('%s [%s]' % (msg, cmd)) stdin = stdout = stderr = devnull
ret = subprocess.call(cmd, shell = True, stdout = devnull, if isinstance(item, dict):
stderr = devnull, cwd = self._base_directory) cmd = item['command']
msg = item.get('description', None)
if item.get('stdin', False) is True:
stdin = None
if item.get('stdout', False) is True:
stdout = None
if item.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('%s [%s]' % (msg, cmd))
ret = subprocess.call(cmd, shell = True, stdin = stdin, stdout = stdout,
stderr = stderr, cwd = self._base_directory)
if ret != 0: if ret != 0:
success = False success = False
self._log.warning('Command [%s] failed' % cmd) self._log.warning('Command [%s] failed' % cmd)