Exemple #1
0
def check_leader():
    invokeCommand = InvokeCommand()
    zk_address, zk_port = get_zk_address()
    cmd = "echo stat |nc %s %s| grep Mode" % (zk_address, zk_port)
    ret_str, _ = invokeCommand._runSysCmd(cmd)
    invokeCommand = None
    if ret_str.find('leader') == -1:
        return False

    return True
    def _send_email(self, data_node_ip, text):
        try:
            # send email
            host_ip = getHostIp()
            invokeCommand = InvokeCommand()
            cmd_str = "rpm -qa beehive"
            version_str = invokeCommand._runSysCmd(cmd_str)
            subject = "[%s] %s" % (data_node_ip, text)

            body = "[%s] %s" % (data_node_ip, text)
            body += "\n" + version_str[0] + "\nip:" + host_ip

            #            email_from = "%s <noreply@%s>" % (options.sitename, options.domain)
            if options.send_email_switch:
                send_email(options.admins, subject, body)
        except Exception, e:
            logging.error("send email process occurs error", e)
    def _send_error_email(self, exception):
        try:
            # send email
            host_ip = getHostIp()
            invokeCommand = InvokeCommand()
            cmd_str = "rpm -qa beehive"
            version_str = invokeCommand._runSysCmd(cmd_str)
            subject = "[%s]Internal Server Error" % options.sitename

            exception += "\n" + version_str[0] + "\nhost ip :" + host_ip

            #            body = self.render_string("errors/500_email.html", exception=exception)

            #            email_from = "%s <noreply@%s>" % (options.sitename, options.domain)
            if options.send_email_switch:
                send_email(options.admins, subject, exception)
        except Exception:
            logging.error(traceback.format_exc())
Exemple #4
0
    def _send_error_email(self, exception):
        try:
            local_ip = get_host_ip()
            invokeCommand = InvokeCommand()
            cmd_str = "rpm -qa jetty-manager"
            version_str = invokeCommand._runSysCmd(cmd_str)
            logging.info("version_str :" + str(version_str))
            # send email
            subject = "[%s]Internal Server Error " % options.sitename
            body = self.render_string("errors/500_email.html",
                                      exception=exception)

            body += "\n" + version_str[0] + "\nip:" + local_ip

            #            email_from = "%s <noreply@%s>" % (options.sitename, options.domain)
            if options.send_email_switch:
                send_email(options.admins, subject, body)
        except Exception:
            logging.error(traceback.format_exc())
Exemple #5
0
    def statistic(self):
        ivk_cmd = InvokeCommand()
        nsenter = options.nsenter % self._container_id
        cmd = join(nsenter, "netstat -i")
        content = ivk_cmd._runSysCmd(cmd)[0]
        trx_list = re.findall(
            '.*pbond0\s+\d+\s+\d+\s+(\d+)\s+\d+\s+\d+\s+\d+\s+(\d+).*', content)

        RX_SUM, TX_SUM = 0, 0
        for RX, TX in trx_list:
            RX_SUM += int(RX)
            TX_SUM += int(TX)

        rx_sum = RX_SUM
        tx_sum = TX_SUM

        if self._total_tx and self._total_rx:
            self._tx = tx_sum - self._total_tx
            self._rx = rx_sum - self._total_rx

        self._total_rx = rx_sum
        self._total_tx = tx_sum
Exemple #6
0
def get_host_ip():
    cmd = """printenv |grep ^HOST_IP |awk -F= '{print $2}' """
    invokeCommand = InvokeCommand()
    ret_str, _ = invokeCommand._runSysCmd(cmd)
    invokeCommand = None
    return ret_str
Exemple #7
0
class NodeOpers(AbstractOpers):
    '''
    classdocs
    '''
    invokeCommand = InvokeCommand()

    def __init__(self):
        '''
        Constructor
        '''

    def start(self):
        ret_val = os.system(options.start_moxi)

        result = {}
        if ret_val != 0:
            result.setdefault("message", "start moxi failed")
        else:
            #container_name = retrieve_node_name()
            #zkOper = Common_ZkOpers()
            #zkOper.write_started_node(container_name)
            result.setdefault("message", "start moxi successfully")

        return result

    def stop(self):
        ret_val = os.system(options.stop_moxi)

        result = {}
        if ret_val != 0:
            result.setdefault("message", "stop moxi failed")
        else:
            #container_name = retrieve_node_name()
            #zkOper = Common_ZkOpers()
            #zkOper.remove_started_node(container_name)
            result.setdefault("message", "stop moxi successfully")

        return result

    def reload(self):
        ret_val = os.system(options.reload_moxi)

        result = {}
        if ret_val != 0:
            result.setdefault("message", "reload moxi failed")
        else:
            result.setdefault("message", "reload moxi successfully")
            #container_name = retrieve_node_name()
            #zkOper = Common_ZkOpers()
            #zkOper.write_started_node(container_name)

        return result

    def config(self, params):
        _cbase_host = params.get('CBASE_HOST')
        _cbase_bucket = params.get('CBASE_BUCKET')
        _cbase_pwd = params.get('CBASE_PWD')
        #cbase_host_list = _cbase_host.split(',') if ',' in _cbase_host else [_cbase_host]

        with open("/etc/sysconfig/moxi", 'w') as moxi:
            message = "CBASE_HOST='{0}'\nCBASE_BUCKET='{1}'\nCBASE_PWD='{2}'\nUSER='******'\nMAXCONN='1024'\nCPROXY_ARG='/etc/moxi.conf'\nOPTIONS=''".format(
                _cbase_host, _cbase_bucket, _cbase_pwd)
            moxi.write(message)

        self.invokeCommand._runSysCmd(options.reload_moxi)
        result = {}
        result.setdefault("message", "node config successfully")
        return result

    def nodestatus(self):
        ret_val = os.system(options.status_moxi)

        result = {}
        if ret_val != 0:
            result.setdefault("message", "moxi status failed")
        else:
            result.setdefault("message", "moxi status successfully")

        return result
Exemple #8
0
class NodeOpers(AbstractOpers):
    '''
    classdocs
    '''
    invokeCommand = InvokeCommand()
    confOpers = ConfigFileOpers()

    def __init__(self):
        '''
        Constructor
        '''

    def createNode(self, params):
        if params == {} or params is None:
            raise UserVisiableException("please set the componentNode info!")

        dataNodeInternalPort = params.get('dataNodeInternalPort')
        if dataNodeInternalPort is not None:
            raise UserVisiableException(
                "no need to set the dataNodeInternalPort param!")

        zkOper = ZkOpers()

        try:
            local_uuid = getClusterUUID()
            existCluster = zkOper.existCluster(local_uuid)
            if not existCluster:
                raise UserVisiableException(
                    "sync componentCluster info error! please check if sync uuid is right!"
                )

            params.setdefault("dataNodeInternalPort", options.port)
            dataNodeExternalPort = params.get('dataNodeExternalPort')
            if dataNodeExternalPort is None or '' == dataNodeExternalPort:
                params.setdefault("dataNodeExternalPort", options.port)

            self.confOpers.setValue(options.data_node_property, params)
            dataNodeProprs = self.confOpers.getValue(
                options.data_node_property)
            zkOper.writeDataNodeInfo(local_uuid, dataNodeProprs)

        finally:
            zkOper.close()

        result = {}
        result.setdefault(
            "message",
            "Configuration on this componentNode has been done successfully")
        return result

    def startNode(self):
        _, ret_val = self.invokeCommand._runSysCmd(options.start_jetty)

        result = {}
        if ret_val != 0:
            result.setdefault("message", "start jetty failed")
        else:
            container_name = retrieve_node_name()
            zkOper = ZkOpers()
            try:
                zkOper.write_started_node(container_name)
            finally:
                zkOper.close()

            result.setdefault("message", "start jetty successfully")

        return result

    def stopNode(self):
        _, ret_val = self.invokeCommand._runSysCmd(options.stop_jetty)

        result = {}
        if ret_val != 0:
            result.setdefault("message", "stop jetty failed")
        else:
            container_name = retrieve_node_name()

            zkOper = ZkOpers()
            try:
                zkOper.remove_started_node(container_name)
            finally:
                zkOper.close()

            result.setdefault("message", "stop jetty successfully")

        return result
Exemple #9
0
class NodeOpers(AbstractOpers):
    '''
    classdocs
    '''
    invokeCommand = InvokeCommand()
    confOpers = ConfigFileOpers()
    base_config_path = '/etc/nginx/'
    man = ConfigOpers(base_config_path)

    def __init__(self):
        '''
        Constructor
        '''

    def create(self, params):
        if params == {} or params is None:
            raise UserVisiableException("please set the componentNode info!")

        dataNodeInternalPort = params.get('dataNodeInternalPort')
        if dataNodeInternalPort is not None:
            raise UserVisiableException(
                "no need to set the dataNodeInternalPort param!")

        zkOper = Common_ZkOpers()

        local_uuid = getClusterUUID()
        existCluster = zkOper.existCluster(local_uuid)
        if not existCluster:
            raise UserVisiableException(
                "sync componentCluster info error! please check if sync uuid is right!"
            )

        params.setdefault("dataNodeInternalPort", options.port)
        dataNodeExternalPort = params.get('dataNodeExternalPort')
        if dataNodeExternalPort is None or '' == dataNodeExternalPort:
            params.setdefault("dataNodeExternalPort", options.port)

        self.confOpers.setValue(options.data_node_property, params)
        dataNodeProprs = self.confOpers.getValue(options.data_node_property)
        zkOper.writeDataNodeInfo(local_uuid, dataNodeProprs)

        result = {}
        result.setdefault(
            "message",
            "Configuration on this componentNode has been done successfully")
        return result

    def start(self):
        _, ret_val = self.invokeCommand._runSysCmd(options.start_nginx)

        result = {}
        if ret_val != 0:
            result.setdefault("message", "start nginx failed")
        else:
            container_name = retrieve_node_name()
            zkOper = Common_ZkOpers()
            zkOper.write_started_node(container_name)
            result.setdefault("message", "start nginx successfully")

        return result

    def stop(self):
        _, ret_val = self.invokeCommand._runSysCmd(options.stop_nginx)

        result = {}
        if ret_val != 0:
            result.setdefault("message", "stop nginx failed")
        else:
            container_name = retrieve_node_name()
            zkOper = Common_ZkOpers()
            zkOper.remove_started_node(container_name)
            result.setdefault("message", "stop nginx successfully")

        return result

    def reload(self):
        _, ret_val = self.invokeCommand._runSysCmd(options.reload_nginx)

        result = {}
        if ret_val != 0:
            result.setdefault("message", "reload nginx failed")
        else:
            result.setdefault("message", "reload nginx successfully")
            container_name = retrieve_node_name()
            zkOper = Common_ZkOpers()
            zkOper.write_started_node(container_name)
        return result

    def config(self, params):

        _upstream_name = params.get('upstreamName')
        _servers_ports = params.get('serverPorts')
        _cluster = params.get('containerClusterName')
        server_list = _servers_ports.split(',') if ',' in _servers_ports else [
            _servers_ports
        ]
        upstream = UpStream(_upstream_name, server_list)
        self.man.save_upstream(upstream)
        self.man.enable_server(os.path.basename(self.man.upstream_file_path))

        server = self.__get_server(_upstream_name)
        filename = '%ssites-available/%s.conf' % (self.base_config_path,
                                                  _cluster)
        set_file_data(filename, str(server), 'w')
        self.man.enable_server('%s.conf' % _cluster)
        self.invokeCommand._runSysCmd(options.reload_nginx)

        result = {}
        result.setdefault("message", "node config upstream successfully")
        return result

    def __get_server(self, upstream_name):

        server = Server(port=8001,
                        server_names=['webportal-app'],
                        params={'error_page': '500 502 503 504  /50x.html'})

        location = Location('/',
                            params={
                                'proxy_pass': '******' % upstream_name,
                                'proxy_set_header': 'Host rds.et.letv.com',
                                'proxy_redirect': 'off',
                                'index': 'index.html index.htm'
                            })

        server.add_location(location=location)

        location = Location('= /50x.html',
                            params={'root': '/usr/share/nginx/html'})
        server.add_location(location=location)

        return server

    def enable(self):
        files = os.listdir(self.man.sites_available)

        for _file in files:
            self.man.enable_server(_file)

        self.invokeCommand._runSysCmd(options.reload_nginx)

    def disable(self):

        files = os.listdir(self.man.sites_available)

        for _file in files:
            self.man.disable_server(_file)

        self.invokeCommand._runSysCmd(options.reload_nginx)