diff --git a/anvil-python/anvil/commands/haproxy.py b/anvil-python/anvil/commands/haproxy.py index f124129..444c993 100644 --- a/anvil-python/anvil/commands/haproxy.py +++ b/anvil-python/anvil/commands/haproxy.py @@ -45,8 +45,8 @@ LOG = logging.getLogger(__name__) -def validate_cert_file(filepath: str | None) -> None: - if filepath is None: +def validate_cert_file(filepath: str) -> None: + if filepath == "": return if not os.path.isfile(filepath): raise ValueError(f"{filepath} does not exist") @@ -58,8 +58,8 @@ def validate_cert_file(filepath: str | None) -> None: raise ValueError(f"Permission denied when trying to read {filepath}") -def validate_key_file(filepath: str | None) -> None: - if filepath is None: +def validate_key_file(filepath: str) -> None: + if filepath == "": return if not os.path.isfile(filepath): raise ValueError(f"{filepath} does not exist") @@ -71,12 +71,12 @@ def validate_key_file(filepath: str | None) -> None: raise ValueError(f"Permission denied when trying to read {filepath}") -def validate_virtual_ip(value: str) -> str: +def validate_virtual_ip(value: str) -> None: """We allow passing an empty IP for virtual_ip""" if value == "": - return "" + return try: - return ipaddress.ip_address(value).exploded + ipaddress.ip_address(value).exploded except ValueError as e: raise ValueError(f"{value} is not a valid IP address: {e}") @@ -90,12 +90,12 @@ def haproxy_questions() -> dict[str, questions.PromptQuestion]: ), "ssl_cert": questions.PromptQuestion( "Path to SSL Certificate for HAProxy (enter nothing to skip TLS)", - default_value=None, + default_value="", validation_function=validate_cert_file, ), "ssl_key": questions.PromptQuestion( "Path to private key for the SSL certificate (enter nothing to skip TLS)", - default_value=None, + default_value="", validation_function=validate_key_file, ), } @@ -152,13 +152,13 @@ def has_prompts(self) -> bool: def prompt(self, console: Console | None = None) -> None: variables = questions.load_answers(self.client, self._HAPROXY_CONFIG) variables.setdefault("virtual_ip", "") - variables.setdefault("ssl_cert", None) - variables.setdefault("ssl_key", None) + variables.setdefault("ssl_cert", "") + variables.setdefault("ssl_key", "") # Set defaults self.preseed.setdefault("virtual_ip", "") - self.preseed.setdefault("ssl_cert", None) - self.preseed.setdefault("ssl_key", None) + self.preseed.setdefault("ssl_cert", "") + self.preseed.setdefault("ssl_key", "") haproxy_config_bank = questions.QuestionBank( questions=haproxy_questions(), @@ -185,7 +185,7 @@ def extra_tfvars(self) -> dict[str, Any]: cert_filepath = variables["ssl_cert"] key_filepath = variables["ssl_key"] - if cert_filepath is not None and key_filepath is not None: + if cert_filepath != "" and key_filepath != "": with open(cert_filepath) as cert_file: variables["ssl_cert_content"] = cert_file.read() with open(key_filepath) as key_file: diff --git a/anvil-python/anvil/jobs/questions.py b/anvil-python/anvil/jobs/questions.py new file mode 100644 index 0000000..1dda79c --- /dev/null +++ b/anvil-python/anvil/jobs/questions.py @@ -0,0 +1,51 @@ +# Copyright (c) 2024 Canonical Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +from sunbeam.jobs.questions import QuestionBank + + +def show_questions( + question_bank: QuestionBank, + section: str | None = None, + subsection: str | None = None, + section_description: str | None = None, + comment_out: bool = False, +) -> list[str]: + """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 + 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 diff --git a/anvil-python/anvil/provider/local/deployment.py b/anvil-python/anvil/provider/local/deployment.py index 8556a7f..1336e24 100644 --- a/anvil-python/anvil/provider/local/deployment.py +++ b/anvil-python/anvil/provider/local/deployment.py @@ -21,7 +21,7 @@ ClusterServiceUnavailableException, ) from sunbeam.commands.juju import BOOTSTRAP_CONFIG_KEY, bootstrap_questions -from sunbeam.jobs.questions import QuestionBank, load_answers, show_questions +from sunbeam.jobs.questions import QuestionBank, load_answers from sunbeam.provider.local.deployment import ( LocalDeployment as SunbeamLocalDeployment, ) @@ -31,6 +31,7 @@ POSTGRESQL_CONFIG_KEY, postgresql_questions, ) +from anvil.jobs.questions import show_questions LOG = logging.getLogger(__name__) LOCAL_TYPE = "local" @@ -76,13 +77,13 @@ def generate_preseed(self, console: Console) -> str: variables = load_answers(client, HAPROXY_CONFIG_KEY) except ClusterServiceUnavailableException: variables = {} - keepalived_config_bank = QuestionBank( + haproxy_config_bank = QuestionBank( questions=haproxy_questions(), console=console, previous_answers=variables, ) preseed_content.extend( - show_questions(keepalived_config_bank, section="haproxy") + show_questions(haproxy_config_bank, section="haproxy") ) preseed_content_final = "\n".join(preseed_content)