-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bump SSH.NET from 2016.1.0 to 2020.0.2
Improve code
- Loading branch information
Showing
10 changed files
with
909 additions
and
952 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
278 changes: 144 additions & 134 deletions
278
src/Corsinvest.ProxmoxVE.NodeProtect.Api/Application.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,167 +1,177 @@ | ||
/* | ||
* This file is part of the cv4pve-node-protect https://github.com/Corsinvest/cv4pve-node-protect, | ||
* | ||
* This source file is available under two different licenses: | ||
* - GNU General Public License version 3 (GPLv3) | ||
* - Corsinvest Enterprise License (CEL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* Copyright (C) 2016 Corsinvest Srl GPLv3 and CEL | ||
* SPDX-License-Identifier: GPL-3.0-only | ||
* SPDX-FileCopyrightText: 2019 Copyright Corsinvest Srl | ||
*/ | ||
|
||
using Corsinvest.ProxmoxVE.Api.Extension.Helpers; | ||
using Renci.SshNet; | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using Microsoft.Extensions.Logging; | ||
using Renci.SshNet; | ||
|
||
namespace Corsinvest.ProxmoxVE.NodeProtect.Api | ||
namespace Corsinvest.ProxmoxVE.NodeProtect.Api; | ||
|
||
/// <summary> | ||
/// Node protect | ||
/// </summary> | ||
public class Application | ||
{ | ||
/// <summary> | ||
/// Node protect | ||
/// Date format directory | ||
/// </summary> | ||
public class Application | ||
public static readonly string FORMAT_DATE = "yyyy-MM-dd-HH-mm-ss"; | ||
|
||
private const string FILE_NAME = "-config.tar.gz"; | ||
|
||
private static DateTime DirectoryToDate(string directory) | ||
=> DateTime.ParseExact(Path.GetFileName(directory), FORMAT_DATE, null); | ||
|
||
private static string FileNameLinuxTarGz(DateTime date) => $"/tmp/{date.ToString(FORMAT_DATE)}.tar.gz"; | ||
|
||
private static (string Host, int Port) ParseHostAndPort(string hostAndPort) | ||
{ | ||
/// <summary> | ||
/// Date format directory | ||
/// </summary> | ||
public static readonly string FORMAT_DATE = "yyyy-MM-dd-HH-mm-ss"; | ||
|
||
private const string FILE_NAME = "-config.tar.gz"; | ||
|
||
private static DateTime DirectoryToDate(string directory) | ||
=> DateTime.ParseExact(Path.GetFileName(directory), FORMAT_DATE, null); | ||
|
||
private static string FileNameLinuxTarGz(DateTime date) => $"/tmp/{date.ToString(FORMAT_DATE)}.tar.gz"; | ||
|
||
/// <summary> | ||
/// Backup | ||
/// </summary> | ||
/// <param name="hostsAndPort"></param> | ||
/// <param name="username"></param> | ||
/// <param name="password"></param> | ||
/// <param name="pathsToBackup"></param> | ||
/// <param name="directoryWork"></param> | ||
/// <param name="keep"></param> | ||
/// <param name="debug"></param> | ||
/// <param name="out"></param> | ||
public static void Backup(string hostsAndPort, | ||
string username, | ||
string password, | ||
string[] pathsToBackup, | ||
string directoryWork, | ||
int keep, | ||
bool debug, | ||
TextWriter @out) | ||
{ | ||
@out.WriteLine($@"ACTION Backup | ||
var data = hostAndPort.Split(':'); | ||
var host = data[0]; | ||
var port = data.Length == 2 | ||
? int.TryParse(data[1], out var result2) ? result2 : 22 | ||
: 22; | ||
|
||
return (host, port); | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Backup | ||
/// </summary> | ||
/// <param name="hostsAndPort"></param> | ||
/// <param name="username"></param> | ||
/// <param name="password"></param> | ||
/// <param name="pathsToBackup"></param> | ||
/// <param name="directoryWork"></param> | ||
/// <param name="keep"></param> | ||
/// <param name="out"></param> | ||
/// <param name="loggerFactory"></param> | ||
public static void Backup(string hostsAndPort, | ||
string username, | ||
string password, | ||
string[] pathsToBackup, | ||
string directoryWork, | ||
int keep, | ||
TextWriter @out, | ||
ILoggerFactory loggerFactory) | ||
{ | ||
var logger = loggerFactory.CreateLogger<Application>(); | ||
|
||
@out.WriteLine($@"ACTION Backup | ||
Keep: {keep} | ||
Directory Work: {directoryWork} | ||
Directory Node to archive:"); | ||
|
||
foreach (var item in pathsToBackup) { @out.WriteLine(item); } | ||
foreach (var item in pathsToBackup) { @out.WriteLine(item); } | ||
|
||
var date = DateTime.Now; | ||
var fileNameTarGz = FileNameLinuxTarGz(date); | ||
var date = DateTime.Now; | ||
var fileNameTarGz = FileNameLinuxTarGz(date); | ||
|
||
//create folder date | ||
var pathSave = Path.Combine(directoryWork, date.ToString(FORMAT_DATE)); | ||
Directory.CreateDirectory(pathSave); | ||
//create folder date | ||
var pathSave = Path.Combine(directoryWork, date.ToString(FORMAT_DATE)); | ||
Directory.CreateDirectory(pathSave); | ||
|
||
foreach (var (host, port) in ClientHelper.GetHostsAndPorts(hostsAndPort, 22, true, null)) | ||
{ | ||
// Execute a (SHELL) Command for download | ||
using var sshClient = new SshClient(host, port, username, password); | ||
//create tar | ||
sshClient.Connect(); | ||
|
||
var cmdCreateTarGz = $"tar -cvzPf {fileNameTarGz} {string.Join(" ", pathsToBackup)}"; | ||
var retCmd = sshClient.CreateCommand(cmdCreateTarGz).Execute(); | ||
if (debug) | ||
{ | ||
@out.WriteLine($"Create file tar.gz: {cmdCreateTarGz}"); | ||
@out.WriteLine($"Result command: {retCmd}"); | ||
} | ||
|
||
var fileToSave = Path.Combine(pathSave, $"{host}{FILE_NAME}"); | ||
|
||
// download | ||
using var sftp = new SftpClient(host, port, username, password); | ||
using var stream = File.OpenWrite(fileToSave); | ||
sftp.Connect(); | ||
if (debug) { @out.WriteLine($"Download file tar.gz: {fileNameTarGz} to {fileToSave}"); } | ||
sftp.DownloadFile(fileNameTarGz, stream); | ||
sftp.Disconnect(); | ||
foreach (var hostAndPort in hostsAndPort.Split(',')) | ||
{ | ||
var (host, port) = ParseHostAndPort(hostAndPort); | ||
|
||
//delete tar | ||
var cmdRmTarGz = $"rm {fileNameTarGz}"; | ||
retCmd = sshClient.CreateCommand(cmdRmTarGz).Execute(); | ||
if (debug) | ||
{ | ||
@out.WriteLine($"Delete tar.gz: {cmdRmTarGz}"); | ||
@out.WriteLine($"Result command: {retCmd}"); | ||
} | ||
sshClient.Disconnect(); | ||
|
||
@out.WriteLine($"Create config: {fileToSave}"); | ||
} | ||
// Execute a (SHELL) Command for download | ||
using var sshClient = new SshClient(host, port, username, password); | ||
//create tar | ||
sshClient.Connect(); | ||
|
||
//keep | ||
foreach (var directoryBackupDateTime in Directory.GetDirectories(directoryWork) | ||
.OrderByDescending(a => a) | ||
.Skip(keep)) | ||
{ | ||
Delete(directoryBackupDateTime, @out); | ||
} | ||
var cmdCreateTarGz = $"tar -cvzPf {fileNameTarGz} {string.Join(" ", pathsToBackup)}"; | ||
var retCmd = sshClient.CreateCommand(cmdCreateTarGz).Execute(); | ||
|
||
logger.LogDebug("Create file tar.gz: {cmdCreateTarGz}", cmdCreateTarGz); | ||
logger.LogDebug("Result command: {retCmd}", retCmd); | ||
|
||
var fileToSave = Path.Combine(pathSave, $"{host}{FILE_NAME}"); | ||
|
||
// download | ||
using var sftp = new SftpClient(host, port, username, password); | ||
using var stream = File.OpenWrite(fileToSave); | ||
sftp.Connect(); | ||
|
||
logger.LogDebug("Download file tar.gz: {fileNameTarGz} to {fileToSave}", fileNameTarGz, fileToSave); | ||
|
||
sftp.DownloadFile(fileNameTarGz, stream); | ||
sftp.Disconnect(); | ||
|
||
//delete tar | ||
var cmdRmTarGz = $"rm {fileNameTarGz}"; | ||
retCmd = sshClient.CreateCommand(cmdRmTarGz).Execute(); | ||
|
||
logger.LogDebug("Delete tar.gz: {cmdRmTarGz}", cmdRmTarGz); | ||
logger.LogDebug("Result command: {retCmd}", retCmd); | ||
|
||
sshClient.Disconnect(); | ||
|
||
@out.WriteLine($"Create config: {fileToSave}"); | ||
} | ||
|
||
/// <summary> | ||
/// Delete directory | ||
/// </summary> | ||
/// <param name="directoryBackupDateTime"></param> | ||
/// <param name="out"></param> | ||
public static void Delete(string directoryBackupDateTime, TextWriter @out) | ||
//keep | ||
foreach (var directoryBackupDateTime in Directory.GetDirectories(directoryWork) | ||
.OrderByDescending(a => a) | ||
.Skip(keep)) | ||
{ | ||
@out.WriteLine($"Delete Backup: {directoryBackupDateTime}"); | ||
Directory.Delete(directoryBackupDateTime, true); | ||
Delete(directoryBackupDateTime, @out); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Delete directory | ||
/// </summary> | ||
/// <param name="directoryBackupDateTime"></param> | ||
/// <param name="out"></param> | ||
public static void Delete(string directoryBackupDateTime, TextWriter @out) | ||
{ | ||
@out.WriteLine($"Delete Backup: {directoryBackupDateTime}"); | ||
Directory.Delete(directoryBackupDateTime, true); | ||
} | ||
|
||
/// <summary> | ||
/// Upload file config TarGz to node | ||
/// </summary> | ||
/// <param name="hostsAndPort"></param> | ||
/// <param name="username"></param> | ||
/// <param name="password"></param> | ||
/// <param name="fileTarGz"></param> | ||
/// <param name="loggerFactory"></param> | ||
public static void UploadToNode(string hostsAndPort, | ||
string username, | ||
string password, | ||
string fileTarGz, | ||
ILoggerFactory loggerFactory) | ||
{ | ||
var logger = loggerFactory.CreateLogger<Application>(); | ||
|
||
/// <summary> | ||
/// Upload file config TarGz to node | ||
/// </summary> | ||
/// <param name="hostsAndPort"></param> | ||
/// <param name="username"></param> | ||
/// <param name="password"></param> | ||
/// <param name="fileTarGz"></param> | ||
/// <param name="debug"></param> | ||
/// <param name="out"></param> | ||
public static void UploadToNode(string hostsAndPort, | ||
string username, | ||
string password, | ||
string fileTarGz, | ||
bool debug, | ||
TextWriter @out) | ||
foreach (var hostAndPort in hostsAndPort.Split(',')) | ||
{ | ||
var (host, port) = ClientHelper.GetHostsAndPorts(hostsAndPort, 22, true, null) | ||
.Where(a => a.Host == fileTarGz.Substring(0, fileTarGz.IndexOf(FILE_NAME))) | ||
.FirstOrDefault(); | ||
var (host, port) = ParseHostAndPort(hostAndPort); | ||
if (host == fileTarGz[..fileTarGz.IndexOf(FILE_NAME)]) | ||
{ | ||
var fileName = FileNameLinuxTarGz(DirectoryToDate(fileTarGz)); | ||
|
||
var fileName = FileNameLinuxTarGz(DirectoryToDate(fileTarGz)); | ||
logger.LogDebug("Host: {host}:{port}", host, port); | ||
|
||
//upload | ||
using var sftp = new SftpClient(host, port, username, password); | ||
using var stream = File.OpenRead(fileTarGz); | ||
sftp.Connect(); | ||
//upload | ||
using var sftp = new SftpClient(host, port, username, password); | ||
using var stream = File.OpenRead(fileTarGz); | ||
sftp.Connect(); | ||
|
||
if (debug) | ||
{ | ||
@out.WriteLine($"Host: {host}:{port}"); | ||
@out.WriteLine($"File upload: {fileName}"); | ||
} | ||
logger.LogDebug("File upload: {fileName}", fileName); | ||
|
||
sftp.UploadFile(stream, fileName); | ||
sftp.Disconnect(); | ||
sftp.UploadFile(stream, fileName); | ||
sftp.Disconnect(); | ||
|
||
break; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.