Skip to content

Commit

Permalink
Merge branch 'main' into tls-passthrough
Browse files Browse the repository at this point in the history
  • Loading branch information
wyattrees authored Nov 13, 2024
2 parents 55982a5 + e4066f4 commit 6e3bc4b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 46 deletions.
20 changes: 9 additions & 11 deletions anvil-python/anvil/commands/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,15 @@ def show(ctx: click.Context, id: str) -> None:

@click.command()
@click.option(
"-f",
"--manifest-file",
"-o",
"--output",
help="Output file for manifest, defaults to $HOME/.config/anvil/manifest.yaml",
type=click.Path(dir_okay=False, path_type=Path),
)
@click.pass_context
def generate(
ctx: click.Context,
manifest_file: Path | None = None,
output: Path | None = None,
) -> None:
"""Generate manifest file.
Expand All @@ -152,14 +152,12 @@ def generate(
"""
deployment: Deployment = ctx.obj

if not manifest_file:
if not output:
home = os.environ.get("SNAP_REAL_HOME", "")
manifest_file = Path(home) / ".config" / "anvil" / "manifest.yaml"
output = Path(home) / ".config" / "anvil" / "manifest.yaml"

LOG.debug(
f"Creating {manifest_file} parent directory if it does not exist"
)
manifest_file.parent.mkdir(mode=0o775, parents=True, exist_ok=True)
LOG.debug(f"Creating {output} parent directory if it does not exist")
output.parent.mkdir(mode=0o775, parents=True, exist_ok=True)

try:
client = deployment.get_client()
Expand All @@ -185,7 +183,7 @@ def generate(
software_content = generate_software_manifest(manifest_obj)

try:
with manifest_file.open("w") as file:
with output.open("w") as file:
file.write("# Generated Anvil Deployment Manifest\n\n")
file.write(preseed_content)
file.write("\n")
Expand All @@ -194,4 +192,4 @@ def generate(
LOG.debug(e)
raise click.ClickException(f"Manifest generation failed: {e!s}")

click.echo(f"Generated manifest is at {manifest_file!s}")
click.echo(f"Generated manifest is at {output!s}")
20 changes: 4 additions & 16 deletions anvil-python/anvil/commands/prepare_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
JUJU_CHANNEL = "3.4/stable"
SUPPORTED_RELEASE = "jammy"

PREPARE_NODE_TEMPLATE = f"""[ $(lsb_release -sc) != '{SUPPORTED_RELEASE}' ] && \
PREPARE_NODE_TEMPLATE = f"""#!/bin/bash
[ $(lsb_release -sc) != '{SUPPORTED_RELEASE}' ] && \
{{ echo 'ERROR: MAAS Anvil deploy only supported on {SUPPORTED_RELEASE}'; exit 1; }}
# :warning: Node Preparation for MAAS Anvil :warning:
Expand Down Expand Up @@ -64,9 +65,7 @@
[ -f $HOME/.ssh/id_rsa ] || ssh-keygen -b 4096 -f $HOME/.ssh/id_rsa -t rsa -N ""
cat $HOME/.ssh/id_rsa.pub >> $HOME/.ssh/authorized_keys
ssh-keyscan -H $(hostname --all-ip-addresses) >> $HOME/.ssh/known_hosts
"""
COMMON_TEMPLATE = f"""
# Install the Juju snap
sudo snap install --channel {JUJU_CHANNEL} juju
Expand All @@ -84,17 +83,6 @@


@click.command()
@click.option(
"--client",
"-c",
is_flag=True,
help="Prepare the node for use as a client.",
default=False,
)
def prepare_node_script(client: bool = False) -> None:
def prepare_node_script() -> None:
"""Generate script to prepare a node for Anvil use."""
script = "#!/bin/bash\n"
if not client:
script += PREPARE_NODE_TEMPLATE
script += COMMON_TEMPLATE
console.print(script, soft_wrap=True)
console.print(PREPARE_NODE_TEMPLATE, soft_wrap=True)
38 changes: 19 additions & 19 deletions anvil-python/anvil/provider/local/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ def bootstrap(

@click.command()
@click.option(
"--name",
"--fqdn",
type=str,
prompt=True,
help="Fully qualified node name",
Expand All @@ -341,33 +341,33 @@ def bootstrap(
help="Output format.",
)
@click.pass_context
def add(ctx: click.Context, name: str, format: str) -> None:
def add(ctx: click.Context, fqdn: str, format: str) -> None:
"""Generate a token for a new node to join the cluster."""
preflight_checks = [DaemonGroupCheck(), VerifyFQDNCheck(name)]
preflight_checks = [DaemonGroupCheck(), VerifyFQDNCheck(fqdn)]
run_preflight_checks(preflight_checks, console)
name = remove_trailing_dot(name)
fqdn = remove_trailing_dot(fqdn)

deployment: LocalDeployment = ctx.obj
client = deployment.get_client()

plan1 = [
JujuLoginStep(deployment.juju_account),
ClusterAddNodeStep(client, name),
CreateJujuUserStep(name),
ClusterAddNodeStep(client, fqdn),
CreateJujuUserStep(fqdn),
]

plan1_results = run_plan(plan1, console)

user_token = get_step_message(plan1_results, CreateJujuUserStep)

plan2 = [ClusterAddJujuUserStep(client, name, user_token)]
plan2 = [ClusterAddJujuUserStep(client, fqdn, user_token)]
run_plan(plan2, console)

def _print_output(token: str) -> None:
"""Helper for printing formatted output."""
if format == FORMAT_DEFAULT:
console.print(
f"Token for the Node {name}: {token}", soft_wrap=True
f"Token for the Node {fqdn}: {token}", soft_wrap=True
)
elif format == FORMAT_YAML:
click.echo(yaml.dump({"token": token}))
Expand Down Expand Up @@ -571,10 +571,10 @@ def list(ctx: click.Context, format: str) -> None:

@click.command()
@click.option(
"--name", type=str, prompt=True, help="Fully qualified node name"
"--fqdn", type=str, prompt=True, help="Fully qualified node name"
)
@click.pass_context
def remove(ctx: click.Context, name: str) -> None:
def remove(ctx: click.Context, fqdn: str) -> None:
"""Remove a node from the cluster."""
deployment: LocalDeployment = ctx.obj
client = deployment.get_client()
Expand All @@ -590,33 +590,33 @@ def remove(ctx: click.Context, name: str) -> None:
plan = [
JujuLoginStep(deployment.juju_account),
RemoveMAASAgentUnitStep(
client, name, jhelper, deployment.infrastructure_model
client, fqdn, jhelper, deployment.infrastructure_model
),
RemoveMAASRegionUnitStep(
client, name, jhelper, deployment.infrastructure_model
client, fqdn, jhelper, deployment.infrastructure_model
),
ReapplyPostgreSQLTerraformPlanStep(
client, manifest_obj, jhelper, deployment.infrastructure_model
),
RemoveHAProxyUnitStep(
client, name, jhelper, deployment.infrastructure_model
client, fqdn, jhelper, deployment.infrastructure_model
),
RemovePostgreSQLUnitStep(
client, name, jhelper, deployment.infrastructure_model
client, fqdn, jhelper, deployment.infrastructure_model
),
RemoveJujuMachineStep(client, name),
RemoveJujuMachineStep(client, fqdn),
# Cannot remove user as the same user name cannot be reused,
# so commenting the RemoveJujuUserStep
# RemoveJujuUserStep(name),
ClusterRemoveNodeStep(client, name),
# RemoveJujuUserStep(fqdn),
ClusterRemoveNodeStep(client, fqdn),
]
run_plan(plan, console)
click.echo(f"Removed node {name} from the cluster")
click.echo(f"Removed node {fqdn} from the cluster")
# Removing machine does not clean up all deployed Juju components. This is
# deliberate, see https://bugs.launchpad.net/juju/+bug/1851489.
# Without the workaround mentioned in LP#1851489, it is not possible to
# reprovision the machine back.
click.echo(
f"Run command 'sudo /sbin/remove-juju-services' on node {name} "
f"Run command 'sudo /sbin/remove-juju-services' on node {fqdn} "
"to reuse the machine."
)

0 comments on commit 6e3bc4b

Please sign in to comment.