diff --git a/projects/IoTDameon/IoTDameon.csproj b/projects/IoTDameon/IoTDameon.csproj index fa49d1cd..4bc8bf03 100644 --- a/projects/IoTDameon/IoTDameon.csproj +++ b/projects/IoTDameon/IoTDameon.csproj @@ -16,6 +16,6 @@ - + \ No newline at end of file diff --git a/projects/IoTDameon/ShellHelper.cs b/projects/IoTDameon/ShellHelper.cs index ed3e1fb5..15c9d3b5 100644 --- a/projects/IoTDameon/ShellHelper.cs +++ b/projects/IoTDameon/ShellHelper.cs @@ -1,4 +1,5 @@ -using System.Diagnostics; +using System; +using System.Diagnostics; using System.Runtime.InteropServices; namespace IoTDameon @@ -7,24 +8,26 @@ namespace IoTDameon { public static string Bash(this string cmd) { + var fileName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "powershell" : "/bin/bash"; var escapedArgs = cmd.Replace("\"", "\\\""); var process = new Process() { StartInfo = new ProcessStartInfo { - FileName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)?"powershell": "/bin/bash", + FileName = fileName, Arguments = $"-c \"{escapedArgs}\"", RedirectStandardOutput = true, UseShellExecute = false, CreateNoWindow = true, } }; - + Console.WriteLine($"# {fileName} {escapedArgs}"); process.Start(); string result = process.StandardOutput.ReadToEnd(); process.WaitForExit(); - + process.Refresh(); + process.Close(); return result; } } diff --git a/projects/IoTDameon/UpdateIoTNodeService.cs b/projects/IoTDameon/UpdateIoTNodeService.cs index cb5c6326..f3513da7 100644 --- a/projects/IoTDameon/UpdateIoTNodeService.cs +++ b/projects/IoTDameon/UpdateIoTNodeService.cs @@ -4,7 +4,6 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using System; -using System.Diagnostics; using System.IO; using System.IO.Compression; using System.Net; @@ -108,8 +107,10 @@ namespace IoTDameon if (!File.Exists(file)) { var bytes = this._httpClientFactory.CreateClient().GetAsync($"{server}/{name}").Result.Content.ReadAsByteArrayAsync().Result; - using var fs = File.Create(file); - fs.Write(bytes); + using (var fs = File.Create(file)) + { + fs.Write(bytes); + } currentCheckSum = getCheckSum(file); if (currentCheckSum != lastCheckSum) { @@ -144,6 +145,10 @@ namespace IoTDameon //备份要更新的程序 try { + if (Directory.Exists(backupPath)) + { + Directory.Delete(backupPath, true); + } Directory.Move(appPath, backupPath); this._logger.LogInformation($"back up {appPath}"); } @@ -154,9 +159,12 @@ namespace IoTDameon } Directory.CreateDirectory(appPath); this._logger.LogInformation($"mkdir {appPath}"); - foreach (var item in Directory.GetFiles(backupPath,"*.db")) + foreach (var item in Directory.GetFiles(backupPath)) { - File.Copy(item, Path.Combine(appPath, Path.GetFileName(item))); + if (item.EndsWith(".db") || item == "appsettings.json") + { + File.Copy(item, Path.Combine(appPath, Path.GetFileName(item))); + } } this._logger.LogInformation($"copy db files to {appPath}"); //更新程序 @@ -176,8 +184,7 @@ namespace IoTDameon if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { var updateScript = Path.Combine(appPath, "update.sh"); - var command = $"-c \"chmod 755 {updateScript}\""; - Console.WriteLine(command); + var command = $"chmod 755 {updateScript}"; Console.WriteLine(command.Bash()); Console.WriteLine(updateScript.Bash()); this._logger.LogInformation($"chmod and run {updateScript}"); @@ -191,10 +198,14 @@ namespace IoTDameon private string getCheckSum(string file) { - using var sha = SHA512.Create(); - using var fs = File.OpenRead(file); - var checksum = BitConverter.ToString(sha.ComputeHash(fs)).Replace("-", "").ToLower(); - return checksum; + using (var sha = SHA512.Create()) + { + using (var fs = File.OpenRead(file)) + { + var checksum = BitConverter.ToString(sha.ComputeHash(fs)).Replace("-", "").ToLower(); + return checksum; + } + } } } } \ No newline at end of file diff --git a/projects/IoTNode/Version.cs b/projects/IoTNode/Version.cs index 60619bd9..401215f8 100644 --- a/projects/IoTNode/Version.cs +++ b/projects/IoTNode/Version.cs @@ -1,4 +1,4 @@ using System.Reflection; [assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyInformationalVersion("1.0.0.20082702")] \ No newline at end of file +[assembly: AssemblyInformationalVersion("1.0.0.20082703")] \ No newline at end of file diff --git a/projects/IoTNode/update.sh b/projects/IoTNode/update.sh index 5e8e2ccc..6713eb85 100644 --- a/projects/IoTNode/update.sh +++ b/projects/IoTNode/update.sh @@ -1,2 +1,2 @@ -chmod 755 ~/publish/apps/IoTNode/IoTNode -chmod 755 ~/publish/apps/IoTNode/wwwroot/ffmpeg-linux-arm64 \ No newline at end of file +chmod 755 /root/publish/apps/IoTNode/IoTNode +chmod 755 /root/publish/apps/IoTNode/wwwroot/ffmpeg-linux-arm64 \ No newline at end of file