import '../../about/external/initial/SystemdSpec.dart'; import '../../about/internal/application/ServerProfile.dart'; import '../../about/internal/application/ServiceConfig.dart'; import '../../about/internal/application/StepTracker.dart'; import '../../comms/Comms.dart' show LogLine; import '../parent/ParentViewModel.dart'; import 'ConnectSystemd.dart'; class ViewSystemd extends ParentViewModel { ConnectSystemd connection; ViewSystemd(super.context, this.connection); Stream get logs => getDataManager().logs; Stream> get steps => getDataManager().stepStream; /// Writes the unit file, reloads systemd and (optionally) enables and starts /// it. On success the unit name is recorded against [service] so the deploy /// and log screens target the right unit from now on. void installUnit( ServerProfile server, SystemdSpec spec, ServiceConfig? service, ) async { if (!await canReach(server, () => installUnit(server, spec, service))) { return; } try { final result = await getDataManager().installSystemdUnit( server: server, unitName: spec.safeName, unitContent: spec.render(), enable: spec.enableOnBoot, startNow: spec.startNow, ); if (result.ok && service != null) { service.serviceName = spec.safeName; await _persistService(server, service); } connection.onUnitInstalled(result); } catch (error) { handleError( error, () => installUnit(server, spec, service), () {}, "Retry", ); } } /// Saves the service back onto its server in storage. Future _persistService( ServerProfile server, ServiceConfig service, ) async { final servers = await getDataManager().getServers(); final serverIndex = servers.indexWhere((s) => s.id == server.id); if (serverIndex < 0) return; final services = servers[serverIndex].services; final serviceIndex = services.indexWhere((s) => s.id == service.id); if (serviceIndex < 0) return; services[serviceIndex] = service; await getDataManager().setServers(servers); } }