Ejemplo n.º 1
0
def gen_config():
    for item in os.listdir(env.get('dir_config')):
        _path = runtime.path.join(env.get('dir_config'), item)
        if os.path.isfile(_path):
            os.remove(_path)
        else:
            shutil.rmtree(_path)
    for bundle_desc in runtime.path.all_bundles_desc():
        ENV_DICT = env.all_dict()
        with open(bundle_desc, 'r') as fp:
            _bundle_desc = yaml.load(fp.read())
            _bundle = store.find(Bundle, Bundle.name == _bundle_desc['name']).one()
            # _bundle = bundle.get(_bundle_desc['name'])
            ENV_DICT.update(_bundle.env())
            for _conf in _bundle_desc.get('conf', []):
                src = _conf['src'].format(**ENV_DICT)
                dst = _conf['dst'].format(**ENV_DICT)
                dst_dir = os.path.dirname(dst)
                if not os.path.exists(dst_dir):
                    os.makedirs(dst_dir)
                content = None
                with open(src, 'r') as fp:
                    try:
                        content = template.Template(fp.read()).generate(**ENV_DICT)
                    except Exception, e:
                        print e
                        print traceback.format_exc()
                with open(dst, 'w') as fp:
                    fp.write(content)
                    # print dst
                    logger.info('genrate config file %s' % dst)
Ejemplo n.º 2
0
def all_services_exe():
    ENV_DICT = env.all_dict()
    items = {}
    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)
        items[_service.name] = _exe
    return items
Ejemplo n.º 3
0
    def directories_env(self):
        _env = {}
        ENV_DICT = env.all_dict()
        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
            _env[_dir_name.upper()] = _dst
        return _env
Ejemplo n.º 4
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)
Ejemplo n.º 5
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()
Ejemplo n.º 6
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())
Ejemplo n.º 7
0
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')

console = logging.StreamHandler()
console.setFormatter(formatter)
console.setLevel(logging.DEBUG)
logger.addHandler(console)
logger.setLevel(logging.DEBUG)

handler = logging.handlers.TimedRotatingFileHandler(
    filename=runtime.path.join(env.get('dir_log'), 'ctl.log'),
    when='midnight', backupCount=7)
handler.setFormatter(formatter)
handler.setLevel(logging.DEBUG)
logger.addHandler(handler)

logger.debug(pformat(env.all_dict()))

store = storage.get_store()

def _load_bundle(bundle_desc):
    # pprint(bundle_desc)
    print "register bundle `%s'" % bundle_desc['name']
    _bundle = store.find(Bundle, Bundle.name == bundle_desc['name']).one() or Bundle()
    _bundle.name = bundle_desc['name']
    _bundle.description = bundle_desc['description']
    store.add(_bundle)

    if 'options' in bundle_desc:
        for _option_desc in bundle_desc['options']:
            _option = store.find(Option, Option.name == _option_desc['name'], Option.bundle == bundle_desc['name']).one() or Option()
            _option.name = _option_desc['name']