From ed0a2878b2afd3ca5b80fb7360acae5a2af69c59 Mon Sep 17 00:00:00 2001 From: Stamatis Katsaounis Date: Wed, 26 Jun 2024 14:16:16 +0300 Subject: [PATCH] fix: allow node roles different than bootstrapped node --- anvil-python/anvil/provider/local/commands.py | 248 ++++++++++-------- 1 file changed, 133 insertions(+), 115 deletions(-) diff --git a/anvil-python/anvil/provider/local/commands.py b/anvil-python/anvil/provider/local/commands.py index fac81c2..0f55a85 100644 --- a/anvil-python/anvil/provider/local/commands.py +++ b/anvil-python/anvil/provider/local/commands.py @@ -24,6 +24,7 @@ from sunbeam import utils # from sunbeam.commands import refresh as refresh_cmds +from sunbeam.clusterd.client import Client from sunbeam.commands.clusterd import ( ClusterAddJujuUserStep, ClusterAddNodeStep, @@ -66,7 +67,7 @@ ) from sunbeam.jobs.deployment import Deployment from sunbeam.jobs.juju import JujuHelper -from sunbeam.jobs.manifest import AddManifestStep +from sunbeam.jobs.manifest import AddManifestStep, BaseStep from sunbeam.provider.base import ProviderBase from sunbeam.provider.local.deployment import LOCAL_TYPE import yaml @@ -117,6 +118,78 @@ def remove_trailing_dot(value: str) -> str: return value.rstrip(".") +def postgresql_steps( + client: Client, + manifest: Manifest, + jhelper: JujuHelper, + deployment: LocalDeployment, + fqdn: str, +) -> List[BaseStep]: + return [ + TerraformInitStep(manifest.get_tfhelper("postgresql-plan")), + DeployPostgreSQLApplicationStep( + client, manifest, jhelper, deployment.infrastructure_model + ), + AddPostgreSQLUnitsStep( + client, fqdn, jhelper, deployment.infrastructure_model + ), + ] + + +def haproxy_steps( + client: Client, + manifest: Manifest, + jhelper: JujuHelper, + deployment: LocalDeployment, + fqdn: str, +) -> List[BaseStep]: + return [ + TerraformInitStep(manifest.get_tfhelper("haproxy-plan")), + DeployHAProxyApplicationStep( + client, manifest, jhelper, deployment.infrastructure_model + ), + AddHAProxyUnitsStep( + client, fqdn, jhelper, deployment.infrastructure_model + ), + ] + + +def maas_region_steps( + client: Client, + manifest: Manifest, + jhelper: JujuHelper, + deployment: LocalDeployment, + fqdn: str, +) -> List[BaseStep]: + return [ + TerraformInitStep(manifest.get_tfhelper("maas-region-plan")), + DeployMAASRegionApplicationStep( + client, manifest, jhelper, deployment.infrastructure_model + ), + AddMAASRegionUnitsStep( + client, fqdn, jhelper, deployment.infrastructure_model + ), + ] + + +def maas_agent_steps( + client: Client, + manifest: Manifest, + jhelper: JujuHelper, + deployment: LocalDeployment, + fqdn: str, +) -> List[BaseStep]: + return [ + TerraformInitStep(manifest.get_tfhelper("maas-agent-plan")), + DeployMAASAgentApplicationStep( + client, manifest, jhelper, deployment.infrastructure_model + ), + AddMAASAgentUnitsStep( + client, fqdn, jhelper, deployment.infrastructure_model + ), + ] + + class LocalProvider(ProviderBase): def register_add_cli(self, add: click.Group) -> None: """A local provider cannot add deployments.""" @@ -220,120 +293,69 @@ def bootstrap( juju_bootstrap_args = manifest_obj.software_config.juju.bootstrap_args # type: ignore[union-attr] data_location = snap.paths.user_data - preflight_checks = [] - preflight_checks.append(SystemRequirementsCheck()) - preflight_checks.append(JujuSnapCheck()) - preflight_checks.append(SshKeysConnectedCheck()) - preflight_checks.append(DaemonGroupCheck()) - preflight_checks.append(LocalShareCheck()) - + preflight_checks = [ + SystemRequirementsCheck(), + JujuSnapCheck(), + SshKeysConnectedCheck(), + DaemonGroupCheck(), + LocalShareCheck(), + ] run_preflight_checks(preflight_checks, console) - plan = [] - plan.append(JujuLoginStep(deployment.juju_account)) - # bootstrapped node is always machine 0 in controller model - plan.append(ClusterInitStep(client, roles_to_str_list(roles), 0)) - if manifest: - plan.append(AddManifestStep(client, manifest)) - plan.append(AddCloudJujuStep(cloud_name, cloud_definition)) - plan.append( + plan = [ + JujuLoginStep(deployment.juju_account), + ClusterInitStep( + client, roles_to_str_list(roles), 0 + ), # bootstrapped node is always machine 0 in controller model + AddManifestStep(client, manifest) if manifest else None, + AddCloudJujuStep(cloud_name, cloud_definition), BootstrapJujuStep( client, cloud_name, cloud_type, CONTROLLER, bootstrap_args=juju_bootstrap_args, - accept_defaults=accept_defaults, deployment_preseed=preseed, - ) - ) - run_plan(plan, console) + accept_defaults=accept_defaults, + ), + ] + run_plan(filter(None, plan), console) - plan2 = [] - plan2.append(CreateJujuUserStep(fqdn)) - plan2.append(ClusterUpdateJujuControllerStep(client, CONTROLLER)) + plan2 = [ + CreateJujuUserStep(fqdn), + ClusterUpdateJujuControllerStep(client, CONTROLLER), + ] plan2_results = run_plan(plan2, console) - token = get_step_message(plan2_results, CreateJujuUserStep) - plan3 = [] - plan3.append(ClusterAddJujuUserStep(client, fqdn, token)) - plan3.append(BackupBootstrapUserStep(fqdn, data_location)) - plan3.append(SaveJujuUserLocallyStep(fqdn, data_location)) - plan3.append( + plan3 = [ + ClusterAddJujuUserStep(client, fqdn, token), + BackupBootstrapUserStep(fqdn, data_location), + SaveJujuUserLocallyStep(fqdn, data_location), RegisterJujuUserStep( client, fqdn, CONTROLLER, data_location, replace=True - ) - ) + ), + ] run_plan(plan3, console) deployment.reload_juju_credentials() jhelper = JujuHelper(deployment.get_connected_controller()) - plan4 = [] - # Deploy PostgreSQL charm - plan4.append( - TerraformInitStep(manifest_obj.get_tfhelper("postgresql-plan")) - ) - plan4.append( - DeployPostgreSQLApplicationStep( - client, manifest_obj, jhelper, deployment.infrastructure_model - ) - ) - plan4.append( - AddPostgreSQLUnitsStep( - client, fqdn, jhelper, deployment.infrastructure_model - ) - ) + plan4 = postgresql_steps(client, manifest_obj, jhelper, deployment, fqdn) if is_haproxy_node: - # Deploy HAProxy charm - plan4.append( - TerraformInitStep(manifest_obj.get_tfhelper("haproxy-plan")) + plan4.extend( + haproxy_steps(client, manifest_obj, jhelper, deployment, fqdn) ) - plan4.append( - DeployHAProxyApplicationStep( - client, manifest_obj, jhelper, deployment.infrastructure_model - ) - ) - plan4.append( - AddHAProxyUnitsStep( - client, fqdn, jhelper, deployment.infrastructure_model - ) - ) - if is_region_node: - # Deploy MAAS Region charm - plan4.append( - TerraformInitStep(manifest_obj.get_tfhelper("maas-region-plan")) + plan4.extend( + maas_region_steps(client, manifest_obj, jhelper, deployment, fqdn) ) - plan4.append( - DeployMAASRegionApplicationStep( - client, manifest_obj, jhelper, deployment.infrastructure_model - ) - ) - plan4.append( - AddMAASRegionUnitsStep( - client, fqdn, jhelper, deployment.infrastructure_model - ) - ) - if is_agent_node: - # Deploy MAAS Agent charm - plan4.append( - TerraformInitStep(manifest_obj.get_tfhelper("maas-agent-plan")) + plan4.extend( + maas_agent_steps(client, manifest_obj, jhelper, deployment, fqdn) ) - plan4.append( - DeployMAASAgentApplicationStep( - client, manifest_obj, jhelper, deployment.infrastructure_model - ) - ) - plan4.append( - AddMAASAgentUnitsStep( - client, fqdn, jhelper, deployment.infrastructure_model - ) - ) - run_plan(plan4, console) + click.echo(f"Node has been bootstrapped with roles: {pretty_roles}") @@ -423,7 +445,7 @@ def join( is_database_node = any(role.is_database_node() for role in roles) is_haproxy_node = any(role.is_haproxy_node() for role in roles) - # Register juju user with same name as Node fqdn + # Register Juju user with same name as Node fqdn name = utils.get_fqdn() ip = utils.get_local_ip_by_default_route() @@ -431,14 +453,14 @@ def join( pretty_roles = ", ".join(role_.name.lower() for role_ in roles) LOG.debug(f"Node joining the cluster with roles: {pretty_roles}") - preflight_checks = [] - preflight_checks.append(SystemRequirementsCheck()) - preflight_checks.append(JujuSnapCheck()) - preflight_checks.append(SshKeysConnectedCheck()) - preflight_checks.append(DaemonGroupCheck()) - preflight_checks.append(LocalShareCheck()) - preflight_checks.append(TokenCheck(name, token)) - + preflight_checks = [ + SystemRequirementsCheck(), + JujuSnapCheck(), + SshKeysConnectedCheck(), + DaemonGroupCheck(), + LocalShareCheck(), + TokenCheck(name, token), + ] run_preflight_checks(preflight_checks, console) controller = CONTROLLER @@ -457,38 +479,34 @@ def join( deployment.reload_juju_credentials() + # Get manifest object once the cluster is joined + manifest_obj = Manifest.load_latest_from_clusterdb( + deployment, include_defaults=True + ) + machine_id = -1 machine_id_result = get_step_message(plan1_results, AddJujuMachineStep) if machine_id_result is not None: machine_id = int(machine_id_result) jhelper = JujuHelper(deployment.get_connected_controller()) - plan2 = [] - plan2.append(ClusterUpdateNodeStep(client, name, machine_id=machine_id)) + plan2 = [ClusterUpdateNodeStep(client, name, machine_id=machine_id)] if is_database_node: - plan2.append( - AddPostgreSQLUnitsStep( - client, name, jhelper, deployment.infrastructure_model - ) + plan2.extend( + postgresql_steps(client, manifest_obj, jhelper, deployment, name) ) if is_haproxy_node: - plan2.append( - AddHAProxyUnitsStep( - client, name, jhelper, deployment.infrastructure_model - ) + plan2.extend( + haproxy_steps(client, manifest_obj, jhelper, deployment, name) ) if is_region_node: - plan2.append( - AddMAASRegionUnitsStep( - client, name, jhelper, deployment.infrastructure_model - ) + plan2.extend( + maas_region_steps(client, manifest_obj, jhelper, deployment, name) ) if is_agent_node: - plan2.append( - AddMAASAgentUnitsStep( - client, name, jhelper, deployment.infrastructure_model - ) + plan2.extend( + maas_agent_steps(client, manifest_obj, jhelper, deployment, name) ) run_plan(plan2, console)