]>
Commit | Line | Data |
---|---|---|
a31c7b27 DM |
1 | package PVE::APIClient::Config; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | use JSON; | |
a31c7b27 | 6 | |
06039c7f | 7 | use File::HomeDir (); |
184877d4 | 8 | use PVE::JSONSchema qw(register_standard_option get_standard_option); |
69aa81b3 | 9 | use PVE::SectionConfig; |
06039c7f | 10 | use PVE::Tools qw(file_get_contents file_set_contents); |
a31c7b27 | 11 | |
69aa81b3 DM |
12 | use base qw(PVE::SectionConfig); |
13 | ||
184877d4 DM |
14 | my $complete_remote_name = sub { |
15 | ||
69aa81b3 DM |
16 | my $config = PVE::APIClient::Config->load(); |
17 | return [keys %{$config->{ids}}]; | |
184877d4 DM |
18 | }; |
19 | ||
20 | register_standard_option('pveclient-remote-name', { | |
21 | description => "The name of the remote.", | |
22 | type => 'string', | |
23 | pattern => qr(\w+), | |
24 | completion => $complete_remote_name, | |
25 | }); | |
26 | ||
a31c7b27 | 27 | |
69aa81b3 DM |
28 | my $defaultData = { |
29 | propertyList => { | |
30 | type => { | |
31 | description => "Section type.", | |
32 | optional => 1, | |
33 | }, | |
34 | name => get_standard_option('pveclient-remote-name'), | |
35 | host => { | |
36 | description => "The host.", | |
37 | type => 'string', format => 'address', | |
38 | optional => 1, | |
39 | }, | |
40 | username => { | |
41 | description => "The username.", | |
42 | type => 'string', | |
43 | optional => 1, | |
44 | }, | |
45 | password => { | |
46 | description => "The users password.", | |
47 | type => 'string', | |
48 | optional => 1, | |
49 | }, | |
50 | port => { | |
51 | description => "The port.", | |
52 | type => 'integer', | |
53 | optional => 1, | |
54 | default => 8006, | |
55 | }, | |
56 | fingerprint => { | |
57 | description => "Fingerprint.", | |
58 | type => 'string', | |
59 | optional => 1, | |
60 | }, | |
61 | comment => { | |
62 | description => "Description.", | |
63 | type => 'string', | |
64 | optional => 1, | |
65 | maxLength => 4096, | |
66 | }, | |
67 | }, | |
68 | }; | |
06039c7f | 69 | |
69aa81b3 DM |
70 | sub type { |
71 | return 'remote'; | |
06039c7f RJ |
72 | } |
73 | ||
69aa81b3 DM |
74 | sub options { |
75 | return { | |
76 | name => { optional => 0 }, | |
77 | host => { optional => 0 }, | |
78 | comment => { optional => 1 }, | |
79 | username => { optional => 0 }, | |
80 | password => { optional => 0 }, | |
81 | port => { optional => 1 }, | |
82 | fingerprint => { optional => 1 }, | |
83 | }; | |
aa570b38 DM |
84 | } |
85 | ||
69aa81b3 DM |
86 | sub private { |
87 | return $defaultData; | |
06039c7f | 88 | } |
aa570b38 | 89 | |
69aa81b3 DM |
90 | sub config_filename { |
91 | my ($class) = @_; | |
aa570b38 | 92 | |
69aa81b3 | 93 | return File::HomeDir::home() . '/.pveclient'; |
06039c7f | 94 | } |
aa570b38 | 95 | |
69aa81b3 DM |
96 | sub load { |
97 | my ($class) = @_; | |
06039c7f | 98 | |
69aa81b3 | 99 | my $filename = $class->config_filename(); |
aa570b38 | 100 | |
69aa81b3 | 101 | my $raw = ''; |
06039c7f | 102 | |
69aa81b3 DM |
103 | if (-e $filename) { |
104 | my $filemode = (stat($filename))[2] & 07777; | |
105 | if ($filemode != 0600) { | |
106 | die sprintf "wrong permissions on '$filename' %04o (expected 0600)\n", $filemode; | |
107 | } | |
06039c7f | 108 | |
69aa81b3 DM |
109 | $raw = file_get_contents($filename); |
110 | } | |
06039c7f | 111 | |
69aa81b3 DM |
112 | return $class->parse_config($filename, $raw); |
113 | } | |
06039c7f | 114 | |
69aa81b3 DM |
115 | sub save { |
116 | my ($class, $cfg) = @_; | |
06039c7f | 117 | |
69aa81b3 DM |
118 | my $filename = $class->config_filename(); |
119 | my $raw = $class->write_config($filename, $cfg); | |
06039c7f | 120 | |
69aa81b3 | 121 | file_set_contents($filename, $raw, 0600); |
06039c7f RJ |
122 | } |
123 | ||
69aa81b3 DM |
124 | sub lookup_remote { |
125 | my ($class, $cfg, $name, $noerr) = @_; | |
06039c7f | 126 | |
69aa81b3 | 127 | my $data = $cfg->{ids}->{$name}; |
06039c7f | 128 | |
69aa81b3 | 129 | return $data if $noerr || defined($data); |
06039c7f | 130 | |
69aa81b3 | 131 | die "unknown remote \"$name\"\n"; |
06039c7f RJ |
132 | } |
133 | ||
134 | sub remote_conn { | |
69aa81b3 | 135 | my ($class, $cfg, $remote) = @_; |
06039c7f | 136 | |
69aa81b3 | 137 | my $section = $class->lookup_remote($cfg, $remote); |
06039c7f RJ |
138 | my $conn = PVE::APIClient::LWP->new( |
139 | username => $section->{username}, | |
140 | password => $section->{password}, | |
141 | host => $section->{host}, | |
142 | port => $section->{port}, | |
143 | cached_fingerprints => { | |
144 | $section->{fingerprint} => 1, | |
145 | } | |
146 | ); | |
aa570b38 | 147 | |
06039c7f | 148 | $conn->login; |
aa570b38 | 149 | |
06039c7f | 150 | return $conn; |
aa570b38 | 151 | } |
a31c7b27 | 152 | |
69aa81b3 DM |
153 | __PACKAGE__->register(); |
154 | __PACKAGE__->init(); | |
155 | ||
a31c7b27 | 156 | 1; |