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('unexported', { |
---|
44 | help => 'unexported yes|no|show - switch or show base mode regarding' . |
---|
45 | ' unexported objects', |
---|
46 | completion => sub { |
---|
47 | if (!$_[2]) { |
---|
48 | return qw(yes no show); |
---|
49 | } |
---|
50 | }, |
---|
51 | code => sub { |
---|
52 | my ($self, $arg) = @_; |
---|
53 | if ($arg eq 'yes') { |
---|
54 | $self->base->unexported(1); |
---|
55 | print $OUT "Unexported are now show\n"; |
---|
56 | } elsif ($arg eq 'no') { |
---|
57 | $self->base->unexported(0); |
---|
58 | print $OUT "Unexported are no longer show\n"; |
---|
59 | } elsif ($arg eq 'show') { |
---|
60 | print $OUT "Unexported objects " . ($self->base->unexported ? |
---|
61 | "enable" : "disable") . "\n"; |
---|
62 | } else { |
---|
63 | print $OUT "wrong argument\n"; |
---|
64 | } |
---|
65 | }, |
---|
66 | }); |
---|
67 | $self->add_func('ls', { |
---|
68 | help => 'ls object_type - list object of type object_type', |
---|
69 | completion => sub { |
---|
70 | if(!$_[2]) { |
---|
71 | return $_[0]->base->list_supported_objects |
---|
72 | } else { () } |
---|
73 | }, |
---|
74 | code => sub { |
---|
75 | if ($_[1]) { |
---|
76 | print $OUT map { "$_\n" } $_[0]->base->list_objects($_[1]); |
---|
77 | } else { |
---|
78 | print $OUT "Object type missing\n"; |
---|
79 | } |
---|
80 | }, |
---|
81 | }); |
---|
82 | $self->add_func('search', { |
---|
83 | help => 'search objecttype filter1 [filter2...] - search object according filter', |
---|
84 | completion => sub { |
---|
85 | if(!$_[2]) { |
---|
86 | return $_[0]->base->list_supported_objects |
---|
87 | } else { return() } |
---|
88 | }, |
---|
89 | code => sub { |
---|
90 | my ($self, @args) = @_; |
---|
91 | if ($_[1]) { |
---|
92 | my @res = $self->base->search_objects(@args); |
---|
93 | print $OUT map { "$_\n" } @res; |
---|
94 | $self->{_lastsearch} = \@res; |
---|
95 | $self->{_lastsearchtype} = $args[0]; |
---|
96 | } else { |
---|
97 | print $OUT "Object type missing\n"; |
---|
98 | } |
---|
99 | }, |
---|
100 | }); |
---|
101 | $self->add_func('expired', { |
---|
102 | help => 'expired [delay] - list expired account more than delay (default is now)', |
---|
103 | code => sub { |
---|
104 | my ($self, $expire) = @_; |
---|
105 | my @users = $self->base->find_expired_users($expire); |
---|
106 | print $OUT map { "$_\n" } @users; |
---|
107 | $self->{_lastsearchtype} = 'user'; |
---|
108 | $self->{_lastsearch} = \@users; |
---|
109 | }, |
---|
110 | }) if ($self->base->can('find_expired_users')); |
---|
111 | $self->add_func('expires', { |
---|
112 | help => 'expires [delay] - list account expiring before delay (default is 1 month)', |
---|
113 | code => sub { |
---|
114 | my ($self, $expire) = @_; |
---|
115 | my @users = $self->base->find_next_expire_users($expire); |
---|
116 | print $OUT map { "$_\n" } @users; |
---|
117 | $self->{_lastsearchtype} = 'user'; |
---|
118 | $self->{_lastsearch} = \@users; |
---|
119 | }, |
---|
120 | }) if ($self->base->can('find_next_expire_users')); |
---|
121 | $self->add_func('select', { |
---|
122 | help => 'select object_type - select objects to perform action on it', |
---|
123 | completion => sub { |
---|
124 | if ($_[2]) { |
---|
125 | return $_[0]->base->list_objects($_[2]); |
---|
126 | } else { |
---|
127 | return '@', $_[0]->base->list_supported_objects; |
---|
128 | } |
---|
129 | }, |
---|
130 | code => sub { |
---|
131 | my ($self, $otype, @ids) = @_; |
---|
132 | my @objs; |
---|
133 | if ($otype eq '@') { |
---|
134 | if (@{$self->{_lastsearch} || []}) { |
---|
135 | $otype = $self->{_lastsearchtype}; |
---|
136 | @ids = @{$self->{_lastsearch}}; |
---|
137 | } else { |
---|
138 | print $OUT "No results store from previous search\n"; |
---|
139 | return; |
---|
140 | } |
---|
141 | } |
---|
142 | if (!@ids) { |
---|
143 | print $OUT 'not enough arguments' . "\n"; |
---|
144 | return; |
---|
145 | } |
---|
146 | foreach (@ids) { |
---|
147 | my $obj = $self->base->get_object($otype, $_) or do { |
---|
148 | print $OUT "Cannot get $otype $_\n"; |
---|
149 | return; |
---|
150 | }; |
---|
151 | push(@objs, $obj); |
---|
152 | } |
---|
153 | print $OUT "Selecting $otype " . join(', ', @ids) . "\n"; |
---|
154 | LATMOS::Accounts::Cli::Object->new( |
---|
155 | Parent => $self, |
---|
156 | Context => $self->Context, |
---|
157 | otype => $otype, |
---|
158 | objs => \@objs, |
---|
159 | )->cli(); |
---|
160 | }, |
---|
161 | }); |
---|
162 | $self->add_func('create', { |
---|
163 | code => sub { |
---|
164 | my ($self, $otype) = @_; |
---|
165 | my $helper = $self->base->ochelper($otype); |
---|
166 | my $info = undef; |
---|
167 | while (1) { |
---|
168 | my $status; |
---|
169 | ($status, $info) = $helper->step($info); |
---|
170 | |
---|
171 | if ($status ne 'NEEDINFO') { |
---|
172 | if ($status eq 'CREATED') { |
---|
173 | print $OUT "Object created\n"; |
---|
174 | $self->commit; |
---|
175 | } else { |
---|
176 | print $OUT "Nothing done\n"; |
---|
177 | $self->rollback; |
---|
178 | } |
---|
179 | return; |
---|
180 | } |
---|
181 | |
---|
182 | if ($info->{name}{ask}) { |
---|
183 | my $line = $self->Context->Term->readline("Name of the object ?"); |
---|
184 | $info->{name}{content} = $line; |
---|
185 | } |
---|
186 | foreach my $attr (@{$info->{ask} || []}) { |
---|
187 | $self->Context->Term->Attribs->{completion_function} = sub { |
---|
188 | $info->{contents}{$attr} |
---|
189 | }; |
---|
190 | my $line = $self->Context->Term->readline(sprintf(' %s %s? ', |
---|
191 | $attr, |
---|
192 | $info->{contents}{$attr} |
---|
193 | ? '(' . $info->{contents}{$attr} . ') ' |
---|
194 | : '' |
---|
195 | )); |
---|
196 | $info->{contents}{$attr} = $line if($line); |
---|
197 | } |
---|
198 | } |
---|
199 | }, |
---|
200 | } |
---|
201 | ); |
---|
202 | $self->add_func('exchangeip', |
---|
203 | { |
---|
204 | help => 'Exchange two IP on host', |
---|
205 | code => sub { |
---|
206 | my ($self, @args) = @_; |
---|
207 | my ($ip1, $ip2) = |
---|
208 | grep { $_ && $_ =~ /\d+\.\d+\.\d+\.\d+/ } @args; |
---|
209 | if (!$ip2) { |
---|
210 | print $OUT "Need two ip to exchange\n"; |
---|
211 | return; |
---|
212 | } |
---|
213 | if ($self->base->nethost_exchange_ip($ip1, $ip2)) { |
---|
214 | print $OUT "$ip1 and $ip2 get exchange\n"; |
---|
215 | $self->commit; |
---|
216 | } else { |
---|
217 | $self->rollback; |
---|
218 | } |
---|
219 | }, |
---|
220 | completion => sub { |
---|
221 | my ($self, $carg, @args) = @_; |
---|
222 | if ($args[-1] && $args[-1] !~ m/\d+\.\d+\.\d+\.\d+/) { |
---|
223 | if (my $obj = $self->base->get_object('nethost', $args[-1])) { |
---|
224 | return $obj->get_attributes('ip'); |
---|
225 | } |
---|
226 | } else { |
---|
227 | my @list = |
---|
228 | ($self->base->attributes_summary('nethost', 'ip'), |
---|
229 | $self->base->list_objects('nethost')); |
---|
230 | return @list; |
---|
231 | } |
---|
232 | }, |
---|
233 | } |
---|
234 | ); |
---|
235 | $self->add_func('user', { alias => [qw'select user' ] }); |
---|
236 | $self->add_func('group', { alias => [qw'select group'] }); |
---|
237 | return $self |
---|
238 | } |
---|
239 | |
---|
240 | 1; |
---|
241 | |
---|
242 | __END__ |
---|
243 | |
---|
244 | =head1 SEE ALSO |
---|
245 | |
---|
246 | L<LATMOS::Accounts> |
---|
247 | |
---|
248 | =head1 AUTHOR |
---|
249 | |
---|
250 | Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
251 | |
---|
252 | =head1 COPYRIGHT AND LICENSE |
---|
253 | |
---|
254 | Copyright (C) 2008, 2009, 2010, 2011, 2012 CNRS SA/CETP/LATMOS |
---|
255 | |
---|
256 | This library is free software; you can redistribute it and/or modify |
---|
257 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
258 | at your option, any later version of Perl 5 you may have available. |
---|
259 | |
---|
260 | =cut |
---|