Skip to content

Commit

Permalink
Timeout remove/create snapshot
Browse files Browse the repository at this point in the history
Support multiple host for HA
Improve output
Check-Update and Upgrade application
  • Loading branch information
franklupo committed Dec 17, 2019
1 parent 1db4739 commit 6daa870
Show file tree
Hide file tree
Showing 8 changed files with 367 additions and 296 deletions.
7 changes: 7 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# All files
[*]
guidelines = 80

# C# or VB files
[*.{cs,vb}]
guidelines = 80, 120
354 changes: 184 additions & 170 deletions README.md

Large diffs are not rendered by default.

57 changes: 35 additions & 22 deletions src/Corsinvest.ProxmoxVE.AutoSnap/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
using System.Linq;
using Corsinvest.ProxmoxVE.Api;
using Corsinvest.ProxmoxVE.Api.Extension;
using Corsinvest.ProxmoxVE.Api.Extension.Node;
using Corsinvest.ProxmoxVE.Api.Extension.VM;
using Corsinvest.ProxmoxVE.Api.Shell.Helpers;
using Newtonsoft.Json;

namespace Corsinvest.ProxmoxVE.AutoSnap
Expand Down Expand Up @@ -71,29 +73,48 @@ private void CallPhaseEvent(string phase, VMInfo vm, string label, int keep, str
/// </summary>
/// <param name="vmIdsOrNames"></param>
/// <param name="label"></param>
public void Status(string vmIdsOrNames, string label = null, OutputType outputType = OutputType.Text)
public void Status(string vmIdsOrNames, string label = null, OutputType outputType = OutputType.Unicode)
{
//select snapshot and filter
var snapshots = FilterApp(_client.GetVMs(vmIdsOrNames).SelectMany(a => a.Snapshots));
var snapshots = FilterApp(_client.GetVMs(vmIdsOrNames)
.Where(a => _client.GetNode(a.Node).IsOnline)
.SelectMany(a => a.Snapshots));

if (!string.IsNullOrWhiteSpace(label)) { snapshots = FilterLabel(snapshots, label); }

switch (outputType)
{
case OutputType.Text:
_stdOut.Write(snapshots.Info(true));
case OutputType.Html:
case OutputType.Markdown:
case OutputType.Unicode:
case OutputType.UnicodeAlt:

var tableOutputType = TableOutputType.Unicode;
switch (outputType)
{
case OutputType.Html: tableOutputType = TableOutputType.Html; break;
case OutputType.Markdown: tableOutputType = TableOutputType.Markdown; break;
case OutputType.Text: tableOutputType = TableOutputType.Text; break;
case OutputType.Unicode: tableOutputType = TableOutputType.Unicode; break;
case OutputType.UnicodeAlt: tableOutputType = TableOutputType.UnicodeAlt; break;
default: tableOutputType = TableOutputType.Unicode; break;
}

_stdOut.Write(snapshots.Info(true,tableOutputType));
break;

case OutputType.Json:
_stdOut.Write(JsonConvert.SerializeObject((snapshots.Select(a => a.GetRowInfo(true)))));
break;

case OutputType.JsonPretty:
_stdOut.Write(JsonConvert.SerializeObject((snapshots.Select(a => a.GetRowInfo(true))), Formatting.Indented));
_stdOut.Write(JsonConvert.SerializeObject((snapshots.Select(a => a.GetRowInfo(true))),
Formatting.Indented));
break;

default:
_stdOut.Write(snapshots.Info(true));
_stdOut.Write(snapshots.Info(true, TableOutputType.Unicode));
break;
}
}
Expand All @@ -114,8 +135,9 @@ private static IEnumerable<Snapshot> FilterLabel(IEnumerable<Snapshot> snapshots
/// <param name="label"></param>
/// <param name="keep"></param>
/// <param name="state"></param>
/// <param name="timeout"></param>
/// <returns></returns>
public bool Snap(string vmIdsOrNames, string label, int keep, bool state)
public bool Snap(string vmIdsOrNames, string label, int keep, bool state, long timeout)
{
_stdOut.WriteLine($@"ACTION Snap
VMs: {vmIdsOrNames}
Expand Down Expand Up @@ -154,10 +176,7 @@ public bool Snap(string vmIdsOrNames, string label, int keep, bool state)
var inError = true;
if (!_dryRun)
{
var oldWaitTimeout = ResultExtension.WaitTimeout;
ResultExtension.WaitTimeout = 30000;

var result = vm.Snapshots.Create(snapName, APPLICATION_NAME, state, true);
var result = vm.Snapshots.Create(snapName, APPLICATION_NAME, state, timeout);
inError = result.LogInError(_stdOut);

//check error in task
Expand All @@ -168,8 +187,6 @@ public bool Snap(string vmIdsOrNames, string label, int keep, bool state)
_stdOut.WriteLine($"Error in task: {data.exitstatus}");
inError = true;
}

ResultExtension.WaitTimeout = oldWaitTimeout;
}

if (inError)
Expand All @@ -183,7 +200,7 @@ public bool Snap(string vmIdsOrNames, string label, int keep, bool state)
CallPhaseEvent("snap-create-post", vm, label, keep, snapName, state);

//remove old snapshot
if (!SnapshotsRemove(vm, label, keep))
if (!SnapshotsRemove(vm, label, keep, timeout))
{
ret = false;
continue;
Expand All @@ -203,8 +220,9 @@ public bool Snap(string vmIdsOrNames, string label, int keep, bool state)
/// <param name="vmIdsOrNames"></param>
/// <param name="label"></param>
/// <param name="keep"></param>
/// <param name="timeout"></param>
/// <returns></returns>
public bool Clean(string vmIdsOrNames, string label, int keep)
public bool Clean(string vmIdsOrNames, string label, int keep, long timeout)
{
_stdOut.WriteLine($@"ACTION Clean
VMs: {vmIdsOrNames}
Expand All @@ -217,15 +235,15 @@ public bool Clean(string vmIdsOrNames, string label, int keep)
foreach (var vm in _client.GetVMs(vmIdsOrNames))
{
_stdOut.WriteLine($"----- VM {vm.Id} -----");
if (!SnapshotsRemove(vm, label, keep)) { ret = false; }
if (!SnapshotsRemove(vm, label, keep, timeout)) { ret = false; }
}

CallPhaseEvent("clean-job-end", null, label, keep, null, false);

return ret;
}

private bool SnapshotsRemove(VMInfo vm, string label, int keep)
private bool SnapshotsRemove(VMInfo vm, string label, int keep, long timeout)
{
foreach (var snapshot in FilterLabel(vm.Snapshots, label).Reverse().Skip(keep).Reverse())
{
Expand All @@ -236,12 +254,7 @@ private bool SnapshotsRemove(VMInfo vm, string label, int keep)
var inError = false;
if (!_dryRun)
{
var oldWaitTimeout = ResultExtension.WaitTimeout;
ResultExtension.WaitTimeout = 30000;

inError = vm.Snapshots.Remove(snapshot, true).LogInError(_stdOut);

ResultExtension.WaitTimeout = oldWaitTimeout;
inError = vm.Snapshots.Remove(snapshot, timeout).LogInError(_stdOut);
}

if (inError)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<Version>1.5.0</Version>
<TargetFramework>netcoreapp3.0</TargetFramework>
<AssemblyName>cv4pve-autosnap</AssemblyName>
<Company>Corsinvest Srl</Company>
<Authors>Daniele Corsini</Authors>
<Copyright>Corsinvest Srl</Copyright>
<Product>Corsinvest for Proxmox VE Auto Snapshot</Product>
<AssemblyTitle>Corsinvest for Proxmox VE Auto Snapshot</AssemblyTitle>
<Description>Corsinvest for Proxmox VE Auto Snapshot</Description>
</PropertyGroup>

<ItemGroup>
<!-- Fix RedHat, Centos,Fedora -->
<RuntimeHostConfigurationOption Include="System.Globalization.Invariant" Value="true" />

<!-- <ProjectReference Include="..\..\..\cv4pve-api-dotnet\src\Corsinvest.ProxmoxVE.Api.Extension\Corsinvest.ProxmoxVE.Api.Extension.csproj" /> -->
<PackageReference Include="Corsinvest.ProxmoxVE.Api.Extension" Version="2.0.0" />
</ItemGroup>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<Version>1.6.0</Version>
<TargetFramework>netcoreapp3.0</TargetFramework>
<AssemblyName>cv4pve-autosnap</AssemblyName>
<Company>Corsinvest Srl</Company>
<Authors>Daniele Corsini</Authors>
<Copyright>Corsinvest Srl</Copyright>
<Product>Corsinvest for Proxmox VE Auto Snapshot</Product>
<AssemblyTitle>Corsinvest for Proxmox VE Auto Snapshot</AssemblyTitle>
<Description>Corsinvest for Proxmox VE Auto Snapshot</Description>
</PropertyGroup>

<ItemGroup>
<!-- Fix RedHat, Centos,Fedora -->
<RuntimeHostConfigurationOption Include="System.Globalization.Invariant" Value="true" />

<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="1.0.0" />
</ItemGroup>
</Project>
20 changes: 20 additions & 0 deletions src/Corsinvest.ProxmoxVE.AutoSnap/OutputType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,31 @@ namespace Corsinvest.ProxmoxVE.AutoSnap
/// </summary>
public enum OutputType
{
/// <summary>
/// Unicode
/// </summary>
Unicode,

/// <summary>
/// Text
/// </summary>
Text,

/// <summary>
/// Unicode
/// </summary>
UnicodeAlt,

/// <summary>
/// Markdown
/// </summary>
Markdown,

/// <summary>
/// Html
/// </summary>
Html,

/// <summary>
/// Json
/// </summary>
Expand Down
52 changes: 26 additions & 26 deletions src/Corsinvest.ProxmoxVE.AutoSnap/Program.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
/*
* This file is part of the cv4pve-autosnap https://github.com/Corsinvest/cv4pve-autosnap,
*
* 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 System;
using Corsinvest.ProxmoxVE.Api.Extension.Helpers.Shell;

namespace Corsinvest.ProxmoxVE.AutoSnap
{
class Program
{
static int Main(string[] args)
{
var app = ShellHelper.CreateConsoleApp(Commands.APPLICATION_NAME, "Automatic snapshot VM/CT with retention");
new ShellCommands(app);
return app.ExecuteConsoleApp(Console.Out, args);
}
}
/*
* This file is part of the cv4pve-autosnap https://github.com/Corsinvest/cv4pve-autosnap,
*
* 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.Shell.Helpers;

namespace Corsinvest.ProxmoxVE.AutoSnap
{
class Program
{
static int Main(string[] args)
{
var app = ShellHelper.CreateConsoleApp(Commands.APPLICATION_NAME,
"Automatic snapshot VM/CT with retention");
new ShellCommands(app);
return app.ExecuteConsoleApp(args);
}
}
}
Loading

0 comments on commit 6daa870

Please sign in to comment.