Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jkeifer committed May 1, 2024
1 parent 715a155 commit 53ca929
Show file tree
Hide file tree
Showing 18 changed files with 326 additions and 293 deletions.
10 changes: 3 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from setuptools import find_namespace_packages, setup

from src.cirrus.plugins.management import DESCRIPTION, NAME
from src.cirrus.management import DESCRIPTION, NAME

HERE = os.path.abspath(os.path.dirname(__file__))
VERSION = os.environ.get("PLUGIN_VERSION", "0.0.0")
Expand Down Expand Up @@ -41,11 +41,7 @@
license="Apache-2.0",
include_package_data=True,
entry_points="""
[cirrus.plugins]
{NAME}=cirrus.plugins.management
[cirrus.commands]
manage=cirrus.plugins.management.commands.manage:manage
payload=cirrus.plugins.management.commands.payload:payload
deployments=cirrus.plugins.management.commands.deployments:deployments
[console_scripts]
cirrus-mgmt=cirrus.management.__main__:main
""",
)
File renamed without changes.
9 changes: 9 additions & 0 deletions src/cirrus/management/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from cirrus.management.cli import cli


def main() -> None:
cli()


if __name__ == "__main__":
main()
73 changes: 73 additions & 0 deletions src/cirrus/management/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import sys
from functools import wraps
from typing import Any, Callable

import boto3
import botocore.exceptions
import click
from cirrus.cli.utils import click as utils_click
from cirrus.cli.utils import logging
from cirrus.core import exceptions

from cirrus.management import DESCRIPTION, NAME
from cirrus.management.commands.deployments import list_deployments
from cirrus.management.commands.manage import manage as manage_group
from cirrus.management.commands.payload import payload as payload_group
from cirrus.management.exceptions import SSOError

logger = logging.getLogger(__name__)


def handle_sso_error(func: Callable) -> Callable:
@wraps(func)
def wrapper(*args, **kwargs) -> Any:
try:
return func(*args, **kwargs)
except (
botocore.exceptions.UnauthorizedSSOTokenError,
botocore.exceptions.TokenRetrievalError,
botocore.exceptions.SSOTokenLoadError,
) as e:
raise SSOError(
"SSO session not authorized. Run `aws sso login` and try again.",
) from e

return wrapper


class MainGroup(utils_click.AliasedShortMatchGroup):
def invoke(self, *args, **kwargs) -> Any:
try:
return handle_sso_error(super().invoke)(*args, **kwargs)
except exceptions.CirrusError as e:
logger.error(
e,
exc_info=(
e if logger.getEffectiveLevel() < logging.logging.INFO else False
),
)
sys.exit(e.exit_code)


@click.group(
name=NAME,
help=DESCRIPTION,
cls=MainGroup,
)
@click.option(
"--profile",
help="AWS CLI profile name to use for session",
)
@click.pass_context
@logging.verbosity()
def cli(ctx, verbose, profile: str | None = None) -> None:
ctx.obj = boto3.Session(profile_name=profile)


cli.add_command(list_deployments)
cli.add_command(manage_group)
cli.add_command(payload_group)


if __name__ == "__main__":
cli()
File renamed without changes.
19 changes: 19 additions & 0 deletions src/cirrus/management/commands/deployments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import logging

import boto3
import click

from cirrus.management.deployment import Deployment
from cirrus.management.utils.click import pass_session

logger = logging.getLogger(__name__)


@click.command()
@pass_session
def list_deployments(session: boto3.Session) -> None:
"""
List all project deployments (accessible via current AWS role)
"""
for deployment in Deployment.yield_deployments(session=session):
click.echo(f"{deployment.name} ({deployment.secret_arn})")
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
import logging
import sys
from functools import wraps
from subprocess import CalledProcessError
from typing import Optional

import boto3
import click
from cirrus.cli.utils import click as utils_click
from click_option_group import RequiredMutuallyExclusiveOptionGroup, optgroup

from cirrus.plugins.management.deployment import (
WORKFLOW_POLL_INTERVAL,
CalledProcessError,
Deployment,
)
from cirrus.plugins.management.utils.click import (
from cirrus.management.deployment import WORKFLOW_POLL_INTERVAL, Deployment
from cirrus.management.utils.click import (
additional_variables,
pass_session,
silence_templating_errors,
)

Expand Down Expand Up @@ -72,17 +72,17 @@ def wrapper(*args, **kwargs):
aliases=["mgmt"],
cls=utils_click.AliasedShortMatchGroup,
)
@utils_click.requires_project
@click.argument(
"deployment",
metavar="DEPLOYMENT_NAME",
)
@pass_session
@click.pass_context
def manage(ctx, project, deployment):
def manage(ctx, session: boto3.Session, deployment: str, profile: Optional[str] = None):
"""
Commands to run management operations against project deployments.
Commands to run management operations against a cirrus deployment.
"""
ctx.obj = Deployment.from_name(deployment, project)
ctx.obj = Deployment.from_name(deployment, session=session)


@manage.command()
Expand All @@ -93,28 +93,6 @@ def show(deployment):
click.secho(deployment.asjson(indent=4), fg=color)


@manage.command("get-path")
@pass_deployment
def get_path(deployment):
"""Get path to deployment directory"""
click.echo(deployment.path)


@manage.command()
@pass_deployment
@click.option(
"--stackname",
)
@click.option(
"--profile",
)
def refresh(deployment, stackname=None, profile=None):
"""Refresh the environment values from the AWS deployment,
optionally changing the stackname or profile.
"""
deployment.refresh(stackname=stackname, profile=profile)


@manage.command("run-workflow")
@click.option(
"-t",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import click
from cirrus.cli.utils import click as utils_click

from cirrus.plugins.management.utils.click import (
from cirrus.management.utils.click import (
additional_variables,
silence_templating_errors,
)
Expand Down Expand Up @@ -43,7 +43,7 @@ def get_id():
@additional_variables
@silence_templating_errors
def template(additional_variables, silence_templating_errors):
from cirrus.plugins.management.utils.templating import template_payload
from cirrus.management.utils.templating import template_payload

click.echo(
template_payload(
Expand Down
Loading

0 comments on commit 53ca929

Please sign in to comment.