Exemple #1
0
    def ensure_present(self, key, user):
        result = StateFuncResult(success=True)

        getent = self.run_command(['getent', 'passwd', user])
        if not getent.was_successful:
            result.merge_with_cmd_result(getent)
            return result

        home_dir = getent.stdout.split(':')[5]
        authed_keys_path = os.path.join(home_dir, '.ssh', 'authorized_keys')

        enc, key, comment = self.parse_key(key)
        if self.key_exists(authed_keys_path, enc, key):
            result.comment = 'key already exists'
            return result

        key_str = '%s %s' % (enc, key)
        key_str_short = '%s %s' % (enc, key[:8] + '...')
        if comment:
            key_str += ' %s' % comment
            key_str_short += ' %s' % comment
        self.run_command('echo "%s" >> %s' % (key_str, authed_keys_path))
        result.changes = 'added public key %r' % key_str_short

        return result
Exemple #2
0
    def install_locale(self, locale):
        result = StateFuncResult(success=True)
        localegen_path = '/etc/locale.gen'

        for variation in _locale_variations(locale):
            search_result = self.run_command('locale -a | grep -P "^%s"' %
                                             variation.replace('.', '\.'))
            if search_result.was_successful:
                result.comment = 'locale already present'
                return result

        result.changes = []

        locale_exists_in_localegen = False
        for variation in _locale_variations(locale):
            search_result = self.run_command(
                'grep -P "^%s" %s' %
                (variation.replace('.', '\.'), localegen_path))
            if search_result.was_successful:
                locale_exists_in_localegen = True
                locale = variation
                break

        if not locale_exists_in_localegen:
            commented_locale_exists = False

            for variation in _locale_variations(locale):
                search_result = self.run_command(
                    'grep -P "^#\s*%s" %s' %
                    (variation.replace('.', '\.'), localegen_path))
                if search_result.was_successful:
                    commented_locale_exists = True
                    locale = variation
                    break

            if commented_locale_exists:
                sed_cmd = 'sed -r -i.bak "s/^#\s*(%s.*)/\1/" %s' % (
                    locale.replace('.', '\.'), localegen_path)
                sed_result = self.run_command(sed_cmd)
                if not sed_result.was_successful:
                    result.success = False
                    result.stderr = sed_result.stderr
                    return result
                result.changes.append('uncommented locale %r from %s' %
                                      (locale, localegen_path))
            else:
                self.run_command('echo "%s" >> %s' %
                                 (_full_locale(locale), localegen_path))
                result.changes.append('added locale %r to %s' %
                                      (locale, localegen_path))

        localegen_result = self.run_command('locale-gen')
        result.merge_with_cmd_result(localegen_result)

        return result
Exemple #3
0
	def run(self, path):
		result = StateFuncResult(success=True)
		exists = self.session.run_command('test -e "%s"' % path).was_successful
		if not exists:
			result.comment = 'file already absent: %r' % path
			return result

		if not self.session.is_dry_run:
			rm_result = self.session.run_command('rm -rfv "%s"' % path)
			result.merge_with_cmd_result(rm_result)
		return result
Exemple #4
0
    def ensure_absent(self, key, user):
        result = StateFuncResult(success=True)

        getent = self.run_command(['getent', 'passwd', user])
        if not getent.was_successful:
            result.merge_with_cmd_result(getent)
            return result

        home_dir = getent.stdout.split(':')[5]
        authed_keys_path = os.path.join(home_dir, '.ssh', 'authorized_keys')

        enc, key, comment = self.parse_key(key)
        if not self.key_exists(authed_keys_path, enc, key):
            result.comment = 'key is already absent'
            return result

        key_str = '%s %s' % (enc, key)
        key_str_short = '%s %s' % (enc, key[:8] + '...')
        self.run_command('sed -r -i.bak "/^%s(\s+.*)?$/d" %s' %
                         (key_str, authed_keys_path))
        result.changes = 'removed public key %r' % key_str_short

        return result