Skip to content

Commit

Permalink
Merge pull request #12724 from terminalmage/issue12696
Browse files Browse the repository at this point in the history
Jinja renderer, timezone module/state fixes
  • Loading branch information
basepi committed May 13, 2014
2 parents 7696fd4 + 83d219f commit 86617cd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
19 changes: 15 additions & 4 deletions salt/modules/timezone.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

# Import salt libs
import salt.utils
from salt.exceptions import SaltInvocationError, CommandExecutionError

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -156,11 +157,21 @@ def zone_compare(timezone):
return 'Error: {0} does not exist.'.format(tzfile)

hash_type = getattr(hashlib, __opts__.get('hash_type', 'md5'))
with salt.utils.fopen(zonepath, 'r') as fp_:
usrzone = hash_type(fp_.read()).hexdigest()

with salt.utils.fopen(tzfile, 'r') as fp_:
etczone = hash_type(fp_.read()).hexdigest()
try:
with salt.utils.fopen(zonepath, 'r') as fp_:
usrzone = hash_type(fp_.read()).hexdigest()
except IOError as exc:
raise SaltInvocationError('Invalid timezone {0!r}'.format(timezone))

try:
with salt.utils.fopen(tzfile, 'r') as fp_:
etczone = hash_type(fp_.read()).hexdigest()
except IOError as exc:
raise CommandExecutionError(
'Problem reading timezone file {0}: {1}'
.format(tzfile, exc.strerror)
)

if usrzone == etczone:
return True
Expand Down
23 changes: 18 additions & 5 deletions salt/states/timezone.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
detail here_.
'''

# Import salt libs
from salt.exceptions import SaltInvocationError, CommandExecutionError


def __virtual__():
'''
Expand All @@ -36,7 +39,7 @@ def __virtual__():
return 'timezone.get_zone' in __salt__


def system(name, utc=''):
def system(name, utc=True):
'''
Set the timezone for the system.
Expand All @@ -53,7 +56,17 @@ def system(name, utc=''):
# Set up metadata
do_utc = False
do_zone = False
compzone = __salt__['timezone.zone_compare'](name)

try:
compzone = __salt__['timezone.zone_compare'](name)
except (SaltInvocationError, CommandExecutionError) as exc:
ret['result'] = False
ret['comment'] = (
'Unable to compare desrired timezone {0!r} to system timezone: {1}'
.format(name, exc)
)
return ret

myutc = True
messages = []
if __salt__['timezone.get_hwclock']() == 'localtime':
Expand All @@ -67,10 +80,10 @@ def system(name, utc=''):
do_zone = True

# If the user passed in utc, do a check
if utc != '' and utc != myutc:
if utc and utc != myutc:
ret['result'] = None
do_utc = True
elif utc != '' and utc == myutc:
elif utc and utc == myutc:
messages.append('UTC already set to {0}'.format(name))

if ret['result'] is True:
Expand All @@ -81,7 +94,7 @@ def system(name, utc=''):
messages = []
if compzone is False:
messages.append('Timezone {0} needs to be set'.format(name))
if utc != '' and myutc != utc:
if utc and myutc != utc:
messages.append('UTC needs to be set to {0}'.format(utc))
ret['comment'] = ', '.join(messages)
return ret
Expand Down
16 changes: 14 additions & 2 deletions salt/utils/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

# Import salt libs
import salt.utils
from salt.exceptions import SaltRenderError
from salt.exceptions import (
SaltRenderError, CommandExecutionError, SaltInvocationError
)
from salt.utils.jinja import ensure_sequence_filter
from salt.utils.jinja import SaltCacheLoader as JinjaSaltCacheLoader
from salt.utils.jinja import SerializerExtension as JinjaSerializerExtension
Expand Down Expand Up @@ -285,7 +287,17 @@ def render_jinja_tmpl(tmplstr, context, tmplpath=None):
exc, out),
line,
tmplstr)
except Exception, exc:
except (SaltInvocationError, CommandExecutionError) as exc:
trace = traceback.extract_tb(sys.exc_info()[2])
line, out = _get_jinja_error(trace, context=unicode_context)
if not line:
tmplstr = ''
raise SaltRenderError(
'Problem running salt function in Jinja template: {0}{1}'.format(
exc, out),
line,
tmplstr)
except Exception as exc:
tracestr = traceback.format_exc()
trace = traceback.extract_tb(sys.exc_info()[2])
line, out = _get_jinja_error(trace, context=unicode_context)
Expand Down

0 comments on commit 86617cd

Please sign in to comment.