Example #1
0
def get_cached_components(node_uuid, name, upgrade_version=None):
    """

    :param node_uuid:
    :param name: u_type
    :param upgrade_version: v2.23.1
    :return:
    """
    cached_key = node_components_cache_key.format(**{
        "node_uuid": node_uuid,
        "component_name": name
    })
    components = cache.get(cached_key, [])
    for comp in components:
        if upgrade_version:
            # 检查版本
            file_version = get_version(upgrade_version, 'int')
            self_version = get_version(comp['version'], 'int')
            status = get_single_node_status(file_version, self_version)
        else:
            status = 0
        comp.update({
            "is_self": 0,
            'u_type': name,
            "status": status,
            "uuid": node_uuid
        })

    return components
Example #2
0
def judge_status(self_node, file_info, parent_node=None, child_node=None):
    """
    判断状态, 假定 parent_version >= self_version >= child_version

    :param self_node: 包含 version 的字典
    :param file_info: 包含 version,u_type 的字典和 u_type
    :param parent_node: 包含 version 的字典
    :param child_node: 包含 version 的字典
    :return:

        0: 不可升级
        1: 可升级
        2: 已升级到相应版本
        3: 上级未升级
    """
    parent_status, self_status, child_status = (0, 0, 0)
    if file_info['u_type'] == 'center':
        # 可比较版本
        self_version = get_version(self_node["version"], 'int')
        file_version = get_version(file_info["version"], 'int')

        # 不可升级:1. 文件版本小 2.上级限制
        # 上级
        if parent_node:
            parent_version = get_version(parent_node["version"], 'int')
            if file_version > parent_version:
                # 上级未升级
                parent_status = 3
                self_status = 0
            elif file_version == parent_version:
                # 本级正常检查
                parent_status = 2
                self_status = get_single_node_status(file_version,
                                                     self_version)
            else:
                # file_version < parent_version:
                parent_status = 0
                self_status = get_single_node_status(file_version,
                                                     self_version)
        else:
            self_status = get_single_node_status(file_version, self_version)

        # 有下级,下级根据本级的升级状态确定即可
        if child_node:
            child_version = get_version(child_node["version"], 'int')
            if self_status in [2]:
                child_status = get_single_node_status(file_version,
                                                      child_version)
            else:
                child_status = 0
    elif file_info['u_type'] == 'vul':
        # 漏洞库暂不需要限制版本
        return (1, 1, 1)
    return parent_status, self_status, child_status
Example #3
0
def clean_appliance(ssh_client, dbsync_local_uninstall=True):
    starttime = time.time()
    ssh_client.run_command('service evmserverd stop')
    ssh_client.run_command('sync; sync; echo 3 > /proc/sys/vm/drop_caches')
    ssh_client.run_command('service collectd stop')
    ver = get_version()
    if ver == '56' or ver == '55':
        ssh_client.run_command('service rh-postgresql94-postgresql restart')
        if dbsync_local_uninstall:
            ssh_client.run_rake_command('evm:dbsync:local_uninstall')
    elif ver == '57' or ver == '58' or ver == '59' or ver == '510':
        ssh_client.run_command('service rh-postgresql95-postgresql restart')
    else:
        raise Exception('Unable to set config: Unrecognized version of appliance')

    # 5.6 requires DISABLE_DATABASE_ENVIRONMENT_CHECK=1
    ssh_client.run_command(
        'cd /var/www/miq/vmdb;DISABLE_DATABASE_ENVIRONMENT_CHECK=1 bin/rake evm:db:reset')
    ssh_client.run_rake_command('db:seed')
    ssh_client.run_command('service collectd start')
    # Work around for https://bugzilla.redhat.com/show_bug.cgi?id=1337525
    ssh_client.run_command('service httpd stop')
    ssh_client.run_command('rm -rf /run/httpd/*')
    ssh_client.run_command('rm -rf /var/www/miq/vmdb/log/*.log*')
    ssh_client.run_command('rm -rf /var/www/miq/vmdb/log/apache/*.log*')
    ssh_client.run_command('service evmserverd start')
    logger.debug('Cleaned appliance in: {}'.format(round(time.time() - starttime, 2)))
Example #4
0
def set_vmdb_yaml_config(ssh_client, yaml_data):
    logger.info('Uploading VMDB yaml Loader')
    ver = get_version()
    if ver == '56' or ver == '57' or ver == '58':
        ssh_client.run_command(
            'echo "{}" > /tmp/yaml_loader.rb'.format(yaml_loader56),
            log_less=True)
        logger.info('Uploading VMDB Config')
        ssh_client.run_command('echo "{}" > /tmp/vmdb.yml'.format(
            yaml.dump(yaml_data, default_flow_style=False)),
                               log_less=True)
    elif ver == '55':
        ssh_client.run_command(
            'echo "{}" > /tmp/yaml_loader.rb'.format(yaml_loader55),
            log_less=True)
        logger.info('Uploading VMDB Config')
        ssh_client.run_command(
            'echo "{}" | tee /tmp/vmdb.yml /var/www/miq/vmdb/config/vmdb.yml.db'
            ''.format(yaml.dump(yaml_data, default_flow_style=False)),
            log_less=True)
    else:
        raise Exception(
            'Unable to set config: Unrecognized version of appliance')
    result = ssh_client.run_rails_command('/tmp/yaml_loader.rb')
    if result.rc:
        raise Exception('Unable to set config')
    else:
        logger.info('Set VMDB Config')
Example #5
0
File: api.py Project: sundw2015/841
 def fetch_auth(self, self_node_info):
     """
     获取认证
     :param self_node_info: node info
     :return:
     """
     params = {
         "auth_key":
         self.auth_key,
         "version":
         version.get_version(self_node_info['version'], type='string'),
         "type":
         self_node_info['type'],
         "name":
         self_node_info['name'],
         "ip":
         self_node_info['ip'],
         "uuid":
         self_node_info['uuid'],
     }
     # 不通过 token 验证
     result = self.request('/api/system/nodes/auth',
                           method="POST",
                           params=params,
                           is_api=False,
                           timeout=5)
     if result["status"] == 500:
         if "connection_error" in result['error']:
             result["msg"] = '请求中心错误,地址不可达'
             result["error"] = '请求中心错误,地址不可达'
     return result
Example #6
0
 def get(self, request):
     """
     返回当前云松的版本, 以 CHANGELOG 为基础
     :return:
     """
     data = {"version": get_version()}
     return Response(data)
Example #7
0
def add_host_credentials(provider, ssh_client):
    """"Adds host credentials to a provider via the REST API"""
    starttime = time.time()
    appliance = cfme_performance['appliance']['ip_address']
    p_id = get_provider_id(provider['name'])
    version = get_version()
    if int(version) < 56:
        command = (
            'p = ExtManagementSystem.find_by_name(\'{}\'); '
            'for host in p.hosts do; '
            'host.update_authentication(:default => {{:userid => \'{}\', :password => \'{}\'}});'
            'host.save; '
            'end'.format(provider['name'],
                         provider['host_credentials']['username'],
                         provider['host_credentials']['password']))
        ssh_client.run_rails_console(command, timeout=None, log_less=True)
    else:
        response = requests.get(
            url="https://{}/api/providers/{}?attributes=hosts".format(
                appliance, p_id),
            auth=(cfme_performance['appliance']['rest_api']['username'],
                  cfme_performance['appliance']['rest_api']['password']),
            verify=False)

        if response.status_code != 200:
            logger.warning('Could not get host list, error: {}'.format(
                response.content))

        host_list = response.json()['hosts']
        id_list = []
        for host in host_list:
            id_list.append(host['id'])

        data_dict = {
            "action": "edit",
            "resource": {
                "credentials": {
                    "userid": provider['host_credentials']['username'],
                    "password": provider['host_credentials']['password']
                }
            }
        }

        json_data = json.dumps(data_dict)
        for host in id_list:
            response = requests.post(
                "https://" + appliance + "/api/hosts/" + str(host),
                data=json_data,
                auth=(cfme_performance['appliance']['rest_api']['username'],
                      cfme_performance['appliance']['rest_api']['password']),
                verify=False,
                headers={"content-type": "application/json"},
                allow_redirects=False)

            if response.status_code != 200:
                logger.debug(response.text)

    elapsed_time = time.time() - starttime
    logger.debug('Added host credentials in {}s'.format(elapsed_time))
Example #8
0
 def _update_center(self):
     """
     更新中心版本
     :return:
     """
     node = models.Node.objects.filter(uuid=self.task.target_uuid,
                                       agent=self.agent).first()
     if node.role == 'self':
         node.version = get_version()
         node.save()
def add_host_credentials(provider, ssh_client):
    """"Adds host credentials to a provider via the REST API"""
    starttime = time.time()
    appliance = cfme_performance['appliance']['ip_address']
    p_id = get_provider_id(provider['name'])
    version = get_version()
    if int(version) < 56:
        command = ('p = ExtManagementSystem.find_by_name(\'{}\'); '
            'for host in p.hosts do; '
            'host.update_authentication(:default => {{:userid => \'{}\', :password => \'{}\'}});'
            'host.save; '
            'end'.format(provider['name'], provider['host_credentials']['username'],
                provider['host_credentials']['password']))
        ssh_client.run_rails_console(command, timeout=None, log_less=True)
    else:
        response = requests.get(
            url="https://{}/api/providers/{}?attributes=hosts".format(appliance, p_id),
            auth=(cfme_performance['appliance']['rest_api']['username'],
                  cfme_performance['appliance']['rest_api']['password']),
            verify=False
        )

        if response.status_code != 200:
            logger.warning('Could not get host list, error: {}'.format(response.content))

        host_list = response.json()['hosts']
        id_list = []
        for host in host_list:
            id_list.append(host['id'])

        data_dict = {
            "action": "edit",
            "resource": {
                "credentials": {
                    "userid": provider['host_credentials']['username'],
                    "password": provider['host_credentials']['password']
                }
            }
        }

        json_data = json.dumps(data_dict)
        for host in id_list:
            response = requests.post("https://" + appliance + "/api/hosts/" + str(host),
                                     data=json_data,
                                     auth=(cfme_performance['appliance']['rest_api']['username'],
                                           cfme_performance['appliance']['rest_api']['password']),
                                     verify=False,
                                     headers={"content-type": "application/json"},
                                     allow_redirects=False)

            if response.status_code != 200:
                logger.debug(response.text)

    elapsed_time = time.time() - starttime
    logger.debug('Added host credentials in {}s'.format(elapsed_time))
Example #10
0
def test_version():
    '''
    expect:
        version: git describe
    '''
    expect = {'version': get_version()}
    client = app.test_client()
    client.testing = True
    result = client.get('/autocert/version')
    assert result.status_code == 200
    actual = jsonify(result.get_data().decode('utf-8'))
    assert expect == actual
Example #11
0
def task_setup():
    '''
    setup venv
    '''
    from utils.version import get_version
    return {
        'actions': [
            'rm -rf auto_cert_cli.egg-info/ venv/ dist/ __pycache__/',
            'virtualenv --python=python3 venv',
            'venv/bin/pip3 install --upgrade pip',
            'venv/bin/pip3 install -r cli/requirements.txt',
            'venv/bin/python3 ./setup.py install',
            'unzip -l venv/lib/python3.5/site-packages/auto_cert_cli-{0}-py3.5.egg'
            .format(get_version()),
        ],
    }
Example #12
0
def get_vmdb_yaml_config(ssh_client):
    ver = get_version()
    if ver == '56' or ver == '57' or ver == '58' or ver == '59' or ver == '510':
        base_data = ssh_client.run_rails_command(
            'puts\(Settings.to_hash.deep_stringify_keys.to_yaml\)', ignore_stderr=True)
        if base_data.rc:
            logger.error("Config couldn't be found")
            logger.error(base_data.output)
            raise Exception('Error obtaining vmdb config')
        yaml_data = base_data.output[:base_data.output.find('DEPRE')]
    elif ver == '55':
        base_data = ssh_client.run_command('cat "/var/www/miq/vmdb/config/vmdb.yml.db"')
        if base_data.rc:
            logger.error("Config couldn't be found")
            logger.error(base_data.output)
            raise Exception('Error obtaining vmdb config')
        yaml_data = base_data.output
    return yaml.load(yaml_data)
Example #13
0
def get_vmdb_yaml_config(ssh_client):
    ver = get_version()
    if ver == '56':
        base_data = ssh_client.run_rails_command(
            'puts\(Settings.to_hash.deep_stringify_keys.to_yaml\)')
        if base_data.rc:
            logger.error("Config couldn't be found")
            logger.error(base_data.output)
            raise Exception('Error obtaining vmdb config')
        yaml_data = base_data.output[:base_data.output.find('DEPRE')]
    elif ver == '55':
        base_data = ssh_client.run_command('cat "/var/www/miq/vmdb/config/vmdb.yml.db"')
        if base_data.rc:
            logger.error("Config couldn't be found")
            logger.error(base_data.output)
            raise Exception('Error obtaining vmdb config')
        yaml_data = base_data.output
    return yaml.load(yaml_data)
class utilpath():
    """A test library providing keywords for running, starting, starting CHE processes.
    """
    ROBOT_LIBRARY_SCOPE = 'GLOBAL'
    ROBOT_LIBRARY_VERSION = get_version()
    
    COMMANDER = ''
    HOSTMANAGER = ''
    STATBLOCKFIELDREADER = ''
    DATAVIEW = ''
    
    def setUtilPath(self):
        """Setting the Utilities paths by searching under BASE_DIR from VenueVariables
        This is called by suite setup in core.robot to make these variables available to the keywords
         """
        utilpath.COMMANDER = _search_file(BASE_DIR,'Commander',True)[0]
        utilpath.STATBLOCKFIELDREADER = _search_file(BASE_DIR,'StatBlockFieldReader',True)[0]
        utilpath.HOSTMANAGER = _search_file(BASE_DIR,'HostManager',True)[0]
        utilpath.DATAVIEW = _search_file(TOOLS_DIR,'DataView',True)[0]
Example #15
0
def set_vmdb_yaml_config(ssh_client, yaml_data):
    logger.info('Uploading VMDB yaml Loader')
    ver = get_version()
    if ver == '56':
        ssh_client.run_command('echo "{}" > /tmp/yaml_loader.rb'.format(yaml_loader56),
            log_less=True)
        logger.info('Uploading VMDB Config')
        ssh_client.run_command('echo "{}" > /tmp/vmdb.yml'.format(
            yaml.dump(yaml_data, default_flow_style=False)), log_less=True)
    elif ver == '55':
        ssh_client.run_command('echo "{}" > /tmp/yaml_loader.rb'.format(yaml_loader55),
            log_less=True)
        logger.info('Uploading VMDB Config')
        ssh_client.run_command('echo "{}" | tee /tmp/vmdb.yml /var/www/miq/vmdb/config/vmdb.yml.db'
            ''.format(yaml.dump(yaml_data, default_flow_style=False)), log_less=True)
    else:
        raise Exception('Unable to set config: Unrecognized version of appliance')
    result = ssh_client.run_rails_command('/tmp/yaml_loader.rb')
    if result.rc:
        raise Exception('Unable to set config')
    else:
        logger.info('Set VMDB Config')
Example #16
0
import __main__ as main
import datetime
import sys
import os

from utils.version import get_version

VERSION = (0, 0, 1, 'alpha', 0)

__version__ = get_version(VERSION)

def get_default_config():
	root_dir = os.path.expanduser("~")

	script_instance_path = os.path.join(root_dir,"lgsm-core")
	script_game_path = os.path.realpath(script_instance_path)

	try:
		if __name__ != "__main__":
			script_instance_path = main.__file__
	except:
		pass

	#root_dir = os.path.dirname(script_game_path)

	date_format = "%Y-%m-%d-%H-%M-%S"
	date_string = datetime.datetime.today().strftime(date_format)
	arch = sys.platform

	config = {
		"lgsm_script": "lgsm-core",
Example #17
0
    def parse(self, dataline):
        _line = py_str(dataline, self.encoding)
        if getargs(_line.split(" "), 1) == "001":
            for command in self.config["IRC"]["autorun-irc-commands"]:
                self.send(command)
            for channel in self.channels:
                self.send("JOIN %s" % channel)
            self.ready = True
            self.log.info("Connected to IRC!")
            self.state = True
            self.nickAttempts = 0
        if getargs(_line.split(" "), 1) == "433":
            self.log.info("Nickname '%s' already in use.", self.nickname)
            self.nickAttempts += 1
            if self.nickAttempts > 2:
                name = bytearray(self.nickname)
                for i in xrange(3):
                    name[len(self.nickname) / 3 * i] = chr(random.randrange(97, 122))
                self.nickname = str(name)
            else:
                self.nickname += "_"
            self.auth()
            self.log.info("Attemping to use nickname '%s'.", self.nickname)
        if getargs(_line.split(" "), 1) == "JOIN":
            nick = getargs(_line.split(" "), 0)[1:getargs(_line.split(" "), 0).find("!")]
            channel = getargs(_line.split(" "), 2)[1:][:-1]
            self.log.info("%s joined %s", nick, channel)
            self.wrapper.events.callevent("irc.join", {"nick": nick, "channel": channel}, abortable=False)
        if getargs(_line.split(" "), 1) == "PART":
            nick = getargs(_line.split(" "), 0)[1:getargs(_line.split(" "), 0).find("!")]
            channel = getargs(_line.split(" "), 2)
            self.log.info("%s parted %s", nick, channel)
            self.wrapper.events.callevent("irc.part", {"nick": nick, "channel": channel}, abortable=False)
        if getargs(_line.split(" "), 1) == "MODE":
            try:
                nick = getargs(_line.split(" "), 0)[1:getargs(_line.split(" "), 0).find('!')]
                channel = getargs(_line.split(" "), 2)
                modes = getargs(_line.split(" "), 3)
                user = getargs(_line.split(" "), 4)[:-1]
                self.console(channel, [{
                    "text": user, 
                    "color": "green"
                }, {
                    "text": " received modes %s from %s" % (modes, nick), 
                    "color": "white"
                }])
            except Exception as e:
                self.log.debug("Exception in IRC in parse (MODE): \n%s", e)
                pass
        if getargs(_line.split(" "), 0) == "PING":
            self.send("PONG %s" % getargs(_line.split(" "), 1))
        if getargs(_line.split(" "), 1) == "QUIT":
            nick = getargs(_line.split(" "), 0)[1:getargs(_line.split(" "), 0).find("!")]
            message = getargsafter(_line.split(" "), 2)[1:].strip("\n").strip("\r")

            self.wrapper.events.callevent("irc.quit", {"nick": nick, "message": message, "channel": None}, abortable=False)
        if getargs(_line.split(" "), 1) == "PRIVMSG":
            channel = getargs(_line.split(" "), 2)
            nick = getargs(_line.split(" "), 0)[1:getargs(_line.split(" "), 0).find("!")]
            message = getargsafter(_line.split(" "), 3)[1:].strip("\n").strip("\r")
            if channel[0] == "#":
                if message.strip() == ".players":
                    users = ""
                    for user in self.javaserver.players:
                        users += "%s " % user
                    self.send("PRIVMSG %s :There are currently %s users on the server: %s" %
                              (channel, len(self.javaserver.players), users))
                elif message.strip() == ".about":
                    self.send("PRIVMSG %s :Wrapper.py Version %s" % (channel, self.wrapper.getbuildstring()))
                else:
                    if not PY3:
                        message = message.decode(self.encoding, "ignore")
                        # TODO - not sure if this part is going to work in PY3
                        # now that message is a properly encoded string, not a b"" sequence
                    if getargs(message.split(" "), 0) == "\x01ACTION":
                        self.wrapper.events.callevent("irc.action", {"nick": nick,
                                                                     "channel": channel,
                                                                     "action":
                                                                         getargsafter(message.split(" "), 1)[:-1]},
                                                      abortable = False
                        )
                        self.log.info("[%s] * %s %s", channel, nick, getargsafter(message.split(" "), 1)[:-1])
                    else:
                        self.wrapper.events.callevent("irc.message", {"nick": nick,
                                                                      "channel": channel,
                                                                      "message": message},
                                                      abortable=False
                                                      )
                        self.log.info("[%s] <%s> %s", channel, nick, message)
            elif self.config["IRC"]["control-from-irc"]:
                self.log.info("[PRIVATE] (%s) %s", nick, message)

                def msg(string):
                    self.log.info("[PRIVATE] (%s) %s", self.nickname, string)
                    self.send("PRIVMSG %s :%s" % (nick, string))
                if self.config["IRC"]["control-irc-pass"] == "password":
                    msg("A new password is required in wrapper.properties. Please change it.")
                if "password" in self.config["IRC"]["control-irc-pass"]:
                    msg("The password is not secure.  You must use the console to enter a password.")
                    return
                if nick in self.authorized:
                    if int(time.time()) - self.authorized[nick] < 900:
                        if getargs(message.split(" "), 0) == 'hi':
                            msg('Hey there!')
                        elif getargs(message.split(" "), 0) == 'help':
                            # eventually I need to make help only one or two
                            # lines, to prevent getting kicked/banned for spam
                            msg("run [command] - run command on server")
                            msg("togglebackups - temporarily turn backups on or off. this setting is not permanent "
                                "and will be lost on restart")
                            msg("halt - shutdown server and Wrapper.py, will not auto-restart")
                            msg("kill - force server restart without clean shutdown - only use when server "
                                "is unresponsive")
                            msg("start/restart/stop - start the server/automatically stop and start server/stop "
                                "the server without shutting down Wrapper")
                            msg("status - show status of the server")
                            msg("check-update - check for new Wrapper.py updates, but don't install them")
                            msg("update-wrapper - check and install new Wrapper.py updates")
                            msg("Wrapper.py Version %s by benbaptist" %
                                self.wrapper.getbuildstring())
                            # msg('console - toggle console output to this private message')
                        elif getargs(message.split(" "), 0) == 'togglebackups':
                            self.config["Backups"]["enabled"] = not self.config["Backups"]["enabled"]
                            if self.config["Backups"]["enabled"]:
                                msg('Backups are now on.')
                            else:
                                msg('Backups are now off.')
                            self.configmgr.save()  # 'config' is just the json dictionary of items, not the Config class
                        elif getargs(message.split(" "), 0) == 'run':
                            if getargs(message.split(" "), 1) == '':
                                msg('Usage: run [command]')
                            else:
                                command = " ".join(message.split(' ')[1:])
                                self.javaserver.console(command)
                        elif getargs(message.split(" "), 0) == 'halt':
                            msg("Halting wrapper... Bye.")
                            self.wrapper.shutdown()
                        elif getargs(message.split(" "), 0) == 'restart':
                            msg("restarting from IRC remote")
                            self.log.info("Restarting server from IRC remote")
                            self.javaserver.restart()
                        elif getargs(message.split(" "), 0) == 'stop':
                            msg("Stopping from IRC remote")
                            self.log.info("Stopped from IRC remote")
                            self.javaserver.stop_server_command()
                        elif getargs(message.split(" "), 0) == 'start':
                            self.javaserver.start()
                            msg("Server starting")
                        elif getargs(message.split(" "), 0) == 'kill':
                            self.javaserver.kill("Killing server from IRC remote")
                            msg("Server terminated.")
                        elif getargs(message.split(" "), 0) == 'status':
                            if self.javaserver.state == 2:
                                msg("Server is running.")
                            elif self.javaserver.state == 1:
                                msg("Server is currently starting/frozen.")
                            elif self.javaserver.state == 0:
                                msg("Server is stopped. Type 'start' to fire it back up.")
                            elif self.javaserver.state == 3:
                                msg("Server is in the process of shutting down/restarting.")
                            else:
                                msg("Server is in unknown state. This is probably a Wrapper.py bug - report it! "
                                    "(state #%d)" % self.javaserver.state)
                            if self.wrapper.javaserver.getmemoryusage():
                                msg("Server Memory Usage: %d bytes" % self.wrapper.javaserver.getmemoryusage())
                        elif getargs(message.split(" "), 0) in ('check-update', 'update-wrapper'):
                            msg("Checking for new updates...")
                            update = self.wrapper.get_wrapper_update_info()
                            repotype = None
                            version = None
                            if update:
                                version, repotype = update
                                build = version[4]
                                newversion = version_handler.get_version(version)
                                yourversion = version_handler.get_version(version_info.__version__)

                                msg(
                                    "New Wrapper.py Version %s (%s) is available! (you have %s)" %
                                    (newversion, repotype, yourversion)
                                )
                                msg("To perform the update, type update-wrapper.")
                            else:
                                msg("No new %s Wrapper.py versions available." % version_info.__branch__)
                            if getargs(message.split(" "), 0) == 'update-wrapper' and update:
                                msg("Performing update..")
                                if self.wrapper.performupdate(version, repotype):
                                    msg(
                                        "Update completed! Version %s (%s) is now installed. Please reboot "
                                        "Wrapper.py to apply changes." % (version, repotype)
                                    )
                                else:
                                    msg("An error occured while performing update.")
                                    msg("Please check the Wrapper.py console as soon as possible for an explanation "
                                        "and traceback.")
                                    msg("If you are unsure of the cause, please file a bug report.")

                        elif getargs(message.split(" "), 0) == "about":
                            msg("Wrapper.py by benbaptist - Version %s (%d)" % (
                                version_info.__version__, version_info.__branch__))
                        else:
                            msg('Unknown command. Type help for more commands')
                    else:
                        msg("Session expired, re-authorize.")
                        del self.authorized[nick]
                else:
                    if getargs(message.split(" "), 0) == 'auth':
                        if self.pass_handler.check_pw(getargs(message.split(" "), 1), self.config["IRC"]["control-irc-pass"]):
                            msg("Authorization success! You'll remain logged in for 15 minutes.")
                            self.authorized[nick] = int(time.time())
                        else:
                            msg("Invalid password.")
                    else:
                        msg('Not authorized. Type "auth [password]" to login.')
Example #18
0
import os, re, fileinput, shutil, urllib2
import simplejson as json
from fabric.api import local
from utils.version import get_version
from itertools import combinations

VERSION = get_version()
DATE = get_version(True)

# Lists all the names of the modules of Tiramisu,
# each module has an identification number (also called "cups_of_coffee")
official_dictionary_names = [
    '', 'selector', 'browserdetect', 'selector.dom', 'ajax', 'selector.event',
    'taskengine', 'json'
]

# all the cups_of_coffee of Tiramisu
ALL_CUPS_OF_COFFEE = '1234567'


def beautify():
    print '\n####### Beautifying tiramisu.js #######'
    local(
        'python utils/jsbeautifier.py src/build/tiramisu.js > src/build/tiramisu-beautified.js'
    )
    local('mv src/build/tiramisu-beautified.js src/build/tiramisu.js')


def beautify_modules():
    print '\n####### Beautifying modules #######'
Example #19
0
def get_version():
    from utils.version import get_version
    return get_version(VERSION)
Example #20
0
import os, re, fileinput, shutil, urllib2
import simplejson as json 
from fabric.api import local
from utils.version import get_version
from itertools import combinations

VERSION = get_version()
DATE = get_version(True)

# Lists all the names of the modules of Tiramisu, 
# each module has an identification number (also called "cups_of_coffee")
official_dictionary_names = [
    '',
    'selector',
    'browserdetect',
    'selector.dom',
    'ajax',
    'selector.event',
    'taskengine',
    'json'
]

# all the cups_of_coffee of Tiramisu
ALL_CUPS_OF_COFFEE = '1234567'


def beautify():
    print '\n####### Beautifying tiramisu.js #######'
    local('python utils/jsbeautifier.py src/build/tiramisu.js > src/build/tiramisu-beautified.js')
    local('mv src/build/tiramisu-beautified.js src/build/tiramisu.js')
Example #21
0
class LinuxFSUtilities():
    """A test library providing keywords for file and directory related operations.

    `LinuxFSUtilities` is GATS's standard library.
    """
    ROBOT_LIBRARY_SCOPE = 'GLOBAL'
    ROBOT_LIBRARY_VERSION = get_version()
    
    def get_remote_file(self, source, destination='.'):
        return G_SSHInstance.get_file(source, destination)
    
    def put_remote_file(self, source, destination='.', mode='0744', newline=''):
        return G_SSHInstance.put_file(source, destination, mode, newline)
    
    def create_remote_file_content(self, destination, content):
        G_SSHInstance.write_bare('cat > %s <<END_OF_DATA\n' %destination)
        if isinstance(content, list):
            for i in content:
                G_SSHInstance.write_bare('%s\n' %i)
        elif isinstance(content, dict):
            for key,value in content.items():
                G_SSHInstance.write_bare('%s=%s\n' %(key,value))
        else:
            G_SSHInstance.write_bare('%s\n' %content)
        G_SSHInstance.write_bare('END_OF_DATA\n')
    
    def read_remote_file(self, path):
        cmd = "cat '" + path + "'"
        stdout, stderr, rc = _exec_command(cmd)
        if rc !=0 or stderr !='':
            raise AssertionError('*ERROR* cmd=%s, rc=%s, %s %s' %(cmd,rc,stdout,stderr))
        return stdout
                    
    def remote_file_should_exist(self, path):
        G_SSHInstance.file_should_exist(path)
        
    def remote_file_should_not_exist(self, path):
        G_SSHInstance.file_should_not_exist(path)
        
    def delete_remote_files(self,*paths):
        """delete one or more files on IP, all files should be provided with fullpath.\n

        Return [0, delete success info, ''] or [1, '', delete error info]

        Examples:
        | ${res} | delete files | /tmp/tmp.exl | /tmp/1.txt |
        | ${res} | delete files | /tmp/tmp.exl | 
        """
        return _delete_file(list(paths), '', False)
    
    def delete_remote_files_matching_pattern(self, path, name, recurse= False):
        """delete all pattern matching files under desired path on IP.\n
        
        path is the directory.\n
        name support wildcard *, for example GATS*.exl.\n
        recurse default to False, if True, mean recursively find all matching files under all subdirectories.\n
        
        Return [0, delete success info, ''] or [1, '', delete error info]

        Examples:
        | ${res} | delete files matching pattern | /tmp | test* | 
        | ${res} | delete files matching pattern | /tmp | test* | ${True} |
        """
        return _delete_file(name, path, recurse)
    
    def search_remote_files(self, path, name, recurse=False):
        """
        Search files which we expect.\n
        
        path is the path where the files are located.\n
        name can be pattern. * matches everything.\n
        recurse default is False, mean only search the current folder.\n
        
        Return the matched files list under the path. Each file is fullpath.
        
        Examples:
        | ${res} | search files | /tmp | smf* |  
        | ${res} | search files | /tmp | smf* | ${true} |
        """
        return _search_file(path, name, recurse)
    
    def count_remote_lines(self, file):
        """
        count the line number of the file.\n
        
        file should be absolute path.\n
        
        Return the number.
        
        Examples:
        | ${res} | count lines | /tmp/1.txt  | 
        """
        return _count_lines(file)
        
    def grep_remote_file(self, filePath, searchKeyWord, isPattern=True, isCaseSensitive=True, Fromline=0, timeout=0, retry_interval=0):
        """
        this keyword is used to grep keywords in a file, then return matching lines.\n

        filePath is the full path of the file.\n
        searchKeyWord is the keyword filter.\n
        isPattern is the flag indicate if searchKeyword is a normal search string or regular expression pattern.\n
        isCaseSensitive is the flag indicate if searchKeyword is case sensitive.\n
        timeout default to 0, mean not timeout wait.\n
        retry_interval is default to 20, mean every 20s will check the log once.\n
        Fromline default to 0, which mean from which line to search the content.\n
        
        The return value is a list for all the matched lines content.\n
        Empty list would be returned if no matched lines found.\n

        Examples:
        | @list | grep remote file | /result.txt | AB\\.C | ${true} | ${true} |
        | @list | grep remote file | /result.txt | timelimit \\\\d+ | ${true} | ${true} |
        | @list | grep remote file | /result.txt | AB.C | ${false} | ${false} | 60 | 
        """
        returnMatchLines = []
        current = time.time()
        timout_value = float(timeout)
        maxtime = current + timout_value
        while (current <= maxtime):
            if isPattern == False:
                if isCaseSensitive:
                    cmd = r'grep -n -F "%s" "%s"' % (searchKeyWord, filePath)
                else:
                    cmd = r'grep -n -i -F "%s" "%s"' % (searchKeyWord, filePath)  
            else:
                if not isCaseSensitive:
                    cmd = r'grep -n -i -P "%s" "%s"' % (searchKeyWord, filePath)  
                else:
                    cmd = r'grep -n -P "%s" "%s"' % (searchKeyWord, filePath)
            #stdout = _run_command(cmd)
            retInfo = _exec_command(cmd)
            stdout = retInfo[0]
            if len(stdout) < 1:
                if timout_value == 0:
                    break
                current = time.time()
                time.sleep(float(retry_interval))
            else:
                break
        for line in stdout.split('\n'):
            if line and int(line.split(':',1)[0]) >= int(Fromline):
                returnMatchLines.append(line.split(':',1)[1])
        return returnMatchLines
    
    def tail_remote_file(self, path, lines=0):
        """
        Tail a remote file.\n
        
        path is the full path of the file.\n
        lines is the number of lines from the bottom of the file to start the output.\n
        
        This is generally used in conjunction with remote_tail_should_contain or remote_tail_should_contain_regexp
        
        Return null.
        
        Examples:
        | tail remote file | /tmp/1.txt  |
        | tail remote file | /tmp/1.txt  | 20 |
        """
        cmd = 'tail --lines=%d --follow=name --retry %s' %(lines,path)
        G_SSHInstance.write(cmd)

    def remote_tail_should_contain(self, text, maxwait='60 seconds'):
        """
        Read the results from a previous call to tail_remote_file and verify that it contains the specified string.
        
        text is a plain text string.
        maxwait is the maximum time to wait/process the search (default is 60 seconds)
        
        See also remote_tail_should_contain_regexp to search for a regular expression
        
        Returns null.  Fails if string not found within maxwait time
        
        Example:
        | remote file should contain | some text  |
        | remote file should contain | some text  | maxwait=2 minutes |
        """
        defaultTimeout = G_SSHInstance.get_connection(timeout=True)
        G_SSHInstance.set_client_configuration(timeout=maxwait)
        G_SSHInstance.read_until(text)
        G_SSHInstance.set_client_configuration(timeout=defaultTimeout)
    
    def remote_tail_should_contain_regexp(self, pattern, maxwait='60 seconds'):
        """
        Read the results from a previous call to tail_remote_file and verify that it contains the specified pattern.
        
        Pattern is in Python regular expression syntax, http://docs.python.org/2/library/re.html|re module
        Pattern matching is case-sensitive regardless the local or remote operating system
        
        maxwait is the maximum time to wait/process the search (default is 60 seconds)
        
        See also remote_tail_should_contain to search for a simple string
        
        Returns null.  Fails if pattern not found within maxwait time
        
        Example:
        | remote file should contain regexp | Processing.*start.*${MTE} |
        | remote file should contain regexp | Processing.*start.*${MTE} | maxwait=2 minutes |
        """
        defaultTimeout = G_SSHInstance.get_connection(timeout=True)
        G_SSHInstance.set_client_configuration(timeout=maxwait)
        G_SSHInstance.read_until_regexp(pattern)
        G_SSHInstance.set_client_configuration(timeout=defaultTimeout)
    
    def wait_for_file_update(self, filename, waittime=2, timeout=60):
        """Wait until the remote file timestamp changes and then wait until the size does not change.

        Argument filename may contain UNIX filename wildcard values.
        Argument 'waittime' specifies the time to wait between checks, in seconds.
        Argument 'timeout' specifies the maximum time to wait, in seconds.
        
        Does not return a value; raises an error if the file does not change within timeout seconds.

        Examples:
        | Wait for file update  | filepathname  |
        | Wait for file update  | filepathname  | 5  | 90  |
        """
        # convert  unicode to int (it is unicode if it came from the Robot test)
        timeout = int(timeout)
        waittime = int(waittime)
        maxtime = time.time() + float(timeout)
        
        fileInfo = _ls(filename,'--full-time')
        if len(fileInfo) == 0:
            raise AssertionError('*ERROR* File %s does not exist' %filename)
        splitInfo = fileInfo.split(' ')
        fileStartTime = ' '.join(splitInfo[5:7])
        
        while time.time() <= maxtime:
            fileInfo = _ls(filename,'--full-time')
            splitInfo = fileInfo.split(' ')
            fileCurrTime = ' '.join(splitInfo[5:7])
#             print 'DEBUG fileStartTime=%s fileCurrTime=%s' %(fileStartTime,fileCurrTime)
            if fileCurrTime > fileStartTime:
                # file has changed, now wait for writing to complete
                fileStartSize = splitInfo[4]
                while time.time() <= maxtime:
                    time.sleep(waittime)
                    fileInfo = _ls(filename,'--full-time')
                    splitInfo = fileInfo.split(' ')
                    fileCurrSize = splitInfo[4]
#                     print 'DEBUG fileStartSize=%s fileCurrSize=%s' %(fileStartSize,fileCurrSize)
                    if fileCurrSize == fileStartSize:
                        return
                    fileStartSize = fileCurrSize
                raise AssertionError('*ERROR* File %s writing did not complete before timeout %ds' %(filename,timeout))
            time.sleep(waittime)
        raise AssertionError('*ERROR* File %s did not change before timeout %ds' %(filename,timeout))

    def wait_for_file_write(self, filename, waittime=2, timeout=60):
        """Wait until the remote file exists and the size does not change.

        Argument filename may contain UNIX filename wildcard values.
        Argument 'waittime' specifies the time to wait between checks, in seconds.
        Argument 'timeout' specifies the maximum time to wait, in seconds.
        
        Does not return a value; raises an error if the file does not exist or writing complete within timeout seconds.

        Examples:
        | Wait for file write  | filepathname  |
        | Wait for file write  | filepathname  | 1  | 10  |
        """
        # convert  unicode to int (it is unicode if it came from the Robot test)
        timeout = int(timeout)
        waittime = int(waittime)
        maxtime = time.time() + float(timeout)

        fileStartSize = -1
        while time.time() <= maxtime:
            fileInfo = _ls(filename,'--full-time')
            if len(fileInfo) == 0:
                continue
            splitInfo = fileInfo.split(' ')
            fileCurrSize = splitInfo[4]
            if fileCurrSize == fileStartSize:
                return
            fileStartSize = fileCurrSize
            time.sleep(waittime)
        if fileStartSize == -1:
            raise AssertionError('*ERROR* File %s does not exist' %filename)
        else:
            raise AssertionError('*ERROR* File %s writing did not complete before timeout %ds' %(filename,timeout))
   
    def wait_for_search_file(self, topdir, filename, waittime=2, timeout=60):
        """Wait until the remote filename to exist somewhere under the specified directory.

        Arguement topdir is the top directory to do the search from.
        Argument filename may contain UNIX filename wildcard values.
        Argument 'waittime' specifies the time to wait between checks, in seconds.
        Argument 'timeout' specifies the maximum time to wait, in seconds.
        
        Does not return a value; raises an error if the file does not exist within timeout seconds.

        Examples:
        | Wait for searchfile  | VENUE_DIR | filepathname  |
        | Wait for searchfile  | VENUE_DIR | filepathname  | 2  | 30  |
        """
        # convert  unicode to int (it is unicode if it came from the Robot test)
        timeout = int(timeout)
        waittime = int(waittime)
        maxtime = time.time() + float(timeout)

        while time.time() <= maxtime:
            foundfiles = _search_file(topdir,filename,True)
            if len(foundfiles) >= 1:
                return foundfiles
            time.sleep(waittime)
        raise AssertionError('*ERROR* File %s does not exist (timeout %ds)' %(filename,timeout))
Example #22
0
    def parse(self, dataline):
        _line = py_str(dataline, self.encoding)
        if getargs(_line.split(" "), 1) == "001":
            for command in self.config["IRC"]["autorun-irc-commands"]:
                self.send(command)
            for channel in self.channels:
                self.send("JOIN %s" % channel)
            self.ready = True
            self.log.info("Connected to IRC!")
            self.state = True
            self.nickAttempts = 0
        if getargs(_line.split(" "), 1) == "433":
            self.log.info("Nickname '%s' already in use.", self.nickname)
            self.nickAttempts += 1
            if self.nickAttempts > 2:
                name = bytearray(self.nickname)
                for i in xrange(3):
                    name[len(self.nickname) / 3 * i] = chr(random.randrange(97, 122))
                self.nickname = str(name)
            else:
                self.nickname += "_"
            self.auth()
            self.log.info("Attemping to use nickname '%s'.", self.nickname)
        if getargs(_line.split(" "), 1) == "JOIN":
            nick = getargs(_line.split(" "), 0)[1:getargs(_line.split(" "), 0).find("!")]
            channel = getargs(_line.split(" "), 2)[1:][:-1]
            self.log.info("%s joined %s", nick, channel)
            self.wrapper.events.callevent("irc.join", {"nick": nick, "channel": channel}, abortable=False)
        if getargs(_line.split(" "), 1) == "PART":
            nick = getargs(_line.split(" "), 0)[1:getargs(_line.split(" "), 0).find("!")]
            channel = getargs(_line.split(" "), 2)
            self.log.info("%s parted %s", nick, channel)
            self.wrapper.events.callevent("irc.part", {"nick": nick, "channel": channel}, abortable=False)
        if getargs(_line.split(" "), 1) == "MODE":
            try:
                nick = getargs(_line.split(" "), 0)[1:getargs(_line.split(" "), 0).find('!')]
                channel = getargs(_line.split(" "), 2)
                modes = getargs(_line.split(" "), 3)
                user = getargs(_line.split(" "), 4)[:-1]
                self.console(channel, [{
                    "text": user, 
                    "color": "green"
                }, {
                    "text": " received modes %s from %s" % (modes, nick), 
                    "color": "white"
                }])
            except Exception as e:
                self.log.debug("Exception in IRC in parse (MODE): \n%s", e)
                pass
        if getargs(_line.split(" "), 0) == "PING":
            self.send("PONG %s" % getargs(_line.split(" "), 1))
        if getargs(_line.split(" "), 1) == "QUIT":
            nick = getargs(_line.split(" "), 0)[1:getargs(_line.split(" "), 0).find("!")]
            message = getargsafter(_line.split(" "), 2)[1:].strip("\n").strip("\r")

            self.wrapper.events.callevent("irc.quit", {"nick": nick, "message": message, "channel": None}, abortable=False)
        if getargs(_line.split(" "), 1) == "PRIVMSG":
            channel = getargs(_line.split(" "), 2)
            nick = getargs(_line.split(" "), 0)[1:getargs(_line.split(" "), 0).find("!")]
            message = getargsafter(_line.split(" "), 3)[1:].strip("\n").strip("\r")
            if channel[0] == "#":
                if message.strip() == ".players":
                    users = ""
                    for user in self.javaserver.players:
                        users += "%s " % user
                    self.send("PRIVMSG %s :There are currently %s users on the server: %s" %
                              (channel, len(self.javaserver.players), users))
                elif message.strip() == ".about":
                    self.send("PRIVMSG %s :Wrapper.py Version %s" % (channel, self.wrapper.getbuildstring()))
                else:
                    if not PY3:
                        message = message.decode(self.encoding, "ignore")
                        # TODO - not sure if this part is going to work in PY3
                        # now that message is a properly encoded string, not a b"" sequence
                    if getargs(message.split(" "), 0) == "\x01ACTION":
                        self.wrapper.events.callevent("irc.action", {"nick": nick,
                                                                     "channel": channel,
                                                                     "action":
                                                                         getargsafter(message.split(" "), 1)[:-1]},
                                                      abortable = False
                        )
                        self.log.info("[%s] * %s %s", channel, nick, getargsafter(message.split(" "), 1)[:-1])
                    else:
                        self.wrapper.events.callevent("irc.message", {"nick": nick,
                                                                      "channel": channel,
                                                                      "message": message},
                                                      abortable=False
                                                      )
                        self.log.info("[%s] <%s> %s", channel, nick, message)
            elif self.config["IRC"]["control-from-irc"]:
                self.log.info("[PRIVATE] (%s) %s", nick, message)

                def msg(string):
                    self.log.info("[PRIVATE] (%s) %s", self.nickname, string)
                    self.send("PRIVMSG %s :%s" % (nick, string))
                if self.config["IRC"]["control-irc-pass"] == "password":
                    msg("A new password is required in wrapper.properties. Please change it.")
                if "password" in self.config["IRC"]["control-irc-pass"]:
                    msg("The password is not secure.  You must use the console to enter a password.")
                    return
                if nick in self.authorized:
                    if int(time.time()) - self.authorized[nick] < 900:
                        if getargs(message.split(" "), 0) == 'hi':
                            msg('Hey there!')
                        elif getargs(message.split(" "), 0) == 'help':
                            # eventually I need to make help only one or two
                            # lines, to prevent getting kicked/banned for spam
                            msg("run [command] - run command on server")
                            msg("togglebackups - temporarily turn backups on or off. this setting is not permanent "
                                "and will be lost on restart")
                            msg("halt - shutdown server and Wrapper.py, will not auto-restart")
                            msg("kill - force server restart without clean shutdown - only use when server "
                                "is unresponsive")
                            msg("start/restart/stop - start the server/automatically stop and start server/stop "
                                "the server without shutting down Wrapper")
                            msg("status - show status of the server")
                            msg("check-update - check for new Wrapper.py updates, but don't install them")
                            msg("update-wrapper - check and install new Wrapper.py updates")
                            msg("Wrapper.py Version %s by benbaptist" %
                                self.wrapper.getbuildstring())
                            # msg('console - toggle console output to this private message')
                        elif getargs(message.split(" "), 0) == 'togglebackups':
                            self.config["Backups"]["enabled"] = not self.config["Backups"]["enabled"]
                            if self.config["Backups"]["enabled"]:
                                msg('Backups are now on.')
                            else:
                                msg('Backups are now off.')
                            self.configmgr.save()  # 'config' is just the json dictionary of items, not the Config class
                        elif getargs(message.split(" "), 0) == 'run':
                            if getargs(message.split(" "), 1) == '':
                                msg('Usage: run [command]')
                            else:
                                command = " ".join(message.split(' ')[1:])
                                self.javaserver.console(command)
                        elif getargs(message.split(" "), 0) == 'halt':
                            msg("Halting wrapper... Bye.")
                            self.wrapper.shutdown()
                        elif getargs(message.split(" "), 0) == 'restart':
                            msg("restarting from IRC remote")
                            self.log.info("Restarting server from IRC remote")
                            self.javaserver.restart()
                        elif getargs(message.split(" "), 0) == 'stop':
                            msg("Stopping from IRC remote")
                            self.log.info("Stopped from IRC remote")
                            self.javaserver.stop_server_command()
                        elif getargs(message.split(" "), 0) == 'start':
                            self.javaserver.start()
                            msg("Server starting")
                        elif getargs(message.split(" "), 0) == 'kill':
                            self.javaserver.kill("Killing server from IRC remote")
                            msg("Server terminated.")
                        elif getargs(message.split(" "), 0) == 'status':
                            if self.javaserver.state == 2:
                                msg("Server is running.")
                            elif self.javaserver.state == 1:
                                msg("Server is currently starting/frozen.")
                            elif self.javaserver.state == 0:
                                msg("Server is stopped. Type 'start' to fire it back up.")
                            elif self.javaserver.state == 3:
                                msg("Server is in the process of shutting down/restarting.")
                            else:
                                msg("Server is in unknown state. This is probably a Wrapper.py bug - report it! "
                                    "(state #%d)" % self.javaserver.state)
                            if self.wrapper.javaserver.getmemoryusage():
                                msg("Server Memory Usage: %d bytes" % self.wrapper.javaserver.getmemoryusage())
                        elif getargs(message.split(" "), 0) in ('check-update', 'update-wrapper'):
                            msg("Checking for new updates...")
                            update = self.wrapper.get_wrapper_update_info()
                            repotype = None
                            version = None
                            if update:
                                version, repotype = update
                                build = version[4]
                                newversion = version_handler.get_version(version)
                                yourversion = version_handler.get_version(version_info.__version__)

                                msg(
                                    "New Wrapper.py Version %s (%s) is available! (you have %s)" %
                                    (newversion, repotype, yourversion)
                                )
                                msg("To perform the update, type update-wrapper.")
                            else:
                                msg("No new %s Wrapper.py versions available." % version_info.__branch__)
                            if getargs(message.split(" "), 0) == 'update-wrapper' and update:
                                msg("Performing update..")
                                if self.wrapper.performupdate(version, repotype):
                                    msg(
                                        "Update completed! Version %s (%s) is now installed. Please reboot "
                                        "Wrapper.py to apply changes." % (version, repotype)
                                    )
                                else:
                                    msg("An error occured while performing update.")
                                    msg("Please check the Wrapper.py console as soon as possible for an explanation "
                                        "and traceback.")
                                    msg("If you are unsure of the cause, please file a bug report.")

                        elif getargs(message.split(" "), 0) == "about":
                            msg("Wrapper.py by benbaptist - Version %s (%d)" % (
                                version_info.__version__, version_info.__branch__))
                        else:
                            msg('Unknown command. Type help for more commands')
                    else:
                        msg("Session expired, re-authorize.")
                        del self.authorized[nick]
                else:
                    if getargs(message.split(" "), 0) == 'auth':
                        if self.pass_handler.check_pw(getargs(message.split(" "), 1), self.config["IRC"]["control-irc-pass"]):
                            msg("Authorization success! You'll remain logged in for 15 minutes.")
                            self.authorized[nick] = int(time.time())
                        else:
                            msg("Invalid password.")
                    else:
                        msg('Not authorized. Type "auth [password]" to login.')
Example #23
0
#source_encoding = 'utf-8-sig'

# The master toctree document.
master_doc = 'index'

# General information about the project.
project = 'Zebra'
copyright = copyright = '{year} by {author}'.format(year=COPYRIGHT[0], author=COPYRIGHT[1])
author = COPYRIGHT[1]

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
# The short X.Y version.
version = '.'.join(get_version(VERSION).split('.')[:2])
# The full version, including alpha/beta/rc tags.
release = get_version(VERSION)

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = 'en'

# There are two options for replacing |today|: either, you set today to some
# non-false value, then it is used:
#today = ''
# Else, today_fmt is used as the format for a strftime call.
#today_fmt = '%B %d, %Y'
Example #24
0
    def handle(self, *args, **options):
        """
        心跳检查, 获取子节点的级联关系
        :param args:
        :param options:
        :return:
        """
        # 更新本级中心版本
        models.Node.objects.filter(role='self').update(
            version=version.get_version())
        # 获取本中心的组件
        self_nodes = models.Node.objects.filter(role='self')
        for self_node in self_nodes:
            console.info('self_node: {}'.format(self_node.uuid))
            comps = system_common.Component(self_node.agent).fetch_all()
            for comp in comps:
                key = system_common.node_components_cache_key.format(
                    **{
                        "agent_id": self_node.agent.id,
                        "node_uuid": self_node.uuid,
                        "component_name": comp
                    })
                cache.set(key, comps[comp], 24 * 60 * 60)

        # 活跃状态的中心每1小时发一次心跳,断线状态的中心心跳发送时间会越来越长
        time_now = timezone.now()
        # 不检查上级中心状态
        active_child_nodes = models.Node.objects.filter(
            next_check__lte=time_now,
            status=1).exclude(role__in=['self', 'parent'])
        for node in active_child_nodes:
            console.info("node: {}".format(node.uuid))
            good = self.send_heartbeat(node)

            if good:
                console.info('  => heartbeat good')
                # 获取下级中心的级联关系
                if node.role == 'child' and node.type == 'sub_center':
                    console.info("  => topo")
                    topo = self.fetch_node_topo(node)
                    # 缓存1天
                    cache.set(
                        system_common.node_child_topo_cache_key.format(
                            **{
                                "agent_id": node.agent.id,
                                "node_uuid": node.uuid
                            }), topo, 24 * 60 * 60)

                    # 获取中心的下级中心的组件 python manage.py check_node_heartbeat
                    children_components = self.fetch_node_children_compoments(
                        node)
                    console.info("  => children components")
                    for child in children_components:
                        console.info("    - {}".format(child))
                        for cc in children_components[child]:
                            cache.set(
                                system_common.node_components_cache_key.format(
                                    **{
                                        "agent_id": node.agent.id,
                                        "node_uuid": node.uuid,
                                        "component_name": cc
                                    }), children_components[child][cc],
                                24 * 60 * 60)

                # 获取中心的拓扑
                components = self.fetch_node_compoments(node)
                console.info("  => components")
                for com in components:
                    cache.set(
                        system_common.node_components_cache_key.format(
                            **{
                                "agent_id": node.agent.id,
                                "node_uuid": node.uuid,
                                "component_name": com
                            }), components[com], 24 * 60 * 60)
            else:
                console.info('  => heartbeat bad')

        # 检查上级中心状态
        parent_nodes = models.Node.objects.filter(role='parent')
        for parent in parent_nodes:
            console.info('  => parent heart {}'.format(parent.uuid))
            result = self.check_parent_heartbeat(parent)
            if result.get("status", 0) == 1:
                console.info(" => parent heart good")
                parent.status = 1
                parent.type = result.get('type')
                parent.name = result.get('name')
                parent.version = result.get("version")
                parent.last_heartbeat = timezone.now()
            else:
                logger.error(result)
                parent.status = 2
            parent.save()
            self.update_next_check_time(parent)
Example #25
0
def get_version():
    from utils.version import get_version
    return get_version(VERSION)
class LinuxCoreUtilities():
    """A test library providing keywords for common basic operations.

    `LinuxCoreUtilities` is GATS's standard library.
    """
    ROBOT_LIBRARY_SCOPE = 'GLOBAL'
    ROBOT_LIBRARY_VERSION = get_version()

    def open_connection(self,
                        host,
                        alias=None,
                        port=22,
                        timeout=None,
                        newline=None,
                        prompt=None,
                        term_type=None,
                        width=None,
                        height=None,
                        path_separator=None,
                        encoding=None):
        return G_SSHInstance.open_connection(host, alias, port, timeout,
                                             newline, prompt, term_type, width,
                                             height, path_separator, encoding)

    def get_current_connection_index(self):
        return G_SSHInstance.get_connection(index=True)

    def close_connection(self):
        G_SSHInstance.close_connection()

    def login(self, username, password, delay='0.5 seconds'):
        return G_SSHInstance.login(username, password, delay)

    def switch_connection(self, index_or_alias):
        return G_SSHInstance.switch_connection(index_or_alias)

    def close_all_connections(self):
        G_SSHInstance.close_all_connections()

    def execute_command(self,
                        command,
                        return_stdout=True,
                        return_stderr=False,
                        return_rc=False):
        return G_SSHInstance.execute_command(command, return_stdout,
                                             return_stderr, return_rc)

    def start_command(self, command):
        G_SSHInstance.start_command(command)

    def read_command_output(self,
                            return_stdout=True,
                            return_stderr=False,
                            return_rc=False):
        return G_SSHInstance.read_command_output(return_stdout, return_stderr,
                                                 return_rc)

    def block_dataflow_by_port_protocol(self, inOrOut, protocol, port):
        """using iptables command to block specific port and protocol data
        
         Argument:
            inOrOut  : either 'INPUT' or 'OUTPUT'
            protocol : UDP, TCP
            port     : port number
        
        Returns    : 

        Examples:
        | block dataflow by port protocol | INPUT | UDP | 9002
        """

        cmd = "/sbin/iptables -A %s -p %s --destination-port %s -j DROP" % (
            inOrOut, protocol, port)

        stdout, stderr, rc = _exec_command(cmd)
        if rc != 0 or stderr != '':
            raise AssertionError('*ERROR* cmd=%s, rc=%s, %s %s' %
                                 (cmd, rc, stdout, stderr))

        cmd = "/sbin/service iptables save"

        stdout, stderr, rc = _exec_command(cmd)
        if rc != 0 or stderr != '':
            raise AssertionError('*ERROR* cmd=%s, rc=%s, %s %s' %
                                 (cmd, rc, stdout, stderr))

    def check_processes(self, *process):
        """
        Check processes existence. Return Foundlist and NotFoundlist.
        
        Process is the process name, which can be provided one or more items.\n
        
        
        Return [Foundlist, NotFoundlist].

        Examples:
        | ${foundlist} | ${notfoundlist} | check processes | svchost | tts |
        | ${foundlist} | ${notfoundlist} | check processes | svchost | 
        ==>\n
        * check two processes 
        * check one process
        """
        return _check_process(list(process))

    def enable_disable_interface(self, interfaceName, status):
        """ Enable or disable the interface

            Argument : interfaceName - should be eth1 ... eth5
                       status - should be enable or disable

            Return :   None
            
            Examples :
            | enable disable interface| eth1 | enable |
        """
        if (status.lower() == 'enable'):
            cmd = 'ifup '
        elif (status.lower() == 'disable'):
            cmd = 'ifdown '
        else:
            raise AssertionError(
                '*ERROR* the status is %s, it should be enable or disable' %
                status)
        cmd = cmd + interfaceName
        stdout, stderr, rc = _exec_command(cmd)
        if rc != 0:
            raise AssertionError('*ERROR* cmd=%s, rc=%s, %s %s' %
                                 (cmd, rc, stdout, stderr))

    def find_processes_by_pattern(self, pattern):
        """
        Find processes that match the given pattern.  Return the matching 'ps' lines.

        Examples:
        | ${result}= | find processes by pattern | MTE -c MFDS1M |
        """
        cmd = "ps -ef | grep '%s' | grep -v grep" % pattern
        stdout, stderr, rc = _exec_command(cmd)
        return stdout.rstrip()

    def get_date_and_time(self):
        """
        Get the date and time value.
        
        The return value is list as [year, month, day, hour, min, second, dayofweek]. Each value is a string.
        dayofweek is 0~6. 0 is Sunday, 1~6 is Mon to Sat.

        Examples:
        | ${res} | get date and time |
        """
        return _get_datetime()

    def get_date_and_time_string(self):
        """
        Get the date and time value in string format.
        
        The return datetime string in format of 'YYYY-MM-DD hh:mm:ss', e.g. 2016-09-23 03:47:50.
        It is the supported Date format in robot framework.

        Examples:
        | ${datetime}= | get date and time string |
        """
        return _get_datetime_string()

    def get_day_of_week_as_string(self):
        """
        Get the current day of week from remote machine.
        
        The return value is a 3-char uppercase string [SUN, MON, TUE, WED, THU, FRI, SAT].

        Examples:
        | ${dow}= | get day of week as string |
        """
        dateString = ['SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT']
        dateInfo = _get_datetime()
        return dateString[int(dateInfo[6])]

    def get_day_of_week_from_date(self, year, month, day):
        """
        Get the day of week from given date.
        
        The return value is a 3-char uppercase string [SUN, MON, TUE, WED, THU, FRI, SAT].

        Examples:
        | ${dow}= | get day of week from date | 2015 | 10 | 31 |
        """
        return date(int(year), int(month), int(day)).strftime('%a').upper()

    def get_hostname(self):
        """
        Get the hostname of remote machine.

        Examples:
        | ${hostname} | get hostname |
        """
        cmd = 'hostname'
        stdout, stderr, rc = _exec_command(cmd)
        if rc != 0 or stderr != '':
            raise AssertionError('*ERROR* cmd=%s, rc=%s, %s %s' %
                                 (cmd, rc, stdout, stderr))
        return stdout.strip()

    def get_interface_name_by_alias(self, alias):
        """Get network ard interface name by given alias

        Argument alias specifies the alias name that we could find in /etc/hosts
        
        Returns string interfance name or prompt error

        Examples:
        | get interface by alias | DDNA |
        """

        aliasIp = self._get_alias_ip(alias)
        interfaceName = self.get_interface_name_by_ip(aliasIp)

        return interfaceName

    def get_interface_name_by_ip(self, ip):
        """Get network card interface name by given ip

        Argument ip specifies the ip address that used to find interface name
        
        Returns string interface name or prompt error

        Examples:
        | get interface name by ip | '192.168.56.10' |
        """

        #Checking if it is valid ip address
        ipComponents = ip.split('.')
        if (len(ipComponents) == 4):
            for component in ipComponents:
                if not (component.isdigit()):
                    raise AssertionError('*ERROR* Invalid IP address %s' % ip)

        cmd = 'ip addr' + '| grep "inet ' + ip + '/"' + '| awk \'BEGIN {FS=" "}{print $7}\''
        stdout, stderr, rc = _exec_command(cmd)

        if rc != 0 or stderr != '':
            raise AssertionError('*ERROR* cmd=%s, rc=%s, %s %s' %
                                 (cmd, rc, stdout, stderr))

        if len(stdout) > 0:
            return stdout.strip()

        raise AssertionError('*ERROR* Fail to get the interface name for %s' %
                             ip)

    def get_memory_usage(self):
        """
        Find the memory usage(%) from system
        
        """

        cmd = "egrep \'MemTotal\' /proc/meminfo | sed \'s/[^0-9]*//g\'"
        stdout, stderr, rc = _exec_command(cmd)
        if rc != 0 or stderr != '':
            raise AssertionError('*ERROR* cmd=%s, rc=%s, %s %s' %
                                 (cmd, rc, stdout, stderr))
        total = float(stdout)

        cmd = "egrep \'MemFree\' /proc/meminfo | sed \'s/[^0-9]*//g\'"
        stdout, stderr, rc = _exec_command(cmd)
        if rc != 0 or stderr != '':
            raise AssertionError('*ERROR* cmd=%s, rc=%s, %s %s' %
                                 (cmd, rc, stdout, stderr))
        free = float(stdout)

        usage = math.ceil(((total - free) / total) * 100)

        return usage

    def Get_process_and_pid_matching_pattern(self, *process_pattern):
        """
        From process patterns return dictionary contains pid and process_pattern
        Examples:
        | MTE -c ${MTE} | FMSClient  | SCWatchdog |
        
        """
        return _get_process_pid_pattern_dict(list(process_pattern))

    def get_time_before_midnight_in_seconds(self):
        """
        Get the time in seconds of remote TD machine before midnight
        E.g. Current local system time of remote TD machine = 23:58:55, it will return the seconds of (00:00:00 - 23:58:55) = 65.
        
        Examples:
        | ${sec} | get time before midnight in seconds |
        """
        #dateInfo return value is list as [year, month, day, hour, min, second, dayofweek]. Each value is a string.
        dateInfo = _get_datetime()
        hour = int(dateInfo[3])
        min = int(dateInfo[4])
        sec = int(dateInfo[5])

        currTimeInSec = hour * 60 * 60 + min * 60 + sec
        totalSec = 24 * 60 * 60
        return totalSec - currTimeInSec

    def get_time_in_microseconds(self):
        """
        Get the local system time of remote TD machine in microsecond from midnight

        Examples:
        | ${microsec} | get time in microseconds |
        """
        #dateInfo return value is list as [year, month, day, hour, min, second, dayofweek]. Each value is a string.
        dateInfo = _get_datetime()

        hour = int(dateInfo[3])
        min = int(dateInfo[4])
        sec = int(dateInfo[5])
        return (hour * 60 * 60 + min * 60 + sec) * 1000000

    def kill_processes(self, *Proc):
        """
        kill process.
        
        Proc is one or more process name.\n
        
        return [rc, not found list]. rc is 0 or 12. 12 mean some processes not found to kill.
        
        Examples:
        | ${res} | kill processes | dataview |
        | ${res} | kill processes | dataview | rdtplybk |
        """
        stdout = _return_pslist()
        pat = re.compile(r'\w+')
        psDict = {}
        PIDlist = []
        not_found_list = []
        # build mapping of process name to the list of associated process ids.
        # the pslist process name is currently limited to display first 15 chars, so we will compare only 15 chars
        for ps in stdout.splitlines():
            psInfo = ps.split()
            if len(psInfo) < 2:
                continue
            # get just the process name, remove everything after first non-alphanumeric
            psProcessName = re.match(pat, psInfo[1]).group()
            if psProcessName != None:
                psCompareProcessName = psProcessName.lower()[:15]
                if psCompareProcessName in psDict:
                    psDict[psCompareProcessName].append(psInfo[0])
                else:
                    psDict[psCompareProcessName] = [psInfo[0]]
        for process in list(Proc):
            compareProcessName = process.lower()[:15]
            if compareProcessName in psDict:
                PIDlist.extend(psDict[compareProcessName])
            else:
                not_found_list.append(process)
        if len(PIDlist):
            _kill_process(PIDlist)
        if len(not_found_list):
            return [12, not_found_list]
        else:
            return [0, []]

    def set_date_and_time(self, year, month, day, hour, min, sec):
        """
        Set the date and time value on Linux.
        
        All parameters can be string or integer. 
        
        The return value is 6 or 0. 6 mean set fail, 0 mean pass.
        
        *NOTE* This is used for Linux system, and if you only set date, please notice the time will be automatically set to 00:00:00.

        Examples:
        | ${res} | set date and time | 2014 | 5 | 6 | 12 | 23 | 59 |
        | ${res} | set date and time | ${2014} | ${5} | ${6} | ${12} | ${23} | ${59} |
        | ${res} | set date and time | ${EMPTY} | ${EMPTY} | ${EMPTY} | 12 | 23 | 59 |
        """
        print '*INFO* Setting date/time to: %04d-%02d-%02d %02d:%02d:%02d' % (
            int(year), int(month), int(day), int(hour), int(min), int(sec))
        return _set_datetime(year, month, day, hour, min, sec, 'linux')

    def set_date_and_time_Unix(self, year, month, day, hour, min, sec):
        """
        Set the date and time value on Unix.
        
        All parameters can be string or integer. 
        
        The return value is 6 or 0. 6 mean set fail, 0 mean pass.
        
        Examples:
        | ${res} | set date and time unix | 2014 | 5 | 6 | 12 | 23 | 59 |
        | ${res} | set date and time unix | ${2014} | ${5} | ${6} | ${12} | ${23} | ${59} |
        | ${res} | set date and time unix | ${EMPTY} | ${EMPTY} | ${EMPTY} | 12 | 23 | 59 |
        """
        print '*INFO* Setting date/time to: %04d-%02d-%02d %02d:%02d:%02d' % (
            int(year), int(month), int(day), int(hour), int(min), int(sec))
        return _set_datetime(year, month, day, hour, min, sec, 'unix')

    def show_processes(self):
        return _return_pslist()

    def unblock_dataflow(self):
        """using iptables command to unblock all ports
        
        Argument   :        
        Returns    : 

        Examples:
        | unblock dataflow | 
        """

        cmd = "iptables -F"

        stdout, stderr, rc = _exec_command(cmd)
        if rc != 0 or stderr != '':
            raise AssertionError('*ERROR* cmd=%s, rc=%s, %s %s' %
                                 (cmd, rc, stdout, stderr))

        cmd = "/sbin/service iptables save"

        stdout, stderr, rc = _exec_command(cmd)
        if rc != 0 or stderr != '':
            raise AssertionError('*ERROR* cmd=%s, rc=%s, %s %s' %
                                 (cmd, rc, stdout, stderr))

    def _get_alias_ip(self, alias):
        """Get ip address by given alias name
    
        Argument alias specifies the alias name that we could find in /etc/hosts
        
        Returns string 'null' if not found or ip address that matched with alias
    
        Examples:
        | get alias ip | 'DDNA' |
        """
        cmd = 'getent hosts ' + alias
        stdout, stderr, rc = _exec_command(cmd)

        if rc == 2:
            print '*INFO* no alias found for given ip'
            return 'null'

        if rc != 0 or stderr != '':
            raise AssertionError('*ERROR* cmd=%s, rc=%s, %s %s' %
                                 (cmd, rc, stdout, stderr))

        if len(stdout) > 0:
            listOfContent = stdout.split()
            return listOfContent[0]

        return 'null'

    def _get_all_interfaces_names(self):
        """Get the name for all avaliable interface from box
    
        Argument NIL
            
        Returns empty list or list of interface name
    
        Examples:
        | get all interfaces names |
        """

        listOfInterfaceName = []

        cmd = 'ip link show | awk \'/eth[0-9]/ {print $0}\''
        stdout, stderr, rc = _exec_command(cmd)

        if rc != 0 or stderr != '':
            raise AssertionError('*ERROR* cmd=%s, rc=%s, %s %s' %
                                 (cmd, rc, stdout, stderr))

        listOfContent = stdout.split('\n')
        for content in listOfContent:
            subcontent = content.split(':')
            if (len(subcontent) > 2):
                listOfInterfaceName.append(subcontent[1].lstrip())

        return listOfInterfaceName
Example #27
0
import __main__ as main
import datetime
import sys
import os

from utils.version import get_version

VERSION = (0, 0, 1, 'alpha', 0)

__version__ = get_version(VERSION)


def get_default_config():
    root_dir = os.path.expanduser("~")

    script_instance_path = os.path.join(root_dir, "lgsm-core")
    script_game_path = os.path.realpath(script_instance_path)

    try:
        if __name__ != "__main__":
            script_instance_path = main.__file__
    except:
        pass

    #root_dir = os.path.dirname(script_game_path)

    date_format = "%Y-%m-%d-%H-%M-%S"
    date_string = datetime.datetime.today().strftime(date_format)
    arch = sys.platform

    config = {