1
0
Fork 0
mirror of https://github.com/Corsinvest/cv4pve-pepper.git synced 2025-03-09 15:39:57 +00:00

add start/resume for stopped/paused vm options and delay options feature

This commit is contained in:
Muzaffer AKYIL 2022-09-03 16:54:25 +03:00
parent a23c0c5c4e
commit d9fadb7712
No known key found for this signature in database
GPG key ID: 92DD746EC5D35158
2 changed files with 48 additions and 5 deletions

3
.gitignore vendored
View file

@ -351,4 +351,5 @@ MigrationBackup/
# Ionide (cross platform F# VS Code tools) working folder # Ionide (cross platform F# VS Code tools) working folder
.ionide/ .ionide/
TestParm.parm TestParm.parm
.idea/.idea.Corsinvest.ProxmoxVE.Pepper/.idea/

View file

@ -13,6 +13,7 @@ using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using Corsinvest.ProxmoxVE.Api.Extension; using Corsinvest.ProxmoxVE.Api.Extension;
using Corsinvest.ProxmoxVE.Api.Extension.Utils; using Corsinvest.ProxmoxVE.Api.Extension.Utils;
using Corsinvest.ProxmoxVE.Api.Shared.Models.Vm;
using Corsinvest.ProxmoxVE.Api.Shell.Helpers; using Corsinvest.ProxmoxVE.Api.Shell.Helpers;
namespace Corsinvest.ProxmoxVE.Pepper namespace Corsinvest.ProxmoxVE.Pepper
@ -35,6 +36,16 @@ namespace Corsinvest.ProxmoxVE.Pepper
var optViewerOptions = app.AddOption("--viewer-options", "Send options directly SPICE Viewer (quote value)."); var optViewerOptions = app.AddOption("--viewer-options", "Send options directly SPICE Viewer (quote value).");
var optStartOrResume = app.AddOption("--start-or-resume", "Run stopped or paused VM");
optStartOrResume.IsRequired = false;
optStartOrResume.SetDefaultValue("false");
var optWaitForStartup = app.AddOption("--wait-for-startup", "Wait for startup VM");
optWaitForStartup.IsRequired = false;
optWaitForStartup.SetDefaultValue(5);
var startOrResume = optStartOrResume.GetValue() == "true";
app.SetHandler(async (InvocationContext ctx) => app.SetHandler(async (InvocationContext ctx) =>
{ {
var loggerFactory = ConsoleHelper.CreateLoggerFactory<Program>(app.GetLogLevelFromDebug()); var loggerFactory = ConsoleHelper.CreateLoggerFactory<Program>(app.GetLogLevelFromDebug());
@ -44,7 +55,37 @@ namespace Corsinvest.ProxmoxVE.Pepper
if (string.IsNullOrWhiteSpace(proxy)) { proxy = client.Host; } if (string.IsNullOrWhiteSpace(proxy)) { proxy = client.Host; }
var vm = await client.GetVm(optVmId.GetValue()); var vm = await client.GetVm(optVmId.GetValue());
var (success, reasonPhrase, content) = await VmHelper.GetQemuSpiceFileVV(client, vm.Node,vm.VmId, proxy);
if (startOrResume && (vm.IsStopped || vm.IsPaused))
{
if (app.DebugIsActive())
{
await Console.Out.WriteLineAsync(
$"VM is {(vm.IsStopped ? "stopped" : "paused")}. {(vm.IsStopped ? "Running" : "Resuming")} now!");
}
await VmHelper.ChangeStatusVm(client, vm.Node, vm.VmType, vm.VmId,
vm.IsStopped ? VmStatus.Start : VmStatus.Resume);
for (var i = 1; i < int.Parse(optWaitForStartup.GetValue()); i++)
{
if (app.DebugIsActive())
{
await Console.Out.WriteAsync($"\r{10 - i}");
}
await Task.Delay(1000);
}
if (app.DebugIsActive())
{
await Console.Out.WriteLineAsync();
await Console.Out.WriteLineAsync("VM is running.");
}
}
var (success, reasonPhrase, content) =
await VmHelper.GetQemuSpiceFileVV(client, vm.Node, vm.VmId, proxy);
if (success) if (success)
{ {
//proxy force //proxy force
@ -59,6 +100,7 @@ namespace Corsinvest.ProxmoxVE.Pepper
break; break;
} }
} }
content = string.Join("\n", lines); content = string.Join("\n", lines);
if (app.DebugIsActive()) if (app.DebugIsActive())
@ -97,8 +139,8 @@ namespace Corsinvest.ProxmoxVE.Pepper
if (app.DebugIsActive()) if (app.DebugIsActive())
{ {
Console.Out.WriteLine($"Run FileName: {process.StartInfo.FileName}"); await Console.Out.WriteLineAsync($"Run FileName: {process.StartInfo.FileName}");
Console.Out.WriteLine($"Run Arguments: {process.StartInfo.Arguments}"); await Console.Out.WriteLineAsync($"Run Arguments: {process.StartInfo.Arguments}");
} }
if (!app.DryRunIsActive()) if (!app.DryRunIsActive())
@ -111,7 +153,7 @@ namespace Corsinvest.ProxmoxVE.Pepper
} }
else else
{ {
Console.Out.WriteLine($"Error: {reasonPhrase}"); await Console.Out.WriteLineAsync($"Error: {reasonPhrase}");
ctx.ExitCode = 1; ctx.ExitCode = 1;
} }
}); });