]> git.proxmox.com Git - pve-container.git/blob - src/pct
split PVE::API2::LXC into smaller parts
[pve-container.git] / src / pct
1 #!/usr/bin/perl -T
2
3 use strict;
4 use warnings;
5 use lib qw(. ..);
6
7 use PVE::SafeSyslog;
8 use PVE::Tools qw(extract_param);
9 use PVE::Cluster;
10 use PVE::INotify;
11 use PVE::RPCEnvironment;
12 use PVE::JSONSchema qw(get_standard_option);
13 use PVE::CLIHandler;
14 use PVE::API2::LXC;
15 use PVE::API2::LXC::Config;
16 use PVE::API2::LXC::Status;
17 use PVE::API2::LXC::Snapshot;
18
19 use Data::Dumper;
20
21 use base qw(PVE::CLIHandler);
22
23 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
24
25 initlog ('pct');
26
27 die "please run as root\n" if $> != 0;
28
29 PVE::INotify::inotify_init();
30
31 my $rpcenv = PVE::RPCEnvironment->init('cli');
32 $rpcenv->init_request();
33 $rpcenv->set_language($ENV{LANG});
34 $rpcenv->set_user('root@pam');
35
36 my $nodename = PVE::INotify::nodename();
37
38 my $upid_exit = sub {
39 my $upid = shift;
40 my $status = PVE::Tools::upid_read_status($upid);
41 exit($status eq 'OK' ? 0 : -1);
42 };
43
44 __PACKAGE__->register_method ({
45 name => 'console',
46 path => 'console',
47 method => 'GET',
48 description => "Launch a console for the specified container.",
49 parameters => {
50 additionalProperties => 0,
51 properties => {
52 vmid => get_standard_option('pve-vmid'),
53 },
54 },
55 returns => { type => 'null' },
56
57 code => sub {
58 my ($param) = @_;
59
60 # test if container exists on this node
61 my $conf = PVE::LXC::load_config($param->{vmid});
62
63 my $cmd = PVE::LXC::get_console_command($param->{vmid}, $conf);
64 exec(@$cmd);
65 }});
66
67 __PACKAGE__->register_method ({
68 name => 'enter',
69 path => 'enter',
70 method => 'GET',
71 description => "Launch a shell for the specified container.",
72 parameters => {
73 additionalProperties => 0,
74 properties => {
75 vmid => get_standard_option('pve-vmid'),
76 },
77 },
78 returns => { type => 'null' },
79
80 code => sub {
81 my ($param) = @_;
82
83 # test if container exists on this node
84 PVE::LXC::load_config($param->{vmid});
85
86 exec('lxc-attach', '-n', $param->{vmid});
87 }});
88
89 my $cmddef = {
90 #test => [ __PACKAGE__, 'test', [], {}, sub {} ],
91 list=> [ 'PVE::API2::LXC', 'vmlist', [], { node => $nodename }, sub {
92 my $res = shift;
93 return if !scalar(@$res);
94 my $format = "%-10s %-10s %-20s\n";
95 printf($format, 'VMID', 'Status', 'Name');
96 foreach my $d (sort {$a->{vmid} <=> $b->{vmid} } @$res) {
97 printf($format, $d->{vmid}, $d->{status}, $d->{name});
98 }
99 }],
100 config => [ "PVE::API2::LXC::Config", 'vm_config', ['vmid'],
101 { node => $nodename }, sub {
102 my $config = shift;
103 foreach my $k (sort (keys %$config)) {
104 next if $k eq 'digest';
105 my $v = $config->{$k};
106 if ($k eq 'description') {
107 $v = PVE::Tools::encode_text($v);
108 }
109 print "$k: $v\n";
110 }
111 }],
112 set => [ 'PVE::API2::LXC::Config', 'update_vm', ['vmid'], { node => $nodename }],
113
114 create => [ 'PVE::API2::LXC', 'create_vm', ['vmid', 'ostemplate'], { node => $nodename }, $upid_exit ],
115 restore => [ 'PVE::API2::LXC', 'create_vm', ['vmid', 'ostemplate'], { node => $nodename, restore => 1 }, $upid_exit ],
116
117 start => [ 'PVE::API2::LXC::Status', 'vm_start', ['vmid'], { node => $nodename }, $upid_exit],
118 suspend => [ 'PVE::API2::LXC::Status', 'vm_suspend', ['vmid'], { node => $nodename }, $upid_exit],
119 resume => [ 'PVE::API2::LXC::Status', 'vm_resume', ['vmid'], { node => $nodename }, $upid_exit],
120 shutdown => [ 'PVE::API2::LXC::Status', 'vm_shutdown', ['vmid'], { node => $nodename }, $upid_exit],
121 stop => [ 'PVE::API2::LXC::Status', 'vm_stop', ['vmid'], { node => $nodename }, $upid_exit],
122
123 migrate => [ "PVE::API2::LXC", 'migrate_vm', ['vmid', 'target'], { node => $nodename }, $upid_exit],
124
125 console => [ __PACKAGE__, 'console', ['vmid']],
126 enter => [ __PACKAGE__, 'enter', ['vmid']],
127
128 destroy => [ 'PVE::API2::LXC', 'destroy_vm', ['vmid'],
129 { node => $nodename }, $upid_exit ],
130
131 snapshot => [ "PVE::API2::LXC::Snapshot", 'snapshot', ['vmid', 'snapname'],
132 { node => $nodename } , $upid_exit ],
133
134 delsnapshot => [ "PVE::API2::LXC::Snapshot", 'delsnapshot', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ],
135
136 rollback => [ "PVE::API2::LXC::Snapshot", 'rollback', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ],
137
138 template => [ "PVE::API2::LXC", 'template', ['vmid'], { node => $nodename }],
139 };
140
141 my $cmd = shift;
142
143 PVE::CLIHandler::handle_cmd($cmddef, "pct", $cmd, \@ARGV, undef, $0);
144
145 exit 0;
146
147 __END__
148
149 =head1 NAME
150
151 pct - Tool to manage Linux Containers on Proxmox VE
152
153 =head1 SYNOPSIS
154
155 =include synopsis
156
157 =head1 DESCRIPTION
158
159 Tool to manage linux containers.
160
161 =include pve_copyright