23
23
_UNIX_USERNAME = "snap_daemon"
24
24
25
25
26
- def install (* , unit : ops .Unit ):
26
+ def install (* , unit : ops .Unit , model_uuid : str ):
27
27
"""Install snap."""
28
- if _snap .present :
28
+ installed_by_unit = pathlib .Path (
29
+ "/var/snap" , _SNAP_NAME , "common" , "installed_by_mysql_router_charm_unit"
30
+ )
31
+ unique_unit_name = f"{ model_uuid } _{ unit .name } "
32
+ # This charm can override/use an existing snap installation only if the snap was previously
33
+ # installed by this charm.
34
+ # Otherwise, the snap could be in use by another charm (e.g. MySQL Server charm, a different
35
+ # MySQL Router charm).
36
+ if _snap .present and not (
37
+ installed_by_unit .exists () and installed_by_unit .read_text () == unique_unit_name
38
+ ):
39
+ logger .debug (
40
+ f"{ installed_by_unit .exists () and installed_by_unit .read_text ()= } { unique_unit_name = } "
41
+ )
29
42
logger .error (f"{ _SNAP_NAME } snap already installed on machine. Installation aborted" )
30
43
raise Exception (f"Multiple { _SNAP_NAME } snap installs not supported on one machine" )
31
44
logger .debug (f"Installing { _SNAP_NAME = } , { _REVISION = } " )
@@ -45,6 +58,8 @@ def _set_retry_status(_) -> None:
45
58
):
46
59
with attempt :
47
60
_snap .ensure (state = snap_lib .SnapState .Present , revision = _REVISION )
61
+ installed_by_unit .write_text (unique_unit_name )
62
+ logger .debug (f"Wrote { unique_unit_name = } to { installed_by_unit .name = } " )
48
63
_snap .hold ()
49
64
logger .debug (f"Installed { _SNAP_NAME = } , { _REVISION = } " )
50
65
0 commit comments