Skip to content

Commit

Permalink
Raises exception with failed salt states message
Browse files Browse the repository at this point in the history
Fixes #316
  • Loading branch information
lorengordon committed Jun 4, 2017
1 parent 8b364c1 commit 18c6641
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
9 changes: 7 additions & 2 deletions src/watchmaker/managers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def _pipe_logger(pipe, logger, prefix_msg='', return_output=False):

return ret or None

def call_process(self, cmd, stdout=False):
def call_process(self, cmd, stdout=False, raise_error=True):
"""
Execute a shell command.
Expand All @@ -234,6 +234,11 @@ def call_process(self, cmd, stdout=False):
Switch to control whether to return stdout.
(*Default*: ``False``)
raise_error: (:obj:`bool`)
Switch to control whether to raise if the command return code
is non-zero.
(*Default*: ``True``)
Returns:
:obj:`None` or :obj:`bytes`:
``None`` unless ``stdout`` is ``True``. In that case, the
Expand Down Expand Up @@ -275,7 +280,7 @@ def call_process(self, cmd, stdout=False):

returncode = process.wait()

if returncode != 0:
if raise_error and returncode != 0:
msg = 'Command failed! Exit code={0}, cmd={1}'.format(
process.returncode, cmd)
self.log.critical(msg)
Expand Down
24 changes: 14 additions & 10 deletions src/watchmaker/workers/salt.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
import codecs
import json
import os
import re
import shutil

import yaml

from watchmaker import static
from watchmaker.exceptions import WatchmakerException
from watchmaker.managers.base import LinuxManager, ManagerBase, WindowsManager


Expand Down Expand Up @@ -138,7 +140,7 @@ def _prepare_for_install(self):
self.salt_state_args = [
'--log-file', self.salt_debug_logfile,
'--log-file-level', 'debug',
'--state-output', 'mixed_id'
'--state_verbose', 'false'
]

for salt_dir in [
Expand Down Expand Up @@ -240,7 +242,7 @@ def _set_grain(self, grain, value):
]
self.run_salt(cmd)

def run_salt(self, command, stdout=False):
def run_salt(self, command, **kwargs):
"""
Execute salt command.
Expand All @@ -250,10 +252,6 @@ def run_salt(self, command, stdout=False):
Watchmaker will always begin the command with the options
``--local``, ``--retcode-passthrough``, and ``--no-color``, so
do not specify those options in the command.
stdout: (:obj:`bool`)
Switch to control whether to return stdout.
(*Default*: ``False``)
"""
cmd = [
self.salt_call,
Expand All @@ -265,9 +263,8 @@ def run_salt(self, command, stdout=False):
cmd.extend(command)
else:
cmd.append(command)
ret = self.call_process(cmd, stdout=stdout)
if stdout:
return ret

return self.call_process(cmd, **kwargs)

def service_status(self, service):
"""
Expand Down Expand Up @@ -435,7 +432,14 @@ def process_states(self, states):
)
cmd.extend(['state.sls', states])

ret = self.run_salt(cmd, stdout=True)
ret = self.run_salt(cmd, stdout=True, raise_error=False)

if ret['retcode'] != 0:
raise WatchmakerException(
'Salt states failed to apply!{0}{1}'
.format(os.linesep, ret['stdout'])
)

self.log.info('Salt states all applied successfully!')


Expand Down

0 comments on commit 18c6641

Please sign in to comment.