1313"""
1414
1515import logging
16+ import os
17+ import socket
1618from pathlib import Path
17- from socket import getfqdn
1819
1920import ops
2021
22+ import environment as env
2123import importer_node as node
2224import launchpad as lp
2325import package_installation as pkgs
3335GIT_UBUNTU_GIT_USER_NAME = "Ubuntu Git Importer"
3436GIT_UBUNTU_GIT_EMAIL = "usd-importer-do-not-mail@canonical.com"
3537GIT_UBUNTU_USER_HOME_DIR = "/var/local/git-ubuntu"
36- GIT_UBUNTU_SOURCE_BASE_URL = "git.launchpad.net/git-ubuntu"
38+ GIT_UBUNTU_SOURCE_URL = "https:// git.launchpad.net/git-ubuntu"
3739GIT_UBUNTU_KEYRING_FOLDER = Path (__file__ ).parent .parent / "keyring"
3840
3941
@@ -138,20 +140,6 @@ def _lpuser_lp_key(self) -> str | None:
138140
139141 return None
140142
141- @property
142- def _git_ubuntu_source_url (self ) -> str :
143- """Get the git-ubuntu source URL based on config.
144-
145- If an SSH private key is provided, get a git+ssh URL, otherwise use https.
146-
147- Returns:
148- The git-ubuntu source URL.
149- """
150- if self ._lpuser_ssh_key is not None :
151- return f"git+ssh://{ self ._lp_username } @{ GIT_UBUNTU_SOURCE_BASE_URL } "
152-
153- return f"https://{ GIT_UBUNTU_SOURCE_BASE_URL } "
154-
155143 @property
156144 def _git_ubuntu_primary_relation (self ) -> ops .Relation | None :
157145 """Get the peer relation that contains the primary node IP.
@@ -195,7 +183,7 @@ def _set_peer_primary_node_address(self) -> bool:
195183 relation = self ._git_ubuntu_primary_relation
196184
197185 if relation :
198- new_primary_address = getfqdn ( )
186+ new_primary_address = socket . gethostbyname ( socket . gethostname () )
199187 relation .data [self .app ]["primary_address" ] = new_primary_address
200188 logger .info ("Updated primary node address to %s" , new_primary_address )
201189 return True
@@ -267,16 +255,44 @@ def _refresh_git_ubuntu_source(self) -> bool:
267255 """
268256 self .unit .status = ops .MaintenanceStatus ("Refreshing git-ubuntu source." )
269257
258+ # Set https proxy environment variable if available.
259+ https_proxy = env .get_juju_https_proxy_url ()
260+
261+ if https_proxy != "" :
262+ logger .info ("Using https proxy %s for git-ubuntu source refresh." , https_proxy )
263+ os .environ ["https_proxy" ] = https_proxy
264+
265+ # Run clone or pull of git-ubuntu source.
270266 if not usr .refresh_git_ubuntu_source (
271267 GIT_UBUNTU_SYSTEM_USER_USERNAME ,
272268 GIT_UBUNTU_USER_HOME_DIR ,
273- self . _git_ubuntu_source_url ,
269+ GIT_UBUNTU_SOURCE_URL ,
274270 ):
275271 self .unit .status = ops .BlockedStatus ("Failed to refresh git-ubuntu source." )
276272 return False
277273
278274 return True
279275
276+ def _refresh_ssh_config (self ) -> bool :
277+ """Refresh the SSH config for the git-ubuntu user.
278+
279+ Returns:
280+ True if the config was updated successfully, False otherwise.
281+ """
282+ self .unit .status = ops .MaintenanceStatus ("Refreshing SSH config." )
283+
284+ if not usr .update_ssh_config (
285+ GIT_UBUNTU_SYSTEM_USER_USERNAME ,
286+ GIT_UBUNTU_USER_HOME_DIR ,
287+ env .get_juju_http_proxy_url (),
288+ ):
289+ self .unit .status = ops .BlockedStatus (
290+ "Failed to update SSH config for git-ubuntu user."
291+ )
292+ return False
293+
294+ return True
295+
280296 def _refresh_importer_node (self ) -> None :
281297 """Remove old and install new git-ubuntu services."""
282298 self .unit .status = ops .MaintenanceStatus ("Refreshing git-ubuntu services." )
@@ -290,6 +306,8 @@ def _refresh_importer_node(self) -> None:
290306 GIT_UBUNTU_USER_HOME_DIR ,
291307 GIT_UBUNTU_SYSTEM_USER_USERNAME ,
292308 self ._controller_port ,
309+ env .get_juju_http_proxy_url (),
310+ env .get_juju_https_proxy_url (),
293311 ):
294312 self .unit .status = ops .BlockedStatus ("Failed to install git-ubuntu services." )
295313 return
@@ -309,6 +327,7 @@ def _refresh_importer_node(self) -> None:
309327 self ._is_publishing_active ,
310328 self ._controller_port ,
311329 primary_ip ,
330+ env .get_juju_https_proxy_url (),
312331 ):
313332 self .unit .status = ops .BlockedStatus ("Failed to install git-ubuntu services." )
314333 return
@@ -423,6 +442,7 @@ def _on_config_changed(self, _: ops.ConfigChangedEvent) -> None:
423442 and self ._update_git_ubuntu_snap ()
424443 and self ._open_controller_port ()
425444 and self ._refresh_secret_keys ()
445+ and self ._refresh_ssh_config ()
426446 and self ._refresh_git_ubuntu_source ()
427447 ):
428448 # Initialize or re-install git-ubuntu services as needed.
0 commit comments