blib/lib/Stackato/Stackrad.pm

filter: Statementstmt 84.6% 11 / 13 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 88.8% 16 / 18 CC 1 SCAR 0.6
CC
1
Cov
85%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 1 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package Stackato::Stackrad;
3 our $VERSION;
4 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 $VERSION = '0.13';
6 }
7  
8 1 ▶use Mo qw'build builder default';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 0 ▶use Curses::UI;
Subroutine BEGIN: called CC=1 SCAR=2.6
10 use LWP::UserAgent;
11 use LWP::Protocol::https;
12 use HTTP::Request;
13 use URI::Escape;
14 use JSON::XS;
15 use YAML::XS;
16  
17 our $SELF;
18 sub PPP {
19 my $self = $SELF;
20 my $text = YAML::XS::Dump(@_);
21 $self->error($text);
22 wantarray ? @_ : $_[0]
23 }
24  
25 use constant app_name => 'Stackrad';
26 use constant target_key_hint => ' (set target with Ctrl+t)';
27 use constant default_title => app_name . target_key_hint;
28 use constant new_target_prompt =>
29 "New target? (e.g., api.stackato.example.com)";
30 use constant username_prompt => "Username:";
31 use constant password_prompt => "Password:";
32 use constant user_agent_string =>
33 app_name . "/$VERSION lwp/$LWP::UserAgent::VERSION";
34 use constant main_color => 'cyan';
35 use constant secondary_color => 'cyan';
36 use constant accent_color => 'red';
37 use constant banner => <<EOT;
38 _____ _ _ _
39 / ____| | | | | | |
40 ____| O_________ ___| |_ __ _ ___| | __ __ _| |_ ___
41 / \\___ \\ \\ / __| __|/ _` |/ __| |/ // _` | __|/ _ \\ TM
42 | .____O | | \\__ \\ |_| (_| | (__| <| (_| | |_| (_) |
43 \\_...|_____...___/ |___/\\__|\\__,_|\\___|_|\\_\\\\__,_|\\__|\\___/
44 by ActiveState
45  
46  
47  
48  
49  
50 EOT
51  
52 has targets => (default => sub{[]});
53 # has targets => (default => sub{[{hostname=>'api.stacka.to'}]});
54  
55 has target_index => ();
56 has cui => ();
57 has win1 => ();
58 has tabs => ();
59 has ui => (default => sub { [
60 {
61 name => 'Targets',
62 on_activate => sub { },
63 contents => undef,
64 },
65 {
66 name => 'Overview',
67 on_activate => sub { },
68 contents => <<'EOT'
69 Memory: [ 128 MB of 256 MB ]
70 [---------------- ]
71  
72 1 / 2 Applications
73 0 / 2 Services
74  
75 Applications:
76 [ ] tty-js [STARTED]
77 Framework: node, Services: 0, Owner: as@sharpsaw.org
78 [Restart] [Stop] [Launch] [Logs] [All Files] [More Info]
79  
80 [ ] pairup [STARTED]
81 Framework: generic, Services: 0, Owner: ingy@ingy.net
82 [Restart] [Stop] [Launch] [Logs] [All Files] [More Info]
83  
84 ...
85  
86 Provisioned Services:
87 [ ] filesystem Provisioned Name: home Bindings: 1
88 [(Cannot Delete Bound Service)]
89 EOT
90 },
91 {
92 name => 'Users',
93 on_activate => sub { $SELF->update_users },
94 contents => <<'EOT'
95 You need to login to a valid target Stackato VM.
96 EOT
97 },
98 {
99 name => 'Groups',
100 on_activate => sub { },
101 contents => <<'EOT'
102 Group Users Apps
103 [ ] pair 5 1
104 EOT
105 },
106 {
107 name => 'App Store',
108 on_activate => sub { },
109 contents => <<'EOT'
110 [ ] Bugzilla - perl / mysql
111 A bug tracking system for individuals or groups of developers
112 256MB Required - License: MPL
113 (Third Party Apps for Stackato)
114  
115 [ ] Currency Converter - python / redis
116 Currency converter using Python bottle framework
117 128MB Required - License: Unknown
118 (ActiveState Stackato Sample Applications)
119  
120 [ ] Drupal - php / filesystem / mysql
121 A popular PHP content management system which uses mysql and
122 the persistent file system
123 128MB Required - License: GPLv2
124 (Third Party Apps for Stackato)
125  
126 [ ] ...
127 EOT
128 },
129 {
130 name => 'Local Apps',
131 on_activate => sub { },
132 contents => <<'EOT'
133 [ ] Node Env - node
134 /home/ingy/src/node-env/
135  
136 [ ] Foozle - ruby / postgresql
137 /home/ingy/src/foozle/
138 EOT
139 },
140 ]});
141  
142  
143 sub run {
144 my $class = shift;
145 my $self = $class->new();
146 $SELF = $self; # XXX, PPP
147 $self->setup_cui;
148 $self->cui->mainloop();
149 }
150  
151 sub setup_cui {
152 my $self = shift;
153 $self->target_index($#{$self->targets}) if @{$self->targets}; # XXX
154 my $cui = $self->{cui} = $self->cui(
155 Curses::UI->new(
156 -color_support => 1,
157 # -debug => 1,
158 )
159 );
160  
161 $cui->set_binding(sub { exit 0 }, "\cC");
162 $cui->set_binding(sub { $self->prompt_for_target }, "\cT");
163 $cui->set_binding(sub { $self->delete_current_target }, "\cX");
164 $cui->set_binding(sub { $self->login_logout }, "\cL");
165 for my $index (1 .. 9) {
166 $cui->set_binding(sub { $self->set_target($index) }, $index);
167 }
168  
169 my $win1 = $self->{win1} =
170 $cui->add('win1', 'Window',
171 -title => default_title,
172 -bfg => main_color,
173 -border => 1,
174 );
175 $win1->add('help_text', 'Label',
176 -y => $win1->height - 3,
177 -width => $win1->width - 2,
178 -text => 'Ctrl+n/PgUp / Ctrl+p/PgDn to switch tabs; Ctrl+C to exit',
179 -textalignment => 'middle',
180 -bold => 1,
181 );
182 my $notebook = $win1->add('notebook', 'Notebook',
183 -height => $win1->height - 3,
184 -border => 1,
185 );
186 for my $tab (@{$self->ui}) {
187 my $name = $tab->{name};
188 my $id = 'tab_'.$name;
189 my $page = $tab->{page} = $notebook->add_page($name,
190 -on_activate => $tab->{on_activate}
191 );
192 $tab->{tv} = $page->add(
193 $id, 'TextViewer',
194 -x => 1,
195 -y => 1,
196 -text => $tab->{contents},
197 );
198 }
199 $self->update_targets_screen;
200 $notebook->focus;
201 }
202  
203 sub tab_named {
204 my ($self, $name) = @_;
205 for (@{$self->ui}) {
206 return $_ if $name eq $_->{name};
207 }
208 }
209  
210 sub current_target {
211 my $self = shift;
212 return unless defined $self->target_index;
213 $self->targets->[$self->target_index]
214 }
215  
216 sub set_target {
217 my ($self, $new_index) = @_;
218 $self->target_index($new_index);
219 $self->update_users;
220 }
221  
222 sub prompt_for_target {
223 my $self = shift;
224 my $answer = $self->cui->question(new_target_prompt); # TODO: "api."
225 return unless $answer;
226  
227 return $self->error($answer . " does not appear to be a valid Stackato VM")
228 unless $self->validate_target($answer);
229 push @{$self->targets}, {hostname => $answer};
230 $self->target_index($#{$self->targets});
231 $self->update_targets_screen;
232 $self->set_title
233 }
234  
235 sub delete_current_target {
236 my $self = shift;
237 my $i = $self->target_index;
238 return unless defined $i;
239 splice @{$self->targets}, $i, 1;
240 $i = undef if --$i < 0;
241 $self->target_index($i);
242 $self->set_title;
243 $self->update_targets_screen;
244 }
245  
246 sub update_targets_screen {
247 my $self = shift;
248 my $tab = $self->tab_named('Targets');
249 my $out = '';
250 for (0 .. $#{$self->targets}) {
251 my $target = $self->targets->[$_];
252 $out .= $_ == $self->target_index ? ' * ' : ' ';
253 $out .= $target->{hostname};
254 $out .= $target->{user}
255 ? " (${\$target->{user}}) "
256 : " (not logged in) ";
257 $out .= "\n";
258 }
259 $out .= "\n\n";
260 if (@{$self->targets}) {
261 $out .= 'Ctrl+L to log' . ($self->logged_in ? 'out' : 'in')."\n";
262 $out .= "Ctrl+x to delete current target.\n"
263 } else {
264 $out .= banner;
265 }
266 $out .= "Ctrl+t to add a target.\n";
267 # $out .= "\n\nPress 'Ctrl+<target #>' to set current target."
268 # if @{$self->targets} > 1;
269 $tab->{tv}{-text} = $out;
270 $self->redraw;
271 }
272  
273 sub update_users {
274 my $self = shift;
275 my $out = '';
276 if (not $self->current_target) {
277 $out = 'You have no Stackato VM as a current target.';
278 }
279 else {
280 my $response = $self->get(path => '/users');
281 my $status = $response->code;
282 if ($status == 403) {
283 $out = "Unauthorized. Maybe you need to login as an admin user?";
284 } else {
285 my $data = decode_json($response->content);
286 for (0 .. $#{$data}) {
287 my $user = $data->[$_];
288 $out .= $_+1 . '. ' . $user->{email} . "\n";
289 }
290 }
291 }
292 $self->tab_named('Users')->{tv}{-text} = $out;
293 }
294  
295 sub login_logout {
296 my $self = shift;
297 return $self->logout if $self->logged_in;
298 my $username = $self->cui->question(username_prompt); # TODO: <prev-user>
299 return unless $username;
300 my $password = $self->cui->question(password_prompt);
301 return unless $password;
302 my $path = '/users/' . uri_escape($username) . '/tokens';
303 $password = quotemeta($password);
304 my $response = $self->post(
305 path => $path,
306 content => qq({"password":"$password"})
307 );
308 unless ($response->is_success) {
309 $self->error("Couldn't login.");
310 return $self->logout;
311 }
312 my $token = decode_json($response->content)->{token};
313 $self->current_target->{user} = $username;
314 $self->current_target->{token} = $token;
315 $self->update_targets_screen;
316 }
317  
318 sub logout {
319 my $self = shift;
320 my $cur = $self->current_target;
321 delete $cur->{user};
322 delete $cur->{token};
323 $self->update_targets_screen;
324 }
325  
326 sub logged_in {
327 my $self = shift;
328 $self->current_target->{user}
329 }
330  
331 sub set_title {
332 my $self = shift;
333 my $title = app_name;
334 $title .= ' - target: ' . (
335 $self->current_target &&
336 $self->current_target->{hostname} ||
337 'No Target'
338 );
339 $self->win1->{-title} = $title;
340 $self->redraw;
341 }
342  
343 sub validate_target {
344 my ($self, $target) = @_;
345 my $response = $self->get_from_target(
346 target => { hostname => $target },
347 path => '/info/'
348 );
349 return unless $response->is_success;
350 decode_json($response->content)
351 }
352  
353 sub post_to_target {
354 my ($self, %args) = @_;
355 $self->ua->simple_request(
356 $self->http_req('POST',
357 host => $args{target}{hostname},
358 path => $args{path},
359 headers => {
360 $args{target}{token}
361 ? ('AUTHORIZATION' => $args{target}{token}) : ()
362 },
363 content => $args{content},
364 )
365 )
366 }
367  
368 sub post {
369 my ($self, %args) = @_;
370 $self->post_to_target(
371 target => $self->current_target,
372 path => $args{path},
373 content => $args{content},
374 )
375 }
376  
377 sub get_from_target {
378 my ($self, %args) = @_;
379 $self->ua->simple_request(
380 $self->http_req('GET',
381 host => $args{target}{hostname},
382 path => $args{path},
383 headers => {
384 $args{target}{token}
385 ? ('AUTHORIZATION' => $args{target}{token}) : ()
386 },
387 content => $args{content},
388 )
389 )
390 }
391  
392 sub get {
393 my ($self, %args) = @_;
394 $self->get_from_target(
395 target => $self->current_target,
396 path => $args{path},
397 content => $args{content},
398 )
399 }
400  
401 sub ua {
402 my $self = shift;
403 my $ua = LWP::UserAgent->new(agent => user_agent_string);
404 # XXX
405 # warn "Stackrad being lazy and disabling SSL cert verification!";
406 $ua->ssl_opts(
407 verify_hostname => 0,
408 #? SSL_ca_path => '/app/fs/pair/certcert/stackato.ddns.us.pem',
409 );
410 $ua
411 }
412  
413 sub http_req {
414 my ($self, $method, %args) = @_;
415 my $url = "https://$args{host}$args{path}";
416 my $request = HTTP::Request->new($method, $url);
417 $request->header('Accept' => 'application/json', %{$args{headers}});
418 $request->content($args{content}) if $args{content};
419 $request
420 }
421  
422 sub error {
423 my $self = shift;
424 $self->cui->error(@_);
425 }
426  
427 sub redraw {
428 my $self = shift;
429 $self->win1->draw(1);
430 }
431  
432 1;
↑ Summary