2022-01-23 16:49:23 +01:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
|
|
|
|
# File: vm-exec
|
|
|
|
#
|
pbot-vm: massive refactor
* replace heartbeat with health-check
* instead of steady stream of newlines every 5s, now awaits input
and responds with `vmstat` output
* more reliably use host/config/vm-exec.json to get libvirt domain name
for snapshot-revert, server address, serial ports, vagrant setting, etc
* use iptables/nftables to disable networking
* added guest/bin/disable-network-[iptables,nftables]
* added guest/bin/enable-network-[iptables,nftables]
* replace ugly ___OUTPUT___ texts in sh, bash, ksh, zsh languages
* documentation updates and tweaks
2024-04-10 10:21:18 +02:00
|
|
|
# Purpose: Process and send commands to the PBot Guest server (guest-server)
|
|
|
|
# using the details from the config/vm-exec.json configuration file.
|
|
|
|
#
|
|
|
|
# Additionally, takes `-revert` and `-health` options to revert VM or check
|
|
|
|
# VM's health.
|
2022-01-23 16:49:23 +01:00
|
|
|
#
|
2022-02-14 18:47:16 +01:00
|
|
|
# Use the PBOTVM_CID, PBOTVM_VPORT and/or PBOTVM_SERIAL environment variables to
|
pbot-vm: massive refactor
* replace heartbeat with health-check
* instead of steady stream of newlines every 5s, now awaits input
and responds with `vmstat` output
* more reliably use host/config/vm-exec.json to get libvirt domain name
for snapshot-revert, server address, serial ports, vagrant setting, etc
* use iptables/nftables to disable networking
* added guest/bin/disable-network-[iptables,nftables]
* added guest/bin/enable-network-[iptables,nftables]
* replace ugly ___OUTPUT___ texts in sh, bash, ksh, zsh languages
* documentation updates and tweaks
2024-04-10 10:21:18 +02:00
|
|
|
# override the config/vm-exec.json values. E.g.:
|
2022-02-14 18:47:16 +01:00
|
|
|
#
|
|
|
|
# $ PBOTVM_CID=42 PBOTVM_SERIAL=7777 vm-exec -lang=sh echo test
|
2022-01-23 16:49:23 +01:00
|
|
|
|
pbot-vm: massive refactor
* replace heartbeat with health-check
* instead of steady stream of newlines every 5s, now awaits input
and responds with `vmstat` output
* more reliably use host/config/vm-exec.json to get libvirt domain name
for snapshot-revert, server address, serial ports, vagrant setting, etc
* use iptables/nftables to disable networking
* added guest/bin/disable-network-[iptables,nftables]
* added guest/bin/enable-network-[iptables,nftables]
* replace ugly ___OUTPUT___ texts in sh, bash, ksh, zsh languages
* documentation updates and tweaks
2024-04-10 10:21:18 +02:00
|
|
|
# SPDX-FileCopyrightText: 2021-2024 Pragmatic Software <pragma78@gmail.com>
|
2022-01-23 16:49:23 +01:00
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
2022-02-14 07:01:06 +01:00
|
|
|
use 5.020;
|
|
|
|
|
2022-01-23 16:49:23 +01:00
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
|
2022-02-14 07:01:06 +01:00
|
|
|
use feature qw(signatures);
|
|
|
|
no warnings qw(experimental::signatures);
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-01-29 21:22:48 +01:00
|
|
|
use constant {
|
pbot-vm: massive refactor
* replace heartbeat with health-check
* instead of steady stream of newlines every 5s, now awaits input
and responds with `vmstat` output
* more reliably use host/config/vm-exec.json to get libvirt domain name
for snapshot-revert, server address, serial ports, vagrant setting, etc
* use iptables/nftables to disable networking
* added guest/bin/disable-network-[iptables,nftables]
* added guest/bin/enable-network-[iptables,nftables]
* replace ugly ___OUTPUT___ texts in sh, bash, ksh, zsh languages
* documentation updates and tweaks
2024-04-10 10:21:18 +02:00
|
|
|
DOMAIN => $ENV{PBOTVM_DOMAIN} // 'pbot-vm',
|
|
|
|
ADDR => $ENV{PBOTVM_ADDR} // '127.0.0.1',
|
|
|
|
SERIAL => $ENV{PBOTVM_SERIAL} // 5555,
|
|
|
|
HEALTH => $ENV{PBOTVM_HEALTH} // 5556,
|
|
|
|
CID => $ENV{PBOTVM_CID} // 7,
|
|
|
|
VPORT => $ENV{PBOTVM_VPORT} // 5555,
|
|
|
|
VAGRANT => $ENV{PBOTVM_VAGRANT} // 0,
|
2022-01-29 21:22:48 +01:00
|
|
|
};
|
|
|
|
|
2022-02-14 07:01:06 +01:00
|
|
|
use File::Basename;
|
|
|
|
use JSON::XS;
|
|
|
|
use IPC::Open2;
|
|
|
|
use IO::Socket;
|
|
|
|
|
|
|
|
use FindBin qw($RealBin);
|
|
|
|
use lib "$RealBin/../lib";
|
|
|
|
|
|
|
|
sub connect_vsock($context) {
|
|
|
|
return undef if not $context->{'vm-cid'};
|
|
|
|
|
|
|
|
print STDERR "Connecting to remote VM socket CID $context->{'vm-cid'} port $context->{'vm-vport'}\n";
|
|
|
|
|
|
|
|
my $command = "socat - VSOCK-CONNECT:$context->{'vm-cid'}:$context->{'vm-vport'}";
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-14 07:01:06 +01:00
|
|
|
my ($pid, $input, $output) = eval {
|
|
|
|
my $pid = open2(my $output, my $input, $command);
|
|
|
|
return ($pid, $input, $output);
|
|
|
|
};
|
|
|
|
|
|
|
|
if ($@) {
|
|
|
|
print STDERR "Failed to connect to VM socket: $@\n";
|
|
|
|
return undef;
|
2022-01-23 16:49:23 +01:00
|
|
|
}
|
|
|
|
|
2022-02-14 07:01:06 +01:00
|
|
|
if (not defined $pid) {
|
|
|
|
print STDERR "Failed to connect to VM socket: $!\n";
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ($input, $output);
|
2022-01-23 16:49:23 +01:00
|
|
|
}
|
|
|
|
|
2022-02-14 07:01:06 +01:00
|
|
|
sub connect_serial($context) {
|
|
|
|
print STDERR "Connecting to remote VM serial port $context->{'vm-serial'}\n";
|
|
|
|
|
|
|
|
my $vm = IO::Socket::INET->new(
|
pbot-vm: massive refactor
* replace heartbeat with health-check
* instead of steady stream of newlines every 5s, now awaits input
and responds with `vmstat` output
* more reliably use host/config/vm-exec.json to get libvirt domain name
for snapshot-revert, server address, serial ports, vagrant setting, etc
* use iptables/nftables to disable networking
* added guest/bin/disable-network-[iptables,nftables]
* added guest/bin/enable-network-[iptables,nftables]
* replace ugly ___OUTPUT___ texts in sh, bash, ksh, zsh languages
* documentation updates and tweaks
2024-04-10 10:21:18 +02:00
|
|
|
PeerAddr => $context->{'vm-addr'},
|
2022-02-14 07:01:06 +01:00
|
|
|
PeerPort => $context->{'vm-serial'},
|
|
|
|
Proto => 'tcp',
|
|
|
|
Type => SOCK_STREAM
|
|
|
|
);
|
|
|
|
|
|
|
|
# return same $vm handle for ($input, $output)
|
|
|
|
return ($vm, $vm);
|
2022-02-08 18:55:00 +01:00
|
|
|
}
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-14 07:01:06 +01:00
|
|
|
sub connect_vm($context) {
|
|
|
|
my ($input, $output);
|
|
|
|
|
|
|
|
# attempt preferred VSOCK connection
|
|
|
|
($input, $output) = connect_vsock($context);
|
|
|
|
|
|
|
|
# fallback to serial
|
|
|
|
if (not defined $input) {
|
|
|
|
($input, $output) = connect_serial($context);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (not defined $input) {
|
|
|
|
die "Could not create connection to VM: $!";
|
|
|
|
}
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-14 07:01:06 +01:00
|
|
|
print STDERR "Connected to VM.\n";
|
|
|
|
return ($input, $output);
|
|
|
|
}
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-14 18:47:16 +01:00
|
|
|
sub make_context_from_args(@args_in) {
|
|
|
|
my $args = join ' ', @args_in;
|
2022-01-23 16:49:23 +01:00
|
|
|
|
pbot-vm: massive refactor
* replace heartbeat with health-check
* instead of steady stream of newlines every 5s, now awaits input
and responds with `vmstat` output
* more reliably use host/config/vm-exec.json to get libvirt domain name
for snapshot-revert, server address, serial ports, vagrant setting, etc
* use iptables/nftables to disable networking
* added guest/bin/disable-network-[iptables,nftables]
* added guest/bin/enable-network-[iptables,nftables]
* replace ugly ___OUTPUT___ texts in sh, bash, ksh, zsh languages
* documentation updates and tweaks
2024-04-10 10:21:18 +02:00
|
|
|
# extract leading options
|
|
|
|
my %opts;
|
|
|
|
while ($args =~ s/^-(revert|health)\s+//) {
|
|
|
|
$opts{$1} = 1;
|
|
|
|
}
|
|
|
|
|
2022-02-14 07:01:06 +01:00
|
|
|
my $context = eval { decode_json $args };
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-14 07:01:06 +01:00
|
|
|
if ($@) {
|
|
|
|
# wasn't JSON; make structure manually
|
2022-02-14 18:47:16 +01:00
|
|
|
$context = { code => $args };
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-14 18:47:16 +01:00
|
|
|
# command-line usage
|
|
|
|
if (not length $context->{code}) {
|
|
|
|
die "Usage: $0 [-lang=<language>] <code>\n";
|
|
|
|
}
|
2022-01-23 16:49:23 +01:00
|
|
|
}
|
|
|
|
|
pbot-vm: massive refactor
* replace heartbeat with health-check
* instead of steady stream of newlines every 5s, now awaits input
and responds with `vmstat` output
* more reliably use host/config/vm-exec.json to get libvirt domain name
for snapshot-revert, server address, serial ports, vagrant setting, etc
* use iptables/nftables to disable networking
* added guest/bin/disable-network-[iptables,nftables]
* added guest/bin/enable-network-[iptables,nftables]
* replace ugly ___OUTPUT___ texts in sh, bash, ksh, zsh languages
* documentation updates and tweaks
2024-04-10 10:21:18 +02:00
|
|
|
# set extracted leading options
|
|
|
|
foreach my $opt (keys %opts) {
|
|
|
|
print STDERR "Setting option `$opt`.\n";
|
|
|
|
$context->{$opt} = 1;
|
|
|
|
}
|
|
|
|
|
2022-02-15 20:28:47 +01:00
|
|
|
# parse options specific to vm-exec
|
pbot-vm: massive refactor
* replace heartbeat with health-check
* instead of steady stream of newlines every 5s, now awaits input
and responds with `vmstat` output
* more reliably use host/config/vm-exec.json to get libvirt domain name
for snapshot-revert, server address, serial ports, vagrant setting, etc
* use iptables/nftables to disable networking
* added guest/bin/disable-network-[iptables,nftables]
* added guest/bin/enable-network-[iptables,nftables]
* replace ugly ___OUTPUT___ texts in sh, bash, ksh, zsh languages
* documentation updates and tweaks
2024-04-10 10:21:18 +02:00
|
|
|
while ($context->{code} =~ s/^-(lang|revert|health|vm-domain|vm-health|vm-cid|vm-vport|vm-serial|vm)=([^ ]+)\s*//) {
|
2022-02-15 20:28:47 +01:00
|
|
|
my ($option, $value) = ($1, $2);
|
|
|
|
print STDERR "Overriding `$option` to `$value`.\n";
|
|
|
|
$context->{$option} = lc $value;
|
2022-01-23 16:49:23 +01:00
|
|
|
}
|
|
|
|
|
2022-02-14 18:47:16 +01:00
|
|
|
return $context;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub load_language($context) {
|
|
|
|
my $language = $context->{lang};
|
2022-02-14 07:01:06 +01:00
|
|
|
|
|
|
|
eval {
|
|
|
|
require "Languages/$language.pm";
|
|
|
|
} or do {
|
2022-02-14 18:47:16 +01:00
|
|
|
my @languages;
|
2022-02-14 07:01:06 +01:00
|
|
|
|
|
|
|
foreach my $module (sort glob "$RealBin/../lib/Languages/*.pm") {
|
|
|
|
$module = basename $module;
|
|
|
|
next if $module =~ m/^_/;
|
2022-02-14 18:47:16 +01:00
|
|
|
$module =~ s/.pm$//;
|
2022-02-14 07:01:06 +01:00
|
|
|
|
|
|
|
require "Languages/$module.pm" or die $!;
|
|
|
|
|
2022-02-14 18:47:16 +01:00
|
|
|
my $mod = "Languages::$module"->new(%$context);
|
|
|
|
|
|
|
|
if (exists $mod->{name} && $mod->{name} eq $language) {
|
|
|
|
return $mod;
|
2022-02-14 07:01:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$module = $mod->{name} if exists $mod->{name};
|
2022-02-14 18:47:16 +01:00
|
|
|
push @languages, $module;
|
2022-02-14 07:01:06 +01:00
|
|
|
}
|
|
|
|
|
2022-02-14 18:47:16 +01:00
|
|
|
print "Language '$language' is not supported.\nSupported languages are: ", join(', ', @languages), "\n";
|
|
|
|
exit 1;
|
2022-02-14 07:01:06 +01:00
|
|
|
};
|
|
|
|
|
2022-02-14 18:47:16 +01:00
|
|
|
return "Languages::$language"->new(%$context);
|
|
|
|
}
|
|
|
|
|
2022-02-15 20:28:47 +01:00
|
|
|
sub load_config($file) {
|
|
|
|
open my $fh, '<', $file or die "load config: could not open $file: $!\n";
|
|
|
|
local $/ = undef;
|
|
|
|
my $json = <$fh>;
|
|
|
|
close $fh;
|
|
|
|
return decode_json($json);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub config_get_machine($config, $machine) {
|
|
|
|
# check if machine is an alias and update to actual machine
|
|
|
|
if (exists $config->{aliases}->{$machine}) {
|
|
|
|
$machine = $config->{aliases}->{$machine};
|
|
|
|
}
|
|
|
|
|
|
|
|
# return pointer to config entry
|
|
|
|
return $config->{machines}->{$machine};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub list_machines($config) {
|
|
|
|
my @machines;
|
|
|
|
|
|
|
|
# quick-and-easy: just list the aliases
|
|
|
|
foreach my $alias (keys %{$config->{aliases}}) {
|
|
|
|
push @machines, $alias;
|
|
|
|
}
|
|
|
|
|
|
|
|
return join ', ', sort @machines;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub configure_context($context, $config) {
|
|
|
|
if (exists $context->{vm}) {
|
|
|
|
# -vm option was passed, forcibly override machine
|
|
|
|
my $machine = $context->{vm};
|
|
|
|
|
|
|
|
# point at config entry
|
|
|
|
my $entry = config_get_machine($config, $machine);
|
|
|
|
|
|
|
|
if (not defined $entry) {
|
|
|
|
my $machines = list_machines($config);
|
|
|
|
print "Unknown machine '$machine'; available machines are: $machines\n";
|
pbot-vm: massive refactor
* replace heartbeat with health-check
* instead of steady stream of newlines every 5s, now awaits input
and responds with `vmstat` output
* more reliably use host/config/vm-exec.json to get libvirt domain name
for snapshot-revert, server address, serial ports, vagrant setting, etc
* use iptables/nftables to disable networking
* added guest/bin/disable-network-[iptables,nftables]
* added guest/bin/enable-network-[iptables,nftables]
* replace ugly ___OUTPUT___ texts in sh, bash, ksh, zsh languages
* documentation updates and tweaks
2024-04-10 10:21:18 +02:00
|
|
|
exit 3;
|
2022-02-15 20:28:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# override values
|
pbot-vm: massive refactor
* replace heartbeat with health-check
* instead of steady stream of newlines every 5s, now awaits input
and responds with `vmstat` output
* more reliably use host/config/vm-exec.json to get libvirt domain name
for snapshot-revert, server address, serial ports, vagrant setting, etc
* use iptables/nftables to disable networking
* added guest/bin/disable-network-[iptables,nftables]
* added guest/bin/enable-network-[iptables,nftables]
* replace ugly ___OUTPUT___ texts in sh, bash, ksh, zsh languages
* documentation updates and tweaks
2024-04-10 10:21:18 +02:00
|
|
|
$context->{'vm-domain'} = $machine;
|
|
|
|
$context->{'vm-addr'} = $entry->{'addr'};
|
|
|
|
$context->{'vm-health'} = $entry->{'health'};
|
|
|
|
$context->{'vm-serial'} = $entry->{'serial'};
|
|
|
|
$context->{'vm-cid'} = $entry->{'cid'};
|
|
|
|
$context->{'vm-vport'} = $entry->{'vport'};
|
|
|
|
$context->{'vm-vagrant'} = $entry->{'vagrant'};
|
2022-02-15 20:28:47 +01:00
|
|
|
} else {
|
|
|
|
# otherwise configure any undefined values as default machine
|
|
|
|
my $machine = $config->{'default-machine'};
|
|
|
|
|
|
|
|
# point at config entry
|
|
|
|
my $entry = config_get_machine($config, $machine);
|
|
|
|
|
|
|
|
if (not defined $entry) {
|
|
|
|
my $machines = list_machines($config);
|
|
|
|
print "Unknown machine '$machine'; available machines are: $machines\n";
|
pbot-vm: massive refactor
* replace heartbeat with health-check
* instead of steady stream of newlines every 5s, now awaits input
and responds with `vmstat` output
* more reliably use host/config/vm-exec.json to get libvirt domain name
for snapshot-revert, server address, serial ports, vagrant setting, etc
* use iptables/nftables to disable networking
* added guest/bin/disable-network-[iptables,nftables]
* added guest/bin/enable-network-[iptables,nftables]
* replace ugly ___OUTPUT___ texts in sh, bash, ksh, zsh languages
* documentation updates and tweaks
2024-04-10 10:21:18 +02:00
|
|
|
exit 3;
|
2022-02-15 20:28:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# update any undefined values, preserving any existing values
|
pbot-vm: massive refactor
* replace heartbeat with health-check
* instead of steady stream of newlines every 5s, now awaits input
and responds with `vmstat` output
* more reliably use host/config/vm-exec.json to get libvirt domain name
for snapshot-revert, server address, serial ports, vagrant setting, etc
* use iptables/nftables to disable networking
* added guest/bin/disable-network-[iptables,nftables]
* added guest/bin/enable-network-[iptables,nftables]
* replace ugly ___OUTPUT___ texts in sh, bash, ksh, zsh languages
* documentation updates and tweaks
2024-04-10 10:21:18 +02:00
|
|
|
$context->{'vm-domain'} //= $machine;
|
|
|
|
$context->{'vm-addr'} //= $entry->{'addr'};
|
|
|
|
$context->{'vm-health'} //= $entry->{'health'};
|
|
|
|
$context->{'vm-serial'} //= $entry->{'serial'};
|
|
|
|
$context->{'vm-cid'} //= $entry->{'cid'};
|
|
|
|
$context->{'vm-vport'} //= $entry->{'vport'};
|
|
|
|
$context->{'vm-vagrant'} //= $entry->{'vagrant'};
|
2022-02-15 20:28:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# set any undefined values to default values
|
pbot-vm: massive refactor
* replace heartbeat with health-check
* instead of steady stream of newlines every 5s, now awaits input
and responds with `vmstat` output
* more reliably use host/config/vm-exec.json to get libvirt domain name
for snapshot-revert, server address, serial ports, vagrant setting, etc
* use iptables/nftables to disable networking
* added guest/bin/disable-network-[iptables,nftables]
* added guest/bin/enable-network-[iptables,nftables]
* replace ugly ___OUTPUT___ texts in sh, bash, ksh, zsh languages
* documentation updates and tweaks
2024-04-10 10:21:18 +02:00
|
|
|
$context->{nick} //= 'vm';
|
|
|
|
$context->{channel} //= 'vm';
|
|
|
|
$context->{lang} //= 'c2x';
|
|
|
|
$context->{'vm-domain'} //= DOMAIN;
|
|
|
|
$context->{'vm-addr'} //= ADDR;
|
|
|
|
$context->{'vm-health'} //= HEALTH;
|
|
|
|
$context->{'vm-serial'} //= SERIAL;
|
|
|
|
$context->{'vm-cid'} //= CID;
|
|
|
|
$context->{'vm-vport'} //= VPORT;
|
|
|
|
$context->{'vm-vagrant'} //= VAGRANT;
|
2022-02-15 20:28:47 +01:00
|
|
|
}
|
|
|
|
|
2022-02-14 18:47:16 +01:00
|
|
|
sub main() {
|
|
|
|
my $context = make_context_from_args(@ARGV);
|
|
|
|
|
2022-02-15 20:28:47 +01:00
|
|
|
my $config = load_config("$RealBin/../config/vm-exec.json");
|
|
|
|
|
|
|
|
configure_context($context, $config);
|
|
|
|
|
2024-04-10 21:53:37 +02:00
|
|
|
if ($context->{revert} && $context->{health}) {
|
|
|
|
print STDERR "-health and -revert cannot be used together; aborting.\n";
|
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
|
pbot-vm: massive refactor
* replace heartbeat with health-check
* instead of steady stream of newlines every 5s, now awaits input
and responds with `vmstat` output
* more reliably use host/config/vm-exec.json to get libvirt domain name
for snapshot-revert, server address, serial ports, vagrant setting, etc
* use iptables/nftables to disable networking
* added guest/bin/disable-network-[iptables,nftables]
* added guest/bin/enable-network-[iptables,nftables]
* replace ugly ___OUTPUT___ texts in sh, bash, ksh, zsh languages
* documentation updates and tweaks
2024-04-10 10:21:18 +02:00
|
|
|
# instructed to revert machine
|
|
|
|
if ($context->{revert}) {
|
|
|
|
if (exists $config->{aliases}->{$context->{'vm-domain'}}) {
|
|
|
|
$context->{'vm-domain'} = $config->{aliases}->{$context->{'vm-domain'}};
|
|
|
|
}
|
|
|
|
|
|
|
|
print STDERR "REVERT $context->{'vm-domain'}\n";
|
|
|
|
|
|
|
|
if ($context->{'vm-vagrant'}) {
|
|
|
|
system("virsh -c qemu:///system snapshot-revert $context->{'vm-domain'} 1");
|
|
|
|
} else {
|
|
|
|
system("virsh snapshot-revert $context->{'vm-domain'} 1");
|
|
|
|
}
|
|
|
|
|
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
# instructed to check health
|
|
|
|
if ($context->{health}) {
|
|
|
|
my $health = IO::Socket::INET->new(
|
|
|
|
PeerAddr => $context->{'vm-addr'},
|
|
|
|
PeerPort => $context->{'vm-health'},
|
|
|
|
Proto => 'tcp',
|
|
|
|
Type => SOCK_STREAM
|
|
|
|
);
|
|
|
|
|
|
|
|
if (not defined $health) {
|
|
|
|
print STDERR "Unable to connect to health $context->{'vm-addr'} $context->{'vm-health'}\n";
|
|
|
|
exit 2;
|
|
|
|
}
|
|
|
|
|
2024-04-12 22:07:27 +02:00
|
|
|
print $health "\n"; # request health
|
pbot-vm: massive refactor
* replace heartbeat with health-check
* instead of steady stream of newlines every 5s, now awaits input
and responds with `vmstat` output
* more reliably use host/config/vm-exec.json to get libvirt domain name
for snapshot-revert, server address, serial ports, vagrant setting, etc
* use iptables/nftables to disable networking
* added guest/bin/disable-network-[iptables,nftables]
* added guest/bin/enable-network-[iptables,nftables]
* replace ugly ___OUTPUT___ texts in sh, bash, ksh, zsh languages
* documentation updates and tweaks
2024-04-10 10:21:18 +02:00
|
|
|
|
|
|
|
eval {
|
|
|
|
alarm 2;
|
|
|
|
local $SIG{ALRM} = sub { die "Health timed-out\n"; };
|
|
|
|
|
|
|
|
while (my $output = <$health>) {
|
|
|
|
last if $output eq ":END\r\n";
|
|
|
|
print $output;
|
|
|
|
}
|
|
|
|
close $health;
|
|
|
|
};
|
|
|
|
|
|
|
|
if ($@) {
|
|
|
|
print STDERR "Failed to get health: $@\n";
|
|
|
|
exit 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
|
2022-02-18 21:45:28 +01:00
|
|
|
# load language before checking usage in order to handle -lang=? flag
|
2022-02-18 21:46:33 +01:00
|
|
|
# to list languages instead of showing a usage message
|
2022-02-18 21:45:28 +01:00
|
|
|
my $lang = load_language($context);
|
|
|
|
|
|
|
|
# now check usage
|
2022-02-14 07:01:06 +01:00
|
|
|
if (not length $context->{code}) {
|
|
|
|
if (exists $context->{usage}) {
|
|
|
|
print "$context->{usage}\n";
|
|
|
|
} else {
|
2022-12-26 03:12:32 +01:00
|
|
|
print "Usage: cc [-lang=<language>] [-vm=<virtual machine>] [-info] [-paste] [-args \"command-line arguments\"] [compiler/language options] <code> [-stdin <stdin input>]\n";
|
2022-02-14 07:01:06 +01:00
|
|
|
}
|
2022-02-14 18:47:16 +01:00
|
|
|
exit 1;
|
2022-01-23 16:49:23 +01:00
|
|
|
}
|
|
|
|
|
2022-02-18 21:45:28 +01:00
|
|
|
# run the language
|
2022-02-14 07:01:06 +01:00
|
|
|
$lang->process_interactive_edit;
|
|
|
|
$lang->process_standard_options;
|
|
|
|
$lang->process_custom_options;
|
|
|
|
$lang->process_cmdline_options;
|
|
|
|
$lang->preprocess_code;
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-14 07:01:06 +01:00
|
|
|
($lang->{'vm-input'}, $lang->{'vm-output'}) = connect_vm($context);
|
|
|
|
$lang->execute;
|
|
|
|
|
|
|
|
$lang->postprocess_output;
|
|
|
|
$lang->show_output;
|
2022-02-24 19:39:15 +01:00
|
|
|
$lang->done;
|
2022-02-14 07:01:06 +01:00
|
|
|
}
|
2022-01-23 16:49:23 +01:00
|
|
|
|
2022-02-14 07:01:06 +01:00
|
|
|
main();
|