def unimplemented_error(self): system = platform.system() distribution = get_distribution() if distribution is not None: msg_platform = '%s (%s)' % (system, distribution) else: msg_platform = system self.module.fail_json( msg='hostname module cannot be used on platform %s' % msg_platform)
def __init__(self, module): super(NosystemdTimezone, self).__init__(module) # Validate given timezone if 'name' in self.value: tzfile = self._verify_timezone() # `--remove-destination` is needed if /etc/localtime is a symlink so # 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_hwclock = self.module.get_bin_path('hwclock', required=True) # Distribution-specific configurations if self.module.get_bin_path('dpkg-reconfigure') is not None: # Debian/Ubuntu if 'name' in self.value: 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) ] self.conf_files['name'] = '/etc/timezone' self.conf_files['hwclock'] = '/etc/default/rcS' self.regexps['name'] = re.compile(r'^([^\s]+)', re.MULTILINE) self.tzline_format = '%s\n' else: # RHEL/CentOS/SUSE if self.module.get_bin_path('tzdata-update') is not None: # tzdata-update cannot update the timezone if /etc/localtime is # a symlink so we have to use cp to update the time zone which # was set above. if not os.path.islink('/etc/localtime'): self.update_timezone = [ self.module.get_bin_path('tzdata-update', required=True) ] # else: # self.update_timezone = 'cp --remove-destination ...' <- configured above self.conf_files['name'] = '/etc/sysconfig/clock' self.conf_files['hwclock'] = '/etc/sysconfig/clock' try: f = open(self.conf_files['name'], 'r') except IOError as err: if self._allow_ioerror(err, 'name'): # If the config file doesn't exist detect the distribution and set regexps. distribution = get_distribution() if distribution == 'SuSE': # For SUSE self.regexps['name'] = self.dist_regexps['SuSE'] self.tzline_format = self.dist_tzline_format['SuSE'] else: # For RHEL/CentOS self.regexps['name'] = self.dist_regexps['redhat'] self.tzline_format = self.dist_tzline_format['redhat'] else: self.abort('could not read configuration file "%s"' % self.conf_files['name']) else: # The key for timezone might be `ZONE` or `TIMEZONE` # (the former is used in RHEL/CentOS and the latter is used in SUSE linux). # So check the content of /etc/sysconfig/clock and decide which key to use. sysconfig_clock = f.read() f.close() if re.search(r'^TIMEZONE\s*=', sysconfig_clock, re.MULTILINE): # For SUSE self.regexps['name'] = self.dist_regexps['SuSE'] self.tzline_format = self.dist_tzline_format['SuSE'] else: # For RHEL/CentOS self.regexps['name'] = self.dist_regexps['redhat'] self.tzline_format = self.dist_tzline_format['redhat']
def test_distro_unknown(self): with patch('ansible.module_utils.distro.id', return_value=""): assert get_distribution() == "OtherLinux"
def test_distro_known(self): with patch('ansible.module_utils.distro.id', return_value="alpine"): assert get_distribution() == "Alpine" with patch('ansible.module_utils.distro.id', return_value="arch"): assert get_distribution() == "Arch" with patch('ansible.module_utils.distro.id', return_value="centos"): assert get_distribution() == "Centos" with patch('ansible.module_utils.distro.id', return_value="clear-linux-os"): assert get_distribution() == "Clear-linux-os" with patch('ansible.module_utils.distro.id', return_value="coreos"): assert get_distribution() == "Coreos" with patch('ansible.module_utils.distro.id', return_value="debian"): assert get_distribution() == "Debian" with patch('ansible.module_utils.distro.id', return_value="linuxmint"): assert get_distribution() == "Linuxmint" with patch('ansible.module_utils.distro.id', return_value="opensuse"): assert get_distribution() == "Opensuse" with patch('ansible.module_utils.distro.id', return_value="oracle"): assert get_distribution() == "Oracle" with patch('ansible.module_utils.distro.id', return_value="raspian"): assert get_distribution() == "Raspian" with patch('ansible.module_utils.distro.id', return_value="rhel"): assert get_distribution() == "Redhat" with patch('ansible.module_utils.distro.id', return_value="ubuntu"): assert get_distribution() == "Ubuntu" with patch('ansible.module_utils.distro.id', return_value="virtuozzo"): assert get_distribution() == "Virtuozzo" with patch('ansible.module_utils.distro.id', return_value="foo"): assert get_distribution() == "Foo"
def test_get_distribution_not_linux(): """If it's not Linux, then it has no distribution""" with patch('platform.system', return_value='Foo'): assert get_distribution() is None
def test_distro_amazon_linux_long(self): with patch('ansible.module_utils.distro.id', return_value="amazon"): assert get_distribution() == "Amazon"