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