]> git.proxmox.com Git - pve-client.git/blob - PVE/APIClient/Commands/lxc.pm
lxc.pm: add test code to read config from ~/.pveclient
[pve-client.git] / PVE / APIClient / Commands / lxc.pm
1 package PVE::APIClient::Commands::lxc;
2
3 use strict;
4 use warnings;
5 use JSON;
6 use File::HomeDir;
7
8 use PVE::Tools;
9 use PVE::CLIHandler;
10
11 use base qw(PVE::CLIHandler);
12
13 my $load_config = sub {
14
15 my $filename = home() . '/.pveclient';
16 my $conf_str = PVE::Tools::file_get_contents($filename);
17
18 my $filemode = (stat($filename))[2] & 07777;
19 if ($filemode != 0600) {
20 die sprintf "wrong permissions on '$filename' %04o (expected 0600)\n", $filemode;
21 }
22
23 return decode_json($conf_str);
24 };
25
26 my $load_remote_config = sub {
27 my ($remote) = @_;
28
29 my $conf = $load_config->();
30
31 my $remote_conf = $conf->{"remote_$remote"} ||
32 die "no such remote '$remote'\n";
33
34 foreach my $opt (qw(hostname username password fingerprint)) {
35 die "missing option '$opt' (remote '$remote')" if !defined($remote_conf->{$opt});
36 }
37
38 return $remote_conf;
39 };
40
41 my $get_remote_connection = sub {
42 my ($remote) = @_;
43
44 my $conf = $load_remote_config->($remote);
45
46 return PVE::APIClient::LWP->new(
47 username => $conf->{username},
48 password => $conf->{password},
49 host => $conf->{hostname},
50 cached_fingerprints => {
51 $conf->{fingerprint} => 1
52 });
53 };
54
55
56 __PACKAGE__->register_method ({
57 name => 'enter',
58 path => 'enter',
59 method => 'POST',
60 description => "Enter container console.",
61 parameters => {
62 additionalProperties => 0,
63 properties => {
64 remote => {
65 description => "The name of the remote.",
66 type => 'string',
67 },
68 vmid => {
69 description => "The container ID",
70 type => 'string',
71 },
72 },
73 },
74 returns => { type => 'null'},
75 code => sub {
76 my ($param) = @_;
77
78 my $conn = $get_remote_connection->($param->{remote});
79 my $node = 'localhost'; # ??
80
81 my $api_path = "api2/json/nodes/$node/lxc/$param->{vmid}";
82
83 my $res = $conn->get($api_path, {});
84
85 print to_json($res, { pretty => 1, canonical => 1});
86 die "implement me";
87
88 }});
89
90 __PACKAGE__->register_method ({
91 name => 'list',
92 path => 'list',
93 method => 'GET',
94 description => "List containers.",
95 parameters => {
96 additionalProperties => 0,
97 properties => {
98 remote => {
99 description => "The remote name.",
100 type => 'string',
101 },
102 },
103 },
104 returns => { type => 'null'},
105 code => sub {
106 my ($param) = @_;
107
108 die "implement me";
109
110 }});
111
112
113 our $cmddef = {
114 enter => [ __PACKAGE__, 'enter', ['remote', 'vmid']],
115 list => [ __PACKAGE__, 'list', ['remote']],
116 };
117
118 1;