Example #1
0
 def load_config(self):
     return skytools.Config(self.service_name,
                            None,
                            user_defs={
                                'use_skylog': '0',
                                'job_name': 'db_upgrade'
                            })
Example #2
0
File: server.py Project: postsql/cc
    def startup(self):
        """Setup sockets and handlers."""

        super(CCServer, self).startup()

        self.log.info("C&C server version %s starting up..", self.__version__)

        self.xtx = CryptoContext(self.cf)
        self.zctx = zmq.Context(self.zmq_nthreads)
        self.ioloop = IOLoop.instance()

        self.local_url = self.cf.get('cc-socket')

        self.cur_role = self.cf.get('cc-role', 'insecure')
        if self.cur_role == 'insecure':
            self.log.warning(
                'CC is running in insecure mode, please add "cc-role = local" or "cc-role = remote" option to config'
            )

        self.stat_level = self.cf.getint('cc-stats', 1)
        if self.stat_level < 1:
            self.log.warning('CC statistics level too low: %d',
                             self.stat_level)

        # initialize local listen socket
        s = self.zctx.socket(zmq.XREP)
        s.setsockopt(zmq.LINGER, self.zmq_linger)
        s.setsockopt(zmq.HWM, self.zmq_hwm)
        if self.zmq_rcvbuf > 0:
            s.setsockopt(zmq.RCVBUF, self.zmq_rcvbuf)
        if self.zmq_sndbuf > 0:
            s.setsockopt(zmq.SNDBUF, self.zmq_sndbuf)
        if self.zmq_tcp_keepalive > 0:
            if getattr(zmq, 'TCP_KEEPALIVE', -1) > 0:
                s.setsockopt(zmq.TCP_KEEPALIVE, self.zmq_tcp_keepalive)
                s.setsockopt(zmq.TCP_KEEPALIVE_INTVL,
                             self.zmq_tcp_keepalive_intvl)
                s.setsockopt(zmq.TCP_KEEPALIVE_IDLE,
                             self.zmq_tcp_keepalive_idle)
                s.setsockopt(zmq.TCP_KEEPALIVE_CNT, self.zmq_tcp_keepalive_cnt)
            else:
                self.log.info("TCP_KEEPALIVE not available")
        s.bind(self.local_url)
        self.local = CCStream(s, self.ioloop, qmaxsize=self.zmq_hwm)
        self.local.on_recv(self.handle_cc_recv)

        self.handlers = {}
        self.routes = {}
        rcf = skytools.Config('routes', self.cf.filename, ignore_defs=True)
        for r, hnames in rcf.cf.items('routes'):
            self.log.info('New route: %s = %s', r, hnames)
            for hname in [hn.strip() for hn in hnames.split(',')]:
                h = self.get_handler(hname)
                self.add_handler(r, h)

        self.stimer = PeriodicCallback(self.send_stats, 30 * 1000, self.ioloop)
        self.stimer.start()
Example #3
0
    def load_config(self):
        """Loads and returns skytools.Config instance.

        By default it uses first command-line argument as config
        file name.  Can be overridden.
        """

        if len(self.args) < 1:
            print("need config file, use --help for help.")
            sys.exit(1)
        conf_file = self.args[0]
        return skytools.Config(self.service_name, conf_file,
                               user_defs = self.cf_defaults,
                               override = self.cf_override)
Example #4
0
    def load_config(self):
        """ Load and return skytools.Config instance. """

        cf = skytools.Config('ccserver', self.args[0])
        self.cc_jobname = cf.get('job_name')
        self.cc_url = cf.get('cc-socket')

        if len(self.args) > 1:
            self.service_name = self.args[1]

        self.cf_defaults = make_job_defaults(cf, self.service_name)

        cf = super(CCJob, self).load_config()
        return cf
Example #5
0
    def cmd_status(self):
        for job in self.job_list:
            os.chdir(job['cwd'])
            cf = skytools.Config(job['service'], job['config'])
            pidfile = cf.getfile('pidfile', '')
            name = job['job_name']
            svc = job['service']
            if job['disabled']:
                name += "  (disabled)"

            if not pidfile:
                print(" pidfile? [%s] %s" % (svc, name))
            elif os.path.isfile(pidfile):
                print(" OK       [%s] %s" % (svc, name))
            else:
                print(" STOPPED  [%s] %s" % (svc, name))
Example #6
0
 def add_job(self, cf_file, service_name):
     svc = self.svc_map[service_name]
     cf = skytools.Config(service_name, cf_file)
     disabled = svc['disabled']
     if not disabled:
         disabled = cf.getboolean('disabled', 0)
     job = {
         'disabled': disabled,
         'config': cf_file,
         'cwd': svc['cwd'],
         'script': svc['script'],
         'args': svc['args'],
         'service': svc['service'],
         'job_name': cf.get('job_name'),
         'pidfile': cf.getfile('pidfile', ''),
     }
     self.job_list.append(job)
     self.job_map[job['job_name']] = job
Example #7
0
    def cmd_status(self, jobs):
        for jn in jobs:
            try:
                job = self.job_map[jn]
            except KeyError:
                self.log.error("Unknown job: %s", jn)
                continue
            os.chdir(job['cwd'])
            cf = skytools.Config(job['service'], job['config'])
            pidfile = job['pidfile']
            name = job['job_name']
            svc = job['service']
            if job['disabled']:
                name += "  (disabled)"

            if not pidfile:
                print(" pidfile? [%s] %s" % (svc, name))
            elif os.path.isfile(pidfile):
                print(" OK       [%s] %s" % (svc, name))
            else:
                print(" STOPPED  [%s] %s" % (svc, name))
Example #8
0
File: jobmgr.py Project: postsql/cc
 def add_job(self, jname, defs):
     jcf = skytools.Config(jname, self.cf.filename, user_defs=defs)
     j = JobState(jname, jcf, self.local_url, self.ioloop, self.xtx)
     self.jobs[jname] = j
     j.start(self.job_args_extra)
Example #9
0
    def startup(self):
        """Setup sockets and handlers."""

        super(CCServer, self).startup()

        self.log.info("C&C server version %s starting up..", self.__version__)

        self.xtx = CryptoContext(self.cf)
        self.zctx = zmq.Context(self.zmq_nthreads)
        self.ioloop = IOLoop.instance()

        self.local_url = self.cf.get('cc-socket')

        self.cur_role = self.cf.get('cc-role', 'insecure')
        if self.cur_role == 'insecure':
            self.log.warning(
                'CC is running in insecure mode, please add "cc-role = local" or "cc-role = remote" option to config'
            )

        self.stat_level = self.cf.getint('cc-stats', 1)
        if self.stat_level < 1:
            self.log.warning('CC statistics level too low: %d',
                             self.stat_level)

        self.infofile = self.cf.getfile('infofile', '')
        self.infofile_level = self.cf.getint('infofile-level', 2)
        if self.infofile_level >= 3:
            self.stats_deque_bucket = 5  # seconds
            self.stats_deque_cursor = int(time.time() /
                                          self.stats_deque_bucket)
            self.stats_timespans = [1 * 60, 5 * 60, 15 * 60]  # seconds
            assert sum([
                ts % self.stats_deque_bucket for ts in self.stats_timespans
            ]) == 0
            self.stats_deque_window = max(
                self.stats_timespans) / self.stats_deque_bucket + 1
            self.stats_deque = deque([{}
                                      for i in range(self.stats_deque_window)],
                                     maxlen=self.stats_deque_window)
        self.stats_total = {}

        # initialize local listen socket
        s = self.zctx.socket(zmq.XREP)
        s.setsockopt(zmq.LINGER, self.zmq_linger)
        try:
            s.setsockopt(zmq.HWM, self.zmq_hwm)
        except AttributeError:
            s.set_hwm(self.zmq_hwm)
        if self.zmq_rcvbuf > 0:
            s.setsockopt(zmq.RCVBUF, self.zmq_rcvbuf)
        if self.zmq_sndbuf > 0:
            s.setsockopt(zmq.SNDBUF, self.zmq_sndbuf)
        if self.zmq_tcp_keepalive > 0:
            if getattr(zmq, 'TCP_KEEPALIVE', -1) > 0:
                s.setsockopt(zmq.TCP_KEEPALIVE, self.zmq_tcp_keepalive)
                s.setsockopt(zmq.TCP_KEEPALIVE_INTVL,
                             self.zmq_tcp_keepalive_intvl)
                s.setsockopt(zmq.TCP_KEEPALIVE_IDLE,
                             self.zmq_tcp_keepalive_idle)
                s.setsockopt(zmq.TCP_KEEPALIVE_CNT, self.zmq_tcp_keepalive_cnt)
            else:
                self.log.info("TCP_KEEPALIVE not available")
        s.bind(self.local_url)
        self.local = CCStream(s, self.ioloop, qmaxsize=self.zmq_hwm)
        self.local.on_recv(self.handle_cc_recv)

        self.handlers = {}
        self.routes = {}
        rcf = skytools.Config('routes', self.cf.filename, ignore_defs=True)
        for r, hnames in rcf.cf.items('routes'):
            self.log.info('New route: %s = %s', r, hnames)
            for hname in [hn.strip() for hn in hnames.split(',')]:
                h = self.get_handler(hname)
                self.add_handler(r, h)

        self.stats_period = self.cf.getint('stats-period', 30)
        self.stimer = PeriodicCallback(self.send_stats,
                                       self.stats_period * 1000, self.ioloop)
        self.stimer.start()