Skip to content

Commit

Permalink
Bump SSH.NET from 2016.1.0 to 2020.0.2
Browse files Browse the repository at this point in the history
Improve code
  • Loading branch information
franklupo committed Jul 14, 2022
1 parent 345b83d commit 85f2dd6
Show file tree
Hide file tree
Showing 10 changed files with 909 additions and 952 deletions.
677 changes: 663 additions & 14 deletions LICENSE.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion README.MD
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# cv4pve-node-protect

[![License](https://img.shields.io/github/license/Corsinvest/cv4pve-node-protect.svg)](LICENSE.md) [![AppVeyor branch](https://img.shields.io/appveyor/ci/franklupo/cv4pve-node-protect/master.svg)](https://ci.appveyor.com/project/franklupo/cv4pve-node-protect)
[![License](https://img.shields.io/github/license/Corsinvest/cv4pve-node-protect.svg)](LICENSE.md)

```text
______ _ __
Expand Down
674 changes: 0 additions & 674 deletions gpl-3.0.txt

This file was deleted.

278 changes: 144 additions & 134 deletions src/Corsinvest.ProxmoxVE.NodeProtect.Api/Application.cs
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;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TargetFramework>net6.0</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>

<Version>1.4.0</Version>
<Version>1.5.0</Version>
<Company>Corsinvest Srl</Company>
<Authors>Daniele Corsini</Authors>
<Copyright>Corsinvest Srl</Copyright>
Expand Down Expand Up @@ -33,8 +33,8 @@

<ItemGroup>
<!-- <ProjectReference Include="..\..\..\cv4pve-api-dotnet\src\Corsinvest.ProxmoxVE.Api.Extension\Corsinvest.ProxmoxVE.Api.Extension.csproj" /> -->
<PackageReference Include="Corsinvest.ProxmoxVE.Api.Extension" Version="2.8.0" />
<PackageReference Include="SSH.NET" Version="2016.1.0" />
<PackageReference Include="Corsinvest.ProxmoxVE.Api.Extension" Version="7.3.6" />

<PackageReference Include="SSH.NET" Version="2020.0.2" />
</ItemGroup>
</Project>
Loading

0 comments on commit 85f2dd6

Please sign in to comment.