Skip to content

Commit 82b5772

Browse files
authored
Merge pull request #25 from canonical/fix-git-ubuntu-source-clone
Refactor git-ubuntu source clone and key extraction
2 parents 3d4f176 + c3c197b commit 82b5772

3 files changed

Lines changed: 63 additions & 41 deletions

File tree

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,5 +136,6 @@ disable = [
136136
"too-many-instance-attributes",
137137
"too-many-arguments",
138138
"too-many-positional-arguments",
139-
"too-many-locals"
139+
"too-many-locals",
140+
"too-many-boolean-expressions",
140141
]

src/charm.py

Lines changed: 58 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -98,32 +98,43 @@ def _num_workers(self) -> int:
9898
return 0
9999

100100
@property
101-
def _lpuser_ssh_key(self) -> str | None:
101+
def _lpuser_secret(self) -> ops.model.Secret | None:
102+
secret_id: str = ""
103+
102104
try:
103105
secret_id = str(self.config["lpuser_secret_id"])
104-
lpuser_secret = self.model.get_secret(id=secret_id)
105-
ssh_key_data = lpuser_secret.get_content().get("sshkey")
106+
except KeyError:
107+
logger.warning("lpuser_secret_id config not available, unable to extract keys.")
108+
return None
109+
110+
try:
111+
return self.model.get_secret(id=secret_id)
112+
except (ops.SecretNotFoundError, ops.model.ModelError):
113+
logger.warning("Failed to get lpuser secret with id %s", secret_id)
114+
115+
return None
106116

107-
if ssh_key_data is not None:
108-
return str(ssh_key_data)
117+
@property
118+
def _lpuser_ssh_key(self) -> str | None:
119+
secret = self._lpuser_secret
109120

110-
except (KeyError, ops.SecretNotFoundError, ops.model.ModelError):
111-
pass
121+
if secret is not None:
122+
try:
123+
return secret.get_content(refresh=True)["sshkey"]
124+
except KeyError:
125+
logger.warning("sshkey secret key not found in lpuser secret.")
112126

113127
return None
114128

115129
@property
116130
def _lpuser_lp_key(self) -> str | None:
117-
try:
118-
secret_id = str(self.config["lpuser_secret_id"])
119-
lpuser_secret = self.model.get_secret(id=secret_id)
120-
lp_key_data = lpuser_secret.get_content().get("lpkey")
131+
secret = self._lpuser_secret
121132

122-
if lp_key_data is not None:
123-
return str(lp_key_data)
124-
125-
except (KeyError, ops.SecretNotFoundError, ops.model.ModelError):
126-
pass
133+
if secret is not None:
134+
try:
135+
return secret.get_content(refresh=True)["lpkey"]
136+
except KeyError:
137+
logger.warning("lpkey secret key not found in lpuser secret.")
127138

128139
return None
129140

@@ -248,6 +259,24 @@ def _refresh_secret_keys(self) -> bool:
248259

249260
return True
250261

262+
def _refresh_git_ubuntu_source(self) -> bool:
263+
"""Refresh the git-ubuntu source code from the configured URL.
264+
265+
Returns:
266+
True if the source was refreshed successfully, False otherwise.
267+
"""
268+
self.unit.status = ops.MaintenanceStatus("Refreshing git-ubuntu source.")
269+
270+
if not usr.refresh_git_ubuntu_source(
271+
GIT_UBUNTU_SYSTEM_USER_USERNAME,
272+
GIT_UBUNTU_USER_HOME_DIR,
273+
self._git_ubuntu_source_url,
274+
):
275+
self.unit.status = ops.BlockedStatus("Failed to refresh git-ubuntu source.")
276+
return False
277+
278+
return True
279+
251280
def _refresh_importer_node(self) -> None:
252281
"""Remove old and install new git-ubuntu services."""
253282
self.unit.status = ops.MaintenanceStatus("Refreshing git-ubuntu services.")
@@ -383,29 +412,21 @@ def _on_install(self, _: ops.InstallEvent) -> None:
383412
self.unit.status = ops.ActiveStatus("Install complete.")
384413

385414
def _on_config_changed(self, _: ops.ConfigChangedEvent) -> None:
386-
"""Handle updates to config items."""
387-
# Update user's git and lpuser config, and git-ubuntu snap
388-
if (
389-
not self._update_git_user_config()
390-
or not self._update_lpuser_config()
391-
or not self._update_git_ubuntu_snap()
392-
or not self._open_controller_port()
393-
or not self._refresh_secret_keys()
394-
):
395-
return
415+
"""Handle updates to config items.
396416
397-
# Refresh git-ubuntu source code
398-
self.unit.status = ops.MaintenanceStatus("Refreshing git-ubuntu source.")
399-
if not usr.refresh_git_ubuntu_source(
400-
GIT_UBUNTU_SYSTEM_USER_USERNAME,
401-
GIT_UBUNTU_USER_HOME_DIR,
402-
self._git_ubuntu_source_url,
417+
Update user settings, git config, the git-ubuntu snap and source, open ports, and keys.
418+
If everything is successful, refresh git-ubuntu services.
419+
"""
420+
if (
421+
self._update_git_user_config()
422+
and self._update_lpuser_config()
423+
and self._update_git_ubuntu_snap()
424+
and self._open_controller_port()
425+
and self._refresh_secret_keys()
426+
and self._refresh_git_ubuntu_source()
403427
):
404-
self.unit.status = ops.BlockedStatus("Failed to refresh git-ubuntu source.")
405-
return
406-
407-
# Initialize or re-install git-ubuntu services as needed.
408-
self._refresh_importer_node()
428+
# Initialize or re-install git-ubuntu services as needed.
429+
self._refresh_importer_node()
409430

410431
def _on_leader_elected(self, _: ops.LeaderElectedEvent) -> None:
411432
"""Refresh services and update peer data when the unit is elected as leader."""

src/user_management.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def refresh_git_ubuntu_source(user: str, home_dir: str, source_url: str) -> bool
157157
clone_dir = pathops.LocalPath(directory_path, "live-allowlist-denylist-source")
158158

159159
if clone_dir.is_dir():
160-
logger.info("Updating existing git-ubuntu source in %s", clone_dir)
160+
logger.info("Updating existing git-ubuntu source in %s", clone_dir.as_posix())
161161

162162
# Update origin to the current source url
163163
if not _run_command_as_user(
@@ -174,8 +174,8 @@ def refresh_git_ubuntu_source(user: str, home_dir: str, source_url: str) -> bool
174174
return True
175175

176176
# Clone the repository
177-
logger.info("Cloning git-ubuntu source to %s", clone_dir)
178-
if not _run_command_as_user(user, f"git clone {source_url} {clone_dir}"):
177+
logger.info("Cloning git-ubuntu source to %s", clone_dir.as_posix())
178+
if not _run_command_as_user(user, f"git clone {source_url} {clone_dir.as_posix()}"):
179179
logger.error("Failed to clone git-ubuntu source.")
180180
return False
181181

0 commit comments

Comments
 (0)