Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2017.7] Porting #47049 to 2017.7. #48061

Merged
merged 2 commits into from
Jun 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 55 additions & 48 deletions salt/modules/timezone.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,60 +484,67 @@ def set_hwclock(clock):

salt '*' timezone.set_hwclock UTC
'''
if 'AIX' in __grains__['os_family']:
if clock.lower() != 'utc':
raise SaltInvocationError(
'UTC is the only permitted value'
)
return True

timezone = get_zone()

if 'Solaris' in __grains__['os_family']:
if clock.lower() not in ('localtime', 'utc'):
raise SaltInvocationError(
'localtime and UTC are the only permitted values'
)
if 'sparc' in __grains__['cpuarch']:
raise SaltInvocationError(
'UTC is the only choice for SPARC architecture'
)
cmd = ['rtc', '-z', 'GMT' if clock.lower() == 'utc' else timezone]
if salt.utils.which('timedatectl'):
cmd = ['timedatectl', 'set-local-rtc',
'true' if clock == 'localtime' else 'false']
return __salt__['cmd.retcode'](cmd, python_shell=False) == 0
else:
os_family = __grains__['os_family']
if os_family in ('AIX', 'NILinuxRT'):
if clock.lower() != 'utc':
raise SaltInvocationError(
'UTC is the only permitted value'
)
return True

zonepath = '/usr/share/zoneinfo/{0}'.format(timezone)
timezone = get_zone()

if not os.path.exists(zonepath):
raise CommandExecutionError(
'Zone \'{0}\' does not exist'.format(zonepath)
)
if 'Solaris' in __grains__['os_family']:
if clock.lower() not in ('localtime', 'utc'):
raise SaltInvocationError(
'localtime and UTC are the only permitted values'
)
if 'sparc' in __grains__['cpuarch']:
raise SaltInvocationError(
'UTC is the only choice for SPARC architecture'
)
cmd = ['rtc', '-z', 'GMT' if clock.lower() == 'utc' else timezone]
return __salt__['cmd.retcode'](cmd, python_shell=False) == 0

os.unlink('/etc/localtime')
os.symlink(zonepath, '/etc/localtime')
zonepath = '/usr/share/zoneinfo/{0}'.format(timezone)

if 'Arch' in __grains__['os_family']:
cmd = ['timezonectl', 'set-local-rtc',
'true' if clock == 'localtime' else 'false']
return __salt__['cmd.retcode'](cmd, python_shell=False) == 0
elif 'RedHat' in __grains__['os_family']:
__salt__['file.sed'](
'/etc/sysconfig/clock', '^ZONE=.*', 'ZONE="{0}"'.format(timezone))
elif 'Suse' in __grains__['os_family']:
__salt__['file.sed'](
'/etc/sysconfig/clock', '^TIMEZONE=.*', 'TIMEZONE="{0}"'.format(timezone))
elif 'Debian' in __grains__['os_family']:
if clock == 'UTC':
__salt__['file.sed']('/etc/default/rcS', '^UTC=.*', 'UTC=yes')
elif clock == 'localtime':
__salt__['file.sed']('/etc/default/rcS', '^UTC=.*', 'UTC=no')
elif 'Gentoo' in __grains__['os_family']:
if clock not in ('UTC', 'localtime'):
raise SaltInvocationError(
'Only \'UTC\' and \'localtime\' are allowed'
if not os.path.exists(zonepath):
raise CommandExecutionError(
'Zone \'{0}\' does not exist'.format(zonepath)
)
if clock == 'localtime':
clock = 'local'
__salt__['file.sed'](
'/etc/conf.d/hwclock', '^clock=.*', 'clock="{0}"'.format(clock))

os.unlink('/etc/localtime')
os.symlink(zonepath, '/etc/localtime')

if 'Arch' in __grains__['os_family']:
cmd = ['timezonectl', 'set-local-rtc',
'true' if clock == 'localtime' else 'false']
return __salt__['cmd.retcode'](cmd, python_shell=False) == 0
elif 'RedHat' in __grains__['os_family']:
__salt__['file.sed'](
'/etc/sysconfig/clock', '^ZONE=.*', 'ZONE="{0}"'.format(timezone))
elif 'Suse' in __grains__['os_family']:
__salt__['file.sed'](
'/etc/sysconfig/clock', '^TIMEZONE=.*', 'TIMEZONE="{0}"'.format(timezone))
elif 'Debian' in __grains__['os_family']:
if clock == 'UTC':
__salt__['file.sed']('/etc/default/rcS', '^UTC=.*', 'UTC=yes')
elif clock == 'localtime':
__salt__['file.sed']('/etc/default/rcS', '^UTC=.*', 'UTC=no')
elif 'Gentoo' in __grains__['os_family']:
if clock not in ('UTC', 'localtime'):
raise SaltInvocationError(
'Only \'UTC\' and \'localtime\' are allowed'
)
if clock == 'localtime':
clock = 'local'
__salt__['file.sed'](
'/etc/conf.d/hwclock', '^clock=.*', 'clock="{0}"'.format(clock))

return True
24 changes: 20 additions & 4 deletions tests/unit/modules/test_timezone.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,21 @@ def test_get_hwclock_aix(self):
with patch.dict(timezone.__grains__, {'os_family': ['AIX']}):
assert timezone.get_hwclock() == hwclock

@skipIf(salt.utils.is_windows(), 'os.symlink not available in Windows')
@patch('salt.utils.which', MagicMock(return_value=True))
def test_set_hwclock_timedatectl(self):
'''
Test set hwclock with timedatectl
:return:
'''
timezone.set_hwclock('UTC')
name, args, kwargs = timezone.__salt__['cmd.retcode'].mock_calls[0]
assert args == (['timedatectl', 'set-local-rtc', 'false'],)

timezone.set_hwclock('localtime')
name, args, kwargs = timezone.__salt__['cmd.retcode'].mock_calls[1]
assert args == (['timedatectl', 'set-local-rtc', 'true'],)

@skipIf(salt.utils.is_windows(), 'os.symlink not available in Windows')
@patch('salt.utils.which', MagicMock(return_value=False))
@patch('os.path.exists', MagicMock(return_value=True))
Expand All @@ -334,10 +349,11 @@ def test_set_hwclock_aix(self):
Test set hwclock on AIX
:return:
'''
with patch.dict(timezone.__grains__, {'os_family': ['AIX']}):
with self.assertRaises(SaltInvocationError):
assert timezone.set_hwclock('forty two')
assert timezone.set_hwclock('UTC')
for osfamily in ['AIX', 'NILinuxRT']:
with patch.dict(timezone.__grains__, {'os_family': osfamily}):
with self.assertRaises(SaltInvocationError):
assert timezone.set_hwclock('forty two')
assert timezone.set_hwclock('UTC')

@skipIf(salt.utils.is_windows(), 'os.symlink not available in Windows')
@patch('salt.utils.which', MagicMock(return_value=False))
Expand Down