Skip to content

Commit

Permalink
Merge pull request #131 from hemanthnakkina/preseed-plugin-tls
Browse files Browse the repository at this point in the history
Generate preseed content for certificates
  • Loading branch information
gboutry authored Feb 19, 2024
2 parents b8a2c73 + d2a4aae commit cda73d1
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 32 deletions.
36 changes: 4 additions & 32 deletions sunbeam-python/sunbeam/commands/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,44 +44,14 @@
from sunbeam.jobs.checks import DaemonGroupCheck, VerifyBootstrappedCheck
from sunbeam.jobs.common import FORMAT_TABLE, FORMAT_YAML, run_preflight_checks
from sunbeam.jobs.manifest import Manifest
from sunbeam.jobs.questions import QuestionBank, load_answers
from sunbeam.jobs.plugin import PluginManager
from sunbeam.jobs.questions import QuestionBank, load_answers, show_questions
from sunbeam.utils import asdict_with_extra_fields

LOG = logging.getLogger(__name__)
console = Console()


def show_questions(
question_bank,
section=None,
subsection=None,
section_description=None,
comment_out=False,
) -> list:
lines = []
space = " "
indent = ""
outer_indent = space * 2
if comment_out:
comment = "# "
else:
comment = ""
if section:
if section_description:
lines.append(f"{outer_indent}{comment}{indent}# {section_description}")
lines.append(f"{outer_indent}{comment}{indent}{section}:")
indent = space * 2
if subsection:
lines.append(f"{outer_indent}{comment}{indent}{subsection}:")
indent = space * 4
for key, question in question_bank.questions.items():
default = question.calculate_default() or ""
lines.append(f"{outer_indent}{comment}{indent}# {question.question}")
lines.append(f"{outer_indent}{comment}{indent}{key}: {default}")

return lines


def generate_deployment_preseed(client: Client) -> str:
"""Generate deployment preseed section."""
name = utils.get_fqdn()
Expand Down Expand Up @@ -155,6 +125,8 @@ def generate_deployment_preseed(client: Client) -> str:
)
)

preseed_content.extend(PluginManager().get_preseed_questions_content(client))

preseed_content_final = "\n".join(preseed_content)
return preseed_content_final

Expand Down
10 changes: 10 additions & 0 deletions sunbeam-python/sunbeam/jobs/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,16 @@ def add_manifest_section(cls, client, software_config) -> None:
plugin = klass(client)
plugin.add_manifest_section(software_config)

@classmethod
def get_preseed_questions_content(cls, client: Client) -> None:
content = []
plugins = cls.get_all_plugin_classes()
for klass in plugins:
plugin = klass(client)
content.extend(plugin.preseed_questions_content())

return content

@classmethod
def get_all_charms_in_openstack_plan(cls, client: Client) -> list:
charms = []
Expand Down
37 changes: 37 additions & 0 deletions sunbeam-python/sunbeam/jobs/questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,40 @@ def load_answers(client: Client, key: str) -> dict:
def write_answers(client: Client, key: str, answers):
"""Write answers to database."""
client.cluster.update_config(key, json.dumps(answers))


def show_questions(
question_bank,
section=None,
subsection=None,
section_description=None,
comment_out=False,
) -> list:
"""Return preseed questions as list."""
lines = []
space = " "
indent = ""
outer_indent = space * 2
if comment_out:
comment = "# "
else:
comment = ""
if section:
if section_description:
lines.append(f"{outer_indent}{comment}{indent}# {section_description}")
lines.append(f"{outer_indent}{comment}{indent}{section}:")
indent = space * 2
# TODO(hemanth): To handle multi level subsections, currently only one level is
# considered
if subsection:
lines.append(f"{outer_indent}{comment}{indent}{subsection}:")
indent = space * 4
# TODO(hemanth): Repeat Questions bank for multiple subsections of same type
# Example: To generate preseed with microceph_config for multiple nodes if values
# are available in cluster db.
for key, question in question_bank.questions.items():
default = question.calculate_default() or ""
lines.append(f"{outer_indent}{comment}{indent}# {question.question}")
lines.append(f"{outer_indent}{comment}{indent}{key}: {default}")

return lines
16 changes: 16 additions & 0 deletions sunbeam-python/sunbeam/plugins/ca/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,22 @@ def manifest_attributes_tfvar_map(self) -> dict:
}
}

def preseed_questions_content(self) -> list:
"""Generate preseed manifest content."""
certificate_question_bank = questions.QuestionBank(
questions=certificate_questions("unit", "subject"),
console=console,
previous_answers={},
)
content = questions.show_questions(
certificate_question_bank,
section="certificates",
subsection="<CSR x500UniqueIdentifier>",
section_description="TLS Certificates",
comment_out=True,
)
return content

@click.command()
@click.option(
"--endpoint",
Expand Down
7 changes: 7 additions & 0 deletions sunbeam-python/sunbeam/plugins/interface/v1/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,13 @@ def manifest_attributes_tfvar_map(self) -> dict:
"""
return {}

def preseed_questions_content(self) -> list:
"""Generate preseed manifest content.
The returned content will be used in generation of manifest.
"""
return []

def get_terraform_plans_base_path(self) -> Path:
"""Return Terraform plan base location."""
return Snap().paths.user_common
Expand Down

0 comments on commit cda73d1

Please sign in to comment.