Skip to content

Commit

Permalink
Use separate terraform variable to set services yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
wyattrees committed Jul 10, 2024
1 parent f06bc1e commit 264f26a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
27 changes: 19 additions & 8 deletions anvil-python/anvil/commands/haproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ class DeployHAProxyApplicationStep(DeployMachineApplicationStep):
server_options: maxconn 100 cookie S{"{i}"} check
crts: [{HAPROXY_CERTS_DIR}]
"""
_DEFAULT_SERVICES_CONFIG: str = """- service_name: haproxy_service
service_host: "0.0.0.0"
service_port: 80
service_options: [balance leastconn, cookie SRVNAME insert]
server_options: maxconn 100 cookie S{i} check
"""

def __init__(
self,
Expand All @@ -133,7 +139,7 @@ def __init__(
)
self.preseed = deployment_preseed or {}
self.accept_defaults = accept_defaults
self.variables: dict[str, Any] = {"charm_haproxy_config": {}}
self.use_tls_termination = False

def get_application_timeout(self) -> int:
return HAPROXY_APP_TIMEOUT
Expand All @@ -144,11 +150,15 @@ 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("charm_haproxy_config", {})
variables.setdefault(
"haproxy_services_yaml", self._DEFAULT_SERVICES_CONFIG
)

# Set defaults
self.preseed.setdefault("virtual_ip", "")
self.preseed.setdefault("charm_haproxy_config", {})
self.preseed.setdefault(
"haproxy_services_yaml", self._DEFAULT_SERVICES_CONFIG
)

haproxy_config_bank = questions.QuestionBank(
questions=haproxy_questions(),
Expand All @@ -173,15 +183,12 @@ def prompt(self, console: Console | None = None) -> None:
os.path.join(HAPROXY_CERTS_DIR, "haproxy.pem"), "w"
) as combined_file:
combined_file.write(key + cert)
variables["charm_haproxy_config"]["services"] = (
self._TLS_SERVICES_CONFIG
)
variables["haproxy_port"] = 443
variables["haproxy_services_yaml"] = self._TLS_SERVICES_CONFIG
self.use_tls_termination = True
else:
LOG.debug(
"No certificate/key provided, skipping TLS configuration"
)
variables["haproxy_port"] = 80

LOG.debug(variables)
questions.write_answers(self.client, self._HAPROXY_CONFIG, variables)
Expand All @@ -190,6 +197,10 @@ def extra_tfvars(self) -> dict[str, Any]:
variables: dict[str, Any] = questions.load_answers(
self.client, self._HAPROXY_CONFIG
)
if self.use_tls_termination:
variables["haproxy_port"] = 443
else:
variables["haproxy_port"] = 80
LOG.debug(f"extra tfvars: {variables}")
return variables

Expand Down
5 changes: 4 additions & 1 deletion cloud/etc/deploy-haproxy/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ resource "juju_application" "haproxy" {
base = "ubuntu@22.04"
}

config = var.charm_haproxy_config
config = merge(
{ services = var.haproxy_services_yaml },
var.charm_haproxy_config,
)
}

resource "juju_application" "keepalived" {
Expand Down
6 changes: 6 additions & 0 deletions cloud/etc/deploy-haproxy/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ variable "virtual_ip" {
default = ""
}

variable "haproxy_services_yaml" {
description = "yaml-formatted services definition for HA proxy charm"
type = string
default = ""
}

variable "haproxy_port" {
description = "The port that HAProxy listens on"
type = string
Expand Down

0 comments on commit 264f26a

Please sign in to comment.