Beispiel #1
0
    def _start(self, **kwargs):
        if self.name in list_running_services():
            runtime.eerror("service `%s' is already running." % self.name)
            return

        if not self.enable:
            runtime.eerror("service `%s' is disabled." % self.name)
            return

        ENV_DICT = env.all_dict()
        ENV_DICT["DIR_BUNDLE"] = runtime.path.join(env.get("dir_bundles"), self.name)
        ENV_DICT["DIR_BUNDLE_CONFIG"] = runtime.path.join(env.get("dir_config"), self.name)
        for _dir in self.directories:
            if _dir.dir is None and _dir.name not in runtime.path.DEFAULT_DIRS.keys():
                raise Exception("Invalid default dir: `%s'" % _dir.name)

            if _dir.dir is None:
                _dir_name = "dir_%s" % _dir.name
                _dst = runtime.path.join(env.get(_dir_name), _dir.bundle)
            else:
                _dst = _dir.dir.format(**ENV_DICT)
            _dir_name = "DIR_BUNDLE_%s" % _dir.name.upper()
            ENV_DICT[_dir_name] = _dst
            if not os.path.exists(_dst):
                os.makedirs(_dst)
        for name, _dir in ENV_DICT.items():
            if name.startswith("DIR_BUNDLE") and not os.path.exists(_dir):
                os.makedirs(_dir)
        cmd = self.start.format(**ENV_DICT)
        _env = self.env
        for key, value in _env.items():
            _env[key] = value.format(**ENV_DICT)
        runtime.einfo(cmd)
        logger.debug(cmd)
        runtime.einfo("start service `%s'." % self.name)
        kwargs = {"env": _env}
        logger.debug(kwargs)
        p = subprocess.Popen(cmd.split(), **kwargs)
        runtime.einfo(p.pid)
Beispiel #2
0
def list_running_services():
    exe2services = dict([(v, k) for k, v in all_services_exe().items()])
    ENV_DICT = env.all_dict()
    rs = []
    _services = []
    for _service in store.find(Service):
        ENV_DICT["DIR_BUNDLE"] = runtime.path.join(env.get("dir_bundles"), _service.name)
        _exe = _service.start.split()[0]
        _exe = _exe.format(**ENV_DICT)
        _services.append(_exe)
    for _p in psutil.process_iter():
        try:
            _exe = _p.exe
            if _exe in _services:
                name = exe2services[_exe]
                if name in rs:
                    continue
                rs.append(name)
        except (psutil.AccessDenied, SystemError, IndexError):
            pass
        except Exception, e:
            runtime.eerror(e)
            traceback.print_exc()
Beispiel #3
0
    def _stop(self, **kwargs):
        if self.name not in list_running_services():
            runtime.eerror("service `%s' is not running." % self.name)
            return

        ENV_DICT = env.all_dict()
        ENV_DICT["DIR_BUNDLE"] = runtime.path.join(env.get("dir_bundles"), self.name)
        _real_exe = self.start.split()[0].format(**ENV_DICT)

        # for normal services
        for _p in psutil.process_iter():
            try:
                # logger.debug('%d %s' % (_p.pid, _p.exe))
                if _p.exe == _real_exe:
                    runtime.einfo(_p.pid)
                    logger.debug("kill %d [%s]" % (_p.pid, self.name))
                    _p.terminate()
                    _p.wait()
                    logger.info(_p.status)
            except (psutil.error.NoSuchProcess, psutil.AccessDenied, IndexError):
                pass
            except Exception, e:
                runtime.eerror(e)
                logger.error(traceback.format_exc())