Exemple #1
0
def new_queryenv():
    queryenv_creds = (__node__['queryenv_url'],
                      __node__['server_id'],
                      os.path.join(scalr_base_dir, __node__['crypto_key_path']))
    queryenv = QueryEnvService(*queryenv_creds)
    api_version = queryenv.get_latest_version()
    queryenv = QueryEnvService(*queryenv_creds, api_version=api_version, autoretry=False) 
    return queryenv
Exemple #2
0
def new_queryenv():
    init_cnf()
    key_path = os.path.join(bus.etc_path, ini.get('general',
                                                  'crypto_key_path'))
    server_id = ini.get('general', 'server_id')
    url = ini.get('general', 'queryenv_url')
    api_version = QUERYENV_API_VERSION
    if not api_version:
        cnf = bus.cnf
        if not bus.scalr_version:
            version_file = cnf.private_path('.scalr-version')
            if os.path.exists(version_file):
                with open(version_file, 'r') as fp:
                    bus.scalr_version = tuple(fp.read().strip().split('.'))

        if bus.scalr_version:
            if bus.scalr_version >= (3, 5, 3):
                api_version = '2012-07-01'
            elif bus.scalr_version >= (3, 1, 0):
                api_version = '2012-04-17'
            else:
                api_version = '2010-09-23'
        else:
            api_version = '2012-07-01'
    return QueryEnvService(url, server_id, key_path, api_version)
Exemple #3
0
def prepare_snmp():
    _init()
    cnf = bus.cnf
    ini = cnf.rawini
    cnf.on('apply_user_data', _apply_user_data)
    cnf.bootstrap()

    server_id = ini.get('general', 'server_id')
    queryenv_url = ini.get('general', 'queryenv_url')
    queryenv = QueryEnvService(queryenv_url, server_id,
                               cnf.key_path(cnf.DEFAULT_KEY))

    bus.queryenv_service = queryenv

    snmp_server = SnmpServer(
        port=int(ini.get(config.SECT_SNMP, config.OPT_PORT)),
        security_name=ini.get(config.SECT_SNMP, config.OPT_SECURITY_NAME),
        community_name=ini.get(config.SECT_SNMP, config.OPT_COMMUNITY_NAME))
    return snmp_server
Exemple #4
0
    def _remove_add_servers_from_queryenv(self):
        cnf = ScalarizrCnf(bus.etc_path)
        cnf.bootstrap()
        globals()['ini'] = cnf.rawini
        key_path = os.path.join(bus.etc_path,
                                ini.get('general', 'crypto_key_path'))
        server_id = ini.get('general', 'server_id')
        url = ini.get('general', 'queryenv_url')
        queryenv = QueryEnvService(url, server_id, key_path)
        result = queryenv.list_roles()
        running_servers = []

        bnds = []
        for elem in self.api.list_listeners():
            bnds.append(elem['backend'])
        bnds = list(set(bnds))

        for bnd in bnds:
            for srv in self.api.list_servers(backend=bnd):
                self.api.remove_server(ipaddr=srv, backend=bnd)

        for d in result:
            behaviour = ', '.join(d.behaviour)
            for host in d.hosts:
                try:
                    if 'role:%s' % d.farm_role_id in bnds:
                        self.api.add_server(ipaddr=host.internal_ip,
                                            backend='role:%s' % d.farm_role_id)
                except:
                    LOG.warn(
                        'HAProxyHandler.on_before_host_up.Failed add_server `%s` in'
                        ' backend=`role:%s`, details: %s' %
                        (host.internal_ip.replace(
                            '.', '-'), d.farm_role_id, sys.exc_info()[1]))
                running_servers.append([d.farm_role_id, host.internal_ip])
        LOG.debug('running_servers: `%s`', running_servers)
Exemple #5
0
def _init_services():
    logger = logging.getLogger(__name__)
    cnf = bus.cnf
    ini = cnf.rawini
    server_id = ini.get('general', 'server_id')
    queryenv_url = ini.get('general', 'queryenv_url')
    messaging_adp = ini.get('messaging', 'adapter')

    # Set base URL
    pr = urlparse(queryenv_url)
    bus.scalr_url = urlunparse((pr.scheme, pr.netloc, '', '', '', ''))
    logger.debug("Got scalr url: '%s'" % bus.scalr_url)

    # Create periodical executor for background tasks (cleanup, rotate, gc, etc...)
    bus.periodical_executor = PeriodicalExecutor()

    logger.debug("Initialize QueryEnv client")
    queryenv = QueryEnvService(queryenv_url, server_id,
                               cnf.key_path(cnf.DEFAULT_KEY), '2008-12-16')
    queryenv_latest = queryenv.get_latest_version()
    queryenv = QueryEnvService(queryenv_url, server_id,
                               cnf.key_path(cnf.DEFAULT_KEY), queryenv_latest)

    if tuple(map(int, queryenv_latest.split('-'))) >= (2012, 7, 1):
        scalr_version = queryenv.get_global_config()['params'].get(
            'scalr.version')
        if scalr_version:
            bus.scalr_version = tuple(map(int, scalr_version.split('.')))
            version_file = cnf.private_path('.scalr-version')
            with open(version_file, 'w') as fp:
                fp.write(scalr_version)

    bus.queryenv_service = queryenv
    bus.queryenv_version = tuple(map(int, queryenv.api_version.split('-')))

    logger.debug("Initialize messaging")
    factory = MessageServiceFactory()
    try:
        params = dict(ini.items("messaging_" + messaging_adp))
        params[P2pConfigOptions.SERVER_ID] = server_id
        params[P2pConfigOptions.CRYPTO_KEY_PATH] = cnf.key_path(
            cnf.DEFAULT_KEY)

        msg_service = factory.new_service(messaging_adp, **params)
        bus.messaging_service = msg_service
    except (BaseException, Exception):
        raise ScalarizrError("Cannot create messaging service adapter '%s'" %
                             (messaging_adp))

    logger.debug('Initialize message handlers')
    consumer = msg_service.get_consumer()
    consumer.listeners.append(MessageListener())

    logger.debug('Schedule SNMP process')
    globals()['_snmp_scheduled_start_time'] = time.time()

    Storage.maintain_volume_table = True

    if not bus.api_server:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        api_port = 8010
        try:
            sock.connect(('0.0.0.0', api_port))
            STATE['global.api_port'] = api_port = 8009
            sock.close()
        except socket.error:
            pass
        api_app = jsonrpc_http.WsgiApplication(rpc.RequestHandler(_api_routes),
                                               cnf.key_path(cnf.DEFAULT_KEY))
        bus.api_server = wsgiref.simple_server.make_server(
            '0.0.0.0', api_port, api_app)
Exemple #6
0
    def _init_services(self):
        logger = logging.getLogger(__name__)
        cnf = bus.cnf
        ini = cnf.rawini
        server_id = ini.get('general', 'server_id')
        queryenv_url = ini.get('general', 'queryenv_url')
        messaging_adp = ini.get('messaging', 'adapter')

        # Set base URL
        pr = urlparse(queryenv_url)
        bus.scalr_url = urlunparse((pr.scheme, pr.netloc, '', '', '', ''))
        logger.debug("Got scalr url: '%s'" % bus.scalr_url)

        if not linux.os.windows and node.__node__['platform'].name in (
                'eucalyptus', 'openstack'):
            self._try_resolver(bus.scalr_url)

        # Create periodical executor for background tasks (cleanup, rotate, gc, etc...)
        bus.periodical_executor = PeriodicalExecutor()

        logger.debug("Initialize QueryEnv client")
        queryenv = QueryEnvService(queryenv_url, server_id,
                                   cnf.key_path(cnf.DEFAULT_KEY), '2008-12-16')
        queryenv_latest = queryenv.get_latest_version()
        queryenv = QueryEnvService(queryenv_url, server_id,
                                   cnf.key_path(cnf.DEFAULT_KEY),
                                   queryenv_latest)

        if tuple(map(int, queryenv_latest.split('-'))) >= (2012, 7, 1):
            scalr_version = queryenv.get_global_config()['params'].get(
                'scalr.version')
            if scalr_version:
                bus.scalr_version = tuple(map(int, scalr_version.split('.')))
                version_file = cnf.private_path('.scalr-version')
                with open(version_file, 'w') as fp:
                    fp.write(scalr_version)

        bus.queryenv_service = queryenv
        bus.queryenv_version = tuple(map(int, queryenv.api_version.split('-')))

        ports_non_default = self._select_control_ports()

        logger.debug("Initialize messaging")
        factory = MessageServiceFactory()
        try:
            params = dict(ini.items("messaging_" + messaging_adp))
            if ports_non_default:
                consumer_url = list(
                    urlparse(params[P2pConfigOptions.CONSUMER_URL]))
                consumer_url[1] = ':'.join(
                    (consumer_url[1].split(':')[0],
                     str(node.__node__['base']['messaging_port'])))
                params[P2pConfigOptions.CONSUMER_URL] = urlunparse(
                    consumer_url)

            params[P2pConfigOptions.SERVER_ID] = server_id
            params[P2pConfigOptions.CRYPTO_KEY_PATH] = cnf.key_path(
                cnf.DEFAULT_KEY)

            msg_service = factory.new_service(messaging_adp, **params)
            bus.messaging_service = msg_service
        except (BaseException, Exception):
            raise ScalarizrError(
                "Cannot create messaging service adapter '%s'" %
                (messaging_adp))

        logger.debug('Initialize message handlers')
        consumer = msg_service.get_consumer()
        consumer.listeners.append(MessageListener())

        if not linux.os.windows_family:
            logger.debug('Schedule SNMP process')
            self._snmp_scheduled_start_time = time.time()

        Storage.maintain_volume_table = True

        if not bus.api_server:
            api_app = jsonrpc_http.WsgiApplication(
                rpc.RequestHandler(_api_routes), cnf.key_path(cnf.DEFAULT_KEY))

            class ThreadingWSGIServer(SocketServer.ThreadingMixIn,
                                      wsgiref.simple_server.WSGIServer):
                pass

            bus.api_server = wsgiref.simple_server.make_server(
                '0.0.0.0',
                node.__node__['base']['api_port'],
                api_app,
                server_class=ThreadingWSGIServer)

        if ports_non_default:
            msg = msg_service.new_message(
                'HostUpdate', None, {
                    'base': {
                        'api_port': node.__node__['base']['api_port'],
                        'messaging_port':
                        node.__node__['base']['messaging_port']
                    }
                })
            msg_service.get_producer().send(Queues.CONTROL, msg)
Exemple #7
0
 def setUp(self):
     self._queryenv = QueryEnvService(
         "http://ec2farm-dev.bsd2.webta.local/query-env/", "127",
         "c+/g0PyouaqXMbuJ5Vtux34Mft7jLe5H5u8tUmyhldjwTfgm7BI6MOA8F6BwkzQnpWEOcHx+A+TRJh0u3PElQQ0SiwdwrlgpQMbj8NBxbxBgfxA9WisgvfQu5ZPYou6Gz3oUAQdWfFlFdY2ACOjmqa3DGogge+TlXtV2Xagm0rw="
     )
Exemple #8
0
    def _init_services(self):
        logger = logging.getLogger(__name__)
        cnf = bus.cnf
        ini = cnf.rawini
        server_id = ini.get('general', 'server_id')
        queryenv_url = ini.get('general', 'queryenv_url')
        messaging_adp = ini.get('messaging', 'adapter')

        # Set base URL
        pr = urlparse(queryenv_url)
        bus.scalr_url = urlunparse((pr.scheme, pr.netloc, '', '', '', ''))
        logger.debug("Got scalr url: '%s'" % bus.scalr_url)

        if not linux.os.windows and __node__['platform'].name == 'openstack':
            self._try_resolver(bus.scalr_url)

        # Create periodical executor for background tasks (cleanup, rotate, gc, etc...)
        bus.periodical_executor = PeriodicalExecutor()

        logger.debug("Initialize QueryEnv client")
        queryenv = QueryEnvService(queryenv_url, server_id,
                                   cnf.key_path(cnf.DEFAULT_KEY), '2008-12-16')
        queryenv_latest = queryenv.get_latest_version()
        queryenv = QueryEnvService(queryenv_url, server_id,
                                   cnf.key_path(cnf.DEFAULT_KEY),
                                   queryenv_latest)

        if tuple(map(int, queryenv_latest.split('-'))) >= (2012, 7, 1):
            scalr_version = queryenv.get_global_config()['params'].get(
                'scalr.version')
            if scalr_version:
                bus.scalr_version = tuple(map(int, scalr_version.split('.')))
                version_file = cnf.private_path('.scalr-version')
                with open(version_file, 'w') as fp:
                    fp.write(scalr_version)

        bus.queryenv_service = queryenv
        bus.queryenv_version = tuple(map(int, queryenv.api_version.split('-')))

        if __node__['state'] != 'importing':
            lfrp = bus.queryenv_service.list_farm_role_params(
                __node__['farm_role_id'])['params']
            __node__['base'].update(lfrp.get('base', {}))
        ports_non_default = self._select_control_ports()

        logger.debug("Initialize messaging")
        factory = MessageServiceFactory()
        try:
            params = dict(ini.items("messaging_" + messaging_adp))
            if ports_non_default:
                consumer_url = list(
                    urlparse(params[P2pConfigOptions.CONSUMER_URL]))
                consumer_url[1] = ':'.join(
                    (consumer_url[1].split(':')[0],
                     str(__node__['base']['messaging_port'])))
                params[P2pConfigOptions.CONSUMER_URL] = urlunparse(
                    consumer_url)

            params[P2pConfigOptions.SERVER_ID] = server_id
            params[P2pConfigOptions.CRYPTO_KEY_PATH] = cnf.key_path(
                cnf.DEFAULT_KEY)

            msg_service = factory.new_service(messaging_adp, **params)
            bus.messaging_service = msg_service
        except (BaseException, Exception):
            raise ScalarizrError(
                "Cannot create messaging service adapter '%s'" %
                (messaging_adp))

        optparser = bus.optparser
        if optparser and not optparser.values.import_server and linux.os[
                'family'] != 'Windows':
            system_packages = pkgmgr.package_mgr().list()
            for behavior in __node__['behavior']:
                if behavior in ['base', 'mongodb'
                                ] or behavior not in api.api_routes.keys():
                    continue
                try:
                    api_cls = util.import_class(api.api_routes[behavior])
                    api_cls.check_software(system_packages)
                except exceptions.NotFound as e:
                    logger.error(e)
                except exceptions.UnsupportedBehavior as e:
                    if e.args[0] == 'chef':
                        # We pass it, cause a lot of roles has chef behavior without chef installed on them
                        continue
                    __node__['messaging'].send('RuntimeError',
                                               body={
                                                   'code':
                                                   'UnsupportedBehavior',
                                                   'message': str(e)
                                               })
                    raise sys.exc_info()[0], sys.exc_info()[1], sys.exc_info(
                    )[2]

        logger.debug('Initialize message handlers')
        consumer = msg_service.get_consumer()
        consumer.listeners.append(MessageListener())

        producer = msg_service.get_producer()

        def msg_meta(queue, message):
            """
            Add scalarizr version to meta
            """
            message.meta.update({
                'szr_version':
                __version__,
                'timestamp':
                os_time.utcnow().strftime("%a %d %b %Y %H:%M:%S %z")
            })

        producer.on('before_send', msg_meta)

        Storage.maintain_volume_table = True

        if not bus.api_server:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            api_port = 8010
            try:
                sock.connect(('0.0.0.0', api_port))
                api_port = 8009
                sock.close()
            except socket.error:
                pass
            STATE['global.api_port'] = api_port
            api_app = jsonrpc_http.WsgiApplication(
                rpc.RequestHandler(api.api_routes),
                cnf.key_path(cnf.DEFAULT_KEY))

            class ThreadingWSGIServer(SocketServer.ThreadingMixIn,
                                      wsgiref.simple_server.WSGIServer):
                pass

            bus.api_server = wsgiref.simple_server.make_server(
                '0.0.0.0',
                __node__['base']['api_port'],
                api_app,
                server_class=ThreadingWSGIServer)

        if ports_non_default:
            msg = msg_service.new_message(
                'HostUpdate', None, {
                    'base': {
                        'api_port': __node__['base']['api_port'],
                        'messaging_port': __node__['base']['messaging_port']
                    }
                })
            msg_service.get_producer().send(Queues.CONTROL, msg)