更新时备份到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.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<AssemblyInformationalVersionAttribute>().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<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 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 errorrestore 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();

@ -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<AssemblyInformationalVersionAttribute>().InformationalVersion;
}
<html>
<html lang="en">
<head>
<meta content="text/html; charset=utf-8" http-equiv="content-type" />
<title>更新程序|@version</title>
</head>
<body>
<h2>文件</h2>
<ul>
@foreach (var item in files)
{
<li>f:@item</li>
}
@foreach (var item in folders)
{
<li>d:@item</li>
}
</ul>
<form action="/Home/Upload" method="post" enctype="multipart/form-data">
<label>file:</label>
<input name="file" type="file" />
<button type="submit">提交</button>
</form>
<div style="margin: 0 auto; width: 1000px;">
<form action="/" method="post" style="margin:0;padding:0;">
<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>
<div style="background:#666;color:#ddd;line-height:1.5em;">
<pre>@Html.Raw(ViewBag.Output)</pre>
</div>
<div style="text-align:center">v @version</div>
</div>
</body>
</html>

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

Loading…
Cancel
Save