|
|
|
@ -15,17 +15,18 @@ using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Xml;
|
|
|
|
|
|
|
|
|
|
namespace IoTNode.Services
|
|
|
|
|
namespace IoTDameon
|
|
|
|
|
{
|
|
|
|
|
public class UpdateDameonService : BackgroundService
|
|
|
|
|
public class UpdateIoTNodeService : BackgroundService
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger<UpdateDameonService> _logger;
|
|
|
|
|
private readonly ILogger<UpdateIoTNodeService> _logger;
|
|
|
|
|
private readonly IWebHostEnvironment _env;
|
|
|
|
|
private readonly IConfiguration _cfg;
|
|
|
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
|
|
|
|
private bool _isUpdating;
|
|
|
|
|
|
|
|
|
|
public UpdateDameonService(ILogger<UpdateDameonService> logger, IWebHostEnvironment env, IConfiguration cfg, IHttpClientFactory httpClientFactory)
|
|
|
|
|
public bool IsUpdating { get; set; }
|
|
|
|
|
|
|
|
|
|
public UpdateIoTNodeService(ILogger<UpdateIoTNodeService> logger, IWebHostEnvironment env, IConfiguration cfg, IHttpClientFactory httpClientFactory)
|
|
|
|
|
{
|
|
|
|
|
this._logger = logger;
|
|
|
|
|
this._env = env;
|
|
|
|
@ -47,13 +48,13 @@ namespace IoTNode.Services
|
|
|
|
|
|
|
|
|
|
private void Update()
|
|
|
|
|
{
|
|
|
|
|
if (this._isUpdating)
|
|
|
|
|
if (this.IsUpdating)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
this._isUpdating = true;
|
|
|
|
|
this.IsUpdating = true;
|
|
|
|
|
}
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
@ -63,23 +64,26 @@ namespace IoTNode.Services
|
|
|
|
|
{
|
|
|
|
|
this._logger.LogError(ex, "update error");
|
|
|
|
|
}
|
|
|
|
|
this._isUpdating = false;
|
|
|
|
|
this.IsUpdating = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateInternal()
|
|
|
|
|
{
|
|
|
|
|
var processName = "iotnode";
|
|
|
|
|
var appFolder = "IoTNode";
|
|
|
|
|
var port = 8002;
|
|
|
|
|
var proxyUrl = "http://localhost:9001/RPC2";
|
|
|
|
|
var processName = "iotdaemon";
|
|
|
|
|
var root = Directory.GetParent(_env.ContentRootPath).FullName;
|
|
|
|
|
var appPath = Path.Combine(root, "IoTDameon");
|
|
|
|
|
var name = "dameon.zip";
|
|
|
|
|
var backupName = Path.Combine(root, "iotdameon.bk.zip");
|
|
|
|
|
var appPath = Path.Combine(root, appFolder);
|
|
|
|
|
var name = $"{appFolder}.zip";
|
|
|
|
|
var backupName = Path.Combine(root, $"{appFolder}.bk.zip");
|
|
|
|
|
var file = Path.Combine(root, name);
|
|
|
|
|
var currentCheckSum = string.Empty;
|
|
|
|
|
//检查是否有更新
|
|
|
|
|
this._logger.LogInformation("check dameon version");
|
|
|
|
|
var currentVersion = this._httpClientFactory.CreateClient().GetAsync("http://localhost:8003/Home/GetVersion").Result.Content.ReadAsStringAsync().Result;
|
|
|
|
|
var info = this._httpClientFactory.CreateClient().GetAsync($"{this._cfg["notify:host"]}/dameon.xml").Result.Content.ReadAsStringAsync().Result;
|
|
|
|
|
this._logger.LogInformation("check version");
|
|
|
|
|
var currentVersion = this._httpClientFactory.CreateClient().GetAsync($"http://localhost:{port}/Home/GetVersion").Result.Content.ReadAsStringAsync().Result;
|
|
|
|
|
var server = this._httpClientFactory.CreateClient().GetAsync($"http://localhost:{port}/Home/GetServer").Result.Content.ReadAsStringAsync().Result;
|
|
|
|
|
var info = this._httpClientFactory.CreateClient().GetAsync($"{server}/{processName}.xml").Result.Content.ReadAsStringAsync().Result;
|
|
|
|
|
var doc = new XmlDocument();
|
|
|
|
|
doc.LoadXml(info);
|
|
|
|
|
var lastVersion = doc.GetElementsByTagName("version")[0].InnerText.Trim();
|
|
|
|
@ -131,15 +135,14 @@ namespace IoTNode.Services
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
ZipFile.CreateFromDirectory(appPath, backupName);
|
|
|
|
|
Directory.Delete(appPath, true);
|
|
|
|
|
Directory.CreateDirectory(appPath);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
this._logger.LogError(ex, ex.Message);
|
|
|
|
|
throw new Exception("备份程序失败", ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Directory.Delete(appPath, true);
|
|
|
|
|
Directory.CreateDirectory(appPath);
|
|
|
|
|
//更新程序
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
@ -154,7 +157,9 @@ namespace IoTNode.Services
|
|
|
|
|
//设置权限
|
|
|
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
|
|
|
|
{
|
|
|
|
|
Process.Start("/bin/bash", $"-c \"chmod 755 {Path.Combine(appPath, "IoTDameon")}\"");
|
|
|
|
|
var updateScript = Path.Combine(appPath, "update.sh");
|
|
|
|
|
Process.Start("/bin/bash", $"-c \"chmod 755 {file}\"");
|
|
|
|
|
Process.Start(updateScript);
|
|
|
|
|
}
|
|
|
|
|
//启动更新程序
|
|
|
|
|
proxy.startProcess(processName);
|