更新时备份到zip修改为移动文件夹

添加shell执行命令


Former-commit-id: 3d3dc48d6f8add12d7dc6df8173bd7eb97e1176c
Former-commit-id: 736700a7cf9d657fd8be1dff5ac3c9435718b7c5
TSXN
wanggang 5 years ago
parent d3eb8eeb17
commit 18dae7dcb4

@ -1,7 +1,6 @@
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System.IO; using System;
using System.IO.Compression;
using System.Reflection; using System.Reflection;
namespace IoTDameon.Controllers namespace IoTDameon.Controllers
@ -15,47 +14,25 @@ namespace IoTDameon.Controllers
this._env = env; this._env = env;
} }
public IActionResult Index() public IActionResult Index(string command)
{ {
var path = Path.Combine(this._env.WebRootPath, "upload"); if (!string.IsNullOrEmpty(command))
return View(model: path);
}
public IActionResult GetVersion()
{
return Content(Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion);
}
public IActionResult Upload()
{
if (Request.Form.Files != null && Request.Form.Files.Count > 0)
{ {
var file = Request.Form.Files[0]; try
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))
{ {
System.IO.File.Delete(fullName); ViewBag.Output = command.Bash();
} }
using (FileStream fs = System.IO.File.Create(fullName)) catch (Exception ex)
{ {
file.CopyTo(fs); ViewBag.Output = ex.ToString();
}
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);
} }
} }
return RedirectToAction("Index"); return View(model: command);
}
public IActionResult GetVersion()
{
return Content(Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion);
} }
} }
} }

@ -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;
}
}
}

@ -76,7 +76,7 @@ namespace IoTDameon
var root = Directory.GetParent(_env.ContentRootPath).FullName; var root = Directory.GetParent(_env.ContentRootPath).FullName;
var appPath = Path.Combine(root, appFolder); var appPath = Path.Combine(root, appFolder);
var name = $"{appFolder}.zip"; 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 file = Path.Combine(root, name);
var currentCheckSum = string.Empty; var currentCheckSum = string.Empty;
//检查是否有更新 //检查是否有更新
@ -144,19 +144,21 @@ namespace IoTDameon
//备份要更新的程序 //备份要更新的程序
try try
{ {
if (File.Exists(backupName)) Directory.Move(appPath, backupPath);
{ this._logger.LogInformation($"back up {appPath}");
File.Delete(backupName);
}
ZipFile.CreateFromDirectory(appPath, backupName, CompressionLevel.Fastest, true);
} }
catch (Exception ex) catch (Exception ex)
{ {
this._logger.LogError(ex, ex.Message); this._logger.LogError(ex, ex.Message);
throw new Exception("backup error", ex); throw new Exception("backup error", ex);
} }
DeleteOldFiles(appPath); Directory.CreateDirectory(appPath);
this._logger.LogInformation($"delete old files in {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 try
{ {
@ -166,8 +168,8 @@ namespace IoTDameon
catch (Exception ex) catch (Exception ex)
{ {
this._logger.LogError(ex, ex.Message); this._logger.LogError(ex, ex.Message);
DeleteOldFiles(appPath); Directory.Delete(appPath, true);
ZipFile.ExtractToDirectory(backupName, root, true); Directory.Move(backupPath, appPath);
throw new Exception("upzip errorrestore old files", ex); throw new Exception("upzip errorrestore old files", ex);
} }
//设置权限 //设置权限
@ -176,9 +178,9 @@ namespace IoTDameon
var updateScript = Path.Combine(appPath, "update.sh"); var updateScript = Path.Combine(appPath, "update.sh");
var command = $"-c \"chmod 755 {updateScript}\""; var command = $"-c \"chmod 755 {updateScript}\"";
Console.WriteLine(command); Console.WriteLine(command);
Process.Start("/bin/bash", command).WaitForExit(); Console.WriteLine(command.Bash());
Process.Start(updateScript).WaitForExit(); Console.WriteLine(updateScript.Bash());
this._logger.LogInformation($"chmod {command}"); this._logger.LogInformation($"chmod and run {updateScript}");
} }
//启动更新程序 //启动更新程序
proxy.startProcess(processName); 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) private string getCheckSum(string file)
{ {
using var sha = SHA512.Create(); using var sha = SHA512.Create();

@ -1,36 +1,23 @@
@using System.IO @model string
@model string @using System.Reflection
@{ @{
Layout = null; Layout = null;
var files = Directory.GetFiles(Model); var version = Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
var folders = Directory.GetDirectories(Model);
var output = "";
var file = System.IO.Path.Combine(Model, "output.txt");
if (File.Exists(file))
{
output = File.ReadAllText(file);
}
} }
<html> <html lang="en">
<head> <head>
<meta content="text/html; charset=utf-8" http-equiv="content-type" />
<title>更新程序|@version</title>
</head> </head>
<body> <body>
<h2>文件</h2> <div style="margin: 0 auto; width: 1000px;">
<ul> <form action="/" method="post" style="margin:0;padding:0;">
@foreach (var item in files) <input type="text" name="command" value="@Model" placeholder="#" style="width:100%;background:#666;color:#ddd;border:none;height:1.5em;line-height:1.5em;outline:none;" />
{ </form>
<li>f:@item</li> <div style="background:#666;color:#ddd;line-height:1.5em;">
} <pre>@Html.Raw(ViewBag.Output)</pre>
@foreach (var item in folders) </div>
{ <div style="text-align:center">v @version</div>
<li>d:@item</li> </div>
}
</ul>
<form action="/Home/Upload" method="post" enctype="multipart/form-data">
<label>file:</label>
<input name="file" type="file" />
<button type="submit">提交</button>
</form>
</body> </body>
</html> </html>

@ -44,8 +44,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JobServer", "JobServer\JobS
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IoTDameon", "IoTDameon\IoTDameon.csproj", "{60596088-3C4E-4EA2-933A-B66CD269845B}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IoTDameon", "IoTDameon\IoTDameon.csproj", "{60596088-3C4E-4EA2-933A-B66CD269845B}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CheckSum", "CheckSum\CheckSum.csproj", "{EE34BB0E-41F3-4F2A-853E-FBFBCF421A5B}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU 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|iPhone.Build.0 = Release|Any CPU
{60596088-3C4E-4EA2-933A-B66CD269845B}.Release|iPhoneSimulator.ActiveCfg = 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 {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 EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@ -205,7 +191,7 @@ Global
{60596088-3C4E-4EA2-933A-B66CD269845B} = {AE34E06D-C5C7-44BC-B168-85808318516C} {60596088-3C4E-4EA2-933A-B66CD269845B} = {AE34E06D-C5C7-44BC-B168-85808318516C}
EndGlobalSection EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0B7095FB-5E70-4EF8-805A-CB4A91AE4B0A}
BuildVersion_StartDate = 2000/1/1 BuildVersion_StartDate = 2000/1/1
SolutionGuid = {0B7095FB-5E70-4EF8-805A-CB4A91AE4B0A}
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal

Loading…
Cancel
Save