]> git.proxmox.com Git - pmg-api.git/blob - PMG/API2/Subscription.pm
fix bux #1776: set http_proxy for sa-update
[pmg-api.git] / PMG / API2 / Subscription.pm
1 package PMG::API2::Subscription;
2
3 use strict;
4 use warnings;
5
6 use PVE::Tools;
7 use PVE::SafeSyslog;
8 use PVE::INotify;
9 use PVE::Exception qw(raise_param_exc);
10 use PVE::RESTHandler;
11 use PMG::RESTEnvironment;
12 use PVE::JSONSchema qw(get_standard_option);
13 use PVE::Subscription;
14
15 use PMG::Utils;
16 use PMG::Config;
17
18 use base qw(PVE::RESTHandler);
19
20 PVE::INotify::register_file('subscription', "/etc/pmg/subscription",
21 \&read_etc_pmg_subscription,
22 \&write_etc_pmg_subscription);
23
24 my $subscription_pattern = 'pmg([cbsp])-[0-9a-f]{10}';
25
26 sub parse_key {
27 my ($key, $noerr) = @_;
28
29 if ($key =~ m/^${subscription_pattern}$/) {
30 return $1 # subscription level
31 }
32 return undef if $noerr;
33
34 die "Wrong subscription key format\n";
35 }
36
37 sub read_etc_pmg_subscription {
38 my ($filename, $fh) = @_;
39
40 my $server_id = PMG::Utils::get_hwaddress();
41
42 my $info = PVE::Subscription::read_subscription($server_id, $filename, $fh);
43 my $level = parse_key($info->{key});
44
45 if ($info->{status} eq 'Active') {
46 $info->{level} = $level;
47 }
48
49 return $info;
50 };
51
52 sub write_etc_pmg_subscription {
53 my ($filename, $fh, $info) = @_;
54
55 my $server_id = PMG::Utils::get_hwaddress();
56
57 PVE::Subscription::write_subscription($server_id, $filename, $fh, $info);
58 }
59
60 __PACKAGE__->register_method ({
61 name => 'get',
62 path => '',
63 method => 'GET',
64 description => "Read subscription info.",
65 proxyto => 'node',
66 permissions => { check => [ 'admin', 'qmanager', 'audit', 'quser'] },
67 parameters => {
68 additionalProperties => 0,
69 properties => {
70 node => get_standard_option('pve-node'),
71 },
72 },
73 returns => { type => 'object'},
74 code => sub {
75 my ($param) = @_;
76
77 my $server_id = PMG::Utils::get_hwaddress();
78 my $url = "https://www.proxmox.com/en/proxmox-mail-gateway/pricing";
79 my $info = PVE::INotify::read_file('subscription');
80 if (!$info) {
81 return {
82 status => "NotFound",
83 message => "There is no subscription key",
84 serverid => $server_id,
85 url => $url,
86 }
87 }
88
89 $info->{serverid} = $server_id;
90 $info->{url} = $url;
91
92 return $info
93 }});
94
95 __PACKAGE__->register_method ({
96 name => 'update',
97 path => '',
98 method => 'POST',
99 description => "Update subscription info.",
100 proxyto => 'node',
101 protected => 1,
102 permissions => { check => [ 'admin' ] },
103 parameters => {
104 additionalProperties => 0,
105 properties => {
106 node => get_standard_option('pve-node'),
107 force => {
108 description => "Always connect to server, even if we have up to date info inside local cache.",
109 type => 'boolean',
110 optional => 1,
111 default => 0
112 }
113 },
114 },
115 returns => { type => 'null'},
116 code => sub {
117 my ($param) = @_;
118
119 my $info = PVE::INotify::read_file('subscription');
120 return undef if !$info;
121
122 my $server_id = PMG::Utils::get_hwaddress();
123 my $key = $info->{key};
124
125 if ($key) {
126 PVE::Subscription::update_apt_auth($key, $server_id);
127 }
128
129 if (!$param->{force} && $info->{status} eq 'Active') {
130 my $age = time() - $info->{checktime};
131 return undef if $age < $PVE::Subscription::localkeydays*60*60*24;
132 }
133
134 my $pmg_cfg = PMG::Config->new();
135 my $proxy = $pmg_cfg->get('admin', 'http_proxy');
136
137 $info = PVE::Subscription::check_subscription($key, $server_id, $proxy);
138
139 PVE::INotify::write_file('subscription', $info);
140
141 return undef;
142 }});
143
144 __PACKAGE__->register_method ({
145 name => 'set',
146 path => '',
147 method => 'PUT',
148 description => "Set subscription key.",
149 proxyto => 'node',
150 protected => 1,
151 parameters => {
152 additionalProperties => 0,
153 properties => {
154 node => get_standard_option('pve-node'),
155 key => {
156 description => "Proxmox Mail Gateway subscription key",
157 type => 'string',
158 pattern => $subscription_pattern,
159 maxLength => 32,
160 },
161 },
162 },
163 returns => { type => 'null'},
164 code => sub {
165 my ($param) = @_;
166
167 my $key = PVE::Tools::trim($param->{key});
168
169 my $level = parse_key($key);
170
171 my $info = {
172 status => 'New',
173 key => $key,
174 checktime => time(),
175 };
176
177 my $server_id = PMG::Utils::get_hwaddress();
178
179 PVE::INotify::write_file('subscription', $info);
180
181 my $pmg_cfg = PMG::Config->new();
182 my $proxy = $pmg_cfg->get('admin', 'http_proxy');
183
184 $info = PVE::Subscription::check_subscription($key, $server_id, $proxy);
185
186 PVE::INotify::write_file('subscription', $info);
187
188 return undef;
189 }});
190
191 1;