]> git.proxmox.com Git - pve-client.git/blobdiff - PVE/APIClient/Commands/lxc.pm
use packages from PVE::APIClient
[pve-client.git] / PVE / APIClient / Commands / lxc.pm
index c60017b551ebd750236568807d4b62d21e4c7024..601f86f208faaf16597ddcf9620c0df5352bb910 100644 (file)
@@ -11,12 +11,11 @@ use MIME::Base64;
 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 base qw(PVE::APIClient::CLIHandler);
 use PVE::APIClient::Config;
 
 my $CRLF = "\x0D\x0A";
@@ -149,6 +148,16 @@ my $full_write = sub {
     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',
@@ -158,10 +167,7 @@ __PACKAGE__->register_method ({
        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'},
@@ -171,6 +177,9 @@ __PACKAGE__->register_method ({
        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;
@@ -268,7 +277,7 @@ __PACKAGE__->register_method ({
 
            my $output_fh = \*STDOUT;
 
-           my $ctrl_a_pressed_before = 0;
+           my $in_escape_sequence;
 
            my $winch_received = 0;
            $SIG{WINCH} = sub { $winch_received = 1; };
@@ -292,7 +301,7 @@ __PACKAGE__->register_method ({
                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;
@@ -344,11 +353,27 @@ __PACKAGE__->register_method ({
 
                            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);
                        }
@@ -390,29 +415,8 @@ __PACKAGE__->register_method ({
        return undef;
     }});
 
-__PACKAGE__->register_method ({
-    name => 'list',
-    path => 'list',
-    method => 'GET',
-    description => "List containers.",
-    parameters => {
-       additionalProperties => 0,
-       properties => {
-           remote => get_standard_option('pveclient-remote-name'),
-       },
-    },
-    returns => { type => 'null'},
-    code => sub {
-       my ($param) = @_;
-
-       die "implement me";
-
-    }});
-
-
 our $cmddef = {
     enter => [ __PACKAGE__, 'enter', ['remote', 'vmid']],
-    list => [ __PACKAGE__, 'list', ['remote']],
 };
 
 1;