Пример #1
0
def init_script():
    _init()
    _init_db()
    
    cnf = bus.cnf
    cnf.bootstrap()
    ini = cnf.rawini

    szr_logger = logging.getLogger('scalarizr')
    for hd in list(szr_logger.handlers):
        if 'MessagingHandler' in hd.__class__.__name__:
            szr_logger.handlers.remove(hd)
    
    logger = logging.getLogger(__name__)
    logger.debug("Initialize script messaging")

    # Script producer url is scalarizr consumer url. 
    # Script can't handle any messages by himself. Leave consumer url blank
    adapter = ini.get(config.SECT_MESSAGING, config.OPT_ADAPTER)    
    kwargs = dict(ini.items("messaging_" + adapter))
    kwargs[P2pConfigOptions.SERVER_ID] = ini.get(config.SECT_GENERAL, config.OPT_SERVER_ID)
    kwargs[P2pConfigOptions.CRYPTO_KEY_PATH] = cnf.key_path(cnf.DEFAULT_KEY)
    kwargs[P2pConfigOptions.PRODUCER_URL] = kwargs[P2pConfigOptions.CONSUMER_URL]
    
    factory = MessageServiceFactory()        
    msg_service = factory.new_service(adapter, **kwargs)
    bus.messaging_service = msg_service
Пример #2
0
def init_script():
    _init()
    _init_db()

    cnf = bus.cnf
    cnf.bootstrap()
    ini = cnf.rawini

    szr_logger = logging.getLogger('scalarizr')
    for hd in list(szr_logger.handlers):
        if 'MessagingHandler' in hd.__class__.__name__:
            szr_logger.handlers.remove(hd)

    logger = logging.getLogger(__name__)
    logger.debug("Initialize script messaging")

    # Script producer url is scalarizr consumer url.
    # Script can't handle any messages by himself. Leave consumer url blank
    adapter = ini.get(config.SECT_MESSAGING, config.OPT_ADAPTER)
    kwargs = dict(ini.items("messaging_" + adapter))
    kwargs[P2pConfigOptions.SERVER_ID] = ini.get(config.SECT_GENERAL,
                                                 config.OPT_SERVER_ID)
    kwargs[P2pConfigOptions.CRYPTO_KEY_PATH] = cnf.key_path(cnf.DEFAULT_KEY)
    kwargs[P2pConfigOptions.PRODUCER_URL] = kwargs[
        P2pConfigOptions.CONSUMER_URL]

    factory = MessageServiceFactory()
    msg_service = factory.new_service(adapter, **kwargs)
    bus.messaging_service = msg_service
Пример #3
0
	def __init__(self):
		cnf = bus.cnf
		f = MessageServiceFactory()
		self._msg_service = f.new_service("p2p", **{
			P2pConfigOptions.SERVER_ID : cnf.rawini.get(config.SECT_GENERAL, config.OPT_SERVER_ID),
			P2pConfigOptions.CRYPTO_KEY_PATH : cnf.key_path(cnf.FARM_KEY),
			P2pConfigOptions.CONSUMER_URL : 'http://0.0.0.0:8012',
			P2pConfigOptions.MSG_HANDLER_ENABLED : False
		})
Пример #4
0
 def __init__(self):
     cnf = bus.cnf
     f = MessageServiceFactory()
     self._msg_service = f.new_service(
         "p2p", **{
             'server_id':
             cnf.rawini.get(config.SECT_GENERAL, config.OPT_SERVER_ID),
             'crypto_key_path':
             cnf.key_path(cnf.FARM_KEY),
             'consumer_url':
             'http://0.0.0.0:8012',
             'msg_handler_enabled':
             False
         })
Пример #5
0
 def __init__(self):
     cnf = bus.cnf
     f = MessageServiceFactory()
     self._msg_service = f.new_service(
         "p2p", **{
             P2pConfigOptions.SERVER_ID:
             cnf.rawini.get(config.SECT_GENERAL, config.OPT_SERVER_ID),
             P2pConfigOptions.CRYPTO_KEY_PATH:
             cnf.key_path(cnf.FARM_KEY),
             P2pConfigOptions.CONSUMER_URL:
             'http://0.0.0.0:8012',
             P2pConfigOptions.MSG_HANDLER_ENABLED:
             False
         })
Пример #6
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 = QueryEnvService(queryenv_url, server_id, cnf.key_path(cnf.DEFAULT_KEY), queryenv.get_latest_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:
		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', 8010, api_app)
Пример #7
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)
Пример #8
0
    def _init_services(self):
        logger = logging.getLogger(__name__)
        cnf = bus.cnf
        ini = cnf.rawini
        messaging_adp = ini.get('messaging', 'adapter')

        # Set base URL
        pr = urlparse(__node__['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._ensure_resolver(bus.scalr_url)

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

        logger.debug("Initialize QueryEnv client")
        queryenv = new_queryenv()

        if tuple(map(int,
                     STATE['queryenv.api_version']['version'].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('-')))

        self._init_bollard()

        if __node__['state'] != 'importing':
            lfrp = bus.queryenv_service.list_farm_role_params(
                __node__['farm_role_id'])['params']
            __node__['base'].update(lfrp.get('base'))
            if __node__['base'][
                    'union_script_executor'] and not linux.os.windows:
                # Replace script execution with a new system
                bus.cnf.rawini.remove_option('handlers', 'script_executor')
                bus.cnf.rawini.remove_option('handlers', 'chef')
                bus.cnf.rawini.set('handlers', 'union_base',
                                   'scalarizr.handlers.union.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['consumer_url']))
                consumer_url[1] = ':'.join(
                    (consumer_url[1].split(':')[0],
                     str(__node__['base']['messaging_port'])))
                params['consumer_url'] = urlunparse(consumer_url)

            params['server_id'] = __node__['server_id']
            params['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

        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)
Пример #9
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)
Пример #10
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)
Пример #11
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)
Пример #12
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)

        # 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())

        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:
            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_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',
                                                api_port, api_app, server_class=ThreadingWSGIServer)
Пример #13
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)