| | 1 | | using System.Text; |
| | 2 | | using System.Text.Json.Serialization; |
| | 3 | | using Dotnet.Installer.Core.Models.Events; |
| | 4 | | using Dotnet.Installer.Core.Services.Contracts; |
| | 5 | |
|
| | 6 | | namespace Dotnet.Installer.Core.Models; |
| | 7 | |
|
| | 8 | | public class Component |
| | 9 | | { |
| 76 | 10 | | public required string Key { get; init; } |
| 9 | 11 | | public required string Name { get; init; } |
| 9 | 12 | | public required string Description { get; init; } |
| 9 | 13 | | public required int MajorVersion { get; init; } |
| 9 | 14 | | public required bool IsLts { get; init; } |
| | 15 | | [JsonPropertyName("eol")] |
| 9 | 16 | | public required DateTime EndOfLife { get; init; } |
| 32 | 17 | | public required IEnumerable<string> Dependencies { get; init; } |
| 9 | 18 | | public Installation? Installation { get; set; } |
| 7 | 19 | | public bool IsInstalled => Installation is not null; |
| | 20 | |
|
| | 21 | | public event EventHandler<InstallationStartedEventArgs>? InstallationStarted; |
| | 22 | | public event EventHandler<InstallationFinishedEventArgs>? InstallationFinished; |
| | 23 | |
|
| | 24 | | public async Task Install(IFileService fileService, IManifestService manifestService, ISnapService snapService, |
| | 25 | | ISystemdService systemdService, ILogger? logger = null) |
| 5 | 26 | | { |
| 5 | 27 | | if (IsInstalled) |
| 0 | 28 | | { |
| 0 | 29 | | logger?.LogInformation($"{Description} already installed!"); |
| 0 | 30 | | return; |
| | 31 | | } |
| | 32 | |
|
| 5 | 33 | | InstallationStarted?.Invoke(this, new InstallationStartedEventArgs(Key)); |
| | 34 | |
|
| | 35 | | // Install content snap on the machine |
| 5 | 36 | | if (!snapService.IsSnapInstalled(Key)) |
| 5 | 37 | | { |
| 5 | 38 | | var result = await snapService.Install(Key); |
| 5 | 39 | | if (!result.IsSuccess) throw new ApplicationException(result.StandardError); |
| 5 | 40 | | } |
| | 41 | |
|
| | 42 | | // Place linking file in the content snap's $SNAP_COMMON |
| 5 | 43 | | await fileService.PlaceLinkageFile(Key); |
| | 44 | |
|
| | 45 | | // Install Systemd mount units |
| 5 | 46 | | await PlaceMountUnits(fileService, manifestService, systemdService, logger); |
| | 47 | |
|
| | 48 | | // Install update watcher unit |
| 5 | 49 | | await PlacePathUnits(fileService, systemdService, logger); |
| | 50 | |
|
| | 51 | | // Register the installation of this component in the local manifest file |
| 5 | 52 | | await manifestService.Add(this); |
| | 53 | |
|
| 19 | 54 | | foreach (var dependency in Dependencies) |
| 2 | 55 | | { |
| 7 | 56 | | var component = manifestService.Remote.First(c => c.Key == dependency); |
| 2 | 57 | | await component.Install(fileService, manifestService, snapService, systemdService, logger); |
| 2 | 58 | | } |
| | 59 | |
|
| 5 | 60 | | InstallationFinished?.Invoke(this, new InstallationFinishedEventArgs(Key)); |
| 5 | 61 | | } |
| | 62 | |
|
| | 63 | | public async Task Uninstall(IFileService fileService, IManifestService manifestService, ISnapService snapService, |
| | 64 | | ISystemdService systemdService, ILogger? logger = default) |
| 1 | 65 | | { |
| 1 | 66 | | if (IsInstalled) |
| 1 | 67 | | { |
| | 68 | | // Uninstall systemd mount units |
| 1 | 69 | | await RemoveMountUnits(fileService, manifestService, systemdService, logger); |
| | 70 | |
|
| | 71 | | // Uninstall systemd path units |
| 1 | 72 | | await RemovePathUnits(fileService, systemdService, logger); |
| | 73 | |
|
| 1 | 74 | | if (snapService.IsSnapInstalled(Key)) |
| 0 | 75 | | { |
| 0 | 76 | | await snapService.Remove(Key, purge: true); |
| 0 | 77 | | } |
| | 78 | |
|
| 1 | 79 | | Installation = null; |
| 1 | 80 | | await manifestService.Remove(this); |
| 1 | 81 | | } |
| 1 | 82 | | } |
| | 83 | |
|
| | 84 | | public async Task PlaceMountUnits(IFileService fileService, IManifestService manifestService, |
| | 85 | | ISystemdService systemdService, ILogger? logger = default) |
| 5 | 86 | | { |
| 5 | 87 | | var units = new StringBuilder(); |
| 5 | 88 | | var unitPaths = fileService.EnumerateContentSnapMountFiles(Key); |
| | 89 | |
|
| 15 | 90 | | foreach (var unitPath in unitPaths) |
| 0 | 91 | | { |
| 0 | 92 | | logger?.LogDebug($"Copying {unitPath} to systemd directory."); |
| 0 | 93 | | fileService.InstallSystemdMountUnit(unitPath); |
| 0 | 94 | | units.AppendLine(unitPath.Split('/').Last()); |
| 0 | 95 | | } |
| | 96 | |
|
| | 97 | | // Save unit names to component .mounts file |
| 5 | 98 | | await fileService.PlaceUnitsFile(manifestService.SnapConfigurationLocation, contentSnapName: Key, |
| 5 | 99 | | units.ToString()); |
| | 100 | |
|
| 5 | 101 | | var result = await systemdService.DaemonReload(); |
| 5 | 102 | | if (!result.IsSuccess) |
| 0 | 103 | | { |
| 0 | 104 | | throw new ApplicationException("Could not reload systemd daemon"); |
| | 105 | | } |
| 5 | 106 | | await Mount(manifestService, fileService, systemdService, logger); |
| 5 | 107 | | } |
| | 108 | |
|
| | 109 | | public async Task RemoveMountUnits(IFileService fileService, IManifestService manifestService, |
| | 110 | | ISystemdService systemdService, ILogger? logger = default) |
| 1 | 111 | | { |
| 1 | 112 | | await Unmount(fileService, manifestService, systemdService, logger); |
| | 113 | |
|
| 1 | 114 | | var units = await fileService.ReadUnitsFile(manifestService.SnapConfigurationLocation, Key); |
| | 115 | |
|
| 3 | 116 | | foreach (var unit in units) |
| 0 | 117 | | { |
| 0 | 118 | | logger?.LogDebug($"Removing {unit} from systemd directory."); |
| 0 | 119 | | fileService.UninstallSystemdMountUnit(unit); |
| 0 | 120 | | } |
| | 121 | |
|
| 1 | 122 | | fileService.DeleteUnitsFile(manifestService.SnapConfigurationLocation, Key); |
| | 123 | |
|
| 1 | 124 | | var result = await systemdService.DaemonReload(); |
| 1 | 125 | | if (!result.IsSuccess) |
| 0 | 126 | | { |
| 0 | 127 | | throw new ApplicationException("Could not reload systemd daemon"); |
| | 128 | | } |
| 1 | 129 | | } |
| | 130 | |
|
| | 131 | | public async Task Mount(IManifestService manifestService, IFileService fileService, ISystemdService systemdService, |
| | 132 | | ILogger? logger = default) |
| 5 | 133 | | { |
| 5 | 134 | | var units = await fileService.ReadUnitsFile(manifestService.SnapConfigurationLocation, Key); |
| | 135 | |
|
| 15 | 136 | | foreach (var unit in units) |
| 0 | 137 | | { |
| 0 | 138 | | var result = await systemdService.EnableUnit(unit); |
| 0 | 139 | | if (!result.IsSuccess) |
| 0 | 140 | | { |
| 0 | 141 | | throw new ApplicationException($"Could not enable unit {unit}"); |
| | 142 | | } |
| 0 | 143 | | logger?.LogDebug($"Enabled {unit}"); |
| | 144 | |
|
| 0 | 145 | | result = await systemdService.StartUnit(unit); |
| 0 | 146 | | if (!result.IsSuccess) |
| 0 | 147 | | { |
| 0 | 148 | | throw new ApplicationException($"Could not start unit {unit}"); |
| | 149 | | } |
| 0 | 150 | | logger?.LogDebug($"Started {unit}"); |
| | 151 | |
|
| 0 | 152 | | logger?.LogDebug($"Finished mounting {unit}"); |
| 0 | 153 | | } |
| 5 | 154 | | } |
| | 155 | |
|
| | 156 | | public async Task Unmount(IFileService fileService, IManifestService manifestService, |
| | 157 | | ISystemdService systemdService, ILogger? logger = default) |
| 1 | 158 | | { |
| 1 | 159 | | var units = await fileService.ReadUnitsFile(manifestService.SnapConfigurationLocation, Key); |
| | 160 | |
|
| 3 | 161 | | foreach (var unit in units) |
| 0 | 162 | | { |
| 0 | 163 | | var result = await systemdService.DisableUnit(unit); |
| 0 | 164 | | if (!result.IsSuccess) |
| 0 | 165 | | { |
| 0 | 166 | | throw new ApplicationException($"Could not disable unit {unit}"); |
| | 167 | | } |
| 0 | 168 | | logger?.LogDebug($"Disabled {unit}"); |
| | 169 | |
|
| 0 | 170 | | result = await systemdService.StopUnit(unit); |
| 0 | 171 | | if (!result.IsSuccess) |
| 0 | 172 | | { |
| 0 | 173 | | throw new ApplicationException($"Could not stop unit {unit}"); |
| | 174 | | } |
| 0 | 175 | | logger?.LogDebug($"Stopped {unit}"); |
| | 176 | |
|
| 0 | 177 | | logger?.LogDebug($"Finished unmounting {unit}"); |
| 0 | 178 | | } |
| | 179 | |
|
| | 180 | | // Check for any empty directories |
| 1 | 181 | | fileService.RemoveEmptyDirectories(manifestService.DotnetInstallLocation); |
| 1 | 182 | | logger?.LogDebug("Removed empty directories."); |
| 1 | 183 | | } |
| | 184 | |
|
| | 185 | | private async Task PlacePathUnits(IFileService fileService, ISystemdService systemdService, ILogger? logger = null) |
| 5 | 186 | | { |
| 5 | 187 | | fileService.InstallSystemdPathUnit(Key); |
| 5 | 188 | | logger?.LogDebug($"Placed upgrade watcher path and service units for snap {Key}"); |
| | 189 | |
|
| 5 | 190 | | var result = await systemdService.DaemonReload(); |
| 5 | 191 | | if (!result.IsSuccess) |
| 0 | 192 | | { |
| 0 | 193 | | throw new ApplicationException("Could not reload systemd daemon"); |
| | 194 | | } |
| | 195 | |
|
| 5 | 196 | | result = await systemdService.EnableUnit($"{Key}-update-watcher.path"); |
| 5 | 197 | | if (!result.IsSuccess) |
| 0 | 198 | | { |
| 0 | 199 | | throw new ApplicationException($"Could not enable {Key}-update-watcher.path"); |
| | 200 | | } |
| 5 | 201 | | logger?.LogDebug($"Enabled {Key}-update-watcher.path"); |
| | 202 | |
|
| 5 | 203 | | result = await systemdService.StartUnit($"{Key}-update-watcher.path"); |
| 5 | 204 | | if (!result.IsSuccess) |
| 0 | 205 | | { |
| 0 | 206 | | throw new ApplicationException($"Could not start {Key}-update-watcher.path"); |
| | 207 | | } |
| 5 | 208 | | logger?.LogDebug($"Started {Key}-update-watcher.path"); |
| 5 | 209 | | } |
| | 210 | |
|
| | 211 | | private async Task RemovePathUnits(IFileService fileService, ISystemdService systemdService, |
| | 212 | | ILogger? logger = default) |
| 1 | 213 | | { |
| 1 | 214 | | var result = await systemdService.DisableUnit($"{Key}-update-watcher.path"); |
| 1 | 215 | | if (!result.IsSuccess) |
| 0 | 216 | | { |
| 0 | 217 | | throw new ApplicationException($"Could not disable {Key}-update-watcher.path"); |
| | 218 | | } |
| 1 | 219 | | logger?.LogDebug($"Disabled {Key}-update-watcher.path"); |
| | 220 | |
|
| 1 | 221 | | result = await systemdService.StopUnit($"{Key}-update-watcher.path"); |
| 1 | 222 | | if (!result.IsSuccess) |
| 0 | 223 | | { |
| 0 | 224 | | throw new ApplicationException($"Could not stop {Key}-update-watcher.path"); |
| | 225 | | } |
| 1 | 226 | | logger?.LogDebug($"Stopped {Key}-update-watcher.path"); |
| | 227 | |
|
| 1 | 228 | | fileService.UninstallSystemdPathUnit(Key); |
| 1 | 229 | | logger?.LogDebug($"Removed upgrade watcher path and service units for snap {Key}"); |
| | 230 | |
|
| 1 | 231 | | result = await systemdService.DaemonReload(); |
| 1 | 232 | | if (!result.IsSuccess) |
| 0 | 233 | | { |
| 0 | 234 | | throw new ApplicationException("Could not reload systemd daemon"); |
| | 235 | | } |
| 1 | 236 | | } |
| | 237 | | } |