Skip to content

Commit

Permalink
fix: allow node roles different than bootstrapped node
Browse files Browse the repository at this point in the history
  • Loading branch information
skatsaounis committed Jun 26, 2024
1 parent 4c0f512 commit ed0a287
Showing 1 changed file with 133 additions and 115 deletions.
248 changes: 133 additions & 115 deletions anvil-python/anvil/provider/local/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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}")


Expand Down Expand Up @@ -423,22 +445,22 @@ 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()

roles_str = roles_to_str_list(roles)
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
Expand All @@ -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)
Expand Down

0 comments on commit ed0a287

Please sign in to comment.