]> git.proxmox.com Git - pve-client.git/blobdiff - PVE/APIClient/Commands/lxc.pm
Add create and destroy subcommands to the lxc command
[pve-client.git] / PVE / APIClient / Commands / lxc.pm
index 66ad704d954fc01a00afc3cf5860edda0fb19b42..2309ec04e359fafc19df2f0ff8e63b86d9e32443 100644 (file)
@@ -11,12 +11,13 @@ 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 PVE::APIClient::Helpers;
+
+use base qw(PVE::APIClient::CLIHandler);
 use PVE::APIClient::Config;
 
 my $CRLF = "\x0D\x0A";
@@ -168,10 +169,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'},
@@ -251,12 +249,12 @@ __PACKAGE__->register_method ({
        $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;
@@ -268,8 +266,8 @@ __PACKAGE__->register_method ({
        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);
@@ -287,7 +285,7 @@ __PACKAGE__->register_method ({
            $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;
@@ -410,7 +408,7 @@ __PACKAGE__->register_method ({
            $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
 
@@ -419,7 +417,72 @@ __PACKAGE__->register_method ({
        return undef;
     }});
 
+__PACKAGE__->register_method ({
+    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'),
+       }),
+    },
+    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 $config = PVE::APIClient::Config->load();
+       my $conn = PVE::APIClient::Config->remote_conn($config, $remote);
+
+       my $upid = $conn->post("/nodes/$node/lxc", $param);
+
+       print PVE::APIClient::Helpers::poll_task($conn, $node, $upid) . "\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) = @_;
+
+       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) . "\n";
+
+       return undef;
+    }});
+
 our $cmddef = {
+    create => [ __PACKAGE__, 'create', ['remote', 'vmid', 'node']],
+    destroy => [ __PACKAGE__, 'destroy', ['remote', 'vmid']],
     enter => [ __PACKAGE__, 'enter', ['remote', 'vmid']],
 };