Exemplo n.º 1
0
def validate(scope: str, update: bool) -> None:
    """Validate the configuration"""
    try:
        check_for_updates()
        console.out("")
        with halo.Halo(text="Loading", spinner="dots") as spinner:
            spinner.text = "Evaluating paths"
            path_current = pathlib.Path(scope).expanduser().resolve()
            path_base = gdbt.code.templates.TemplateLoader(
                path_current).base_path

            spinner.text = "Loading configuration"
            configuration = gdbt.code.configuration.load(path_current)
            templates = gdbt.code.templates.load(path_current)

            spinner.text = "Resolving resources"
            for name, template in templates.items():
                template.resolve(name, configuration, str(path_base), update)

            spinner.succeed(
                rich.style.Style(
                    color="green",
                    bold=True).render("Configuration is valid!\n"))
    except gdbt.errors.Error as exc:
        console.print(f"[red][b]ERROR[/b] {exc.text}")
        raise SystemExit(1)
Exemplo n.º 2
0
def plan(scope: str, update: bool) -> None:
    """Plan the changes"""
    try:
        check_for_updates()
        console.out("")
        with halo.Halo(text="Loading", spinner="dots") as spinner:
            spinner.text = "Evaluating paths"
            path_current = pathlib.Path(scope).expanduser().resolve()
            path_base = gdbt.code.templates.TemplateLoader(
                path_current).base_path
            path_relative = path_current.relative_to(path_base)

            spinner.text = "Loading configuration"
            configuration = gdbt.code.configuration.load(path_current)
            templates = gdbt.code.templates.load(path_current)

            spinner.text = "Resolving resources"
            resources_desired = {
                name: typing.cast(
                    gdbt.resource.ResourceGroup,
                    template.resolve(name, configuration, str(path_base),
                                     update),
                )
                for name, template in templates.items()
            }

            spinner.text = "Loading resource state"
            states = gdbt.state.StateLoader(configuration).load(path_relative)
            resources_current_meta = {
                name: state.resource_meta
                for name, state in states.items()
            }

            spinner.text = "Refreshing resource state"
            resources_current = gdbt.resource.ResourceLoader(
                configuration).load(resources_current_meta)

            spinner.text = "Calculating plan"
            plan = gdbt.state.Plan.plan(resources_current, resources_desired)
            summary = gdbt.state.Plan.summary(resources_current,
                                              resources_desired, plan)
            spinner.text = "Rendering plan"
            plan_rendered, changes_pending = gdbt.state.PlanRenderer(
                plan).render(summary)

            if not changes_pending:
                spinner.succeed(
                    rich.style.Style(
                        color="green",
                        bold=True).render("Dashboards are up to date!\n"))
                return

        console.out(plan_rendered)
        os._exit(0)
    except gdbt.errors.Error as exc:
        console.print(f"[red][b]ERROR[/b] {exc.text}")
        raise SystemExit(1)
Exemplo n.º 3
0
def test_out() -> None:
    console = Console(width=10)
    console.begin_capture()
    console.out(*(["foo bar"] * 5), sep=".", end="X")
    assert console.end_capture() == "foo bar.foo bar.foo bar.foo bar.foo barX"
Exemplo n.º 4
0
def destroy(scope: str, auto_approve: bool) -> None:
    """Destroy resources"""
    try:
        check_for_updates()
        console.out("")
        with halo.Halo(text="Loading", spinner="dots") as spinner:
            spinner.text = "Evaluating paths"
            path_current = pathlib.Path(scope).expanduser().resolve()
            path_base = gdbt.code.templates.TemplateLoader(
                path_current).base_path
            path_relative = path_current.relative_to(path_base)

            spinner.text = "Loading configuration"
            configuration = gdbt.code.configuration.load(path_current)

            spinner.text = "Loading resource state"
            states = gdbt.state.StateLoader(configuration).load(path_relative)
            resources_current_meta = {
                name: state.resource_meta
                for name, state in states.items()
            }

            spinner.text = "Refreshing resource state"
            resources_current = gdbt.resource.ResourceLoader(
                configuration).load(resources_current_meta)

            spinner.text = "Calculating plan"
            plan = gdbt.state.Plan.plan(resources_current, {})
            summary = gdbt.state.Plan.summary(resources_current, {}, plan)
            spinner.text = "Rendering plan"
            plan_rendered, changes_pending = gdbt.state.PlanRenderer(
                plan).render(summary)

            if not changes_pending:
                spinner.succeed(
                    rich.style.Style(
                        color="green",
                        bold=True).render("Dashboards are up to date!\n"))
                return

        console.out(plan_rendered)

        if not auto_approve:
            click.confirm("Apply?", abort=True)
            console.print("\n")

        for s in (signal.SIGHUP, signal.SIGINT, signal.SIGQUIT,
                  signal.SIGTERM):
            signal.signal(s, signal.SIG_IGN)

        with halo.Halo(text="Loading", spinner="dots") as spinner:
            spinner.text = "Applying changes"
            t_start = time.time()
            gdbt.state.PlanRunner(summary).apply(configuration,
                                                 resources_current, {})

            spinner.text = "Uploading resource state"
            gdbt.state.StateLoader(configuration).upload(
                path_relative,
                {
                    group_name: typing.cast(gdbt.resource.ResourceGroup, {})
                    for group_name in resources_current
                },
            )
            t_end = time.time()
            duration = t_end - t_start
            spinner.succeed(
                rich.style.Style(color="green", bold=True).render(
                    f"Done! Apply took {duration:.2f} seconds.\n"))
        os._exit(0)
    except gdbt.errors.Error as exc:
        console.print(f"[red][b]ERROR[/b] {exc.text}")
        raise SystemExit(1)