def main(): # the start and stop stuff will be handled from the freevo script if os.path.isdir(os.path.join(os.environ['FREEVO_PYTHON'], 'www/htdocs')): docRoot = os.path.join(os.environ['FREEVO_PYTHON'], 'www/htdocs') else: docRoot = os.path.join(config.SHARE_DIR, 'htdocs') root = static.File(docRoot) root.processors = { '.rpy': script.ResourceScript, } child_resources = [] child_resources.extend(config.VIDEO_ITEMS) child_resources.extend(config.AUDIO_ITEMS) child_resources.extend(config.IMAGE_ITEMS) child_resources.extend([('Recorded TV', config.TV_RECORD_DIR)]) child_resources.extend([('Webserver Cache', config.WEBSERVER_CACHEDIR)]) for item in child_resources: if isinstance(item, tuple) and len(item) == 2: (title, path) = item root.putChild(path.replace("/", "_"), static.File(path)) root.putChild('vhost', vhost.VHostMonsterResource()) rewriter = rewrite.RewriterResource(root, helpimagesrewrite) site = server.Site(rewriter) try: application = service.Application('web', uid=eval(uid), gid=eval(gid)) except Exception, e: application = service.Application('web') print e
def getChild(self, path, request): logger.info('Processing HTTP request: %s', request.path) logger.debug('HTTP request: %s', request) logger.debug('postpath: %s', request.postpath) try: device, pg_id = yield self._process_service.process(request, REQUEST_TYPE_HTTP) except Exception: logger.error('Error while processing HTTP request:', exc_info=True) defer.returnValue(ErrorPage(INTERNAL_SERVER_ERROR, 'Internal processing error', 'Internal processing error')) else: # Here we 'inject' the device object into the request object request.prov_dev = device service = self.default_service if pg_id in self._pg_mgr: plugin = self._pg_mgr[pg_id] if plugin.http_service is not None: _log_sensitive_request(plugin, request, REQUEST_TYPE_HTTP) service = self.service_factory(pg_id, plugin.http_service) # If the plugin specifies a path preprocessing method, use it if hasattr(service, 'path_preprocess'): logger.debug('Rewriting paths to the HTTP Service') service = rewrite.RewriterResource(service, service.path_preprocess) if service.isLeaf: request.postpath.insert(0, request.prepath.pop()) defer.returnValue(service) else: defer.returnValue(service.getChildWithDefault(path, request))
def createService(sname, config): global monitor_threads, monitors, servicename try: application = connexion.FlaskApp(__name__, specification_dir='swagger/') flask_app = application.app flask_app.url_map.strict_slashes = False anchore_engine.subsys.metrics.init_flask_metrics(flask_app, servicename=servicename) application.add_api('swagger.yaml') except Exception as err: traceback.print_exc() raise err try: myconfig = config['services'][sname] servicename = sname except Exception as err: raise err try: kick_timer = int(myconfig['cycle_timer_seconds']) except: kick_timer = 1 doapi = False try: if myconfig['listen'] and myconfig['port'] and myconfig['endpoint_hostname']: doapi = True except: doapi = False kwargs = {} kwargs['kick_timer'] = kick_timer kwargs['monitors'] = monitors kwargs['monitor_threads'] = monitor_threads kwargs['servicename'] = servicename if doapi: # start up flask service flask_site = WSGIResource(reactor, reactor.getThreadPool(), application=flask_app) realroot = Resource() realroot.putChild(b"v1", anchore_engine.services.common.getAuthResource(flask_site, sname, config)) realroot.putChild(b"health", anchore_engine.services.common.HealthResource()) # this will rewrite any calls that do not have an explicit version to the base path before being processed by flask root = rewrite.RewriterResource(realroot, default_version_rewrite) #root = anchore_engine.services.common.getAuthResource(flask_site, sname, config) ret_svc = anchore_engine.services.common.createServiceAPI(root, sname, config) # start up the monitor as a looping call lc = LoopingCall(anchore_engine.services.common.monitor, **kwargs) lc.start(1) else: # start up the monitor as a timer service svc = internet.TimerService(1, anchore_engine.services.common.monitor, **kwargs) svc.setName(sname) ret_svc = svc return (ret_svc)
def main(): options = parse_options() class PHPScript(twcgi.FilteredScript): filter = find_php() def runProcess(self, env, request, qargs=[]): env['SCRIPT_NAME'] = '/index.php' env['REDIRECT_STATUS'] = '200' twcgi.FilteredScript.runProcess(self, env, request, qargs) root = static.File(options.webroot) root.processors = {'.php' : PHPScript} root.indexNames = ['index.php'] def rewrite_rule(request): # Emulate Apache's mod_rewrite - if the file does not exist, then # rewrite as a suffix to '/index.php?url=' if not os.access("%s/%s" % (options.webroot, request.path), os.F_OK): request.uri = "/index.php?url=%s" % request.path request.postpath = ['index.php'] return request def logger_rule(request): print '[%s] "%s %s"' % \ (time.strftime("%d/%b/%Y %H:%M:%S"), request.method, request.path) if options.rewrite: root = rewrite.RewriterResource(root, rewrite_rule) if not options.quiet: root = rewrite.RewriterResource(root, logger_rule) try: reactor.listenTCP( options.port, server.Site(root), interface=options.interface, ) except error.CannotListenError, e: print >>sys.stderr, "%s: Couldn't listen on port %d: %s" % \ (sys.argv[0], options.port, e.socketError[1]) sys.exit(-1)
def _build_api_service(self): """ Once called, the resource is initialized. Any calls to self._add_resource() should be done before calling this fn. :return: """ wsgi_app = self.anchore_service.get_api_application() wsgi_site = wsgi.WSGIResource(reactor, reactor.getThreadPool(), application=wsgi_app) self._add_resource(self.anchore_service.__service_api_version__.encode('utf-8'), wsgi_site) self.root_resource = web.resource.Resource() # Add nodes for name, resource in self.resource_nodes.items(): self.root_resource.putChild(name, resource) # this will rewrite any calls that do not have an explicit version to the base path before being processed by flask self._api_version_bytes = self.anchore_service.__service_api_version__.encode('utf-8') # This is optimization # Handle the auth vs non-auth child resources to not consume a path element root = rewrite.RewriterResource(self.root_resource, self._default_version_rewrite) # Build the main site server site = server.Site(root) listen = self.anchore_service.configuration['listen'] if str(self.anchore_service.configuration.get('ssl_enable', '')).lower() == 'true': try: ssl_data = { 'ssl_cert': _load_ssl_cert(self.anchore_service.configuration['ssl_cert']) if 'ssl_cert' in self.anchore_service.configuration else None, 'ssl_chain': _load_ssl_cert(self.anchore_service.configuration['ssl_chain']) if 'ssl_chain' in self.anchore_service.configuration else None, 'ssl_key': _load_ssl_key(self.anchore_service.configuration['ssl_key']) if 'ssl_key' in self.anchore_service.configuration else None } if ssl_data['ssl_chain']: sfact = ssl.CertificateOptions(privateKey=ssl_data['ssl_key'], certificate=ssl_data['ssl_cert'], extraCertChain=[ssl_data['ssl_chain']]) else: sfact = ssl.CertificateOptions(privateKey=ssl_data['ssl_key'], certificate=ssl_data['ssl_cert']) endpoint = SSL4ServerEndpoint(reactor=reactor, port=int(self.anchore_service.configuration['port']), sslContextFactory=sfact, interface=listen) except Exception as err: raise err else: endpoint = TCP4ServerEndpoint(reactor=reactor, port=int(self.anchore_service.configuration['port']), interface=listen) ret_svc = StreamServerEndpointService(endpoint=endpoint, factory=site) ret_svc.setName(self.anchore_service.name) return ret_svc
def start(): root = static.File(os.environ['JPD_HTDOCS_PATH']) root.processors = {'.rpy': script.ResourceScript} root = rewrite.RewriterResource( root, rewrite.alias('cgi-bin/get_icon.py', 'get_icon.rpy')) site = JolicloudWSSite(root) site.addHandler('/jolicloud/', JolicloudWSHandler) # Setting up the log file path if os.environ.get('JPD_SYSTEM', '0') == '1': if os.getuid(): log.err('You must be root to run this daemon in system mode.') exit() log_path = '/var/log' else: try: import xdg.BaseDirectory log_path = xdg.BaseDirectory.save_data_path( 'Jolicloud', 'jolicloud-daemon') except ImportError: log_path = os.path.join(os.getenv('HOME'), '.local', 'share', 'Jolicloud', 'jolicloud-daemon') port = int( os.environ.get('JPD_PORT', 804 if os.environ.get('JPD_SYSTEM', None) else 8004)) # http://twistedmatrix.com/documents/9.0.0/web/howto/using-twistedweb.html#auto5 if os.environ.get('JPD_DEBUG', '0') == '1': log.startLogging(sys.stdout) log.startLogging( LogFile('jolicloud-daemon.log', log_path, maxRotatedFiles=2)) reactor.listenTCP(port, site) else: log.startLogging( LogFile('jolicloud-daemon.log', log_path, maxRotatedFiles=2)) reactor.listenTCP(port, site, interface='127.0.0.1') # TODO, use random port for session daemon # We load the plugins: if os.environ.get('JPD_SYSTEM', '0') == '1': log.msg('We load the system plugins.') plugins = getPlugins(ijolidaemon.ISystemManager, managers) else: log.msg('We load the session plugins.') plugins = getPlugins(ijolidaemon.ISessionManager, managers) for plugin in plugins: log.msg(plugin.__class__.__name__) reactor.run()
def main(): # the start and stop stuff will be handled from the freevo script logfile = '%s/%s-%s.log' % (config.LOGDIR, appname, os.getuid()) log.startLogging(open(logfile, 'a')) if os.path.isdir(os.path.join(os.environ['FREEVO_PYTHON'], 'www/htdocs')): docRoot = os.path.join(os.environ['FREEVO_PYTHON'], 'www/htdocs') else: docRoot = os.path.join(config.SHARE_DIR, 'htdocs') root = static.File(docRoot) root.processors = { '.rpy': script.ResourceScript, } child_resources = [] child_resources.extend(config.VIDEO_ITEMS) child_resources.extend(config.AUDIO_ITEMS) child_resources.extend(config.IMAGE_ITEMS) child_resources.extend([('Recorded TV', config.TV_RECORD_DIR)]) child_resources.extend([('Webserver Cache', config.WEBSERVER_CACHEDIR)]) for item in child_resources: if isinstance(item, tuple) and len(item) == 2: (title, path) = item root.putChild(path.replace("/", "_"), static.File(path)) root.putChild('vhost', vhost.VHostMonsterResource()) rewriter = rewrite.RewriterResource(root, helpimagesrewrite) if (config.DEBUG == 0): site = server.Site(rewriter, logPath='/dev/null') else: site = server.Site(rewriter) application = app.Application('web') application.listenTCP(config.WEBSERVER_PORT, site) application.run(save=0)
def createService(sname, config): global application flask_site = WSGIResource(reactor, reactor.getThreadPool(), application=application) # original (with base_path="/v1" in swagger.yaml) #root = anchore_engine.services.common.getAuthResource(flask_site, sname, config) # new (with base_path="/" in swagger.yaml) realroot = Resource() realroot.putChild( b"v1", anchore_engine.services.common.getAuthResource(flask_site, sname, config)) realroot.putChild(b"health", anchore_engine.services.common.HealthResource()) # this will rewrite any calls that do not have an explicit version to the base path before being processed by flask root = rewrite.RewriterResource(realroot, default_version_rewrite) return (anchore_engine.services.common.createServiceAPI( root, sname, config))
def _build_api_service(self): """ Once called, the resource is initialized. Any calls to self._add_resource() should be done before calling this fn. :return: """ thread_count = int( self.service_config.get('max_request_threads', localconfig.DEFAULT_SERVICE_THREAD_COUNT)) wsgi_app = self.anchore_service.get_api_application() wsgi_site = wsgi.WSGIResource(reactor, reactor.getThreadPool(), application=wsgi_app) reactor.getThreadPool().adjustPoolsize(maxthreads=thread_count) logger.debug('Thread pool size stats. Min={}, Max={}'.format( reactor.getThreadPool().min, reactor.getThreadPool().max)) self._add_resource( self.anchore_service.__service_api_version__.encode('utf-8'), wsgi_site) if enable_thread_dumper: logger.warn( "Adding thread dump route for debugging since debug flag is set. This is dangerous and should not be done in normal production" ) self._add_resource(b'threads', ThreadDumperResource()) self.root_resource = web.resource.Resource() # Add nodes for name, resource in self.resource_nodes.items(): self.root_resource.putChild(name, resource) # this will rewrite any calls that do not have an explicit version to the base path before being processed by flask self._api_version_bytes = self.anchore_service.__service_api_version__.encode( 'utf-8') # This is optimization # Handle the auth vs non-auth child resources to not consume a path element root = rewrite.RewriterResource(self.root_resource, self._default_version_rewrite) # Build the main site server site = server.Site(root) listen = self.anchore_service.configuration['listen'] # Disable the twisted access logging by overriding the log function as it uses a raw 'write' and cannot otherwise be disabled, iff enable_access_logging is set to False in either the service or global config try: eal = True if "enable_access_logging" in self.anchore_service.configuration: eal = self.anchore_service.configuration.get( "enable_access_logging", True) elif "enable_access_logging" in self.configuration: eal = self.configuration.get("enable_access_logging", True) if not eal: def _null_logger(request): pass site.log = _null_logger except: pass if str(self.anchore_service.configuration.get('ssl_enable', '')).lower() == 'true': try: ssl_data = { 'ssl_cert': _load_ssl_cert( self.anchore_service.configuration['ssl_cert']) if 'ssl_cert' in self.anchore_service.configuration else None, 'ssl_chain': _load_ssl_cert( self.anchore_service.configuration['ssl_chain']) if 'ssl_chain' in self.anchore_service.configuration else None, 'ssl_key': _load_ssl_key( self.anchore_service.configuration['ssl_key']) if 'ssl_key' in self.anchore_service.configuration else None } if ssl_data['ssl_chain']: sfact = ssl.CertificateOptions( privateKey=ssl_data['ssl_key'], certificate=ssl_data['ssl_cert'], extraCertChain=[ssl_data['ssl_chain']]) else: sfact = ssl.CertificateOptions( privateKey=ssl_data['ssl_key'], certificate=ssl_data['ssl_cert']) endpoint = SSL4ServerEndpoint( reactor=reactor, port=int(self.anchore_service.configuration['port']), sslContextFactory=sfact, interface=listen) except Exception as err: raise err else: endpoint = TCP4ServerEndpoint( reactor=reactor, port=int(self.anchore_service.configuration['port']), interface=listen) ret_svc = StreamServerEndpointService(endpoint=endpoint, factory=site) ret_svc.setName(self.anchore_service.name) return ret_svc