标签:tpc .com http 升级 out 直接 systemctl turn image
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="ServerIPAddress" value="***.***.***.***"/>
<add key="SSHUserName" value="***"/>
<add key="SSHPassWord" value="*********"/>
<add key="ServerPath" value="/***/***/"/>
<add key="ClientPath" value="D:\p\JiaoNiA\code\JNA\JNA.Web\bin\Release\netcoreapp2.0\centos.7-x64\publish\"/>
<add key="IgnorFilePatten" value="System\..+;Microsoft\..+;.+\.so" />
<add key="HttpServerStartCommand" value="systemctl start jna.service"/>
<add key="HttpServerStopCommand" value="systemctl stop jna.service"/>
<add key="WebSiteUrl" value="http://www.jiaonia.com"/>
<add key="WebSiteAssertString" value="教你啊-知识复利制造平台"/>
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
</configuration>
dotnet publish -c release -r centos.7-x64
static List<FileInfo> clientFileInfos = new List<FileInfo>();//用于存储本地待对比的文件
static List<string> IgnorFilePattens = new List<string>();//用于存储过滤器,过滤器命中的文件不用参与对比
static Dictionary<FileInfo,string> prepareFileInfo = new Dictionary<FileInfo, string>();//用于存储对比后待上传的文件
static NameValueCollection setting = ConfigurationManager.AppSettings;
static void Main(string[] args)
{
Console.WriteLine("开始比对文件(根据文件的修改时间)?y:开始。其他按键退出程序:");
if (Console.ReadLine() != "y")
{
return;
}
Console.WriteLine("开始比对文件...");
IgnorFilePattens.AddRange(setting["IgnorFilePatten"].Split(‘;‘));//把过滤器先缓存起来
getClientFileInfos(setting["ClientPath"]);//递归取本地文件,过滤器命中的文件跳过
sftpCompareFile(sftpClient => //本地文件与服务器文件对比
{
if (prepareFileInfo.Count < 1)
{
Console.WriteLine("没有需要更新的文件,按任意键退出程序!");
Console.ReadKey();
return;
}
Console.WriteLine("开始停机上传文件?y:开始。其他按键退出程序:");
if (Console.ReadLine() != "y")
{
return;
}
sshStartAndStopWebServer(() => //启停Web服务器
{
foreach (var fileInfo in prepareFileInfo.Keys)
{
using (var fileStream = fileInfo.OpenRead())
{
sftpClient.BufferSize = 6 * 1024;
sftpClient.UploadFile(fileStream, prepareFileInfo[fileInfo],true); //上传文件
}
Console.WriteLine("上传完成:" + prepareFileInfo[fileInfo]);
}
});
Thread.Sleep(918); //留给服务器喘息的时间
Console.WriteLine("开始请求目标网站...");
var html = GetHtml(setting["WebSiteUrl"]); //请求Web的URL
if (html.Contains(setting["WebSiteAssertString"])) //判断断言是否命中
{
Console.WriteLine("升级成功,按任意键退出程序");
}
else
{
Console.WriteLine("升级失败,请联系管理员!按任意键退出程序!");
}
Console.ReadKey();
});
}
static void getClientFileInfos(string path)
{
var directoryPaths = Directory.GetDirectories(path);
foreach (var directoryPath in directoryPaths)
{
getClientFileInfos(directoryPath); //递归,自己调自己
}
var filePaths = Directory.GetFiles(path);
foreach(var filePath in filePaths)
{
var fi = new FileInfo(filePath);
var flag = false;
foreach(var patten in IgnorFilePattens)
{
flag = Regex.IsMatch(fi.Name, patten);
if (flag)
{
break; //有一个过滤器命中,则不用管其他过滤器了
}
}
if (flag)
{
continue; //命中的文件,则跳过
}
clientFileInfos.Add(fi);
}
}
static void sftpCompareFile(Action<SftpClient> actor)
{
using (var client = new SftpClient(setting["ServerIPAddress"], setting["SSHUserName"], setting["SSHPassWord"]))
{
client.Connect();
foreach (var fileInfo in clientFileInfos)
{
var subName = fileInfo.FullName.Remove(0, setting["ClientPath"].Length);
if (subName.StartsWith("\\"))
{
subName = subName.Remove(0, 1);
}
subName = subName.Replace(‘\\‘, ‘/‘);
var serverPath = setting["ServerPath"] + subName;//前面几行代码都是为了拿到该文件在服务端的绝对路径,配置里的serverPath必须以/结尾,此处不做校验;
var flag = client.Exists(serverPath);
if (!flag)
{
prepareFileInfo.Add(fileInfo, serverPath); //如果服务端不存在这个文件,则这个文件是肯定要上传上去的,注意:这里没管目录存在不存在
Console.WriteLine("待上传文件:" + subName);
}
else
{
var dt = client.GetLastWriteTime(serverPath);
if (dt < fileInfo.LastWriteTime) //文件最后一次更新时间比较,本地的时间比服务端的时间新,则需要上传
{
prepareFileInfo.Add(fileInfo, serverPath);
Console.WriteLine("待上传文件:" + subName);
}
}
}
actor(client);
}
}
static void sshStartAndStopWebServer(Action actor)
{
using (var sshclient = new SshClient(setting["ServerIPAddress"], setting["SSHUserName"], setting["SSHPassWord"]))
{
sshclient.Connect();
using (var cmd = sshclient.CreateCommand(setting["HttpServerStopCommand"]))
{
cmd.Execute();
Console.WriteLine("停用HttpServer");
}
actor();
using (var cmd = sshclient.CreateCommand(setting["HttpServerStartCommand"]))
{
cmd.Execute();
Console.WriteLine("启用HttpServer");
}
sshclient.Disconnect();
}
}
static string GetHtml(string url)
{
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Timeout = 16 * 1000;
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
string html = reader.ReadToEnd();
stream.Close();
return html;
}
ASP.NET CORE Linux发布工具(文件对比 只上传差异文件;自动启停WebServer命令;上传完成自动预热WebServer)
标签:tpc .com http 升级 out 直接 systemctl turn image
原文地址:https://www.cnblogs.com/liulun/p/9066493.html