]>
Commit | Line | Data |
---|---|---|
87952a8d DM |
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); | |
d34cada7 | 13 | use PVE::Subscription; |
87952a8d DM |
14 | |
15 | use PMG::Utils; | |
d34cada7 | 16 | use PMG::Config; |
87952a8d DM |
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 | ||
d34cada7 DM |
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 | ||
87952a8d DM |
37 | sub read_etc_pmg_subscription { |
38 | my ($filename, $fh) = @_; | |
39 | ||
d34cada7 DM |
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 | } | |
87952a8d DM |
48 | |
49 | return $info; | |
d34cada7 | 50 | }; |
87952a8d DM |
51 | |
52 | sub write_etc_pmg_subscription { | |
53 | my ($filename, $fh, $info) = @_; | |
54 | ||
d34cada7 | 55 | my $server_id = PMG::Utils::get_hwaddress(); |
87952a8d | 56 | |
d34cada7 | 57 | PVE::Subscription::write_subscription($server_id, $filename, $fh, $info); |
87952a8d DM |
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; | |
87952a8d DM |
90 | $info->{url} = $url; |
91 | ||
92 | return $info | |
93 | }}); | |
94 | ||
d34cada7 DM |
95 | __PACKAGE__->register_method ({ |
96 | name => 'update', | |
97 | path => '', | |
98 | method => 'POST', | |
99 | description => "Update subscription info.", | |
100 | proxyto => 'node', | |
101 | protected => 1, | |
b894a99a | 102 | permissions => { check => [ 'admin' ] }, |
d34cada7 DM |
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 | ||
87952a8d | 191 | 1; |