def handler(configRegistry, changes):
    log = getLogger(__name__)

    log.debug('Loading service information...')
    si = ServiceInfo()
    for name in si.get_services():
        service = si.get_service(name)
        if not service:
            log.debug('Service not found: %s', name)
            continue

        try:
            var = service['start_type']
            unit = service.get('systemd', '%s.service' % (name, ))
        except KeyError:
            log.debug('Incomplete service information: %s', service)
            continue

        if var not in changes:
            log.debug('Not changed: %s', name)
            continue

        if configRegistry.is_false(var, False):
            log.info('Disabling %s...', unit)
            ctl('disable', unit)
            ctl('mask', unit)
        elif configRegistry.get(var, '').lower() == 'manually':
            log.info('Manual %s...', unit)
            ctl('unmask', unit)
            ctl('disable', unit)
        else:
            log.info('Enabling %s...', unit)
            ctl('unmask', unit)
            ctl('enable', unit)
    def query(self, pattern):
        ucr.load()
        srvs = ServiceInfo()

        lang = _.im_self.locale.language
        if lang in (None, 'C'):
            lang = 'en'

        result = []
        for name, srv in srvs.services.items():
            key = srv.get('start_type', '%s/autostart' % (name, ))
            entry = {
                'service':
                name,
                'description':
                srv.get('description[%s]' % (lang, ), srv.get('description')),
                'autostart':
                ucr.get(key, 'yes'),
                'isRunning':
                srv.running,
            }
            if entry['autostart'] not in ('yes', 'no', 'manually'):
                entry['autostart'] = 'yes'
            for value in entry.values():
                if pattern.match(str(value)):
                    result.append(entry)
                    break
        return result
	def _change_start_type(self, service_names, start_type):
		service_info = ServiceInfo()
		services = [(service_name, service_info.services[service_name]) for service_name in service_names if service_name in service_info.services]
		values = ['%s=%s' % (service.get('start_type', '%s/autostart' % (service_name,)), start_type) for service_name, service in services]
		univention.config_registry.handler_set(values)
		failed = [x for x in service_names if not service_info.services.get(x)]
		if failed:
			raise UMC_Error('%s %s' % (_('Could not change start type of the following services:'), ', '.join(failed)))
def handler(configRegistry, changes):
    log = getLogger(__name__)

    log.debug('Loading service informations...')
    si = ServiceInfo()
    for name in si.get_services():
        service = si.get_service(name)
        if not service:
            log.debug('Service not found: %s', name)
            continue

        try:
            var = service['start_type']
            unit = service.get('systemd', '%s.service' % (name, ))
        except KeyError:
            log.debug('Incomplete service information: %s', service)
            continue

        try:
            old, new = changes[var]
            old = check(old)
            new = check(new)
        except KeyError:
            log.debug('Not changed: %s', name)
            continue

        if old == new:
            log.debug('No change: %s: %s', name, old)
            continue

        if new == NO:
            log.info('Disabling %s...', unit)
            ctl('disable', unit)
            ctl('mask', unit)
        elif new == MANUALLY:
            log.info('Manual %s...', unit)
            ctl('unmask', unit)
            ctl('disable', unit)
        elif new == YES:
            log.info('Enabling %s...', unit)
            ctl('unmask', unit)
            ctl('enable', unit)
        else:
            log.error('Unknown mode %s for %s', new, unit)
    def _change_services(self, services, action):
        error_messages = []
        srvs = ServiceInfo()
        for srv in services:
            service = srvs.get_service(srv)
            try:
                getattr(service, action)()
            except ServiceError as exc:
                MODULE.warn('Error during %s of %s: %s' % (action, srv, exc))
                error_messages.append(
                    '%s\n%s' %
                    ({
                        'start': _('Starting the service %s failed:'),
                        'stop': _('Stopping the service %s failed:'),
                        'restart': _('Restarting the service %s failed:'),
                    }[action] % srv, exc))

        if error_messages:
            raise UMC_Error('\n\n'.join(error_messages))
        return {'success': True}
Example #6
0
def test_check_univention_service():
	services_info = ServiceInfo()
	service_problems = services_info.check_services()
	print('Services with problems:\n{}'.format(service_problems))
	assert len(service_problems) == 0