Merge pull request #40020 from dmurphy18/aix_timezone

Full support for execution module timezone on AIX
This commit is contained in:
Mike Place 2017-03-14 15:05:30 -06:00 committed by GitHub
commit 8db74fb275

View file

@ -116,10 +116,27 @@ def _get_zone_etc_timezone():
return fp_.read().strip()
def _get_zone_aix():
tzfile = '/etc/environment'
with salt.utils.fopen(tzfile, 'r') as fp_:
for line in fp_:
if 'TZ=' in line:
zonepart = line.rstrip('\n').split('=')[-1]
return zonepart.strip('\'"') or 'UTC'
raise CommandExecutionError('Unable to get timezone from ' + tzfile)
def get_zone():
'''
Get current timezone (i.e. America/Denver)
.. versionchanged:: 2016.11.4
.. note::
On AIX operating systems, Posix values can also be returned
'CST6CDT,M3.2.0/2:00:00,M11.1.0/2:00:00'
CLI Example:
.. code-block:: bash
@ -153,6 +170,8 @@ def get_zone():
return _get_zone_etc_localtime()
elif 'Solaris' in os_family:
return _get_zone_solaris()
elif 'AIX' in os_family:
return _get_zone_aix()
raise CommandExecutionError('Unable to get timezone')
@ -179,7 +198,15 @@ def get_offset():
salt '*' timezone.get_offset
'''
return __salt__['cmd.run'](['date', '+%z'], python_shell=False)
if 'AIX' not in __grains__['os_family']:
return __salt__['cmd.run'](['date', '+%z'], python_shell=False)
salt_path = '/opt/salt/bin/date'
if not os.path.exists(salt_path):
return 'date in salt binaries does not exist: {0}'.format(salt_path)
return __salt__['cmd.run']([salt_path, '+%z'], python_shell=False)
def set_zone(timezone):
@ -196,6 +223,17 @@ def set_zone(timezone):
.. code-block:: bash
salt '*' timezone.set_zone 'America/Denver'
.. versionchanged:: 2016.11.4
.. note::
On AIX operating systems, Posix values are also allowed, see below
.. code-block:: bash
salt '*' timezone.set_zone 'CST6CDT,M3.2.0/2:00:00,M11.1.0/2:00:00'
'''
if salt.utils.which('timedatectl'):
try:
@ -203,11 +241,12 @@ def set_zone(timezone):
except CommandExecutionError:
pass
if 'Solaris' in __grains__['os_family']:
if 'Solaris' in __grains__['os_family'] or 'AIX' in __grains__['os_family']:
zonepath = '/usr/share/lib/zoneinfo/{0}'.format(timezone)
else:
zonepath = '/usr/share/zoneinfo/{0}'.format(timezone)
if not os.path.exists(zonepath):
if not os.path.exists(zonepath) and 'AIX' not in __grains__['os_family']:
return 'Zone does not exist: {0}'.format(zonepath)
if os.path.exists('/etc/localtime'):
@ -216,6 +255,18 @@ def set_zone(timezone):
if 'Solaris' in __grains__['os_family']:
__salt__['file.sed'](
'/etc/default/init', '^TZ=.*', 'TZ={0}'.format(timezone))
elif 'AIX' in __grains__['os_family']:
## timezone could be Olson or Posix
curtzstring = get_zone()
cmd = ['chtz', timezone]
result = __salt__['cmd.retcode'](cmd, python_shell=False)
if result == 0:
return True
# restore orig timezone, since AIX chtz failure sets UTC
cmd = ['chtz', curtzstring]
__salt__['cmd.retcode'](cmd, python_shell=False)
return False
else:
os.symlink(zonepath, '/etc/localtime')
@ -249,13 +300,19 @@ def zone_compare(timezone):
On Solaris-link operating systems only a string comparison is done.
.. versionchanged:: 2016.11.4
.. note::
On AIX operating systems only a string comparison is done.
CLI Example:
.. code-block:: bash
salt '*' timezone.zone_compare 'America/Denver'
'''
if 'Solaris' in __grains__['os_family']:
if 'Solaris' in __grains__['os_family'] or 'AIX' in __grains__['os_family']:
return timezone == get_zone()
curtzstring = get_zone()
@ -382,6 +439,23 @@ def get_hwclock():
.format(offset_file, exc.strerror)
)
if 'AIX' in __grains__['os_family']:
offset_file = '/etc/environment'
try:
with salt.utils.fopen(offset_file, 'r') as fp_:
for line in fp_:
if line.startswith('TZ=UTC'):
return 'UTC'
return 'localtime'
except IOError as exc:
if exc.errno == errno.ENOENT:
# offset file does not exist
return 'UTC'
raise CommandExecutionError(
'Problem reading offset file {0}: {1}'
.format(offset_file, exc.strerror)
)
def set_hwclock(clock):
'''
@ -393,6 +467,13 @@ 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']: