Skip to content

Commit

Permalink
Merge manifest and preseed files
Browse files Browse the repository at this point in the history
Use single file for both manifest and preseed.
The file should have deployment as key for preseed
entries and software for software config entries.
Remove --preseed option from commands and pass the
preseed dict from manifest to the plans.
  • Loading branch information
hemanthnakkina committed Feb 6, 2024
1 parent 84876b2 commit 3fc153d
Show file tree
Hide file tree
Showing 14 changed files with 425 additions and 247 deletions.
19 changes: 7 additions & 12 deletions sunbeam-python/sunbeam/commands/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,6 @@

@click.command()
@click.option("-a", "--accept-defaults", help="Accept all defaults.", is_flag=True)
@click.option(
"-p",
"--preseed",
help="Preseed file.",
type=click.Path(exists=True, dir_okay=False, path_type=Path),
)
@click.option(
"-m",
"--manifest",
Expand Down Expand Up @@ -138,7 +132,6 @@ def bootstrap(
topology: str,
database: str,
manifest: Optional[Path] = None,
preseed: Optional[Path] = None,
accept_defaults: bool = False,
) -> None:
"""Bootstrap the local node.
Expand All @@ -156,7 +149,9 @@ def bootstrap(
else:
manifest_obj = Manifest.get_default_manifest(client)

LOG.debug(f"Manifest used for deployment: {manifest_obj}")
LOG.debug(f"Manifest used for deployment - preseed: {manifest_obj.deployment}")
LOG.debug(f"Manifest used for deployment - software: {manifest_obj.software}")
preseed = manifest_obj.deployment

# Bootstrap node must always have the control role
if Role.CONTROL not in roles:
Expand All @@ -174,7 +169,7 @@ def bootstrap(

cloud_type = snap.config.get("juju.cloud.type")
cloud_name = snap.config.get("juju.cloud.name")
juju_bootstrap_args = manifest_obj.juju.bootstrap_args
juju_bootstrap_args = manifest_obj.software.juju.bootstrap_args
data_location = snap.paths.user_data

preflight_checks = []
Expand Down Expand Up @@ -204,7 +199,7 @@ def bootstrap(
CONTROLLER,
bootstrap_args=juju_bootstrap_args,
accept_defaults=accept_defaults,
preseed_file=preseed,
deployment_preseed=preseed,
)
)
run_plan(plan, console)
Expand Down Expand Up @@ -240,7 +235,7 @@ def bootstrap(
manifest_obj,
jhelper,
accept_defaults=accept_defaults,
preseed_file=preseed,
deployment_preseed=preseed,
)
)
plan4.append(AddMicrok8sUnitStep(client, fqdn, jhelper))
Expand All @@ -258,7 +253,7 @@ def bootstrap(
fqdn,
jhelper,
accept_defaults=accept_defaults,
preseed_file=preseed,
deployment_preseed=preseed,
)
)

Expand Down
59 changes: 32 additions & 27 deletions sunbeam-python/sunbeam/commands/configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,15 +434,15 @@ def __init__(
self,
client: Client,
answer_file: str,
preseed_file: str | None = None,
deployment_preseed: dict | None = None,
accept_defaults: bool = False,
):
super().__init__(
"Collect cloud configuration", "Collecting cloud configuration"
)
self.client = client
self.accept_defaults = accept_defaults
self.preseed_file = preseed_file
self.preseed = deployment_preseed or {}
self.answer_file = answer_file

def has_prompts(self) -> bool:
Expand All @@ -462,14 +462,11 @@ def prompt(self, console: Optional[Console] = None) -> None:
for section in ["user", "external_network"]:
if not self.variables.get(section):
self.variables[section] = {}
if self.preseed_file:
preseed = sunbeam.jobs.questions.read_preseed(Path(self.preseed_file))
else:
preseed = {}

user_bank = sunbeam.jobs.questions.QuestionBank(
questions=user_questions(),
console=console,
preseed=preseed.get("user"),
preseed=self.preseed.get("user"),
previous_answers=self.variables.get("user"),
accept_defaults=self.accept_defaults,
)
Expand All @@ -481,15 +478,15 @@ def prompt(self, console: Optional[Console] = None) -> None:
ext_net_bank = sunbeam.jobs.questions.QuestionBank(
questions=ext_net_questions_local_only(),
console=console,
preseed=preseed.get("external_network"),
preseed=self.preseed.get("external_network"),
previous_answers=self.variables.get("external_network"),
accept_defaults=self.accept_defaults,
)
else:
ext_net_bank = sunbeam.jobs.questions.QuestionBank(
questions=ext_net_questions(),
console=console,
preseed=preseed.get("external_network"),
preseed=self.preseed.get("external_network"),
previous_answers=self.variables.get("external_network"),
accept_defaults=self.accept_defaults,
)
Expand Down Expand Up @@ -633,7 +630,7 @@ def __init__(
name: str,
jhelper: JujuHelper,
join_mode: bool = False,
preseed_file: Path | None = None,
deployment_preseed: dict | None = None,
):
super().__init__(
"Apply local hypervisor settings", "Applying local hypervisor settings"
Expand All @@ -642,8 +639,7 @@ def __init__(
self.name = name
self.jhelper = jhelper
self.join_mode = join_mode
self.preseed_file = preseed_file
self.preseed_file = preseed_file
self.preseed = deployment_preseed or {}

def has_prompts(self) -> bool:
return True
Expand Down Expand Up @@ -686,14 +682,10 @@ def prompt(self, console: Optional[Console] = None) -> None:
remote_access_location = self.variables.get("user", {}).get(
"remote_access_location"
)
if self.preseed_file:
preseed = sunbeam.jobs.questions.read_preseed(self.preseed_file)
else:
preseed = {}
# If adding new nodes to the cluster then local access makes no sense
# so always prompt for the nic.
if self.join_mode or remote_access_location == utils.REMOTE_ACCESS:
ext_net_preseed = preseed.get("external_network", {})
ext_net_preseed = self.preseed.get("external_network", {})
# If nic is in the preseed assume the user knows what they are doing and
# bypass validation
if ext_net_preseed.get("nic"):
Expand Down Expand Up @@ -730,21 +722,34 @@ def run(self, status: Optional[Status] = None) -> Result:
def _configure(
client: Client,
openrc: Optional[Path] = None,
preseed: Optional[Path] = None,
manifest: Optional[Path] = None,
accept_defaults: bool = False,
):
preflight_checks = []
preflight_checks.append(DaemonGroupCheck())
preflight_checks.append(VerifyBootstrappedCheck(client))
run_preflight_checks(preflight_checks, console)

manifest_obj = Manifest.load_latest_from_clusterdb(client, include_defaults=True)
# Validate manifest file
manifest_obj = None
if manifest:
manifest_obj = Manifest.load(
client, manifest_file=manifest, include_defaults=True
)
else:
manifest_obj = Manifest.load_latest_from_clusterdb(
client, include_defaults=True
)

LOG.debug(f"Manifest used for deployment - preseed: {manifest_obj.deployment}")
LOG.debug(f"Manifest used for deployment - software: {manifest_obj.software}")
preseed = manifest_obj.deployment or {}

name = utils.get_fqdn()
tfplan = "demo-setup"
tfplan_dir = TERRAFORM_DIR_NAMES.get(tfplan)
snap = Snap()
manifest_tfplans = manifest_obj.terraform
manifest_tfplans = manifest_obj.software.terraform
src = manifest_tfplans.get(tfplan).source
dst = snap.paths.user_common / "etc" / tfplan_dir
try:
Expand Down Expand Up @@ -776,7 +781,7 @@ def _configure(
UserQuestions(
client,
answer_file=answer_file,
preseed_file=preseed,
deployment_preseed=preseed,
accept_defaults=accept_defaults,
),
TerraformDemoInitStep(client, tfhelper),
Expand Down Expand Up @@ -805,7 +810,7 @@ def _configure(
# Accept preseed file but do not allow 'accept_defaults' as nic
# selection may vary from machine to machine and is potentially
# destructive if it takes over an unintended nic.
preseed_file=preseed,
deployment_preseed=preseed,
)
)
run_plan(plan, console)
Expand All @@ -815,9 +820,9 @@ def _configure(
@click.pass_context
@click.option("-a", "--accept-defaults", help="Accept all defaults.", is_flag=True)
@click.option(
"-p",
"--preseed",
help="Preseed file.",
"-m",
"--manifest",
help="Manifest file.",
type=click.Path(exists=True, dir_okay=False, path_type=Path),
)
@click.option(
Expand All @@ -829,14 +834,14 @@ def _configure(
def configure(
ctx: click.Context,
openrc: Optional[Path] = None,
preseed: Optional[Path] = None,
manifest: Optional[Path] = None,
accept_defaults: bool = False,
) -> None:
"""Configure cloud with some sensible defaults."""
if ctx.invoked_subcommand is not None:
return
client: Client = ctx.obj
_configure(client, openrc, preseed, accept_defaults)
_configure(client, openrc, manifest, accept_defaults)
for name, command in configure.commands.items():
LOG.debug("Running configure %r", name)
cmd_ctx = click.Context(
Expand Down
10 changes: 3 additions & 7 deletions sunbeam-python/sunbeam/commands/juju.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ def __init__(
cloud_type: str,
controller: str,
bootstrap_args: list = [],
preseed_file: Optional[Path] = None,
deployment_preseed: dict | None = None,
accept_defaults: bool = False,
):
super().__init__("Bootstrap Juju", "Bootstrapping Juju onto machine")
Expand All @@ -312,7 +312,7 @@ def __init__(
self.cloud_type = cloud_type
self.controller = controller
self.bootstrap_args = bootstrap_args
self.preseed_file = preseed_file
self.preseed = deployment_preseed or {}
self.accept_defaults = accept_defaults
self.juju_clouds = []
self.client = client
Expand All @@ -330,14 +330,10 @@ def prompt(self, console: Optional[Console] = None) -> None:
self.variables = questions.load_answers(self.client, self._CONFIG)
self.variables.setdefault("bootstrap", {})

if self.preseed_file:
preseed = questions.read_preseed(self.preseed_file)
else:
preseed = {}
bootstrap_bank = questions.QuestionBank(
questions=bootstrap_questions(),
console=console, # type: ignore
preseed=preseed.get("bootstrap"),
preseed=self.preseed.get("bootstrap"),
previous_answers=self.variables.get("bootstrap", {}),
accept_defaults=self.accept_defaults,
)
Expand Down
Loading

0 comments on commit 3fc153d

Please sign in to comment.