use Digest::SHA;
use HTTP::Response;
-use PVE::Tools;
-use PVE::JSONSchema qw(get_standard_option);
-use PVE::CLIHandler;
-use PVE::PTY;
+use PVE::APIClient::JSONSchema qw(get_standard_option);
+use PVE::APIClient::CLIHandler;
+use PVE::APIClient::PTY;
-use base qw(PVE::CLIHandler);
+use PVE::APIClient::Helpers;
+
+use base qw(PVE::APIClient::CLIHandler);
use PVE::APIClient::Config;
my $CRLF = "\x0D\x0A";
return $len;
};
+# Takes an escape character with an optional '^' prefix and returns an escape
+# character code.
+my $escapekey_to_char = sub {
+ my ($def) = @_;
+ if ($def =~ /^\^?([a-zA-Z])$/) {
+ return 1 + ord(lc($1)) - ord('a');
+ }
+ die "bad escape key definition: $def\n";
+};
+
__PACKAGE__->register_method ({
name => 'enter',
path => 'enter',
additionalProperties => 0,
properties => {
remote => get_standard_option('pveclient-remote-name'),
- vmid => {
- description => "The container ID",
- type => 'string',
- },
+ vmid => get_standard_option('pve-vmid')
},
},
returns => { type => 'null'},
my $config = PVE::APIClient::Config->load();
my $conn = PVE::APIClient::Config->remote_conn($config, $param->{remote});
+ # FIXME: This should come from $config
+ my $escape_char = $escapekey_to_char->('a');
+
# Get the real node from the resources endpoint
my $resource_list = $conn->get("api2/json/cluster/resources", { type => 'vm'});
my ($resource) = grep { $_->{type} eq "lxc" && $_->{vmid} eq $param->{vmid}} @$resource_list;
my $wb_socket_read_available_bytes = sub {
my $nr = $web_socket->sysread($wsbuf, $max_payload_size, length($wsbuf));
- die "web socket read error - $!\n" if $nr < 0;
+ if (!defined($nr) && !($! == EINTR || $! == EAGAIN)) {
+ die "web socket read error - $!\n";
+ }
return $nr;
};
$full_write->($web_socket, $frame);
# Send resize command
- my ($columns, $rows) = PVE::PTY::tcgetsize(*STDIN);
+ my ($columns, $rows) = PVE::APIClient::PTY::tcgetsize(*STDIN);
$frame = $create_websockt_frame->("1:$columns:$rows:");
$full_write->($web_socket, $frame);
# Set STDIN to "raw -echo" mode
- my $old_termios = PVE::PTY::tcgetattr(*STDIN);
+ my $old_termios = PVE::APIClient::PTY::tcgetattr(*STDIN);
my $raw_termios = {%$old_termios};
my $read_select = IO::Select->new;
eval {
$SIG{TERM} = $SIG{INT} = $SIG{KILL} = sub { die "received interrupt\n"; };
- PVE::PTY::cfmakeraw($raw_termios);
- PVE::PTY::tcsetattr(*STDIN, $raw_termios);
+ PVE::APIClient::PTY::cfmakeraw($raw_termios);
+ PVE::APIClient::PTY::tcsetattr(*STDIN, $raw_termios);
# And set it to non-blocking so we can every char with IO::Select.
STDIN->blocking(0);
+ STDOUT->blocking(0);
$web_socket->blocking(0);
$read_select->add($web_socket);
my $input_fh = \*STDIN;
my $output_fh = \*STDOUT;
- my $ctrl_a_pressed_before = 0;
+ my $in_escape_sequence;
my $winch_received = 0;
$SIG{WINCH} = sub { $winch_received = 1; };
my $check_terminal_size = sub {
- my ($ncols, $nrows) = PVE::PTY::tcgetsize(*STDIN);
+ my ($ncols, $nrows) = PVE::APIClient::PTY::tcgetsize(*STDIN);
if ($ncols != $columns or $nrows != $rows) {
$columns = $ncols;
$rows = $nrows;
my $len = length($$buffer_ref);
my $nr = syswrite($fh, $$buffer_ref);
if (!defined($nr)) {
- next if $! == EINTR || $! == EAGAIN;
+ return if $! == EINTR || $! == EAGAIN;
die "drain buffer - write error - $!\n";
}
return $nr if !$nr;
my $nr = $wb_socket_read_available_bytes->();
if (!defined($nr)) {
- die "web socket read error $!\n";
+ # wait
} elsif ($nr == 0) {
return; # EOF
} else {
my $char = ord($buff);
- # check for CTRL-a-q
- return if $ctrl_a_pressed_before == 1 && $char == hex("0x71");
-
- $ctrl_a_pressed_before = ($char == hex("0x01") && $ctrl_a_pressed_before == 0) ? 1 : 0;
+ # Handle escape sequences:
+ if ($in_escape_sequence) {
+ $in_escape_sequence = 0;
+ if ($char == 0x71) {
+ # (escape, 'q')
+ return;
+ } elsif ($char == $escape_char) {
+ # (escape, escape)
+ # Pass this one through as a single escapekey
+ } else {
+ # Unknown escape sequence
+ # We could generate a bell or something...
+ # but for now just skip it
+ next;
+ }
+ } elsif ($char == $escape_char) {
+ $in_escape_sequence = 1;
+ next;
+ }
+ # Pass the key through:
$websock_buffer .= $create_websockt_frame->("0:" . $nr . ":" . $buff);
$write_select->add($web_socket);
}
$full_write->(\*STDOUT, $output_buffer);
$output_buffer = '';
- PVE::PTY::tcsetattr(*STDIN, $old_termios);
+ PVE::APIClient::PTY::tcsetattr(*STDIN, $old_termios);
};
warn $@ if $@; # show cleanup errors
}});
__PACKAGE__->register_method ({
- name => 'list',
- path => 'list',
- method => 'GET',
- description => "List containers.",
+ name => 'create',
+ path => 'create',
+ method => 'POST',
+ description => "Create a container",
+ parameters => {
+ additionalProperties => 0,
+ properties => PVE::APIClient::Helpers::merge_api_definition_properties(
+ '/nodes/{node}/lxc', 'POST', {
+ remote => get_standard_option('pveclient-remote-name'),
+ vmid => get_standard_option('pve-vmid'),
+ node => get_standard_option('pve-node'),
+ quiet => {
+ description => "Suppress log output.",
+ type => 'boolean',
+ optional => 1,
+ },
+ background => {
+ description => "Do not wait for the command to complete.",
+ type => 'boolean',
+ optional => 1,
+ },
+ }),
+ },
+ returns => { type => 'null'},
+ code => sub {
+ my ($param) = @_;
+
+ my $remote = PVE::APIClient::Tools::extract_param($param, 'remote');
+ my $vmid = $param->{vmid};
+ my $node = PVE::APIClient::Tools::extract_param($param, 'node');
+
+ my $quiet = PVE::APIClient::Tools::extract_param($param, 'quiet');
+ my $background = PVE::APIClient::Tools::extract_param($param, 'background');
+
+ my $config = PVE::APIClient::Config->load();
+ my $conn = PVE::APIClient::Config->remote_conn($config, $remote);
+
+ my $upid = $conn->post("/nodes/$node/lxc", $param);
+
+ if (!$background) {
+ print PVE::APIClient::Helpers::poll_task($conn, $node, $upid, $quiet) . "\n";
+ }
+
+ return undef;
+ }});
+
+__PACKAGE__->register_method ({
+ name => 'destroy',
+ path => 'destroy',
+ method => 'DELETE',
+ description => "Destroy a container",
parameters => {
additionalProperties => 0,
properties => {
remote => get_standard_option('pveclient-remote-name'),
+ vmid => get_standard_option('pve-vmid'),
},
},
returns => { type => 'null'},
code => sub {
my ($param) = @_;
- die "implement me";
+ my $remote = PVE::APIClient::Tools::extract_param($param, 'remote');
+ my $vmid = PVE::APIClient::Tools::extract_param($param, 'vmid');
- }});
+ my $config = PVE::APIClient::Config->load();
+ my $conn = PVE::APIClient::Config->remote_conn($config, $remote);
+
+ my $resource = PVE::APIClient::Helpers::get_vmid_resource($conn, $vmid);
+ my $upid = $conn->delete("/nodes/$resource->{node}/lxc/$resource->{vmid}", $param);
+
+ print PVE::APIClient::Helpers::poll_task($conn, $resource->{node}, $upid, 1) . "\n";
+
+ return undef;
+ }});
our $cmddef = {
+ create => [ __PACKAGE__, 'create', ['remote', 'vmid', 'node']],
+ destroy => [ __PACKAGE__, 'destroy', ['remote', 'vmid']],
enter => [ __PACKAGE__, 'enter', ['remote', 'vmid']],
- list => [ __PACKAGE__, 'list', ['remote']],
};
1;