def config(ctx, project, plugin_type, plugin_name, format): plugin_type = PluginType.from_cli_argument( plugin_type) if plugin_type else None config = ConfigService(project) plugin = config.find_plugin(plugin_name, plugin_type=plugin_type, configurable=True) _, Session = project_engine(project) session = Session() try: settings = PluginSettingsService(project).build(plugin) ctx.obj["settings"] = settings ctx.obj["session"] = session if ctx.invoked_subcommand is None: if format == "json": config = settings.as_config(session=session) print(json.dumps(config)) elif format == "env": for env, value in settings.as_env(session=session).items(): print(f"{env}={value}") finally: session.close()
def plugin_config(self, project): _, Session = project_engine(project) session = Session() try: plugin_settings_service = PluginSettingsService(project) raw_config = plugin_settings_service.as_config(session, self) finally: session.close() config = {} for key, value in raw_config.items(): nest(config, key, value, maxsplit=1) return config
def get_plugin_configuration(plugin_ref) -> Response: """ endpoint for getting a plugin's configuration """ project = Project.find() settings = PluginSettingsService(project) config = flatten(settings.as_config(db.session, plugin_ref, redacted=True), reducer="dot") return jsonify({ # freeze the keys because they are used for lookups "config": freeze_keys(config), "settings": settings.get_definition(plugin_ref).settings, })
def config(ctx, project, plugin_name, format): config = ConfigService(project) plugin = config.find_plugin(plugin_name) _, Session = project_engine(project) session = Session() settings = PluginSettingsService(project) ctx.obj["settings"] = settings ctx.obj["plugin"] = plugin ctx.obj["session"] = session if ctx.invoked_subcommand is None: if format == "json": print(settings.as_config(session, plugin)) if format == "env": for env, value in settings.as_env(session, plugin).items(): print(f"{env}={value}")
def save_plugin_configuration(plugin_ref) -> Response: """ endpoint for persisting a plugin configuration """ project = Project.find() payload = request.get_json() settings = PluginSettingsService(project) for name, value in payload.items(): # we want to prevent the edition of protected settings from the UI if settings.find_setting(plugin_ref, name).get("protected"): logging.warning( "Cannot set a 'protected' configuration externally.") continue if value == "": settings.unset(db.session, plugin_ref, name) else: settings.set(db.session, plugin_ref, name, value) return jsonify(settings.as_config(db.session, plugin_ref, redacted=True))
def after_install(self, project, args=[]): _, Session = project_engine(project) session = Session() plugin_config_service = PluginConfigService( self, config_dir=project.plugin_dir(self), run_dir=project.run_dir(self.name), ) plugin_settings_service = PluginSettingsService(project) airflow_cfg_path = plugin_config_service.run_dir.joinpath( "airflow.cfg") stub_path = plugin_config_service.config_dir.joinpath("airflow.cfg") invoker = invoker_factory( project, self, prepare_with_session=session, plugin_config_service=plugin_config_service, ) try: # generate the default `airflow.cfg` handle = invoker.invoke("--help", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) handle.wait() logging.debug(f"Generated default '{str(airflow_cfg_path)}'") # move it to the config dir shutil.move(airflow_cfg_path, stub_path) airflow_cfg_path = stub_path logging.debug(f"Moved to '{str(stub_path)}'") # open the configuration and update it # now we let's update the config to use our stubs airflow_cfg = configparser.ConfigParser() with airflow_cfg_path.open() as cfg: airflow_cfg.read_file(cfg) logging.debug(f"Loaded '{str(airflow_cfg_path)}'") config = {} for key, value in plugin_settings_service.as_config(session, self).items(): nest(config, key, str(value)) for section, cfg in config.items(): airflow_cfg[section].update(cfg) logging.debug(f"\tUpdated section [{section}] with {cfg}") with airflow_cfg_path.open("w") as cfg: airflow_cfg.write(cfg) logging.debug(f"Saved '{str(airflow_cfg_path)}'") # we've changed the configuration here, so we need to call # prepare again on the invoker so it re-reads the configuration # for the Airflow plugin invoker.prepare(session) handle = invoker.invoke( "initdb", stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, ) initdb = handle.wait() if initdb: raise SubprocessError("airflow initdb failed", handle) logging.debug(f"Completed `airflow initdb`") finally: session.close()