This file is indexed.

/usr/share/perl5/Rex/Commands/Cloud.pm is in rex 1.4.1-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
#
# (c) Jan Gehring <jan.gehring@gmail.com>
#
# vim: set ts=2 sw=2 tw=0:
# vim: set expandtab:

=head1 NAME

Rex::Commands::Cloud - Cloud Management Commands

=head1 DESCRIPTION

With this Module you can manage different Cloud services. Currently it supports Amazon EC2, Jiffybox and OpenStack.

Version <= 1.0: All these functions will not be reported.

=head1 SYNOPSIS

 use Rex::Commands::Cloud;

 cloud_service "Amazon";
 cloud_auth "your-access-key", "your-private-access-key";
 cloud_region "ec2.eu-west-1.amazonaws.com";

 task "list", sub {
   print Dumper cloud_instance_list;
   print Dumper cloud_volume_list;
 };

 task "create", sub {
   my $vol_id = cloud_volume create => { size => 1, zone => "eu-west-1a", };

   cloud_instance create => {
       image_id => "ami-xxxxxxx",
       name    => "test01",
       key    => "my-key",
       volume  => $vol_id,
       zone    => "eu-west-1a",
     };
 };

 task "destroy", sub {
   cloud_volume detach => "vol-xxxxxxx";
   cloud_volume delete => "vol-xxxxxxx";

   cloud_instance terminate => "i-xxxxxxx";
 };

=head1 EXPORTED FUNCTIONS

=cut

package Rex::Commands::Cloud;

use strict;
use warnings;

our $VERSION = '1.4.1'; # VERSION

require Rex::Exporter;
use base qw(Rex::Exporter);
use vars qw(@EXPORT $cloud_service $cloud_region @cloud_auth);

use Rex::Logger;
use Rex::Config;
use Rex::Cloud;
use Rex::Group::Entry::Server;

@EXPORT = qw(cloud_instance cloud_volume cloud_network
  cloud_instance_list cloud_volume_list cloud_network_list
  cloud_service cloud_auth cloud_region
  get_cloud_instances_as_group get_cloud_regions get_cloud_availability_zones
  get_cloud_plans
  get_cloud_operating_systems
  cloud_image_list
  cloud_object
  get_cloud_floating_ip
  cloud_upload_key);

Rex::Config->register_set_handler(
  "cloud" => sub {
    my ( $name, @options ) = @_;
    my $sub_name = "cloud_$name";

    if ( $name eq "service" ) {
      cloud_service(@options);
    }

    if ( $name eq "auth" ) {
      cloud_auth(@options);
    }

    if ( $name eq "region" ) {
      cloud_region(@options);
    }
  }
);

=head2 cloud_service($cloud_service)

Define which cloud service to use.

=over 4

=item Services

=over 4

=item Amazon

=item Jiffybox

=item OpenStack

=back

=back


=cut

sub cloud_service {
  ($cloud_service) = @_;

  # set retry counter to a higher value
  if ( Rex::Config->get_max_connect_fails() < 5 ) {
    Rex::Config->set_max_connect_fails(15);
  }
}

=head2 cloud_auth($param1, $param2, ...)

Set the authentication for the cloudservice.

For example for Amazon it is:

 cloud_auth($access_key, $secret_access_key);

For JiffyBox:

 cloud_auth($auth_key);

For OpenStack:

 cloud_auth(
  tenant_name => 'tenant',
  username    => 'user',
  password    => 'password',
 );

=cut

sub cloud_auth {
  @cloud_auth = @_;
}

=head2 cloud_region($region)

Set the cloud region.

=cut

sub cloud_region {
  ($cloud_region) = @_;
}

=head2 cloud_instance_list

Get all instances of a cloud service.

 task "list", sub {
   for my $instance (cloud_instance_list()) {
     say "Arch  : " . $instance->{"architecture"};
     say "IP   : " . $instance->{"ip"};
     say "ID   : " . $instance->{"id"};
     say "State : " . $instance->{"state"};
   }
 };

There are some parameters for this function that can change the gathering of ip addresses for some cloud providers (like OpenStack).

 task "list", sub {
   my @instances = cloud_instance_list 
                      private_network => 'private',
                      public_network  => 'public',
                      public_ip_type  => 'floating',
                      private_ip_type => 'fixed';
 };

=cut

sub cloud_instance_list {
  return cloud_object()->list_instances(@_);
}

=head2 cloud_volume_list

Get all volumes of a cloud service.

 task "list-volumes", sub {
   for my $volume (cloud_volume_list()) {
     say "ID     : " . $volume->{"id"};
     say "Zone    : " . $volume->{"zone"};
     say "State   : " . $volume->{"state"};
     say "Attached : " . $volume->{"attached_to"};
   }
 };

=cut

sub cloud_volume_list {
  return cloud_object()->list_volumes();
}

=head2 cloud_network_list

Get all networks of a cloud service.

 task "network-list", sub {
   for my $network (cloud_network_list()) {
     say "network  : " . $network->{network};
     say "name    : " . $network->{name};
     say "id     : " . $network->{id};
   }
 };

=cut

sub cloud_network_list {
  return cloud_object()->list_networks();
}

=head2 cloud_image_list

Get a list of all available cloud images.

=cut

sub cloud_image_list {
  return cloud_object()->list_images();
}

=head2 cloud_upload_key

Upload public SSH key to cloud provider

 private_key '~/.ssh/mykey
 public_key  '~/.ssh/mykey.pub';
 
 task "cloudprovider", sub {
   cloud_upload_key;

   cloud_instance create => {
     ...
   };
 };

=cut

sub cloud_upload_key {
  return cloud_object()->upload_key();
}

=head2 get_cloud_instances_as_group

Get a list of all running instances of a cloud service. This can be used for a I<group> definition.

 group fe  => "fe01", "fe02", "fe03";
 group ec2 => get_cloud_instances_as_group();

=cut

sub get_cloud_instances_as_group {

  my @list = cloud_object()->list_running_instances();

  my @ret;

  for my $instance (@list) {
    push( @ret, Rex::Group::Entry::Server->new( name => $instance->{"ip"} ) );
  }

  return @ret;
}

=head2 cloud_instance($action, $data)

This function controls all aspects of a cloud instance.

=cut

sub cloud_instance {

  my ( $action, $data ) = @_;
  my $cloud = cloud_object();

  if ( $action eq "list" ) {
    return $cloud->list_running_instances();
  }

=head2 create

Create a new instance.

 cloud_instance create => {
     image_id => "ami-xxxxxx",
     key    => "ssh-key",
     name    => "fe-ec2-01",  # name is not necessary
     volume  => "vol-yyyyy",  # volume is not necessary
     zone    => "eu-west-1a",  # zone is not necessary
     floating_ip  => "89.39.38.160" # floating_ip is not necessary
   };

=cut

  elsif ( $action eq "create" ) {
    my %data_hash = (

      # image_id => $data->{"image_id"},
      # name    => $data->{"name"} || undef,
      # key    => $data->{"key"} || undef,
      # zone    => $data->{"zone"} || undef,
      # volume  => $data->{"volume"} || undef,
      # password => $data->{"password"} || undef,
      # plan_id  => $data->{"plan_id"} || undef,
      # type    => $data->{"type"} || undef,
      # security_group => $data->{"security_group"} || undef,
      %{$data},
    );

    $cloud->run_instance(%data_hash);
  }

=head2 start

Start an existing instance

 cloud_instance start => "instance-id";

=cut

  elsif ( $action eq "start" ) {
    $cloud->start_instance( instance_id => $data );
  }

=head2 stop

Stop an existing instance

 cloud_instance stop => "instance-id";

=cut

  elsif ( $action eq "stop" ) {
    $cloud->stop_instance( instance_id => $data );
  }

=head2 terminate

Terminate an instance. This will destroy all data and remove the instance.

 cloud_instance terminate => "i-zzzzzzz";

=cut

  elsif ( $action eq "terminate" ) {
    $cloud->terminate_instance( instance_id => $data );
  }

}

=head2 get_cloud_regions

Returns all regions as an array.

=cut

sub get_cloud_regions {
  return cloud_object()->get_regions;
}

=head2 cloud_volume($action , $data)

This function controls all aspects of a cloud volume.

=cut

sub cloud_volume {

  my ( $action, @_data ) = @_;
  my $data;
  if ( @_data == 1 ) {
    if ( ref $_data[0] ) {
      $data = $_data[0];
    }
    else {
      $data = { id => $_data[0] };
    }
  }
  else {
    $data = { "id", @_data };
  }

  my $cloud = cloud_object();

=head2 create

Create a new volume. Size is in Gigabytes.

 task "create-vol", sub {
   my $vol_id = cloud_volume create => { size => 1, zone => "eu-west-1a", };
 };

=cut

  if ( $action eq "create" ) {
    $cloud->create_volume(
      size => $data->{"size"} || 1,
      %{$data},
    );
  }

=head2 attach

Attach a volume to an instance.

 task "attach-vol", sub {
   cloud_volume attach => "vol-xxxxxx", to => "server-id";
 };

=cut

  elsif ( $action eq "attach" ) {
    my $vol_id = $data->{id};
    my $srv_id = $data->{to};

    $cloud->attach_volume(
      volume_id   => $vol_id,
      server_id   => $srv_id,
      device_name => $data->{device}
    );
  }

=head2 detach

Detach a volume from an instance.

 task "detach-vol", sub {
   cloud_volume detach => "vol-xxxxxx", from => "server-id";
 };

=cut

  elsif ( $action eq "detach" ) {
    my $vol_id = $data->{id};
    my $srv_id = $data->{from};

    $cloud->detach_volume(
      volume_id => $vol_id,
      server_id => $srv_id,
      attach_id => $data->{attach_id}
    );
  }

=head2 delete

Delete a volume. This will destroy all data.

 task "delete-vol", sub {
   cloud_volume delete => "vol-xxxxxx";
 };

=cut

  elsif ( $action eq "delete" ) {
    $cloud->delete_volume( volume_id => $data->{id} );
  }

  elsif ( $action eq "list" ) {
    return $cloud->list_volumes();
  }

}

=head2 get_cloud_floating_ip

Returns first available floating IP

 task "get_floating_ip", sub {

   my $ip = get_cloud_floating_ip;

   my $instance = cloud_instance create => {
      image_id => 'edffd57d-82bf-4ffe-b9e8-af22563741bf',
      name => 'instance1',
      plan_id => 17,
      floating_ip => $ip
    };
 };

=cut

sub get_cloud_floating_ip {
  return cloud_object()->get_floating_ip;
}

=head2 cloud_network

=cut

sub cloud_network {

  my ( $action, $data ) = @_;
  my $cloud = cloud_object();

=head2 create

Create a new network.

 task "create-net", sub {
   my $net_id = cloud_network create => { cidr => '192.168.0.0/24', name => "mynetwork", };
 };

=cut

  if ( $action eq "create" ) {
    $cloud->create_network( %{$data} );
  }

=head2 delete

Delete a network.

 task "delete-net", sub {
   cloud_network delete => '18a4ccf8-f14a-a10d-1af4-4ac7fee08a81';
 };

=cut

  elsif ( $action eq "delete" ) {
    $cloud->delete_network($data);
  }
}

=head2 get_cloud_availability_zones

Returns all availability zones of a cloud services. If available.

 task "get-zones", sub {
   print Dumper get_cloud_availability_zones;
 };

=cut

sub get_cloud_availability_zones {
  return cloud_object()->get_availability_zones();
}

=head2 get_cloud_plans

Retrieve information of the available cloud plans. If supported.

=cut

sub get_cloud_plans {
  return cloud_object()->list_plans;
}

=head2 get_cloud_operating_systems

Retrieve information of the available cloud plans. If supported.

=cut

sub get_cloud_operating_systems {
  return cloud_object()->list_operating_systems;
}

=head2 cloud_object

Returns the cloud object itself.

=cut

sub cloud_object {
  my $cloud = get_cloud_service($cloud_service);

  $cloud->set_auth(@cloud_auth);
  $cloud->set_endpoint($cloud_region);

  return $cloud;
}

1;