Skip to content

Commit

Permalink
fix: manifest with empty default values (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
skatsaounis authored Sep 2, 2024
1 parent 8519c14 commit 05502dc
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 17 deletions.
28 changes: 14 additions & 14 deletions anvil-python/anvil/commands/haproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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}")

Expand All @@ -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,
),
}
Expand Down Expand Up @@ -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(),
Expand All @@ -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:
Expand Down
51 changes: 51 additions & 0 deletions anvil-python/anvil/jobs/questions.py
Original file line number Diff line number Diff line change
@@ -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
7 changes: 4 additions & 3 deletions anvil-python/anvil/provider/local/deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand All @@ -31,6 +31,7 @@
POSTGRESQL_CONFIG_KEY,
postgresql_questions,
)
from anvil.jobs.questions import show_questions

LOG = logging.getLogger(__name__)
LOCAL_TYPE = "local"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 05502dc

Please sign in to comment.