Skip to content

Commit

Permalink
[refactor] 重构服务安装,外部传入服务模型ServiceModel,统一解析命令行参数。fix: #22
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Dec 2, 2024
1 parent ad0f4c7 commit f1f22f2
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 133 deletions.
2 changes: 1 addition & 1 deletion NewLife.Agent/Command/CommandHandlerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace NewLife.Agent.Command;
public class CommandFactory
{
private readonly List<BaseCommandHandler> _commandHandlerList;
private Dictionary<String, BaseCommandHandler> _commandHandlerDict = new(StringComparer.OrdinalIgnoreCase);
private readonly Dictionary<String, BaseCommandHandler> _commandHandlerDict = new(StringComparer.OrdinalIgnoreCase);

/// <summary>
/// 命令工厂
Expand Down
41 changes: 38 additions & 3 deletions NewLife.Agent/CommandHandler/Install.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using NewLife.Agent.Command;
using NewLife.Agent.Models;
using NewLife.Log;

namespace NewLife.Agent.CommandHandler;

Expand Down Expand Up @@ -46,15 +48,34 @@ public override void Process(String[] args)
exe = dll;
}

var service = new ServiceModel
{
ServiceName = Service.ServiceName,
DisplayName = Service.DisplayName,
Description = Service.Description,
FileName = exe
};

// 从文件名中分析工作目录
var ss = service.FileName.Split(" ");
if (ss.Length >= 2 && ss[0].EndsWithIgnoreCase("dotnet", "java"))
{
var file = ss[1];
if (File.Exists(file))
service.WorkingDirectory = Path.GetDirectoryName(file).GetFullPath();
else
service.WorkingDirectory = ".".GetFullPath();
}

//var arg = UseAutorun ? "-run" : "-s";
var arg = "-s";

// 兼容更多参数做为服务启动,譬如:--urls
if (args.Length > 2)
if (args.Length > 1)
{
// 跳过系统内置参数
var list = new List<String>();
for (var i = 2; i < args.Length; i++)
for (var i = 1; i < args.Length; i++)
{
if (args[i].EqualIgnoreCase("-server", "-user", "-group"))
i++;
Expand All @@ -64,9 +85,23 @@ public override void Process(String[] args)
list.Add(args[i]);
}
if (list.Count > 0) arg += " " + list.Join(" ");

for (var i = 0; i < args.Length; i++)
{
if (args[i].EqualIgnoreCase("-user") && i + 1 < args.Length)
{
service.User = args[i + 1];
}
if (args[i].EqualIgnoreCase("-group") && i + 1 < args.Length)
{
service.Group = args[i + 1];
}
}
}

Service.Host.Install(Service.ServiceName, Service.DisplayName, exe, arg, Service.Description);
service.Arguments = arg;
//Service.Host.Install(Service.ServiceName, Service.DisplayName, exe, arg, Service.Description);
Service.Host.Install(service);

// 稍微等一下,以便后续状态刷新
Thread.Sleep(500);
Expand Down
8 changes: 2 additions & 6 deletions NewLife.Agent/DefaultHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,9 @@ public class DefaultHost : DisposeBase, IHost
public virtual Boolean IsRunning(String serviceName) => false;

/// <summary>安装服务</summary>
/// <param name="serviceName">服务名</param>
/// <param name="displayName">显示名</param>
/// <param name="fileName">文件路径</param>
/// <param name="arguments">命令参数</param>
/// <param name="description">描述信息</param>
/// <param name="service">服务</param>
/// <returns></returns>
public virtual Boolean Install(String serviceName, String displayName, String fileName, String arguments, String description) => false;
public virtual Boolean Install(ServiceModel service) => false;

/// <summary>卸载服务</summary>
/// <param name="serviceName">服务名</param>
Expand Down
8 changes: 2 additions & 6 deletions NewLife.Agent/IHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,9 @@ public interface IHost
Boolean IsRunning(String serviceName);

/// <summary>安装服务</summary>
/// <param name="serviceName">服务名</param>
/// <param name="displayName">显示名</param>
/// <param name="fileName">文件路径</param>
/// <param name="arguments">命令参数</param>
/// <param name="description">描述信息</param>
/// <param name="service">服务</param>
/// <returns></returns>
Boolean Install(String serviceName, String displayName, String fileName, String arguments, String description);
Boolean Install(ServiceModel service);

/// <summary>卸载服务</summary>
/// <param name="serviceName">服务名</param>
Expand Down
43 changes: 13 additions & 30 deletions NewLife.Agent/OSXLaunch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace NewLife.Agent;
public class OSXLaunch : DefaultHost
{
#region 静态
private static String _path;
private static readonly String _path;

/// <summary>是否可用</summary>
public static Boolean Available => !_path.IsNullOrEmpty();
Expand Down Expand Up @@ -90,41 +90,24 @@ public override Boolean IsRunning(String serviceName)
}

/// <summary>安装服务</summary>
/// <param name="serviceName">服务名</param>
/// <param name="displayName">显示名</param>
/// <param name="fileName">文件路径</param>
/// <param name="arguments">命令参数</param>
/// <param name="description">描述信息</param>
/// <param name="service">服务</param>
/// <returns></returns>
public override Boolean Install(String serviceName, String displayName, String fileName, String arguments, String description)
public override Boolean Install(ServiceModel service)
{
if (User.IsNullOrEmpty())
{
// 从命令行参数加载用户设置 -user
var args = Environment.GetCommandLineArgs();
if (args.Length >= 1)
{
for (var i = 0; i < args.Length; i++)
{
if (args[i].EqualIgnoreCase("-user") && i + 1 < args.Length)
{
User = args[i + 1];
break;
}
if (args[i].EqualIgnoreCase("-group") && i + 1 < args.Length)
{
Group = args[i + 1];
break;
}
}
if (!User.IsNullOrEmpty() && Group.IsNullOrEmpty()) Group = User;
}
}
var serviceName = service.ServiceName;
var displayName = service.DisplayName;
var description = service.Description;
var fileName = service.FileName;
var arguments = service.Arguments;

if (!service.User.IsNullOrEmpty()) User = service.User;
if (!service.Group.IsNullOrEmpty()) Group = service.Group;
if (!User.IsNullOrEmpty() && Group.IsNullOrEmpty()) Group = User;

return Install(_path, serviceName, displayName, fileName, arguments, description, User, Group, DependOnNetwork);
}

static String _template = """
static readonly String _template = """
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
Expand Down
18 changes: 12 additions & 6 deletions NewLife.Agent/Procd.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Text;
using NewLife.Agent.Models;
using NewLife.Log;

namespace NewLife.Agent;
Expand Down Expand Up @@ -105,13 +106,18 @@ public override Boolean IsRunning(String serviceName)
}

/// <summary>安装服务</summary>
/// <param name="serviceName">服务名</param>
/// <param name="displayName">显示名</param>
/// <param name="fileName">文件路径</param>
/// <param name="arguments">命令参数</param>
/// <param name="description">描述信息</param>
/// <param name="service">服务</param>
/// <returns></returns>
public override Boolean Install(String serviceName, String displayName, String fileName, String arguments, String description) => Install(_path, serviceName, fileName, arguments, displayName, description);
public override Boolean Install(ServiceModel service)
{
var serviceName = service.ServiceName;
var displayName = service.DisplayName;
var description = service.Description;
var fileName = service.FileName;
var arguments = service.Arguments;

return Install(_path, serviceName, fileName, arguments, displayName, description);
}

/// <summary>安装服务</summary>
/// <param name="systemPath">system目录</param>
Expand Down
26 changes: 16 additions & 10 deletions NewLife.Agent/RcInit.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Text;
using NewLife.Agent.Models;
using NewLife.Log;

namespace NewLife.Agent;
Expand Down Expand Up @@ -84,19 +85,24 @@ public override Boolean IsInstalled(String serviceName)
/// <returns></returns>
public override Boolean IsRunning(String serviceName)
{
var p = GetProcess(serviceName, out _);
var p = RcInit.GetProcess(serviceName, out _);

return p != null && !GetHasExited(p);
}

/// <summary>安装服务</summary>
/// <param name="serviceName">服务名</param>
/// <param name="displayName">显示名</param>
/// <param name="fileName">文件路径</param>
/// <param name="arguments">命令参数</param>
/// <param name="description">描述信息</param>
/// <param name="service">服务</param>
/// <returns></returns>
public override Boolean Install(String serviceName, String displayName, String fileName, String arguments, String description) => Install(_path, serviceName, fileName, arguments, displayName, description);
public override Boolean Install(ServiceModel service)
{
var serviceName = service.ServiceName;
var displayName = service.DisplayName;
var description = service.Description;
var fileName = service.FileName;
var arguments = service.Arguments;

return Install(_path, serviceName, fileName, arguments, displayName, description);
}

/// <summary>安装服务</summary>
/// <param name="systemPath">system目录</param>
Expand Down Expand Up @@ -256,7 +262,7 @@ public override Boolean Start(String serviceName)
XTrace.WriteLine("{0}.Start {1}", Name, serviceName);

// 判断服务是否已启动
var p = GetProcess(serviceName, out _);
var p = RcInit.GetProcess(serviceName, out _);
if (p != null && !GetHasExited(p)) return false;

//var file = _path.CombinePath($"{serviceName}");
Expand All @@ -277,7 +283,7 @@ public override Boolean Stop(String serviceName)
{
XTrace.WriteLine("{0}.Stop {1}", Name, serviceName);

var p = GetProcess(serviceName, out var pid);
var p = RcInit.GetProcess(serviceName, out var pid);
if (p == null) return false;

try
Expand Down Expand Up @@ -311,7 +317,7 @@ public override Boolean Restart(String serviceName)
return true;
}

private Process GetProcess(String serviceName, out String pid)
private static Process GetProcess(String serviceName, out String pid)
{
var id = 0;
pid = $"{serviceName}.pid".GetFullPath();
Expand Down
8 changes: 2 additions & 6 deletions NewLife.Agent/SysVinit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,9 @@ public override Boolean IsRunning(String serviceName)
}

/// <summary>安装服务</summary>
/// <param name="serviceName">服务名</param>
/// <param name="displayName">显示名</param>
/// <param name="fileName">文件路径</param>
/// <param name="arguments">命令参数</param>
/// <param name="description">描述信息</param>
/// <param name="service">服务</param>
/// <returns></returns>
public override Boolean Install(String serviceName, String displayName, String fileName, String arguments, String description)
public override Boolean Install(ServiceModel service)
{
//暂不实现SysVinit服务安装
throw new NotImplementedException("SysVinit installation is not implemented.");
Expand Down
57 changes: 12 additions & 45 deletions NewLife.Agent/Systemd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,54 +107,21 @@ public override Boolean IsRunning(String serviceName)
}

/// <summary>安装服务</summary>
/// <param name="serviceName">服务名</param>
/// <param name="displayName">显示名</param>
/// <param name="fileName">文件路径</param>
/// <param name="arguments">命令参数</param>
/// <param name="description">描述信息</param>
/// <param name="service">服务</param>
/// <returns></returns>
public override Boolean Install(String serviceName, String displayName, String fileName, String arguments, String description)
public override Boolean Install(ServiceModel service)
{
var set = Setting;
set.ServiceName = serviceName;
set.DisplayName = displayName;
set.Description = description;
set.FileName = fileName;
set.Arguments = arguments;

// 从文件名中分析工作目录
var ss = fileName.Split(" ");
if (ss.Length >= 2 && ss[0].EndsWithIgnoreCase("dotnet", "java"))
{
var file = ss[1];
if (File.Exists(file))
set.WorkingDirectory = Path.GetDirectoryName(file).GetFullPath();
else
set.WorkingDirectory = ".".GetFullPath();
}

if (set.User.IsNullOrEmpty())
{
// 从命令行参数加载用户设置 -user
var args = Environment.GetCommandLineArgs();
if (args.Length >= 1)
{
for (var i = 0; i < args.Length; i++)
{
if (args[i].EqualIgnoreCase("-user") && i + 1 < args.Length)
{
set.User = args[i + 1];
break;
}
if (args[i].EqualIgnoreCase("-group") && i + 1 < args.Length)
{
set.Group = args[i + 1];
break;
}
}
if (!set.User.IsNullOrEmpty() && set.Group.IsNullOrEmpty()) set.Group = set.User;
}
}
set.ServiceName = service.ServiceName;
set.DisplayName = service.DisplayName;
set.Description = service.Description;
set.FileName = service.FileName;
set.Arguments = service.Arguments;
set.WorkingDirectory = service.WorkingDirectory;

set.User = service.User;
set.Group = service.Group;
if (!set.User.IsNullOrEmpty() && set.Group.IsNullOrEmpty()) set.Group = set.User;

return Install(ServicePath, set);
}
Expand Down
13 changes: 7 additions & 6 deletions NewLife.Agent/WindowsAutorun.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,18 @@ public override unsafe Boolean IsRunning(String serviceName)
}

/// <summary>安装服务</summary>
/// <param name="serviceName">服务名</param>
/// <param name="displayName"></param>
/// <param name="fileName">文件路径</param>
/// <param name="arguments">命令参数</param>
/// <param name="description"></param>
/// <param name="service">服务</param>
/// <returns></returns>
#if NET5_0_OR_GREATER
[SupportedOSPlatform("windows")]
#endif
public override Boolean Install(String serviceName, String displayName, String fileName, String arguments, String description)
public override Boolean Install(ServiceModel service)
{
var serviceName = service.ServiceName;
var displayName = service.DisplayName;
var description = service.Description;
var fileName = service.FileName;
var arguments = service.Arguments;
XTrace.WriteLine("{0}.Install {1}, {2}, {3}, {4}", GetType().Name, serviceName, displayName, fileName, arguments, description);

if (!WindowsService.IsAdministrator()) return WindowsService.RunAsAdministrator("-i");
Expand Down
Loading

0 comments on commit f1f22f2

Please sign in to comment.