1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

timezone: add gentoo and alpine linux support (#1722)

* add alpine linux as NosystemdTimezone

* syntax error fix and more self variables, so it works now...

* use timezone name instead of timezone path for setup-timezone command

* alpine linux zoneinfo links to /etc/zoneinfo instead of /usr/share/zoneinfo, so correct re.search() pattern

* add changelog fragment

* add gentoo linux support

* Update 1722_timezone.yml

* refactor code
This commit is contained in:
Anatoly Pugachev 2021-02-10 21:44:09 +03:00 committed by GitHub
parent af64c9a432
commit 9aef0ed17e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 5 deletions

View file

@ -0,0 +1,2 @@
minor_changes:
- timezone - add Gentoo and Alpine Linux support (https://github.com/ansible-collections/community.general/issues/781).

View file

@ -355,20 +355,26 @@ class NosystemdTimezone(Timezone):
# Validate given timezone # Validate given timezone
if 'name' in self.value: if 'name' in self.value:
tzfile = self._verify_timezone() tzfile = self._verify_timezone()
planned_tz = self.value['name']['planned']
# `--remove-destination` is needed if /etc/localtime is a symlink so # `--remove-destination` is needed if /etc/localtime is a symlink so
# that it overwrites it instead of following it. # that it overwrites it instead of following it.
self.update_timezone = ['%s --remove-destination %s /etc/localtime' % (self.module.get_bin_path('cp', required=True), tzfile)] self.update_timezone = ['%s --remove-destination %s /etc/localtime' % (self.module.get_bin_path('cp', required=True), tzfile)]
self.update_hwclock = self.module.get_bin_path('hwclock', required=True) self.update_hwclock = self.module.get_bin_path('hwclock', required=True)
distribution = get_distribution()
self.conf_files['name'] = '/etc/timezone'
self.regexps['name'] = re.compile(r'^([^\s]+)', re.MULTILINE)
self.tzline_format = '%s\n'
# Distribution-specific configurations # Distribution-specific configurations
if self.module.get_bin_path('dpkg-reconfigure') is not None: if self.module.get_bin_path('dpkg-reconfigure') is not None:
# Debian/Ubuntu # Debian/Ubuntu
if 'name' in self.value: if 'name' in self.value:
self.update_timezone = ['%s -sf %s /etc/localtime' % (self.module.get_bin_path('ln', required=True), tzfile), self.update_timezone = ['%s -sf %s /etc/localtime' % (self.module.get_bin_path('ln', required=True), tzfile),
'%s --frontend noninteractive tzdata' % self.module.get_bin_path('dpkg-reconfigure', required=True)] '%s --frontend noninteractive tzdata' % self.module.get_bin_path('dpkg-reconfigure', required=True)]
self.conf_files['name'] = '/etc/timezone'
self.conf_files['hwclock'] = '/etc/default/rcS' self.conf_files['hwclock'] = '/etc/default/rcS'
self.regexps['name'] = re.compile(r'^([^\s]+)', re.MULTILINE) elif distribution == 'Alpine' or distribution == 'Gentoo':
self.tzline_format = '%s\n' self.conf_files['hwclock'] = '/etc/conf.d/hwclock'
if distribution == 'Alpine':
self.update_timezone = ['%s -z %s' % (self.module.get_bin_path('setup-timezone', required=True), planned_tz)]
else: else:
# RHEL/CentOS/SUSE # RHEL/CentOS/SUSE
if self.module.get_bin_path('tzdata-update') is not None: if self.module.get_bin_path('tzdata-update') is not None:
@ -386,7 +392,6 @@ class NosystemdTimezone(Timezone):
except IOError as err: except IOError as err:
if self._allow_ioerror(err, 'name'): if self._allow_ioerror(err, 'name'):
# If the config file doesn't exist detect the distribution and set regexps. # If the config file doesn't exist detect the distribution and set regexps.
distribution = get_distribution()
if distribution == 'SuSE': if distribution == 'SuSE':
# For SUSE # For SUSE
self.regexps['name'] = self.dist_regexps['SuSE'] self.regexps['name'] = self.dist_regexps['SuSE']
@ -536,7 +541,9 @@ class NosystemdTimezone(Timezone):
# to other zone files, so it's hard to get which TZ is actually set # to other zone files, so it's hard to get which TZ is actually set
# if we follow the symlink. # if we follow the symlink.
path = os.readlink('/etc/localtime') path = os.readlink('/etc/localtime')
linktz = re.search(r'/usr/share/zoneinfo/(.*)', path, re.MULTILINE) # most linuxes has it in /usr/share/zoneinfo
# alpine linux links under /etc/zoneinfo
linktz = re.search(r'(?:/(?:usr/share|etc)/zoneinfo/)(.*)', path, re.MULTILINE)
if linktz: if linktz:
valuelink = linktz.group(1) valuelink = linktz.group(1)
if valuelink != planned: if valuelink != planned: