Skip to content

Commit

Permalink
Improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
franklupo committed Jul 30, 2020
1 parent 8039831 commit d6c5ede
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 129 deletions.
246 changes: 123 additions & 123 deletions src/Corsinvest.ProxmoxVE.NodeProtect.Api/Application.cs
Original file line number Diff line number Diff line change
@@ -1,77 +1,77 @@
/*
* 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
*/

using Corsinvest.ProxmoxVE.Api.Extension.Helpers;
using Renci.SshNet;
using System;
using System.IO;
using System.Linq;

namespace Corsinvest.ProxmoxVE.NodeProtect.Api
{
/// <summary>
/// Node protect
/// </summary>
public class Application
{
/// <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
Keep: {keep}
Directory Work: {directoryWork}
Directory Node to archive:");

foreach (var item in pathsToBackup) { @out.WriteLine(item); }

var date = DateTime.Now;
var fileNameTarGz = FileNameLinuxTarGz(date);

//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
/*
* 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
*/

using Corsinvest.ProxmoxVE.Api.Extension.Helpers;
using Renci.SshNet;
using System;
using System.IO;
using System.Linq;

namespace Corsinvest.ProxmoxVE.NodeProtect.Api
{
/// <summary>
/// Node protect
/// </summary>
public class Application
{
/// <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
Keep: {keep}
Directory Work: {directoryWork}
Directory Node to archive:");

foreach (var item in pathsToBackup) { @out.WriteLine(item); }

var date = DateTime.Now;
var fileNameTarGz = FileNameLinuxTarGz(date);

//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();
Expand Down Expand Up @@ -104,52 +104,52 @@ public static void Backup(string hostsAndPort,
}
sshClient.Disconnect();

@out.WriteLine($"Create config: {fileToSave}");
}

//keep
foreach (var directoryBackupDateTime in Directory.GetDirectories(directoryWork)
.OrderByDescending(a => a)
.Skip(keep))
{
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="debug"></param>
/// <param name="out"></param>
public static void UploadToNode(string hostsAndPort,
string username,
string password,
string fileTarGz,
bool debug,
TextWriter @out)
{
var (host, port) = ClientHelper.GetHostsAndPorts(hostsAndPort, 22, true, null)
.Where(a => a.Host == fileTarGz.Substring(0, fileTarGz.IndexOf(FILE_NAME)))
.FirstOrDefault();

var fileName = FileNameLinuxTarGz(DirectoryToDate(fileTarGz));

//upload
@out.WriteLine($"Create config: {fileToSave}");
}

//keep
foreach (var directoryBackupDateTime in Directory.GetDirectories(directoryWork)
.OrderByDescending(a => a)
.Skip(keep))
{
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="debug"></param>
/// <param name="out"></param>
public static void UploadToNode(string hostsAndPort,
string username,
string password,
string fileTarGz,
bool debug,
TextWriter @out)
{
var (host, port) = ClientHelper.GetHostsAndPorts(hostsAndPort, 22, true, null)
.Where(a => a.Host == fileTarGz.Substring(0, fileTarGz.IndexOf(FILE_NAME)))
.FirstOrDefault();

var fileName = FileNameLinuxTarGz(DirectoryToDate(fileTarGz));

//upload
using var sftp = new SftpClient(host, port, username, password);
using var stream = File.OpenRead(fileTarGz);
sftp.Connect();
Expand All @@ -161,7 +161,7 @@ public static void UploadToNode(string hostsAndPort,
}

sftp.UploadFile(stream, fileName);
sftp.Disconnect();
}
}
sftp.Disconnect();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>

<Version>1.3.0</Version>
<Version>1.3.1</Version>
<Company>Corsinvest Srl</Company>
<Authors>Daniele Corsini</Authors>
<Copyright>Corsinvest Srl</Copyright>
<Product>Corsinvest for Proxmox VE Node Protrect</Product>
<AssemblyTitle>Corsinvest for Proxmox VE Node Protrect</AssemblyTitle>
<Description>Corsinvest for Proxmox VE Node Protrect</Description>

<PackageProjectUrl>https://github.com/Corsinvest/cv4pve-node-protect</PackageProjectUrl>
<PackageTags>ProxmoxVE;Api,Client;Rest;Corsinvest;Node;Protect</PackageTags>
<PackageIcon>icon.png</PackageIcon>
<PackageReleaseNotes></PackageReleaseNotes>
Expand All @@ -32,7 +33,7 @@

<ItemGroup>
<!-- <ProjectReference Include="..\..\..\cv4pve-api-dotnet\src\Corsinvest.ProxmoxVE.Api.Extension\Corsinvest.ProxmoxVE.Api.Extension.csproj" /> -->
<PackageReference Include="Corsinvest.ProxmoxVE.Api.Extension" Version="2.6.0" />
<PackageReference Include="Corsinvest.ProxmoxVE.Api.Extension" Version="2.6.2" />

<PackageReference Include="SSH.NET" Version="2016.1.0" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<Version>1.1.0</Version>
<Version>1.1.1</Version>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AssemblyName>cv4pve-node-protect</AssemblyName>
<Company>Corsinvest Srl</Company>
Expand All @@ -20,9 +20,9 @@
<TrimmerRootAssembly Include="System.Net.WebClient" />

<!-- <ProjectReference Include="..\..\..\cv4pve-api-dotnet\src\Corsinvest.ProxmoxVE.Api.Shell\Corsinvest.ProxmoxVE.Api.Shell.csproj" /> -->
<PackageReference Include="Corsinvest.ProxmoxVE.Api.Shell" Version="2.5.3" />
<PackageReference Include="Corsinvest.ProxmoxVE.Api.Shell" Version="2.6.3" />

<!-- <ProjectReference Include="..\Corsinvest.ProxmoxVE.NodeProtect.Api\Corsinvest.ProxmoxVE.NodeProtect.Api.csproj" /> -->
<PackageReference Include="Corsinvest.ProxmoxVE.NodeProtect.Api" Version="1.2.0" />
<PackageReference Include="Corsinvest.ProxmoxVE.NodeProtect.Api" Version="1.3.1" />
</ItemGroup>
</Project>
1 change: 1 addition & 0 deletions src/Corsinvest.ProxmoxVE.NodeProtect/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Program
static int Main(string[] args)
{
var app = ShellHelper.CreateConsoleApp("cv4pve-node-protect", "Node protect for Proxmox VE");
app.GetApiToken().ShowInHelpText = false;
new ShellCommands(app);
return app.ExecuteConsoleApp(args);
}
Expand Down

0 comments on commit d6c5ede

Please sign in to comment.