探测并安装星尘代理,Windows测试通过
This commit is contained in:
parent
6ab301ee16
commit
db5d5cab2f
|
@ -204,8 +204,8 @@ namespace StarAgent
|
|||
{
|
||||
StopWork("Upgrade");
|
||||
|
||||
Environment.Exit(0);
|
||||
var p = Process.GetCurrentProcess();
|
||||
p.Close();
|
||||
p.Kill();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
|
|||
<PublishProtocol>FileSystem</PublishProtocol>
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Any CPU</Platform>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
<PublishDir>..\Bin\Agent_publish\</PublishDir>
|
||||
<RuntimeIdentifier>linux-x64</RuntimeIdentifier>
|
||||
<SelfContained>false</SelfContained>
|
||||
|
|
|
@ -1,8 +1,13 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using NewLife;
|
||||
using NewLife.Http;
|
||||
using NewLife.Log;
|
||||
using NewLife.Net;
|
||||
using NewLife.Remoting;
|
||||
|
||||
namespace Stardust
|
||||
|
@ -24,6 +29,7 @@ namespace Stardust
|
|||
|
||||
_client = new ApiClient("udp://127.0.0.1:5500")
|
||||
{
|
||||
Timeout = 3_000,
|
||||
Log = XTrace.Log,
|
||||
};
|
||||
|
||||
|
@ -70,11 +76,99 @@ namespace Stardust
|
|||
|
||||
#region 安装星尘代理
|
||||
/// <summary>探测并安装星尘代理</summary>
|
||||
/// <param name="url"></param>
|
||||
/// <param name="version"></param>
|
||||
public Boolean TestAndInstall(String url, String version)
|
||||
/// <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)
|
||||
{
|
||||
if (url.IsNullOrEmpty()) throw new ArgumentNullException(nameof(url));
|
||||
//if (url.IsNullOrEmpty()) throw new ArgumentNullException(nameof(url));
|
||||
if (url.IsNullOrEmpty())
|
||||
{
|
||||
var set = NewLife.Setting.Current;
|
||||
if (Environment.Version.Major >= 5)
|
||||
url = set.PluginServer.CombinePath("staragent50.zip");
|
||||
else if (Environment.Version.Major >= 4)
|
||||
url = set.PluginServer.CombinePath("staragent45.zip");
|
||||
else
|
||||
url = set.PluginServer.CombinePath("staragent31.zip");
|
||||
}
|
||||
|
||||
// 尝试连接,获取版本
|
||||
var ver = "";
|
||||
var pid = 0;
|
||||
try
|
||||
{
|
||||
var info = GetInfo();
|
||||
|
||||
// 比目标版本高,不需要安装
|
||||
ver = info["Version"] + "";
|
||||
if (String.Compare(ver, version) >= 0) return true;
|
||||
|
||||
if (info["Process"] is IDictionary<String, Object> dic) pid = dic["ProcessId"].ToInt();
|
||||
|
||||
if (target.IsNullOrEmpty() && pid > 0)
|
||||
{
|
||||
var p = Process.GetProcessById(pid);
|
||||
if (p != null) target = Path.GetDirectoryName(p.MainModule.FileName);
|
||||
}
|
||||
|
||||
XTrace.WriteLine("StarAgent在用版本 v{0},低于目标版本 v{1}", ver, version);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
XTrace.WriteLine("没有探测到StarAgent,{0}", ex.GetTrue().Message);
|
||||
}
|
||||
|
||||
// 准备安装,甭管是否能够成功重启,先覆盖了文件再说
|
||||
{
|
||||
if (target.IsNullOrEmpty())
|
||||
{
|
||||
target = Runtime.Windows ? "C:\\StarAgent" : $"\\home\\{Environment.UserName}";
|
||||
}
|
||||
target = target.GetFullPath();
|
||||
target.EnsureDirectory(false);
|
||||
|
||||
var ug = new Upgrade
|
||||
{
|
||||
SourceFile = Path.GetFileName(url),
|
||||
DestinationPath = target,
|
||||
|
||||
Log = XTrace.Log,
|
||||
};
|
||||
|
||||
XTrace.WriteLine("下载:{0}", url);
|
||||
|
||||
var client = new HttpClient();
|
||||
client.DownloadFileAsync(url, ug.SourceFile).Wait();
|
||||
|
||||
ug.Update();
|
||||
|
||||
File.Delete(ug.SourceFile);
|
||||
}
|
||||
|
||||
{
|
||||
// 在进程中查找
|
||||
var p = Process.GetProcesses().FirstOrDefault(e => e.ProcessName == "StarAgent");
|
||||
if (p == null && pid > 0) p = Process.GetProcessById(pid);
|
||||
|
||||
// 重启目标
|
||||
if (p != null) p.Kill();
|
||||
|
||||
var fileName = target.CombinePath("StarAgent.exe");
|
||||
if (File.Exists(fileName))
|
||||
{
|
||||
if (Runtime.Linux) Process.Start("chmod", $"+x {fileName}");
|
||||
|
||||
var si = new ProcessStartInfo(fileName, "-run")
|
||||
{
|
||||
WorkingDirectory = Path.GetDirectoryName(fileName),
|
||||
UseShellExecute = true
|
||||
};
|
||||
Process.Start(si);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
|
|
@ -445,11 +445,20 @@ namespace Stardust
|
|||
{
|
||||
XTrace.WriteLine("执行更新命令:{0}", cmd);
|
||||
|
||||
var si = new ProcessStartInfo
|
||||
{
|
||||
UseShellExecute = true,
|
||||
};
|
||||
var p = cmd.IndexOf(' ');
|
||||
if (p < 0)
|
||||
Process.Start(cmd);
|
||||
si.FileName = cmd;
|
||||
else
|
||||
Process.Start(cmd.Substring(0, p), cmd.Substring(p + 1));
|
||||
{
|
||||
si.FileName = cmd.Substring(0, p);
|
||||
si.Arguments = cmd.Substring(p + 1);
|
||||
}
|
||||
|
||||
Process.Start(si);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
|
|
@ -16,8 +16,7 @@ namespace Test
|
|||
{
|
||||
XTrace.UseConsole();
|
||||
|
||||
Test2();
|
||||
//Thread.Sleep(-1);
|
||||
Test3();
|
||||
|
||||
Console.WriteLine("OK!");
|
||||
Console.ReadKey();
|
||||
|
@ -56,5 +55,11 @@ namespace Test
|
|||
var rs = dal.RestoreAll("../data/Node_20200903215342.zip", null);
|
||||
//Assert.NotNull(rs);
|
||||
}
|
||||
|
||||
static void Test3()
|
||||
{
|
||||
var client = new LocalStarClient();
|
||||
client.ProbeAndInstall(null, "1.1");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue