fix: initialize private Linux agent config and user service from desktop

This commit is contained in:
tongtong committed 2026-11-13 10:28:29 +08:00
1 parent 316292bdfb
commit e9cf55f597
3 files changed
+20 -1

No files matched your search

@@ -203,11 +203,13 @@ public partial class AgentManagementViewModel : ObservableObject, IDisposable
var args=new List<string>{"--json","--config",ConfigPath,"--server",root.GetProperty("server").GetString()!,"init","--id",root.GetProperty("id").GetString()!,"--name",root.GetProperty("name").GetString()!};
// Origin is a global CLI option and must precede the subcommand.
if(root.TryGetProperty("connect_address",out var origin)&&origin.ValueKind==JsonValueKind.String)args.InsertRange(5,["--connect-address",origin.GetString()!]);
if(OperatingSystem.IsLinux())Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(ConfigPath))!,UnixFileMode.UserRead|UnixFileMode.UserWrite|UnixFileMode.UserExecute);
var info=ProcessTools.CreateStartInfo(DesktopPaths.ResolveTool(DesktopPaths.FindTool("adsctl"),"adsctl"),args);info.Environment["ADS_AGENT_TOKEN"]=EnrollmentToken;
var result=await ProcessTools.RunAsync(info,lifetime.Token);if(result.ExitCode!=0)throw new InvalidOperationException(result.Error);
EnrollmentToken="";Notice="本机配置已初始化,可启动后台 Agent 后应用设备配置。";
});
[RelayCommand(CanExecute=nameof(CanLocal))]private Task StartLocalAsync()=>Guard(async()=>{
if(!File.Exists(ConfigPath))throw new InvalidOperationException("请先导入 agent.env 并初始化本机配置,再启动 Agent。");
try { await LocalRequestAsync(new{op="status"});Notice="本机 Agent 已在运行。";return; } catch(InvalidOperationException){}
var info=ProcessTools.CreateStartInfo(DesktopPaths.ResolveTool(DesktopPaths.FindTool("ads-agent"),"ads-agent"),["--quiet","--supervise","--config",ConfigPath]);
info.RedirectStandardOutput=false;info.RedirectStandardError=false;
@@ -215,7 +217,13 @@ public partial class AgentManagementViewModel : ObservableObject, IDisposable
await Task.Delay(700,lifetime.Token);await RefreshCoreAsync();Notice="后台 Agent 已启动,退出托盘不会停止它。";
});
[RelayCommand(CanExecute=nameof(CanLocal))]private Task InstallServiceAsync()=>Guard(async()=>{
if(!OperatingSystem.IsWindows())throw new InvalidOperationException("请在终端运行 adsctl service install;系统级服务可加 --system。");
if(OperatingSystem.IsLinux()) {
var start=ProcessTools.CreateStartInfo(DesktopPaths.ResolveTool(DesktopPaths.FindTool("adsctl"),"adsctl"),new[]{"--config",ConfigPath,"service","install"});
var result=await ProcessTools.RunAsync(start,lifetime.Token);
if(result.ExitCode!=0)throw new InvalidOperationException(result.Error);
await RefreshCoreAsync();Notice="已安装用户级 systemd 服务;无登录开机运行需启用该用户的 linger。";return;
}
if(!OperatingSystem.IsWindows())throw new InvalidOperationException("此平台请手动运行 Agent。");
var info=new ProcessStartInfo(DesktopPaths.ResolveTool(DesktopPaths.FindTool("adsctl"),"adsctl")){UseShellExecute=true,Verb="runas",WindowStyle=ProcessWindowStyle.Hidden};
foreach(var value in new[]{"--config",ConfigPath,"service","install"})info.ArgumentList.Add(value);
using var process=Process.Start(info) ?? throw new InvalidOperationException("安装命令无法启动。");await process.WaitForExitAsync(lifetime.Token);