def _start_api(self): LOG.info('Starting API on port %s', self.api.api_port) try: wsgi_app = jsonrpc_http.WsgiApplication( rpc.RequestHandler(self.api), self.api.crypto_file) class ThreadingWSGIServer(SocketServer.ThreadingMixIn, wsgiref.simple_server.WSGIServer): pass self.api_server = wsgiref.simple_server.make_server( '0.0.0.0', int(self.api.api_port), wsgi_app, server_class=ThreadingWSGIServer) except socket.error: LOG.error('Cannot create API server on port %s', self.api.api_port) raise if linux.os.windows_family: def serve(): try: self.api_server.serve_forever() except: LOG.exception('API thread died unexpectedly') self.api_thread = threading.Thread(target=serve) self.api_thread.start()
def app_creator(): app = jsonrpc_http.WsgiApplication( rpc.RequestHandler({'myservice': MyService()}), self.crypto_key_path) app.req_handler.handle_request = mock.Mock( side_effect=Exception('error in handle request')) return app
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)
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)
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)
def app_creator(): return jsonrpc_http.WsgiApplication( rpc.RequestHandler({'myservice': MyService()}), self.crypto_key_path)
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)