]> 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 f0c85f76a1e65086ff29c4ce909cc1f3f017176a..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;
@@ -202,7 +211,9 @@ __PACKAGE__->register_method ({
 
        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;
        };
 
@@ -244,7 +255,11 @@ __PACKAGE__->register_method ({
        my $old_termios = PVE::PTY::tcgetattr(*STDIN);
        my $raw_termios = {%$old_termios};
 
-       my $select = IO::Select->new;
+       my $read_select = IO::Select->new;
+       my $write_select = IO::Select->new;
+
+       my $output_buffer = ''; # write buffer for STDOUT
+       my $websock_buffer = ''; # write buffer for $web_socket
 
        eval {
            $SIG{TERM} = $SIG{INT} = $SIG{KILL} = sub { die "received interrupt\n"; };
@@ -254,13 +269,15 @@ __PACKAGE__->register_method ({
 
            # 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;
+           $read_select->add($input_fh);
 
-           $web_socket->blocking(1);
-           $select->add($web_socket);
-           my $input_fh = fileno(STDIN);
-           $select->add($input_fh);
+           my $output_fh = \*STDOUT;
 
-           my $ctrl_a_pressed_before = 0;
+           my $in_escape_sequence;
 
            my $winch_received = 0;
            $SIG{WINCH} = sub { $winch_received = 1; };
@@ -270,30 +287,60 @@ __PACKAGE__->register_method ({
                if ($ncols != $columns or $nrows != $rows) {
                    $columns = $ncols;
                    $rows = $nrows;
-                   $frame = $create_websockt_frame->("1:$columns:$rows:");
-                   $full_write->($web_socket, $frame);
+                   $websock_buffer .= $create_websockt_frame->("1:$columns:$rows:");
+                   $write_select->add($web_socket);
                }
                $winch_received = 0;
            };
 
+           my $max_buffer_len = 256*1024;
+
+           my $drain_buffer = sub {
+               my ($fh, $buffer_ref) = @_;
+
+               my $len = length($$buffer_ref);
+               my $nr = syswrite($fh, $$buffer_ref);
+               if (!defined($nr)) {
+                   return if $! == EINTR || $! == EAGAIN;
+                   die "drain buffer - write error - $!\n";
+               }
+               return $nr if !$nr;
+               substr($$buffer_ref, 0, $nr, '');
+               $len = length($$buffer_ref);
+               $write_select->remove($fh) if !$len;
+           };
+
            while (1) {
-               while(my @ready = $select->can_read(3)) {
+               while(my ($readable, $writable) = IO::Select->select($read_select, $write_select, undef, 3)) {
                    $check_terminal_size->() if $winch_received;
 
-                   foreach my $fh (@ready) {
+                   foreach my $fh (@$writable) {
+                       if ($fh == $output_fh) {
+                           $drain_buffer->($output_fh, \$output_buffer);
+                           $read_select->add($web_socket) if length($output_buffer) <= $max_buffer_len;
+                       } elsif ($fh == $web_socket) {
+                           $drain_buffer->($web_socket, \$websock_buffer);
+                       }
+                   }
+
+                   foreach my $fh (@$readable) {
 
                        if ($fh == $web_socket) {
                            # Read from WebSocket
 
                            my $nr = $wb_socket_read_available_bytes->();
                            if (!defined($nr)) {
-                               die "web socket read error $!\n";
+                               # wait
                            } elsif ($nr == 0) {
                                return; # EOF
                            } else {
                                my ($payload, $req_close) = $parse_web_socket_frame->(\$wsbuf);
-                               if ($payload) {
-                                   $full_write->(\*STDOUT, $payload);
+                               if (defined($payload) && length($payload)) {
+                                   $output_buffer .= $payload;
+                                   $write_select->add($output_fh);
+                                   if (length($output_buffer) > $max_buffer_len) {
+                                       $read_select->remove($web_socket);
+                                   }
                                }
                                return if $req_close;
                            }
@@ -301,25 +348,42 @@ __PACKAGE__->register_method ({
                        } elsif ($fh == $input_fh) {
                            # Read from STDIN
 
-                           my $nr = read(\*STDIN, my $buff, 4096);
+                           my $nr = sysread($input_fh, my $buff, 4096);
                            return if !$nr; # EOF or error
 
                            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;
+                           }
 
-                           my $frame = $create_websockt_frame->("0:" . $nr . ":" . $buff);
-                           $full_write->($web_socket, $frame);
+                           # Pass the key through:
+                           $websock_buffer .= $create_websockt_frame->("0:" . $nr . ":" . $buff);
+                           $write_select->add($web_socket);
                        }
                    }
                }
                $check_terminal_size->() if $winch_received;
 
                # got timeout
-               $full_write->($web_socket, $create_websockt_frame->("2")); # ping server to keep connection alive
+               $websock_buffer .= $create_websockt_frame->("2"); # ping server to keep connection alive
+               $write_select->add($web_socket);
            }
        };
        my $err = $@;
@@ -331,13 +395,17 @@ __PACKAGE__->register_method ({
 
            if ($web_socket->connected) {
                # close connection
-               my $msg = "\x88" . pack('N', 0) . pack('n', 0); # Opcode, mask, statuscode
-               $full_write->($web_socket, $msg);
+               $websock_buffer .= "\x88" . pack('N', 0) . pack('n', 0); # Opcode, mask, statuscode
+               $full_write->($web_socket, $websock_buffer);
+               $websock_buffer = '';
                close($web_socket);
            }
 
            # Reset the terminal parameters.
-           $full_write->(\*STDOUT, "\e[24H\r\n");
+           $output_buffer .= "\e[24H\r\n";
+           $full_write->(\*STDOUT, $output_buffer);
+           $output_buffer = '';
+
            PVE::PTY::tcsetattr(*STDIN, $old_termios);
        };
        warn $@ if $@; # show cleanup errors
@@ -347,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;