]> git.proxmox.com Git - pve-container.git/blob - src/PVE/API2/LXC/Config.pm
Refactor config-related methods into AbstractConfig
[pve-container.git] / src / PVE / API2 / LXC / Config.pm
1 package PVE::API2::LXC::Config;
2
3 use strict;
4 use warnings;
5
6 use PVE::SafeSyslog;
7 use PVE::Tools qw(extract_param run_command);
8 use PVE::Exception qw(raise raise_param_exc);
9 use PVE::INotify;
10 use PVE::Cluster qw(cfs_read_file);
11 use PVE::AccessControl;
12 use PVE::Firewall;
13 use PVE::Storage;
14 use PVE::RESTHandler;
15 use PVE::RPCEnvironment;
16 use PVE::LXC;
17 use PVE::LXC::Create;
18 use PVE::JSONSchema qw(get_standard_option);
19 use base qw(PVE::RESTHandler);
20
21 use Data::Dumper; # fixme: remove
22
23 __PACKAGE__->register_method({
24 name => 'vm_config',
25 path => '',
26 method => 'GET',
27 proxyto => 'node',
28 description => "Get container configuration.",
29 permissions => {
30 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
31 },
32 parameters => {
33 additionalProperties => 0,
34 properties => {
35 node => get_standard_option('pve-node'),
36 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }),
37 },
38 },
39 returns => {
40 type => "object",
41 properties => {
42 digest => {
43 type => 'string',
44 description => 'SHA1 digest of configuration file. This can be used to prevent concurrent modifications.',
45 }
46 },
47 },
48 code => sub {
49 my ($param) = @_;
50
51 my $conf = PVE::LXC::Config->load_config($param->{vmid});
52
53 delete $conf->{snapshots};
54 delete $conf->{lxc};
55
56 return $conf;
57 }});
58
59 my $vm_config_perm_list = [
60 'VM.Config.Disk',
61 'VM.Config.CPU',
62 'VM.Config.Memory',
63 'VM.Config.Network',
64 'VM.Config.Options',
65 ];
66
67 __PACKAGE__->register_method({
68 name => 'update_vm',
69 path => '',
70 method => 'PUT',
71 protected => 1,
72 proxyto => 'node',
73 description => "Set container options.",
74 permissions => {
75 check => ['perm', '/vms/{vmid}', $vm_config_perm_list, any => 1],
76 },
77 parameters => {
78 additionalProperties => 0,
79 properties => PVE::LXC::json_config_properties(
80 {
81 node => get_standard_option('pve-node'),
82 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }),
83 delete => {
84 type => 'string', format => 'pve-configid-list',
85 description => "A list of settings you want to delete.",
86 optional => 1,
87 },
88 digest => {
89 type => 'string',
90 description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
91 maxLength => 40,
92 optional => 1,
93 }
94 }),
95 },
96 returns => { type => 'null'},
97 code => sub {
98 my ($param) = @_;
99
100 my $rpcenv = PVE::RPCEnvironment::get();
101
102 my $authuser = $rpcenv->get_user();
103
104 my $node = extract_param($param, 'node');
105
106 my $vmid = extract_param($param, 'vmid');
107
108 my $digest = extract_param($param, 'digest');
109
110 die "no options specified\n" if !scalar(keys %$param);
111
112 my $delete_str = extract_param($param, 'delete');
113 my @delete = PVE::Tools::split_list($delete_str);
114
115 PVE::LXC::check_ct_modify_config_perm($rpcenv, $authuser, $vmid, undef, {}, [@delete]);
116
117 foreach my $opt (@delete) {
118 raise_param_exc({ delete => "you can't use '-$opt' and " .
119 "-delete $opt' at the same time" })
120 if defined($param->{$opt});
121
122 if (!PVE::LXC::option_exists($opt)) {
123 raise_param_exc({ delete => "unknown option '$opt'" });
124 }
125 }
126
127 PVE::LXC::check_ct_modify_config_perm($rpcenv, $authuser, $vmid, undef, $param, []);
128
129 my $storage_cfg = cfs_read_file("storage.cfg");
130
131 my $code = sub {
132
133 my $conf = PVE::LXC::Config->load_config($vmid);
134 PVE::LXC::Config->check_lock($conf);
135
136 PVE::Tools::assert_if_modified($digest, $conf->{digest});
137
138 my $running = PVE::LXC::check_running($vmid);
139
140 PVE::LXC::update_pct_config($vmid, $conf, $running, $param, \@delete);
141
142 PVE::LXC::Config->write_config($vmid, $conf);
143 PVE::LXC::update_lxc_config($storage_cfg, $vmid, $conf);
144 };
145
146 PVE::LXC::Config->lock_config($vmid, $code);
147
148 return undef;
149 }});
150
151 1;