Exemplo n.º 1
0
 def get_service(self, _id):
     svc = Service(self)
     svc.id = svc.name = _id
     try:
         svc.running = self._run_action(_id, 'status')
         svc.state = 'running' if svc.running else 'stopped'
     except Exception as e:
         svc.running = False
     return svc
Exemplo n.º 2
0
 def get_service(self, _id):
     svc = Service(self)
     svc.id = svc.name = _id
     try:
         svc.running = self._run_action(_id, 'status')
         svc.state = 'running' if svc.running else 'stopped'
     except:
         svc.running = False
     return svc
Exemplo n.º 3
0
    def get_service(self, _id):
        """
        Get status for one specified init script.

        :param _id: Script name
        :type _id: string
        :return: Service object
        :rtype: Service
        """

        svc = Service(self)
        svc.id = svc.name = _id
        try:
            svc.running = self._run_action(_id, 'status')
            svc.state = 'running' if svc.running else 'stopped'
        except Exception as e:
            svc.running = False
        return svc
Exemplo n.º 4
0
 def __make_service(self, info):
     svc = Service(self)
     svc.id = info['name']
     svc.name = info['name']
     svc.state = info['statename']
     svc.running = svc.state == 'RUNNING'
     return svc
Exemplo n.º 5
0
    def list(self, units=None):
        if not units:
            units = [x.split()[0] for x in subprocess.check_output(['systemctl', 'list-unit-files', '--no-legend', '--no-pager', '-la']).splitlines() if x]
            units = [x for x in units if x.endswith('.service') and '@' not in x]
            units = list(set(units))

        cmd = ['systemctl', 'show', '-o', 'json', '--full', '--all'] + units

        used_names = set()
        unit = {}
        for l in subprocess.check_output(cmd).splitlines() + [None]:
            if not l:
                if len(unit) > 0:
                    svc = Service(self)
                    svc.id = unit['Id']
                    svc.name, type = svc.id.rsplit('.', 1)

                    svc.name = svc.name.replace('\\x2d', '\x2d')
                    svc.running = unit['SubState'] == 'running'
                    svc.state = 'running' if svc.running else 'stopped'

                    if svc.name not in used_names:
                        yield svc

                    used_names.add(svc.name)
                unit = {}
            elif '=' in l:
                k, v = l.split('=', 1)
                unit[k] = v
Exemplo n.º 6
0
 def get(self, _id):
     job = UpstartJob(_id, bus=self.bus)
     svc = Service(self)
     svc.id = _id
     svc.name = self.__fix_name(_id)
     try:
         svc.state = job.get_status()['state']
         svc.running = svc.state == 'running'
     except:
         svc.running = False
     return svc
Exemplo n.º 7
0
    def list(self, units=None):
        """
        Generator of all units service in systemd.

        :param units: List of services names
        :type units: list of strings
        :return: Service object
        :rtype: Service
        """

        if not units:
            units = [x.split()[0] for x in subprocess.check_output(['systemctl', 'list-unit-files', '--no-legend', '--no-pager', '-la']).decode().splitlines() if x]
            units = [x for x in units if x.endswith('.service') and '@.service' not in x]
            units = list(set(units))

        cmd = ['systemctl', 'show', '-o', 'json', '--full', '--all'] + units

        used_names = set()
        unit = {}
        for l in subprocess.check_output(cmd).decode().splitlines() + [None]:
            if not l:
                if len(unit) > 0:
                    svc = Service(self)
                    svc.id = unit['Id']
                    svc.name, _ = svc.id.rsplit('.', 1)

                    svc.name = svc.name.replace('\\x2d', '\x2d')
                    svc.running = unit['SubState'] == 'running'
                    svc.state = 'running' if svc.running else 'stopped'
                    svc.enabled = unit['UnitFileState'] == 'enabled'
                    svc.static = unit['UnitFileState'] == 'static'

                    if svc.name not in used_names:
                        yield svc

                    used_names.add(svc.name)
                unit = {}
            elif '=' in l:
                k, v = l.split('=', 1)
                unit[k] = v
Exemplo n.º 8
0
 def get_service(self, _id):
     job = UpstartJob(_id, bus=self.bus)
     svc = Service(self)
     svc.id = _id
     svc.name = self.__fix_name(_id)
     try:
         svc.state = job.get_status()['state']
         svc.running = svc.state == 'running'
     except:
         svc.running = False
     return svc
Exemplo n.º 9
0
 def list(self):
     for l in subprocess.check_output(['systemctl', 'list-units',
                                       '-la']).splitlines()[1:]:
         if not l:
             break
         tokens = l.split(None, 4)
         if len(tokens) != 5:
             continue
         svc = Service(self)
         svc.id, load_state, active_state, sub_state, name = tokens
         svc.name, type = svc.id.rsplit('.', 1)
         svc.name = svc.name.replace('\\x2d', '\x2d')
         if type != 'svc':
             continue
         svc.running = sub_state == 'running'
         svc.state = 'running' if svc.running else 'stopped'
         yield svc
Exemplo n.º 10
0
    def get_service(self, _id):
        """
        Get informations from module upstart for one specified job.

        :param _id: Job name
        :type _id: string
        :return: Service object
        :rtype: Service
        """

        job = UpstartJob(_id, bus=self.bus)
        svc = Service(self)
        svc.id = _id
        svc.name = self.__fix_name(_id)
        try:
            svc.state = job.get_status()['state']
            svc.running = svc.state == 'running'
        except Exception as e:
            svc.running = False
        return svc
Exemplo n.º 11
0
    def list(self, units=None):
        if not units:
            units = [
                x.split()[0] for x in subprocess.check_output([
                    'systemctl', 'list-unit-files', '--no-legend',
                    '--no-pager', '-la'
                ]).splitlines() if x
            ]
            units = [
                x for x in units if x.endswith(b'.service') and b'@' not in x
            ]
            units = list(set(units))

        cmd = ['systemctl', 'show', '-o', 'json', '--full', '--all'] + units

        used_names = set()
        unit = {}
        for l in subprocess.check_output(cmd).splitlines() + [None]:
            if not l:
                if len(unit) > 0:
                    svc = Service(self)
                    svc.id = unit[b'Id']
                    svc.name, type = svc.id.rsplit(b'.', 1)

                    svc.name = svc.name.replace(b'\\x2d', b'\x2d')
                    svc.running = unit[b'SubState'] == b'running'
                    svc.state = b'running' if svc.running else b'stopped'

                    if svc.name not in used_names:
                        yield svc

                    used_names.add(svc.name)
                unit = {}
            elif b'=' in l:
                k, v = l.split(b'=', 1)
                unit[k] = v