diff --git a/elementary/monitor/cli.py b/elementary/monitor/cli.py index 36d63eac0..9f2338c8f 100644 --- a/elementary/monitor/cli.py +++ b/elementary/monitor/cli.py @@ -11,6 +11,7 @@ ) from elementary.monitor.data_monitoring.schema import FiltersSchema from elementary.monitor.data_monitoring.selector_filter import SelectorFilter +from elementary.monitor.dbt_init import DBTInit from elementary.monitor.debug import Debug from elementary.tracking.anonymous_tracking import AnonymousCommandLineTracking from elementary.utils import bucket_path @@ -24,6 +25,7 @@ class Command: REPORT = "monitor-report" SEND_REPORT = "monitor-send-report" DEBUG = "debug" + DBT_INIT = "dbt-init" # Displayed in reverse order in --help. @@ -774,5 +776,25 @@ def debug(ctx, profiles_dir): anonymous_tracking.track_cli_end(Command.DEBUG, None, ctx.command.name) +@monitor.command() +@click.pass_context +def dbt_init(ctx): + """ + Initializes the Elementary internal dbt project by installing its dbt deps. + Run this command after installing EDR as part of builds or CI/CD pipelines when the target + environment does not have write permissions on disk or does not have internet connection. + This command is not needed in most cases as the dbt deps are installed automatically when running `edr monitor`. + """ + config = Config() + anonymous_tracking = AnonymousCommandLineTracking(config) + anonymous_tracking.track_cli_start(Command.DEBUG, None, ctx.command.name) + dbtinit = DBTInit() + success = dbtinit.setup_internal_dbt_packages() + if not success: + sys.exit(1) + click.echo("Elementary internal dbt project has been initialized successfully. ") + anonymous_tracking.track_cli_end(Command.DEBUG, None, ctx.command.name) + + if __name__ == "__main__": monitor() diff --git a/elementary/monitor/dbt_init.py b/elementary/monitor/dbt_init.py new file mode 100644 index 000000000..d1615de86 --- /dev/null +++ b/elementary/monitor/dbt_init.py @@ -0,0 +1,19 @@ +from elementary.clients.dbt.factory import create_dbt_runner +from elementary.monitor import dbt_project_utils + + +class DBTInit: + """ + Class to handle the initialization of dbt for the Elementary CLI. + This can contain all dbt static setup to avoid pulling in runtime dependencies or any other information from internet + that is internally used by edr. + """ + + def setup_internal_dbt_packages(self): + """ + Run dbt deps to install internal dbt packages if needed. + It intentionally does not use self.config.run_dbt_deps_if_needed parameter in create_dbt_runner to ensure + that dbt deps is always run when setting up the internal dbt packages. + """ + dbt_runner = create_dbt_runner(dbt_project_utils.CLI_DBT_PROJECT_PATH) + return dbt_runner.deps()