diff --git a/projects/IoTDameon/Controllers/HomeController.cs b/projects/IoTDameon/Controllers/HomeController.cs index 31aef74f..4f05bbcd 100644 --- a/projects/IoTDameon/Controllers/HomeController.cs +++ b/projects/IoTDameon/Controllers/HomeController.cs @@ -1,7 +1,6 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; -using System.IO; -using System.IO.Compression; +using System; using System.Reflection; namespace IoTDameon.Controllers @@ -15,47 +14,25 @@ namespace IoTDameon.Controllers this._env = env; } - public IActionResult Index() + public IActionResult Index(string command) { - var path = Path.Combine(this._env.WebRootPath, "upload"); - return View(model: path); - } - - public IActionResult GetVersion() - { - return Content(Assembly.GetEntryAssembly().GetCustomAttribute().InformationalVersion); - } - - public IActionResult Upload() - { - if (Request.Form.Files != null && Request.Form.Files.Count > 0) + if (!string.IsNullOrEmpty(command)) { - var file = Request.Form.Files[0]; - using Stream stream = file.OpenReadStream(); - var phicyPath = Path.Combine(this._env.WebRootPath, "upload"); - Directory.CreateDirectory(phicyPath); - var name = file.FileName; - var fullName = Path.Combine(phicyPath, name); - if (System.IO.File.Exists(fullName)) + try { - System.IO.File.Delete(fullName); + ViewBag.Output = command.Bash(); } - using (FileStream fs = System.IO.File.Create(fullName)) + catch (Exception ex) { - file.CopyTo(fs); - } - var ext = Path.GetExtension(fullName); - if (ext == "zip") - { - var zipDirectory = Path.Combine(phicyPath, Path.GetFileName(fullName)); - if (!Directory.Exists(zipDirectory)) - { - Directory.CreateDirectory(zipDirectory); - } - ZipFile.ExtractToDirectory(fullName, zipDirectory); + ViewBag.Output = ex.ToString(); } } - return RedirectToAction("Index"); + return View(model: command); + } + + public IActionResult GetVersion() + { + return Content(Assembly.GetEntryAssembly().GetCustomAttribute().InformationalVersion); } } } \ No newline at end of file diff --git a/projects/IoTDameon/ShellHelper.cs b/projects/IoTDameon/ShellHelper.cs new file mode 100644 index 00000000..ed3e1fb5 --- /dev/null +++ b/projects/IoTDameon/ShellHelper.cs @@ -0,0 +1,31 @@ +using System.Diagnostics; +using System.Runtime.InteropServices; + +namespace IoTDameon +{ + public static class ShellHelper + { + public static string Bash(this string cmd) + { + var escapedArgs = cmd.Replace("\"", "\\\""); + + var process = new Process() + { + StartInfo = new ProcessStartInfo + { + FileName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)?"powershell": "/bin/bash", + Arguments = $"-c \"{escapedArgs}\"", + RedirectStandardOutput = true, + UseShellExecute = false, + CreateNoWindow = true, + } + }; + + process.Start(); + string result = process.StandardOutput.ReadToEnd(); + process.WaitForExit(); + + return result; + } + } +} \ No newline at end of file diff --git a/projects/IoTDameon/UpdateIoTNodeService.cs b/projects/IoTDameon/UpdateIoTNodeService.cs index b9edbe00..cb5c6326 100644 --- a/projects/IoTDameon/UpdateIoTNodeService.cs +++ b/projects/IoTDameon/UpdateIoTNodeService.cs @@ -76,7 +76,7 @@ namespace IoTDameon var root = Directory.GetParent(_env.ContentRootPath).FullName; var appPath = Path.Combine(root, appFolder); var name = $"{appFolder}.zip"; - var backupName = Path.Combine(root, $"{appFolder}.bk.zip"); + var backupPath = Path.Combine(root, $"{appFolder}_bk"); var file = Path.Combine(root, name); var currentCheckSum = string.Empty; //检查是否有更新 @@ -144,19 +144,21 @@ namespace IoTDameon //备份要更新的程序 try { - if (File.Exists(backupName)) - { - File.Delete(backupName); - } - ZipFile.CreateFromDirectory(appPath, backupName, CompressionLevel.Fastest, true); + Directory.Move(appPath, backupPath); + this._logger.LogInformation($"back up {appPath}"); } catch (Exception ex) { this._logger.LogError(ex, ex.Message); throw new Exception("backup error", ex); } - DeleteOldFiles(appPath); - this._logger.LogInformation($"delete old files in {appPath}"); + Directory.CreateDirectory(appPath); + this._logger.LogInformation($"mkdir {appPath}"); + foreach (var item in Directory.GetFiles(backupPath,"*.db")) + { + File.Copy(item, Path.Combine(appPath, Path.GetFileName(item))); + } + this._logger.LogInformation($"copy db files to {appPath}"); //更新程序 try { @@ -166,8 +168,8 @@ namespace IoTDameon catch (Exception ex) { this._logger.LogError(ex, ex.Message); - DeleteOldFiles(appPath); - ZipFile.ExtractToDirectory(backupName, root, true); + Directory.Delete(appPath, true); + Directory.Move(backupPath, appPath); throw new Exception("upzip error,restore old files", ex); } //设置权限 @@ -176,9 +178,9 @@ namespace IoTDameon var updateScript = Path.Combine(appPath, "update.sh"); var command = $"-c \"chmod 755 {updateScript}\""; Console.WriteLine(command); - Process.Start("/bin/bash", command).WaitForExit(); - Process.Start(updateScript).WaitForExit(); - this._logger.LogInformation($"chmod {command}"); + Console.WriteLine(command.Bash()); + Console.WriteLine(updateScript.Bash()); + this._logger.LogInformation($"chmod and run {updateScript}"); } //启动更新程序 proxy.startProcess(processName); @@ -187,21 +189,6 @@ namespace IoTDameon } } - private static void DeleteOldFiles(string appPath) - { - foreach (var item in Directory.GetFiles(appPath)) - { - if (!item.EndsWith(".db")) - { - File.Delete(item); - } - } - foreach (var item in Directory.GetDirectories(appPath)) - { - Directory.Delete(item, true); - } - } - private string getCheckSum(string file) { using var sha = SHA512.Create(); diff --git a/projects/IoTDameon/Views/Home/Index.cshtml b/projects/IoTDameon/Views/Home/Index.cshtml index a6ec031c..3fb5ed70 100644 --- a/projects/IoTDameon/Views/Home/Index.cshtml +++ b/projects/IoTDameon/Views/Home/Index.cshtml @@ -1,36 +1,23 @@ -@using System.IO -@model string +@model string +@using System.Reflection @{ Layout = null; - var files = Directory.GetFiles(Model); - var folders = Directory.GetDirectories(Model); - var output = ""; - var file = System.IO.Path.Combine(Model, "output.txt"); - if (File.Exists(file)) - { - output = File.ReadAllText(file); - } + var version = Assembly.GetEntryAssembly().GetCustomAttribute().InformationalVersion; } - + + + 更新程序|@version -

文件

-
    - @foreach (var item in files) - { -
  • f:@item
  • - } - @foreach (var item in folders) - { -
  • d:@item
  • - } -
- -
- - - -
+
+
+ +
+
+
@Html.Raw(ViewBag.Output)
+
+
v @version
+
\ No newline at end of file diff --git a/projects/projects.sln b/projects/projects.sln index 28646a8e..181d9b72 100644 --- a/projects/projects.sln +++ b/projects/projects.sln @@ -44,8 +44,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JobServer", "JobServer\JobS EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IoTDameon", "IoTDameon\IoTDameon.csproj", "{60596088-3C4E-4EA2-933A-B66CD269845B}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CheckSum", "CheckSum\CheckSum.csproj", "{EE34BB0E-41F3-4F2A-853E-FBFBCF421A5B}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -176,18 +174,6 @@ Global {60596088-3C4E-4EA2-933A-B66CD269845B}.Release|iPhone.Build.0 = Release|Any CPU {60596088-3C4E-4EA2-933A-B66CD269845B}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU {60596088-3C4E-4EA2-933A-B66CD269845B}.Release|iPhoneSimulator.Build.0 = Release|Any CPU - {EE34BB0E-41F3-4F2A-853E-FBFBCF421A5B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {EE34BB0E-41F3-4F2A-853E-FBFBCF421A5B}.Debug|Any CPU.Build.0 = Debug|Any CPU - {EE34BB0E-41F3-4F2A-853E-FBFBCF421A5B}.Debug|iPhone.ActiveCfg = Debug|Any CPU - {EE34BB0E-41F3-4F2A-853E-FBFBCF421A5B}.Debug|iPhone.Build.0 = Debug|Any CPU - {EE34BB0E-41F3-4F2A-853E-FBFBCF421A5B}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU - {EE34BB0E-41F3-4F2A-853E-FBFBCF421A5B}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU - {EE34BB0E-41F3-4F2A-853E-FBFBCF421A5B}.Release|Any CPU.ActiveCfg = Release|Any CPU - {EE34BB0E-41F3-4F2A-853E-FBFBCF421A5B}.Release|Any CPU.Build.0 = Release|Any CPU - {EE34BB0E-41F3-4F2A-853E-FBFBCF421A5B}.Release|iPhone.ActiveCfg = Release|Any CPU - {EE34BB0E-41F3-4F2A-853E-FBFBCF421A5B}.Release|iPhone.Build.0 = Release|Any CPU - {EE34BB0E-41F3-4F2A-853E-FBFBCF421A5B}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU - {EE34BB0E-41F3-4F2A-853E-FBFBCF421A5B}.Release|iPhoneSimulator.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -205,7 +191,7 @@ Global {60596088-3C4E-4EA2-933A-B66CD269845B} = {AE34E06D-C5C7-44BC-B168-85808318516C} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {0B7095FB-5E70-4EF8-805A-CB4A91AE4B0A} BuildVersion_StartDate = 2000/1/1 + SolutionGuid = {0B7095FB-5E70-4EF8-805A-CB4A91AE4B0A} EndGlobalSection EndGlobal