def absent(self, name): result = StateFuncResult() if not self._group_exists(name): result.success = True result.comment = 'Group {} already absent'.format(name) return result cmd_result = self.session.run_command(['delgroup', name]) result.success = cmd_result.was_successful if result.success: result.changes = 'Group {} deleted'.format(name) result.comment = cmd_result.text
def run(self, path, user=None, group=None, mode=None): result = StateFuncResult() result.changes = [] dir_exists = self.session.run_command('test -d "%s"' % path).was_successful if not dir_exists: if not self.session.is_dry_run: self.session.run_command('mkdir -p "%s"' % path) result.changes.append('created directory %r' % path) if user is not None: old_user, new_user = self.ensure_user(path, user) if old_user != new_user: result.changes.append('user changed from {} to {}'.format(old_user, new_user)) if group is not None: old_group, new_group = self.ensure_group(path, group) if old_group != new_group: result.changes.append('group changed from {} to {}'.format(old_group, new_group)) if mode is not None: old_mode, new_mode = self.ensure_mode(path, mode) if old_mode != new_mode: result.changes.append('mode changed from {} to {}'.format(old_mode, new_mode)) if not result.changes: result.comment = 'no changes to directory %r' % path result.success = True return result
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
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
def set_timezone(self, timezone): result = StateFuncResult(success=True) old_tz = self._get_tz() if old_tz == timezone: result.comment = 'timezone already set to %s' % old_tz else: result.success = self._set_tz(timezone) if result.success: result.changes = 'timezone changed from %s to %s' % (old_tz, timezone) return result
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
def present(self, name, gid=None, system=False): result = StateFuncResult() if self._group_exists(name): result.success = True result.comment = 'Group {} already exists'.format(name) return result cmd = ['addgroup'] if system: cmd.append('--system') if gid: cmd.append('--gid {}'.format(gid)) cmd.append(name) cmd_result = self.session.run_command(cmd) result.success = cmd_result.was_successful if result.success: result.changes = 'Group {} added'.format(name) result.comment = cmd_result.text return result
def running(self, session, args): result = StateFuncResult() if self.is_service_running(session, args['service']): result.success = True result.comment = 'service {} is already running'.format(args['service']) return result cmd_result = self.start_service(session, args['service']) if cmd_result.was_successful: result.success = True result.comment = 'service {} was started'.format(args['service']) return result result.success = False result.comment = 'service {} could not be started'.format(args['service']) if cmd_result.stderr: result.comment += '\n' + cmd_result.stderr if cmd_result.stdout: result.comment += '\n' + cmd_result.stdout return result
def enabled(self, session, args): result = StateFuncResult() if self.is_service_enabled(session, args['service']): result.success = True result.comment = 'service {} is already enabled'.format(args['service']) return result cmd_result = self.enable_service(session, args['service']) if cmd_result.was_successful: result.success = True result.comment = 'service {} was enabled'.format(args['service']) return result result.success = False result.comment = 'service {} could not be enabled'.format(args['service']) if cmd_result.stderr: result.comment += '\n' + cmd_result.stderr if cmd_result.stdout: result.comment += '\n' + cmd_result.stdout return result
def installed(self, args): result = StateFuncResult() if self._is_installed(args['package']): result.success = True result.comment = 'package {} is already installed'.format( args['package']) return result cmd_result = self._install(args['package']) if cmd_result.was_successful: result.success = True result.comment = 'package {} was installed'.format(args['package']) result.success = False result.comment = 'package {} could not be installed'.format( args['package']) if cmd_result.stderr: result.comment += '\n' + cmd_result.stderr if cmd_result.stdout: result.comment += '\n' + cmd_result.stdout return result
def run(self, path, source=None, content=None, user=None, group=None, mode=None): result = StateFuncResult() result.changes = [] file_exists = self.session.run_command('test -f %s' % path).exit_code == 0 tmpfile = None if source or content: tmpfile = self.write_tmpfile(source=source, content=content) if tmpfile: should_move_file = False if file_exists: diff = self.get_diff(path, tmpfile) if diff: result.changes.extend(diff) should_move_file = True else: should_move_file = True result.changes.append('created file %r' % path) if should_move_file and not self.session.is_dry_run: self.move_tmpfile(tmpfile, path) if user is not None: old_user, new_user = self.ensure_user(path, user) if old_user != new_user: result.changes.append('user changed from {} to {}'.format(old_user, new_user)) if group is not None: old_group, new_group = self.ensure_group(path, group) if old_group != new_group: result.changes.append('group changed from {} to {}'.format(old_group, new_group)) if mode is not None: old_mode, new_mode = self.ensure_mode(path, mode) if old_mode != new_mode: result.changes.append('mode changed from {} to {}'.format(old_mode, new_mode)) if not result.changes: result.comment = 'no changes to file %r' % path result.success = True return result
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