From 47c7f96a1c598259d05bb14cb7b1134ec78567da Mon Sep 17 00:00:00 2001 From: Wyatt Rees Date: Tue, 12 Nov 2024 23:51:03 -0700 Subject: [PATCH 1/2] remove --client flag from prepare-node-script command (#76) --- anvil-python/anvil/commands/prepare_node.py | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/anvil-python/anvil/commands/prepare_node.py b/anvil-python/anvil/commands/prepare_node.py index 2be47a4..4b2df92 100644 --- a/anvil-python/anvil/commands/prepare_node.py +++ b/anvil-python/anvil/commands/prepare_node.py @@ -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: @@ -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 @@ -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) From e4066f4865e03471d8f9c7a232b95d6b5f9a4dec Mon Sep 17 00:00:00 2001 From: Wyatt Rees Date: Tue, 12 Nov 2024 23:53:39 -0700 Subject: [PATCH 2/2] rename --name and --manifest-file flags to be more explicit; --fqdn and --output (#75) --- anvil-python/anvil/commands/manifest.py | 20 +++++----- anvil-python/anvil/provider/local/commands.py | 38 +++++++++---------- 2 files changed, 28 insertions(+), 30 deletions(-) diff --git a/anvil-python/anvil/commands/manifest.py b/anvil-python/anvil/commands/manifest.py index db4bbe9..5f6fa31 100644 --- a/anvil-python/anvil/commands/manifest.py +++ b/anvil-python/anvil/commands/manifest.py @@ -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. @@ -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() @@ -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") @@ -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}") diff --git a/anvil-python/anvil/provider/local/commands.py b/anvil-python/anvil/provider/local/commands.py index e129ad6..15311da 100644 --- a/anvil-python/anvil/provider/local/commands.py +++ b/anvil-python/anvil/provider/local/commands.py @@ -326,7 +326,7 @@ def bootstrap( @click.command() @click.option( - "--name", + "--fqdn", type=str, prompt=True, help="Fully qualified node name", @@ -339,33 +339,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})) @@ -567,10 +567,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() @@ -586,33 +586,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." )