Skip to content

Commit

Permalink
Linux下,需要给予可执行权限
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Oct 21, 2023
1 parent 96a14d7 commit 2a2b0bc
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 74 deletions.
32 changes: 17 additions & 15 deletions Stardust/Deployment/ZipDeploy.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Xml.Serialization;
using NewLife;
using NewLife.Log;

Expand All @@ -12,41 +9,41 @@ public class ZipDeploy
{
#region 属性
/// <summary>应用名称</summary>
public String Name { get; set; }
public String Name { get; set; } = null!;

/// <summary>文件名</summary>
public String FileName { get; set; }
public String? FileName { get; set; }

/// <summary>启动参数</summary>
public String Arguments { get; set; }
public String? Arguments { get; set; }

/// <summary>工作目录</summary>
public String WorkingDirectory { get; set; }
public String? WorkingDirectory { get; set; }

/// <summary>影子目录。应用将在其中执行</summary>
/// <remarks>默认使用上一级的shadow目录,无权时使用临时目录</remarks>
public String Shadow { get; set; }
public String? Shadow { get; set; }

/// <summary>可执行文件路径</summary>
public String ExecuteFile { get; set; }
public String? ExecuteFile { get; set; }

/// <summary>用户。以该用户执行应用</summary>
public String UserName { get; set; }
public String? UserName { get; set; }

/// <summary>覆盖文件。需要拷贝覆盖已存在的文件或子目录,支持*模糊匹配,多文件分号隔开。如果目标文件不存在,配置文件等自动拷贝</summary>
public String Overwrite { get; set; }
public String? Overwrite { get; set; }

/// <summary>进程</summary>
public Process Process { get; private set; }
public Process? Process { get; private set; }

/// <summary>是否调试模式。在调试模式下,重定向控制台输出到日志</summary>
public Boolean Debug { get; set; }

/// <summary>最后的错误信息</summary>
public String LastError { get; set; }
public String? LastError { get; set; }

/// <summary>链路追踪</summary>
public ITracer Tracer { get; set; }
public ITracer? Tracer { get; set; }
#endregion

#region 方法
Expand All @@ -66,7 +63,7 @@ public Boolean Parse(String[] args)
// 在参数中找到zip文件
var name = "";
var shadow = "";
var gs = new String[args.Length];
var gs = new String?[args.Length];
for (var i = 0; i < args.Length; i++)
{
if (args[i].EndsWithIgnoreCase(".zip"))
Expand Down Expand Up @@ -238,6 +235,11 @@ public Boolean Execute(Int32 msWait = 3_000)
si.FileName = "java";
si.Arguments = $"{runfile.FullName} {Arguments}";
}
else if (Runtime.Linux)
{
// Linux下,需要给予可执行权限
Process.Start("chmod", $"+x {runfile.FullName}");
}

// 指定用户时,以特定用户启动进程
if (!UserName.IsNullOrEmpty())
Expand Down
46 changes: 26 additions & 20 deletions Stardust/LocalStarClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,13 @@ public Boolean KillAndRestartMySelf()
Init();

var p = Process.GetCurrentProcess();
var fileName = p.MainModule.FileName;
var args = Environment.CommandLine.TrimStart(Path.ChangeExtension(fileName, ".dll")).Trim();
var fileName = p.MainModule?.FileName;
var args = "";
if (!fileName.IsNullOrEmpty())
{
var ext = Path.ChangeExtension(fileName, ".dll");
args = Environment.CommandLine.TrimStart(ext).Trim();
}

// 发起命令
var rs = _client.Invoke<String>("KillAndStart", new
Expand All @@ -148,7 +153,7 @@ public Boolean KillAndRestartMySelf()
/// <param name="url">zip包下载源</param>
/// <param name="version">版本号</param>
/// <param name="target">目标目录</param>
public Boolean ProbeAndInstall(String url = null, String version = null, String target = null)
public Boolean ProbeAndInstall(String? url = null, String? version = null, String? target = null)
{
//if (url.IsNullOrEmpty()) throw new ArgumentNullException(nameof(url));
if (url.IsNullOrEmpty())
Expand Down Expand Up @@ -183,7 +188,7 @@ public Boolean ProbeAndInstall(String url = null, String version = null, String
}
catch (Exception ex)
{
WriteLog("没有探测到StarAgent,{0}", ex.GetTrue().Message);
WriteLog("没有探测到StarAgent,{0}", ex.GetTrue()?.Message);
}

if (target.IsNullOrEmpty())
Expand All @@ -194,12 +199,12 @@ public Boolean ProbeAndInstall(String url = null, String version = null, String
{
try
{
target = Path.GetDirectoryName(p.MainModule.FileName);
}
catch
{
target = Path.GetDirectoryName(p.MainWindowTitle);
if (p.MainModule != null)
target = Path.GetDirectoryName(p.MainModule.FileName);
}
catch { }

if (target.IsNullOrEmpty()) target = Path.GetDirectoryName(p.MainWindowTitle);

WriteLog("发现进程StarAgent,ProcessId={0},target={1}", p.Id, target);
}
Expand Down Expand Up @@ -255,7 +260,7 @@ public Boolean ProbeAndInstall(String url = null, String version = null, String
}

var fileName = info?.FileName;
if (!fileName.IsNullOrEmpty() && Path.GetFullPath(fileName).EqualIgnoreCase("dotnet.exe")) fileName = info.Arguments;
if (!fileName.IsNullOrEmpty() && Path.GetFullPath(fileName).EqualIgnoreCase("dotnet.exe")) fileName = info?.Arguments;

var rs = false;
if (Runtime.Windows)
Expand All @@ -269,7 +274,7 @@ public Boolean ProbeAndInstall(String url = null, String version = null, String
return true;
}

private Boolean RunAgentOnWindows(String fileName, String target, Boolean inService)
private Boolean RunAgentOnWindows(String? fileName, String target, Boolean inService)
{
if (!fileName.IsNullOrEmpty() && Path.GetExtension(fileName) == ".dll") return false;
if (fileName.IsNullOrEmpty()) fileName = target.CombinePath("StarAgent.exe").GetFullPath();
Expand All @@ -293,13 +298,13 @@ private Boolean RunAgentOnWindows(String fileName, String target, Boolean inServ
};
var p = Process.Start(si);

WriteLog("启动进程成功 pid={0}", p.Id);
WriteLog("启动进程成功 pid={0}", p?.Id);
}

return true;
}

private Boolean RunAgentOnLinux(String fileName, String target, Boolean inService)
private Boolean RunAgentOnLinux(String? fileName, String target, Boolean inService)
{
if (!fileName.IsNullOrEmpty() && Path.GetExtension(fileName) == ".dll") return false;
if (fileName.IsNullOrEmpty()) fileName = target.CombinePath("StarAgent").GetFullPath();
Expand All @@ -326,13 +331,13 @@ private Boolean RunAgentOnLinux(String fileName, String target, Boolean inServic
};
var p = Process.Start(si);

WriteLog("启动进程成功 pid={0}", p.Id);
WriteLog("启动进程成功 pid={0}", p?.Id);
}

return true;
}

private Boolean RunAgentOnDotnet(String fileName, String target, Boolean inService)
private Boolean RunAgentOnDotnet(String? fileName, String target, Boolean inService)
{
if (fileName.IsNullOrEmpty()) fileName = target.CombinePath("StarAgent.dll").GetFullPath();
if (!File.Exists(fileName)) return false;
Expand All @@ -355,7 +360,7 @@ private Boolean RunAgentOnDotnet(String fileName, String target, Boolean inServi
};
var p = Process.Start(si);

WriteLog("启动进程成功 pid={0}", p.Id);
WriteLog("启动进程成功 pid={0}", p?.Id);
}

return true;
Expand Down Expand Up @@ -423,7 +428,7 @@ public static Task ProbeAsync(String? url = null, String? version = null, String
/// <param name="local">本地信息,用于告知对方我是谁</param>
/// <param name="timeout"></param>
/// <returns></returns>
public static IEnumerable<AgentInfo> Scan(AgentInfo local = null, Int32 timeout = 15_000)
public static IEnumerable<AgentInfo> Scan(AgentInfo? local = null, Int32 timeout = 15_000)
{
var encoder = new JsonEncoder { Log = XTrace.Log };
// 构造请求消息
Expand All @@ -438,7 +443,8 @@ public static IEnumerable<AgentInfo> Scan(AgentInfo local = null, Int32 timeout
//};
//var buf = msg.ToPacket().ToArray();

var buf = encoder.CreateRequest("Info", null).ToPacket().ToArray();
var buf = encoder.CreateRequest("Info", null).ToPacket()?.ToArray();
if (buf == null) yield break;

// 在局域网中广播消息
var udp = new UdpClient();
Expand All @@ -448,7 +454,7 @@ public static IEnumerable<AgentInfo> Scan(AgentInfo local = null, Int32 timeout
while (DateTime.Now < end)
{
var rs = new DefaultMessage();
IPEndPoint ep = null;
IPEndPoint? ep = null;
buf = udp.Receive(ref ep);
if (buf != null && rs.Read(buf))
{
Expand All @@ -467,7 +473,7 @@ public static IEnumerable<AgentInfo> Scan(AgentInfo local = null, Int32 timeout

#region 日志
/// <summary>日志</summary>
public ILog Log { get; set; }
public ILog Log { get; set; } = Logger.Null;

/// <summary>写日志</summary>
/// <param name="format"></param>
Expand Down
4 changes: 2 additions & 2 deletions Stardust/Managers/ProcessInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
public class ProcessInfo
{
/// <summary>名称</summary>
public String Name { get; set; }
public String Name { get; set; } = null!;

/// <summary>进程Id</summary>
public Int32 ProcessId { get; set; }

/// <summary>进程名</summary>
public String ProcessName { get; set; }
public String? ProcessName { get; set; }

/// <summary>创建时间</summary>
public DateTime CreateTime { get; set; }
Expand Down
Loading

0 comments on commit 2a2b0bc

Please sign in to comment.