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 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 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