Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions snap/hooks/configure
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
#!/usr/bin/env python3
#
# This hook reads snap options set via `snap set`, writes them to
# landscape-client.conf, and then unsets them.
# The landscape-client.conf file is the single source of truth,
# snap options are transient inputs only.

import os
import subprocess

from landscape.client.deployment import Configuration


def _snapctl(action, setting):
snapctl = subprocess.run(
["snapctl", action, setting],
capture_output=True,
text=True,
)
def _snapctl(*args):
snapctl = subprocess.run(["snapctl", *args], capture_output=True, text=True)
return snapctl.stdout


def update_ssl_cert(cert: str) -> str | None:
cert_path = os.path.join(os.getenv("SNAP_COMMON"), "etc/ssl/certs")
if not os.path.isdir(cert_path):
Expand All @@ -28,12 +30,13 @@ def update_ssl_cert(cert: str) -> str | None:
config = Configuration()
config.load([])

# Each entry in config_entries maps a snap option to a landscape
# config key, with an optional mapping applied to the value.
config_entries = [
# (snapctl key, landscape client config key, mapping function)
("account-name", "account_name", None),
("computer-title", "computer_title", None),
("landscape-url", "url", lambda url: f"{url}/message-system"),
("landscape-ping-url", "ping_url", lambda url: f"{url}/ping"),
("url", "url", None),
("ping-url", "ping_url", None),
("ssl-public-key", "ssl_public_key", update_ssl_cert),
("log-level", "log_level", None),
("script-users", "script_users", None),
Expand All @@ -51,8 +54,15 @@ config_entries = [
("cloud", "cloud", None),
]

# Deprecated keys kept for backward compatibility.
# New keys take precedence.
legacy_config_entries = [
("landscape-url", "url", lambda url: f"{url}/message-system"),
("landscape-ping-url", "ping_url", lambda url: f"{url}/ping"),
]

Comment thread
wck0 marked this conversation as resolved.
changed = set()
for snapctl_key, landscape_key, mapping_fn in config_entries:
for snapctl_key, landscape_key, mapping_fn in [*legacy_config_entries, *config_entries]:
value = _snapctl("get", snapctl_key).strip()
if not value:
# empty, value has not changed
Expand All @@ -66,7 +76,6 @@ for snapctl_key, landscape_key, mapping_fn in config_entries:

config.write()

# unset the values from snapctl so that in the next run
# we know what has changed
# the landscape-client.conf file is still the main source of truth
_snapctl("unset", " ".join(changed))
# Unset changed keys so that on the next run we know what has changed.
if changed:
_snapctl("unset", *changed)
13 changes: 9 additions & 4 deletions snap/hooks/default-configure
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ _access_group=$(snapctl get access-group)
_account=$(snapctl get account-name)
_registration_key=$(snapctl get registration-key)
_title=$(snapctl get computer-title)
_url=$(snapctl get landscape-url)
_url=$(snapctl get url)
_ping_url=$(snapctl get ping-url)
_log_level=$(snapctl get log-level)
_script_users=$(snapctl get script-users)
_manager_plugins=$(snapctl get manager-plugins)
_monitor_plugins=$(snapctl get monitor-plugins)

if [ -z "$_url" ]; then
_url="https://landscape.canonical.com"
_url="https://landscape.canonical.com/message-system"
fi

if [ -z "$_ping_url" ]; then
_ping_url="http://landscape.canonical.com/ping"
fi

if [ -z "$_log_level" ]; then
Expand All @@ -36,8 +41,8 @@ cat > "$CLIENT_CONF" << EOF
[client]
account_name = $_account
computer_title = $_title
url = ${_url}/message-system
ping_url = ${_url}/ping
url = $_url
ping_url = $_ping_url
log_level = $_log_level
script_users = $_script_users
manager_plugins = $_manager_plugins
Expand Down
Loading