]> git.proxmox.com Git - pve-manager.git/blob - PVE/CLI/pvenode.pm
api2: network: improve code readability
[pve-manager.git] / PVE / CLI / pvenode.pm
1 package PVE::CLI::pvenode;
2
3 use strict;
4 use warnings;
5
6 use PVE::API2::ACME;
7 use PVE::API2::ACMEAccount;
8 use PVE::API2::ACMEPlugin;
9 use PVE::API2::Certificates;
10 use PVE::API2::NodeConfig;
11 use PVE::API2::Nodes;
12 use PVE::API2::Tasks;
13
14 use PVE::ACME::Challenge;
15 use PVE::CertHelpers;
16 use PVE::Certificate;
17 use PVE::Exception qw(raise_param_exc raise);
18 use PVE::JSONSchema qw(get_standard_option);
19 use PVE::NodeConfig;
20 use PVE::RPCEnvironment;
21 use PVE::CLIFormatter;
22 use PVE::RESTHandler;
23 use PVE::CLIHandler;
24
25 use Term::ReadLine;
26
27 use base qw(PVE::CLIHandler);
28
29 my $nodename = PVE::INotify::nodename();
30
31 sub setup_environment {
32 PVE::RPCEnvironment->setup_default_cli_env();
33 }
34
35 my $upid_exit = sub {
36 my $upid = shift;
37 my $status = PVE::Tools::upid_read_status($upid);
38 print "Task $status\n";
39 exit(PVE::Tools::upid_status_is_error($status) ? -1 : 0);
40 };
41
42 sub param_mapping {
43 my ($name) = @_;
44
45 my $load_file_and_encode = sub {
46 my ($filename) = @_;
47
48 return PVE::ACME::Challenge->encode_value('string', 'data', PVE::Tools::file_get_contents($filename));
49 };
50
51 my $mapping = {
52 'upload_custom_cert' => [
53 'certificates',
54 'key',
55 ],
56 'add_plugin' => [
57 ['data', $load_file_and_encode, "File with one key-value pair per line, will be base64url encode for storage in plugin config.", 0],
58 ],
59 'update_plugin' => [
60 ['data', $load_file_and_encode, "File with one key-value pair per line, will be base64url encode for storage in plugin config.", 0],
61 ],
62 };
63
64 return $mapping->{$name};
65 }
66
67 __PACKAGE__->register_method({
68 name => 'acme_register',
69 path => 'acme_register',
70 method => 'POST',
71 description => "Register a new ACME account with a compatible CA.",
72 parameters => {
73 additionalProperties => 0,
74 properties => {
75 name => get_standard_option('pve-acme-account-name'),
76 contact => get_standard_option('pve-acme-account-contact'),
77 directory => get_standard_option('pve-acme-directory-url', {
78 optional => 1,
79 }),
80 },
81 },
82 returns => { type => 'null' },
83 code => sub {
84 my ($param) = @_;
85
86 if (!$param->{directory}) {
87 my $directories = PVE::API2::ACMEAccount->get_directories({});
88 print "Directory endpoints:\n";
89 my $i = 0;
90 while ($i < @$directories) {
91 print $i, ") ", $directories->[$i]->{name}, " (", $directories->[$i]->{url}, ")\n";
92 $i++;
93 }
94 print $i, ") Custom\n";
95
96 my $term = Term::ReadLine->new('pvenode');
97 my $get_dir_selection = sub {
98 my $selection = $term->readline("Enter selection: ");
99 if ($selection =~ /^(\d+)$/) {
100 $selection = $1;
101 if ($selection == $i) {
102 $param->{directory} = $term->readline("Enter custom URL: ");
103 return;
104 } elsif ($selection < $i && $selection >= 0) {
105 $param->{directory} = $directories->[$selection]->{url};
106 return;
107 }
108 }
109 print "Invalid selection.\n";
110 };
111
112 my $attempts = 0;
113 while (!$param->{directory}) {
114 die "Aborting.\n" if $attempts > 3;
115 $get_dir_selection->();
116 $attempts++;
117 }
118 }
119 print "\nAttempting to fetch Terms of Service from '$param->{directory}'..\n";
120 my $tos = PVE::API2::ACMEAccount->get_tos({ directory => $param->{directory} });
121 if ($tos) {
122 print "Terms of Service: $tos\n";
123 my $term = Term::ReadLine->new('pvenode');
124 my $agreed = $term->readline('Do you agree to the above terms? [y|N]: ');
125 die "Cannot continue without agreeing to ToS, aborting.\n"
126 if ($agreed !~ /^y$/i);
127
128 $param->{tos_url} = $tos;
129 } else {
130 print "No Terms of Service found, proceeding.\n";
131 }
132 print "\nAttempting to register account with '$param->{directory}'..\n";
133
134 $upid_exit->(PVE::API2::ACMEAccount->register_account($param));
135 }});
136
137 my $print_cert_info = sub {
138 my ($schema, $cert, $options) = @_;
139
140 my $order = [qw(filename fingerprint subject issuer notbefore notafter public-key-type public-key-bits san)];
141 PVE::CLIFormatter::print_api_result(
142 $cert, $schema, $order, { %$options, noheader => 1, sort_key => 0 });
143 };
144
145 our $cmddef = {
146 config => {
147 get => [ 'PVE::API2::NodeConfig', 'get_config', [], { node => $nodename }, sub {
148 my ($res) = @_;
149 print PVE::NodeConfig::write_node_config($res);
150 }],
151 set => [ 'PVE::API2::NodeConfig', 'set_options', [], { node => $nodename } ],
152 },
153
154 startall => [ 'PVE::API2::Nodes::Nodeinfo', 'startall', [], { node => $nodename } ],
155 stopall => [ 'PVE::API2::Nodes::Nodeinfo', 'stopall', [], { node => $nodename } ],
156 migrateall => [ 'PVE::API2::Nodes::Nodeinfo', 'migrateall', [ 'target' ], { node => $nodename } ],
157
158 cert => {
159 info => [ 'PVE::API2::Certificates', 'info', [], { node => $nodename }, sub {
160 my ($res, $schema, $options) = @_;
161
162 if (!$options->{'output-format'} || $options->{'output-format'} eq 'text') {
163 for my $cert (sort { $a->{filename} cmp $b->{filename} } @$res) {
164 $print_cert_info->($schema->{items}, $cert, $options);
165 }
166 } else {
167 PVE::CLIFormatter::print_api_result($res, $schema, undef, $options);
168 }
169
170 }, $PVE::RESTHandler::standard_output_options],
171 set => [ 'PVE::API2::Certificates', 'upload_custom_cert', ['certificates', 'key'], { node => $nodename }, sub {
172 my ($res, $schema, $options) = @_;
173 $print_cert_info->($schema, $res, $options);
174 }, $PVE::RESTHandler::standard_output_options],
175 delete => [ 'PVE::API2::Certificates', 'remove_custom_cert', ['restart'], { node => $nodename } ],
176 },
177
178 task => {
179 list => [ 'PVE::API2::Tasks', 'node_tasks', [], { node => $nodename }, sub {
180 my ($data, $schema, $options) = @_;
181 foreach my $task (@$data) {
182 if (!defined($task->{status})) {
183 $task->{status} = 'UNKNOWN';
184 # RUNNING is set by the API call and needs to be checked explicitly
185 } elsif (PVE::Tools::upid_status_is_error($task->{status}) &&
186 $task->{status} ne 'RUNNING')
187 {
188 $task->{status} = 'ERROR';
189 }
190 }
191 PVE::CLIFormatter::print_api_result($data, $schema, ['upid', 'type', 'id', 'user', 'starttime', 'endtime', 'status' ], $options);
192 }, $PVE::RESTHandler::standard_output_options],
193 status => [ 'PVE::API2::Tasks', 'read_task_status', [ 'upid' ], { node => $nodename }, sub {
194 my ($data, $schema, $options) = @_;
195 PVE::CLIFormatter::print_api_result($data, $schema, undef, $options);
196 }, $PVE::RESTHandler::standard_output_options],
197 # set limit to 1000000, so we see the whole log, not only the first 50 lines by default
198 log => [ 'PVE::API2::Tasks', 'read_task_log', [ 'upid' ], { node => $nodename, limit => 1000000 }, sub {
199 my ($data, $resultprops) = @_;
200 foreach my $line (@$data) {
201 print $line->{t} . "\n";
202 }
203 }],
204 },
205
206 acme => {
207 account => {
208 list => [ 'PVE::API2::ACMEAccount', 'account_index', [], {}, sub {
209 my ($res) = @_;
210 for my $acc (@$res) {
211 print "$acc->{name}\n";
212 }
213 }],
214 register => [ __PACKAGE__, 'acme_register', ['name', 'contact'], {}, $upid_exit ],
215 deactivate => [ 'PVE::API2::ACMEAccount', 'deactivate_account', ['name'], {}, $upid_exit ],
216 info => [ 'PVE::API2::ACMEAccount', 'get_account', ['name'], {}, sub {
217 my ($data, $schema, $options) = @_;
218 PVE::CLIFormatter::print_api_result($data, $schema, undef, $options);
219 }, $PVE::RESTHandler::standard_output_options],
220 update => [ 'PVE::API2::ACMEAccount', 'update_account', ['name'], {}, $upid_exit ],
221 },
222 cert => {
223 order => [ 'PVE::API2::ACME', 'new_certificate', [], { node => $nodename }, $upid_exit ],
224 renew => [ 'PVE::API2::ACME', 'renew_certificate', [], { node => $nodename }, $upid_exit ],
225 revoke => [ 'PVE::API2::ACME', 'revoke_certificate', [], { node => $nodename }, $upid_exit ],
226 },
227 plugin => {
228 list => [ 'PVE::API2::ACMEPlugin', 'index', [], {}, sub {
229 my ($data, $schema, $options) = @_;
230 PVE::CLIFormatter::print_api_result($data, $schema, undef, $options);
231 }, $PVE::RESTHandler::standard_output_options ],
232 config => [ 'PVE::API2::ACMEPlugin', 'get_plugin_config', ['id'], {}, sub {
233 my ($data, $schema, $options) = @_;
234 PVE::CLIFormatter::print_api_result($data, $schema, undef, $options);
235 }, $PVE::RESTHandler::standard_output_options ],
236 add => [ 'PVE::API2::ACMEPlugin', 'add_plugin', ['type', 'id'] ],
237 set => [ 'PVE::API2::ACMEPlugin', 'update_plugin', ['id'] ],
238 remove => [ 'PVE::API2::ACMEPlugin', 'delete_plugin', ['id'] ],
239 },
240
241 },
242
243 wakeonlan => [ 'PVE::API2::Nodes::Nodeinfo', 'wakeonlan', [ 'node' ], {}, sub {
244 my ($mac_addr) = @_;
245
246 print "Wake on LAN packet send for '$mac_addr'\n";
247 } ],
248
249 };
250
251 1;