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