]> git.proxmox.com Git - pve-manager.git/blame - PVE/API2/Subscription.pm
cli: subscription: simplify printing output in get command
[pve-manager.git] / PVE / API2 / Subscription.pm
CommitLineData
21ace8d3
DM
1package PVE::API2::Subscription;
2
3use strict;
4use warnings;
5use Digest::MD5 qw(md5_hex md5_base64);
6use MIME::Base64;
7use HTTP::Request;
8use LWP::UserAgent;
43fc27a4 9use JSON;
21ace8d3 10
d017de1f
FG
11use Proxmox::RS::Subscription;
12
21ace8d3 13use PVE::Tools;
8f9217dc 14use PVE::ProcFSTools;
21ace8d3
DM
15use PVE::Exception qw(raise_param_exc);
16use PVE::INotify;
17use PVE::Cluster qw (cfs_read_file cfs_write_file);
3ac3653e 18use PVE::DataCenterConfig;
21ace8d3
DM
19use PVE::AccessControl;
20use PVE::Storage;
21use PVE::JSONSchema qw(get_standard_option);
22
23use PVE::SafeSyslog;
24
25use PVE::API2Tools;
26use PVE::RESTHandler;
27
28use base qw(PVE::RESTHandler);
29
9fdfebf7 30my $subscription_pattern = 'pve([1248])([cbsp])-[0-9a-f]{10}';
d017de1f 31my $filename = "/etc/subscription";
21ace8d3 32
21ace8d3 33sub get_sockets {
8f9217dc
DM
34 my $info = PVE::ProcFSTools::read_cpuinfo();
35 return $info->{sockets};
21ace8d3
DM
36}
37
38sub parse_key {
43fc27a4 39 my ($key, $noerr) = @_;
21ace8d3 40
43fc27a4 41 if ($key =~ m/^${subscription_pattern}$/) {
16b69b6c 42 return wantarray ? ($1, $2) : $1; # number of sockets, level
21ace8d3 43 }
43fc27a4
DM
44 return undef if $noerr;
45
46 die "Wrong subscription key format\n";
21ace8d3
DM
47}
48
43fc27a4
DM
49sub check_key {
50 my ($key, $req_sockets) = @_;
21ace8d3 51
43fc27a4 52 my ($sockets, $level) = parse_key($key);
21ace8d3
DM
53 if ($sockets < $req_sockets) {
54 die "wrong number of sockets ($sockets < $req_sockets)\n";
55 }
43fc27a4 56 return ($sockets, $level);
21ace8d3
DM
57}
58
d017de1f 59sub read_etc_subscription {
43fc27a4
DM
60 my $req_sockets = get_sockets();
61 my $server_id = PVE::API2Tools::get_hwaddress();
21ace8d3 62
d017de1f 63 my $info = Proxmox::RS::Subscription::read_subscription($filename);
21ace8d3 64
25eaf729 65 return $info if !$info || $info->{status} ne 'active';
21ace8d3 66
43fc27a4
DM
67 my ($sockets, $level);
68 eval { ($sockets, $level) = check_key($info->{key}, $req_sockets); };
69 if (my $err = $@) {
70 chomp $err;
d017de1f 71 $info->{status} = 'invalid';
43fc27a4
DM
72 $info->{message} = $err;
73 } else {
16b69b6c
DM
74 $info->{level} = $level;
75 }
76
21ace8d3
DM
77 return $info;
78}
79
fc3e061b
TL
80my sub cache_is_valid {
81 my ($info) = @_;
82
83 return if !$info || $info->{status} ne 'active';
84
85 my $checked_info = Proxmox::RS::Subscription::check_age($info, 1);
86 return $checked_info->{status} eq 'active'
87}
88
d017de1f
FG
89sub write_etc_subscription {
90 my ($info) = @_;
21ace8d3 91
2ba6d822 92 my $server_id = PVE::API2Tools::get_hwaddress();
d017de1f 93 mkdir "/etc/apt/auth.conf.d";
76a165a1
TL
94 Proxmox::RS::Subscription::write_subscription(
95 $filename, "/etc/apt/auth.conf.d/pve.conf", "enterprise.proxmox.com/debian/pve", $info);
21ace8d3
DM
96}
97
98__PACKAGE__->register_method ({
43fc27a4
DM
99 name => 'get',
100 path => '',
21ace8d3
DM
101 method => 'GET',
102 description => "Read subscription info.",
103 proxyto => 'node',
e721a829 104 permissions => { user => 'all' },
21ace8d3
DM
105 parameters => {
106 additionalProperties => 0,
107 properties => {
108 node => get_standard_option('pve-node'),
109 },
110 },
111 returns => { type => 'object'},
112 code => sub {
113 my ($param) = @_;
114
7d6fba8f 115 my $node = $param->{node};
00a93a4b 116
2d2ed7ab
DM
117 my $rpcenv = PVE::RPCEnvironment::get();
118 my $authuser = $rpcenv->get_user();
d8645329 119 my $has_permission = $rpcenv->check($authuser, "/nodes/$node", ['Sys.Audit'], 1);
2d2ed7ab 120
7d762f4c 121 my $server_id = PVE::API2Tools::get_hwaddress();
93580fec 122 my $url = "https://www.proxmox.com/proxmox-ve/pricing";
7d762f4c 123
d017de1f 124 my $info = read_etc_subscription();
21ace8d3 125 if (!$info) {
2d2ed7ab 126 my $no_subscription_info = {
d017de1f 127 status => "notfound",
21ace8d3 128 message => "There is no subscription key",
2d2ed7ab
DM
129 url => $url,
130 };
131 $no_subscription_info->{serverid} = $server_id if $has_permission;
132 return $no_subscription_info;
133 }
134
135 if (!$has_permission) {
136 return {
137 status => $info->{status},
138 message => $info->{message},
43fc27a4 139 url => $url,
21ace8d3
DM
140 }
141 }
00a93a4b
DM
142
143 $info->{serverid} = $server_id;
144 $info->{sockets} = get_sockets();
43fc27a4 145 $info->{url} = $url;
00a93a4b 146
21ace8d3
DM
147 return $info
148 }});
149
150__PACKAGE__->register_method ({
43fc27a4
DM
151 name => 'update',
152 path => '',
21ace8d3 153 method => 'POST',
69bbb885
TL
154 permissions => {
155 check => ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
156 },
21ace8d3
DM
157 description => "Update subscription info.",
158 proxyto => 'node',
159 protected => 1,
160 parameters => {
161 additionalProperties => 0,
162 properties => {
163 node => get_standard_option('pve-node'),
164 force => {
76a165a1 165 description => "Always connect to server, even if local cache is still valid.",
21ace8d3
DM
166 type => 'boolean',
167 optional => 1,
168 default => 0
169 }
170 },
171 },
172 returns => { type => 'null'},
173 code => sub {
174 my ($param) = @_;
175
d017de1f 176 my $info = read_etc_subscription();
21ace8d3
DM
177 return undef if !$info;
178
43fc27a4
DM
179 my $server_id = PVE::API2Tools::get_hwaddress();
180 my $key = $info->{key};
181
d4df1b14
FG
182 die "Updating offline key not possible - please remove and re-add subscription key to switch to online key.\n"
183 if $info->{signature};
184
fc3e061b 185 return undef if !$param->{force} && cache_is_valid($info); # key has been recently checked
21ace8d3 186
43fc27a4
DM
187 my $req_sockets = get_sockets();
188 check_key($key, $req_sockets);
189
190 my $dccfg = PVE::Cluster::cfs_read_file('datacenter.cfg');
191 my $proxy = $dccfg->{http_proxy};
192
d017de1f 193 $info = Proxmox::RS::Subscription::check_subscription($key, $server_id, "", "Proxmox VE", $proxy);
21ace8d3 194
d017de1f 195 write_etc_subscription($info);
21ace8d3
DM
196
197 return undef;
198 }});
199
200__PACKAGE__->register_method ({
43fc27a4
DM
201 name => 'set',
202 path => '',
21ace8d3 203 method => 'PUT',
69bbb885
TL
204 permissions => {
205 check => ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
206 },
21ace8d3
DM
207 description => "Set subscription key.",
208 proxyto => 'node',
209 protected => 1,
210 parameters => {
e82ed167 211 additionalProperties => 0,
21ace8d3
DM
212 properties => {
213 node => get_standard_option('pve-node'),
214 key => {
215 description => "Proxmox VE subscription key",
216 type => 'string',
43fc27a4
DM
217 pattern => $subscription_pattern,
218 maxLength => 32,
21ace8d3
DM
219 },
220 },
221 },
222 returns => { type => 'null'},
223 code => sub {
224 my ($param) = @_;
225
43fc27a4 226 my $key = PVE::Tools::trim($param->{key});
94cc9560 227
76a165a1 228 my $new_info = {
21ace8d3 229 status => 'New',
43fc27a4 230 key => $key,
21ace8d3
DM
231 checktime => time(),
232 };
233
234 my $req_sockets = get_sockets();
2ba6d822 235 my $server_id = PVE::API2Tools::get_hwaddress();
21ace8d3 236
43fc27a4 237 check_key($key, $req_sockets);
21ace8d3 238
76a165a1 239 write_etc_subscription($new_info);
21ace8d3 240
43fc27a4
DM
241 my $dccfg = PVE::Cluster::cfs_read_file('datacenter.cfg');
242 my $proxy = $dccfg->{http_proxy};
243
76a165a1
TL
244 my $checked_info = Proxmox::RS::Subscription::check_subscription(
245 $key, $server_id, "", "Proxmox VE", $proxy);
21ace8d3 246
76a165a1 247 write_etc_subscription($checked_info);
21ace8d3
DM
248
249 return undef;
250 }});
251
85222f82
MA
252__PACKAGE__->register_method ({
253 name => 'delete',
254 path => '',
255 method => 'DELETE',
256 permissions => {
257 check => ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
258 },
bc12fba5 259 description => "Delete subscription key of this node.",
85222f82
MA
260 proxyto => 'node',
261 protected => 1,
262 parameters => {
e82ed167 263 additionalProperties => 0,
85222f82
MA
264 properties => {
265 node => get_standard_option('pve-node'),
266 },
267 },
268 returns => { type => 'null'},
269 code => sub {
270 my $subscription_file = '/etc/subscription';
e82ed167 271 return if ! -e $subscription_file;
85222f82
MA
272 unlink($subscription_file) or die "cannot delete subscription key: $!";
273 return undef;
274 }});
275
21ace8d3 2761;