1 | package LATMOS::Accounts::Cli; |
---|
2 | |
---|
3 | # $Id: Cli.pm 2145 2018-08-29 18:15:46Z nanardon $ |
---|
4 | |
---|
5 | use strict; |
---|
6 | use warnings; |
---|
7 | use Moose; |
---|
8 | use LATMOS::Accounts::Log; |
---|
9 | use LATMOS::Accounts::Utils; |
---|
10 | use Term::ReadLine; |
---|
11 | use Text::ParseWords; |
---|
12 | use Getopt::Long; |
---|
13 | use LATMOS::Accounts::Cli::Object; |
---|
14 | |
---|
15 | extends 'LATMOS::Accounts::Cli::Base'; |
---|
16 | |
---|
17 | =head1 NAME |
---|
18 | |
---|
19 | LATMOS::Accounts::Cli - Command line interface functions |
---|
20 | |
---|
21 | =head1 DESCRIPTION |
---|
22 | |
---|
23 | This module handle envirronment and functons for L<la-cli> tools. |
---|
24 | |
---|
25 | =cut |
---|
26 | |
---|
27 | =head1 FUNCTIONS |
---|
28 | |
---|
29 | =cut |
---|
30 | |
---|
31 | =head2 globalenv |
---|
32 | |
---|
33 | Return the main envirronement object |
---|
34 | |
---|
35 | =cut |
---|
36 | |
---|
37 | sub BUILD { |
---|
38 | my ( $self ) = @_; |
---|
39 | |
---|
40 | my $labase = $self->base; |
---|
41 | my $OUT = $self->Context->Out; |
---|
42 | |
---|
43 | $self->add_func('ls', { |
---|
44 | help => 'ls object_type - list object of type object_type', |
---|
45 | completion => sub { |
---|
46 | if(!$_[2]) { |
---|
47 | return $_[0]->base->list_supported_objects |
---|
48 | } else { () } |
---|
49 | }, |
---|
50 | code => sub { |
---|
51 | my $env = shift; |
---|
52 | my @args = $self->getoption( |
---|
53 | { |
---|
54 | 'fmt=s' => \my $fmt, |
---|
55 | 'filefmt=s' => \my $filefmt, |
---|
56 | }, @_ |
---|
57 | ); |
---|
58 | |
---|
59 | my $otype = $args[0] or do { |
---|
60 | print $OUT "Object type missing\n"; |
---|
61 | return 1; |
---|
62 | }; |
---|
63 | |
---|
64 | if ($filefmt){ |
---|
65 | open(my $hfmt, '<', $filefmt) or die "Cannot open $filefmt\n"; |
---|
66 | $fmt ||= ''; # avoid undef warning |
---|
67 | while (<$hfmt>) { |
---|
68 | chomp($fmt .= $_); |
---|
69 | } |
---|
70 | close $hfmt; |
---|
71 | } |
---|
72 | |
---|
73 | if ($fmt) { |
---|
74 | foreach ($env->base->list_objects($otype)) { |
---|
75 | my $obj = $env->base->get_object($otype, $_) or next; |
---|
76 | print $OUT $obj->queryformat($fmt); |
---|
77 | } |
---|
78 | print $OUT "\n"; |
---|
79 | } else { |
---|
80 | print $OUT map { "$_\n" } $env->base->list_objects($otype); |
---|
81 | } |
---|
82 | }, |
---|
83 | }); |
---|
84 | $self->add_func('search', { |
---|
85 | help => 'search objecttype filter1 [filter2...] - search object according filter', |
---|
86 | completion => sub { |
---|
87 | if(!$_[2]) { |
---|
88 | return $_[0]->base->list_supported_objects |
---|
89 | } else { return() } |
---|
90 | }, |
---|
91 | code => sub { |
---|
92 | my ($self, @args) = @_; |
---|
93 | if ($_[1]) { |
---|
94 | my @res = $self->base->search_objects(@args); |
---|
95 | print $OUT map { "$_\n" } @res; |
---|
96 | $self->{_lastsearch} = \@res; |
---|
97 | $self->{_lastsearchtype} = $args[0]; |
---|
98 | } else { |
---|
99 | print $OUT "Object type missing\n"; |
---|
100 | } |
---|
101 | }, |
---|
102 | }); |
---|
103 | $self->add_func('expired', { |
---|
104 | help => 'expired [delay] - list expired account more than delay (default is now)', |
---|
105 | code => sub { |
---|
106 | my ($self, $expire) = @_; |
---|
107 | my @users = $self->base->find_expired_users($expire); |
---|
108 | print $OUT map { "$_\n" } @users; |
---|
109 | $self->{_lastsearchtype} = 'user'; |
---|
110 | $self->{_lastsearch} = \@users; |
---|
111 | }, |
---|
112 | }) if ($self->base->can('find_expired_users')); |
---|
113 | $self->add_func('expires', { |
---|
114 | help => 'expires [delay] - list account expiring before delay (default is 1 month)', |
---|
115 | code => sub { |
---|
116 | my ($self, $expire) = @_; |
---|
117 | my @users = $self->base->find_next_expire_users($expire); |
---|
118 | print $OUT map { "$_\n" } @users; |
---|
119 | $self->{_lastsearchtype} = 'user'; |
---|
120 | $self->{_lastsearch} = \@users; |
---|
121 | }, |
---|
122 | }) if ($self->base->can('find_next_expire_users')); |
---|
123 | $self->add_func('select', { |
---|
124 | help => 'select object_type - select objects to perform action on it', |
---|
125 | completion => sub { |
---|
126 | if ($_[2]) { |
---|
127 | return $_[0]->base->list_objects($_[2]); |
---|
128 | } else { |
---|
129 | return '@', $_[0]->base->list_supported_objects; |
---|
130 | } |
---|
131 | }, |
---|
132 | code => sub { |
---|
133 | my ($self, $otype, @ids) = @_; |
---|
134 | my @objs; |
---|
135 | if ($otype eq '@') { |
---|
136 | if (@{$self->{_lastsearch} || []}) { |
---|
137 | $otype = $self->{_lastsearchtype}; |
---|
138 | @ids = @{$self->{_lastsearch}}; |
---|
139 | } else { |
---|
140 | print $OUT "No results store from previous search\n"; |
---|
141 | return; |
---|
142 | } |
---|
143 | } |
---|
144 | if (!@ids) { |
---|
145 | print $OUT 'not enough arguments' . "\n"; |
---|
146 | return; |
---|
147 | } |
---|
148 | foreach (@ids) { |
---|
149 | my $obj = $self->base->get_object($otype, $_) or do { |
---|
150 | print $OUT "Cannot get $otype $_\n"; |
---|
151 | return; |
---|
152 | }; |
---|
153 | push(@objs, $obj); |
---|
154 | } |
---|
155 | print $OUT "Selecting $otype " . join(', ', @ids) . "\n"; |
---|
156 | LATMOS::Accounts::Cli::Object->new( |
---|
157 | Parent => $self, |
---|
158 | Context => $self->Context, |
---|
159 | otype => $otype, |
---|
160 | objs => \@objs, |
---|
161 | )->cli(); |
---|
162 | }, |
---|
163 | }); |
---|
164 | $self->add_func('create', { |
---|
165 | code => sub { |
---|
166 | my ($self, $otype) = @_; |
---|
167 | my $helper = $self->base->ochelper($otype); |
---|
168 | my $info = undef; |
---|
169 | while (1) { |
---|
170 | my $status; |
---|
171 | ($status, $info) = $helper->step($info); |
---|
172 | |
---|
173 | if ($status ne 'NEEDINFO') { |
---|
174 | if ($status eq 'CREATED') { |
---|
175 | print $OUT "Object created\n"; |
---|
176 | $self->commit; |
---|
177 | } else { |
---|
178 | print $OUT "Nothing done\n"; |
---|
179 | $self->rollback; |
---|
180 | } |
---|
181 | return; |
---|
182 | } |
---|
183 | |
---|
184 | if ($info->{name}{ask}) { |
---|
185 | my $line = $self->Context->Term->readline("Name of the object ?"); |
---|
186 | $info->{name}{content} = $line; |
---|
187 | } |
---|
188 | foreach my $attr (@{$info->{ask} || []}) { |
---|
189 | $self->Context->Term->Attribs->{completion_function} = sub { |
---|
190 | $info->{contents}{$attr} |
---|
191 | }; |
---|
192 | my $line = $self->Context->Term->readline(sprintf(' %s %s? ', |
---|
193 | $attr, |
---|
194 | $info->{contents}{$attr} |
---|
195 | ? '(' . $info->{contents}{$attr} . ') ' |
---|
196 | : '' |
---|
197 | )); |
---|
198 | $info->{contents}{$attr} = $line if($line); |
---|
199 | } |
---|
200 | } |
---|
201 | }, |
---|
202 | } |
---|
203 | ); |
---|
204 | $self->add_func('exchangeip', |
---|
205 | { |
---|
206 | help => 'Exchange two IP on host', |
---|
207 | code => sub { |
---|
208 | my ($self, @args) = @_; |
---|
209 | my ($ip1, $ip2) = |
---|
210 | grep { $_ && $_ =~ /\d+\.\d+\.\d+\.\d+/ } @args; |
---|
211 | if (!$ip2) { |
---|
212 | print $OUT "Need two ip to exchange\n"; |
---|
213 | return; |
---|
214 | } |
---|
215 | if ($self->base->nethost_exchange_ip($ip1, $ip2)) { |
---|
216 | print $OUT "$ip1 and $ip2 get exchange\n"; |
---|
217 | $self->commit; |
---|
218 | } else { |
---|
219 | $self->rollback; |
---|
220 | } |
---|
221 | }, |
---|
222 | completion => sub { |
---|
223 | my ($self, $carg, @args) = @_; |
---|
224 | if ($args[-1] && $args[-1] !~ m/\d+\.\d+\.\d+\.\d+/) { |
---|
225 | if (my $obj = $self->base->get_object('nethost', $args[-1])) { |
---|
226 | return $obj->get_attributes('ip'); |
---|
227 | } |
---|
228 | } else { |
---|
229 | my @list = |
---|
230 | ($self->base->attributes_summary('nethost', 'ip'), |
---|
231 | $self->base->list_objects('nethost')); |
---|
232 | return @list; |
---|
233 | } |
---|
234 | }, |
---|
235 | } |
---|
236 | ); |
---|
237 | |
---|
238 | $self->add_func('loadcsv', |
---|
239 | { |
---|
240 | help => 'Load CSV file to create object', |
---|
241 | code => sub { |
---|
242 | my ($self, $otype, $file) = @_; |
---|
243 | |
---|
244 | open(my $fh, '<', $file) or do { |
---|
245 | warn "Cannot open $file $!\n"; |
---|
246 | return; |
---|
247 | }; |
---|
248 | |
---|
249 | my @ids; |
---|
250 | |
---|
251 | loadCSV( |
---|
252 | $fh, |
---|
253 | cb => sub { |
---|
254 | my ($res, $linecount) = @_; |
---|
255 | |
---|
256 | my $ochelper = $labase->ochelper($otype); |
---|
257 | |
---|
258 | my $info = { |
---|
259 | contents => $res |
---|
260 | }; |
---|
261 | if ($res->{name}) { |
---|
262 | $info->{name}{content} = $res->{name}; |
---|
263 | } |
---|
264 | |
---|
265 | if (my $id = $ochelper->Automate($info)) { |
---|
266 | push(@ids, $id); |
---|
267 | } else { |
---|
268 | warn "Cannot create object line $linecount (not enough information ?)\n"; |
---|
269 | } |
---|
270 | }, |
---|
271 | ); |
---|
272 | |
---|
273 | close($fh); |
---|
274 | |
---|
275 | my @objs; |
---|
276 | foreach (@ids) { |
---|
277 | my $obj = $self->base->get_object($otype, $_) or do { |
---|
278 | print $OUT "Cannot get $otype $_\n"; |
---|
279 | return; |
---|
280 | }; |
---|
281 | push(@objs, $obj); |
---|
282 | } |
---|
283 | |
---|
284 | print $OUT "Selecting $otype " . join(', ', @ids) . "\n"; |
---|
285 | LATMOS::Accounts::Cli::Object->new( |
---|
286 | Parent => $self, |
---|
287 | Context => $self->Context, |
---|
288 | otype => $otype, |
---|
289 | objs => \@objs, |
---|
290 | )->cli(); |
---|
291 | }, |
---|
292 | completion => sub { |
---|
293 | if ($_[2]) { |
---|
294 | return Term::ReadLine::Gnu::filename_list(@_); |
---|
295 | } else { |
---|
296 | return '@', $_[0]->base->list_supported_objects; |
---|
297 | } |
---|
298 | }, |
---|
299 | } |
---|
300 | ); |
---|
301 | |
---|
302 | $self->add_func('user', { alias => [qw'select user' ] }); |
---|
303 | $self->add_func('group', { alias => [qw'select group'] }); |
---|
304 | return $self |
---|
305 | } |
---|
306 | |
---|
307 | 1; |
---|
308 | |
---|
309 | __END__ |
---|
310 | |
---|
311 | =head1 SEE ALSO |
---|
312 | |
---|
313 | L<LATMOS::Accounts> |
---|
314 | |
---|
315 | =head1 AUTHOR |
---|
316 | |
---|
317 | Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
318 | |
---|
319 | =head1 COPYRIGHT AND LICENSE |
---|
320 | |
---|
321 | Copyright (C) 2008, 2009, 2010, 2011, 2012 CNRS SA/CETP/LATMOS |
---|
322 | |
---|
323 | This library is free software; you can redistribute it and/or modify |
---|
324 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
325 | at your option, any later version of Perl 5 you may have available. |
---|
326 | |
---|
327 | =cut |
---|