def run_apiserver(system_config, engine, http_port): """ Run api server on the specified port. """ registry().system_config = system_config registry().engine = engine schema = graphene.Schema(query=Query, mutation=Mutation, subscription=Subscription) logger.info('Starting GraphQL server on %s' % http_port) logger.info(' GraphiQL: http://localhost:%s/graphiql' % http_port) logger.info(' Queries and Mutations: http://localhost:%s/graphql' % http_port) logger.info(' Subscriptions: ws://localhost:%s/subscriptions' % http_port) app_endpoints = [ (r'/subscriptions', GraphQLSubscriptionHandler, dict(opts=SETTINGS)), (r'/graphql', GraphQLHandler), (r'/graphiql', GraphiQLHandler), (r'/.*', FallbackHandler) ] TornadoQL.schema = schema app = tornado.web.Application(app_endpoints, **SETTINGS) app.listen(http_port) tornado.ioloop.IOLoop.current().start()
async def project_stop_impl(subject: ReplaySubject, project_name: str, services: List[str]): try: project = load_single_project(project_name).config except GraphQLError as ex: subject.on_next(StartStopEndStep( error_string=str(ex), is_fatal_error=True )) subject.on_completed() return engine = registry().engine if len(services) < 1: services = project["app"]["services"].keys() last_steps: Dict[str, int] = {} try: async for service_name, status, finished in engine.stop_project(project, services): _handle_update(finished, last_steps, service_name, status, subject, "Service stopped!") except Exception as err: print(traceback.format_exc()) subject.on_next(StartStopEndStep( error_string="Error stopping the services: " + str(err), is_fatal_error=True )) else: subject.on_next(StartStopEndStep()) subject.on_completed()
async def update_repositories_impl(subject: ReplaySubject): subject.on_next(ResultStep( steps=2, current_step=1, text="Starting repository update..." )) try: repositories.update(registry().system_config, lambda msg: subject.on_next(ResultStep( steps=2, current_step=1, text=msg ))) except Exception as e: subject.on_next(ResultStep( steps=2, current_step=1, text="Fatal error during repository update: " + str(e), is_end=True, is_error=True )) subject.on_completed() return subject.on_next(ResultStep( steps=2, current_step=2, text="Reloading configuration..." )) # Reload system config try: config_path = riptide_main_config_file() system_config = Config.from_yaml(config_path) system_config.validate() registry.system_config = system_config except FileNotFoundError as e: subject.on_next(ResultStep( steps=2, current_step=2, text="Main config file not found! Could not reload system config.", is_end=True, is_error=True )) except Exception as e: subject.on_next(ResultStep( steps=2, current_step=2, text="Could not reload system config: " + str(e), is_end=True, is_error=True )) else: subject.on_next(ResultStep( steps=2, current_step=2, text="Repository update done!", is_end=True )) finally: subject.on_completed()
def get_for_external(system_config, engine, hostname): """ Return Tornado routes for use in external servers """ registry().system_config = system_config registry().engine = engine schema = graphene.Schema(query=Query, mutation=Mutation, subscription=Subscription) TornadoQL.schema = schema return [ (HostnameMatcher(r'/subscriptions', hostname), GraphQLSubscriptionHandler, dict(opts=SETTINGS)), (HostnameMatcher(r'/graphql', hostname), GraphQLHandler), (HostnameMatcher(r'/graphiql', hostname), GraphiQLHandler), (HostnameMatcher(r'/.*', hostname), FallbackHandler) ]
async def db_copy_impl(subject: ReplaySubject, project_name: str, source: str, target:str, switch: bool): project = try_loading_project(project_name, subject, 1, 4) if not project: return engine = registry().engine dbenv = DbEnvironments(project, engine) try: subject.on_next(ResultStep( steps=4, current_step=1, text=f"Copying database environment {source} to {target}..." )) dbenv.new(target, copy_from=source) except FileNotFoundError: subject.on_next(ResultStep( steps=4, current_step=1, text=f"Environment {source} not found.", is_end=True, is_error=True )) except FileExistsError: subject.on_next(ResultStep( steps=4, current_step=1, text=f"Environment with this name already exists.", is_end=True, is_error=True )) except NameError: subject.on_next(ResultStep( steps=4, current_step=1, text=f"Invalid name for new environment, do not use special characters.", is_end=True, is_error=True )) except Exception as ex: subject.on_next(ResultStep( steps=4, current_step=1, text=f"Error creating environment: {ex}", is_end=True, is_error=True )) else: if switch: await db_switch_impl(subject, project, target, 1, 1) else: subject.on_next(ResultStep( steps=4, current_step=4, text=f"Finished creating environment: {target}.", is_end=True ))
async def update_images_impl(subject: ReplaySubject, project_name: str): subject.on_next(ResultStep( steps=1, current_step=1, text="Starting image update..." )) project = try_loading_project(project_name, subject, 1, 1) if not project: return try: registry().engine.pull_images(project, line_reset="", update_func=lambda msg: subject.on_next( ResultStep( steps=1, current_step=1, text=msg ) )) subject.on_next(ResultStep( steps=1, current_step=1, text="Done updating images!", is_end=True )) except Exception as ex: subject.on_next(ResultStep( steps=1, current_step=1, text="Error updating an image: " + str(ex), is_end=True, is_error=True )) finally: subject.on_completed() pass
async def db_drop_impl(subject: ReplaySubject, project_name: str, name: str): project = try_loading_project(project_name, subject, 1, 4) if not project: return engine = registry().engine dbenv = DbEnvironments(project, engine) try: subject.on_next(ResultStep( steps=1, current_step=1, text=f"Deleting database environment {name}..." )) dbenv.drop(name) except FileNotFoundError: subject.on_next(ResultStep( steps=1, current_step=1, text=f"Environment with this name does not exist.", is_end=True, is_error=True )) except OSError: subject.on_next(ResultStep( steps=1, current_step=1, text=f"Can not delete the environment that is currently active.", is_end=True, is_error=True )) except Exception as ex: subject.on_next(ResultStep( steps=1, current_step=1, text=f"Error deleting environment: {ex}", is_end=True, is_error=True )) else: subject.on_next(ResultStep( steps=1, current_step=1, text=f"Finished deleting environment: {name}.", is_end=True ))
def resolve_config(parent, info): return ConfigGraphqlDocument(registry().system_config)
def resolve_db_current(parent, info): if not DbEnvironments.has_db(_get_project_doc(parent)): return None dbenv = DbEnvironments(_get_project_doc(parent), registry().engine) return dbenv.currently_selected_name()
def resolve_db_list(parent, info): if not DbEnvironments.has_db(_get_project_doc(parent)): return None dbenv = DbEnvironments(_get_project_doc(parent), registry().engine) return dbenv.list()
async def db_switch_impl( subject: ReplaySubject, project: Project, name: str, total_steps_from_ctx=0, current_step_in_ctx=0 ): """Switches db env. Has 3 total steps and can be used in other db operations.""" engine = registry().engine dbenv = DbEnvironments(project, engine) db_name = dbenv.db_service["$name"] subject.on_next(ResultStep( steps=total_steps_from_ctx + 3, current_step=current_step_in_ctx + 1, text="Switching database..." )) # 1. If running, stop database was_running = engine.service_status(project, db_name, registry().system_config) if was_running: subject.on_next(ResultStep( steps=total_steps_from_ctx + 3, current_step=current_step_in_ctx + 1, text="Stopping database service..." )) async for _ in registry().engine.stop_project(project, [db_name]): pass # 2. Switch environment try: subject.on_next(ResultStep( steps=total_steps_from_ctx + 3, current_step=current_step_in_ctx + 2, text=f"Switching environment to {name}..." )) dbenv.switch(name) except FileNotFoundError: subject.on_next(ResultStep( steps=total_steps_from_ctx + 3, current_step=current_step_in_ctx + 2, text=f"Environment {name} does not exist.", is_end=True, is_error=True )) except Exception as ex: subject.on_next(ResultStep( steps=total_steps_from_ctx + 3, current_step=current_step_in_ctx + 2, text=f"Error switching environment: {str(ex)}", is_end=True, is_error=True )) else: # 3. If was running: start database again if was_running: subject.on_next(ResultStep( steps=total_steps_from_ctx + 3, current_step=current_step_in_ctx + 3, text=f"Starting database...", )) async for _ in registry().engine.start_project(project, [db_name]): pass subject.on_next(ResultStep( steps=total_steps_from_ctx + 3, current_step=current_step_in_ctx + 3, text=f"Finished switching database environment to {name}.", is_end=True ))
def resolve_running(parent, info): service: ServiceDoc = _get_service_doc(parent) return registry().engine.service_status(service.parent().parent(), service["$name"], registry().system_config)
def resolve_container_name(parent, info): service: ServiceDoc = _get_service_doc(parent) return registry().engine.container_name_for(service.parent().parent(), service["$name"])