Changeset 21205 for lang/perl/Net-Amazon-HadoopEC2
- Timestamp:
- 10/13/08 02:21:16 (5 years ago)
- Location:
- lang/perl/Net-Amazon-HadoopEC2/trunk
- Files:
-
- 2 added
- 2 modified
-
lib/Net/Amazon/HadoopEC2 (added)
-
lib/Net/Amazon/HadoopEC2.pm (modified) (3 diffs)
-
lib/Net/Amazon/HadoopEC2/Cluster.pm (added)
-
t/01_module.t (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/perl/Net-Amazon-HadoopEC2/trunk/lib/Net/Amazon/HadoopEC2.pm
r20994 r21205 1 1 package Net::Amazon::HadoopEC2; 2 use 5.008000;3 2 use Moose; 3 use Net::Amazon::EC2; 4 use Net::Amazon::HadoopEC2::Cluster; 4 5 our $VERSION = '0.01'; 5 6 6 has 'aws_access_key_id' => ( 7 is => 'rw', 8 isa => 'Str', 7 has aws_access_key_id => ( is => 'ro', isa => 'Str', required => 1 ); 8 has aws_secret_access_key => ( is => 'ro', isa => 'Str', required => 1 ); 9 has aws_account_id => ( is => 'ro', isa => 'Str', required => 1 ); 10 11 has ec2 => ( 12 is => 'ro', 13 isa => 'Net::Amazon::EC2', 9 14 required => 1, 15 lazy => 1, 16 default => sub { 17 Net::Amazon::EC2->new( 18 AWSAccessKeyId => $_[0]->aws_access_key_id, 19 SecretAccessKey => $_[0]->aws_secret_access_key, 20 ); 21 } 10 22 ); 11 23 12 has 'aws_secret_access_key' => (13 is => 'rw',14 isa => 'Str',15 required => 1,16 );17 18 24 no Moose; 25 26 sub launch_cluster { 27 my ($self, $args) = @_; 28 $self->_assert_group({group => $args->{name}}) or return; 29 my $cluster = Net::Amazon::HadoopEC2::Cluster->new( 30 { 31 ec2 => $self->ec2, 32 name => $args->{name}, 33 image_id => $args->{image_id}, 34 slaves => $args->{slaves} || 2, 35 key_name => $args->{key_name} || 'gsg-keypair', 36 key_file => $args->{key_file}, 37 } 38 ); 39 $cluster->launch or return; 40 return $cluster; 41 } 42 43 sub _assert_group { 44 my ($self, $args) = @_; 45 my @groups = ($args->{group}, "$args->{group}-master"); 46 my $g = $self->ec2->describe_security_groups( 47 GroupName => [ @groups ], 48 ); 49 if (ref $g eq 'Net::Amazon::EC2::Errors') { 50 $g->errors->[0]->code eq 'InvalidGroup.NotFound' or return; 51 } else { 52 return scalar @{$g} == 2 ? 1 : 0; 53 } 54 return $self->_create_group($args); 55 } 56 57 sub _create_group { 58 my ($self, $args) = @_; 59 my @groups = ($args->{group}, "$args->{group}-master"); 60 for my $target ( @groups ) { 61 my $desc = 'Group for Hadoop ' . ($target =~ m{master} ? 'Master' : 'Slaves') . '.'; 62 $self->ec2->create_security_group( 63 GroupName => $target, 64 GroupDescription => $desc, 65 ) == 1 or return; 66 } 67 my $success = 0; 68 my $result; 69 for my $target ( @groups ) { 70 my ($peer_target) = grep {$_ ne $target} @groups; 71 $result = $self->ec2->authorize_security_group_ingress( 72 GroupName => $target, 73 IpProtocol => 'tcp', 74 FromPort => 22, 75 ToPort => 22, 76 CidrIp => '0.0.0.0/0', 77 ); 78 $result == 1 or last; 79 $result = $self->ec2->authorize_security_group_ingress( 80 GroupName => $target, 81 SourceSecurityGroupName => $peer_target, 82 SourceSecurityGroupOwnerId => $self->aws_account_id, 83 ); 84 $result == 1 or last; 85 $result = $self->ec2->authorize_security_group_ingress( 86 GroupName => $target, 87 SourceSecurityGroupName => $target, 88 SourceSecurityGroupOwnerId => $self->aws_account_id, 89 ); 90 $result == 1 or last; 91 if ( $args->{web_ports} ) { 92 $result = $self->ec2->authorize_security_group_ingress( 93 GroupName => $target, 94 IpProtocol => 'tcp', 95 FromPort => 50030, 96 ToPort => 50030, 97 CidrIp => '0.0.0.0/0', 98 ); 99 $result == 1 or last; 100 $result = $self->ec2->authorize_security_group_ingress( 101 GroupName => $target, 102 IpProtocol => 'tcp', 103 FromPort => 50060, 104 ToPort => 50060, 105 CidrIp => '0.0.0.0/0', 106 ); 107 $result == 1 or last; 108 } 109 $success++; 110 } 111 return $success == 2; 112 } 113 114 sub _remove_group { 115 my ($self, $args) = @_; 116 my @groups = ($args->{group}, "$args->{group}-master"); 117 my $success = 0; 118 my $result; 119 for my $target ( @groups ) { 120 my ($peer_target) = grep {$_ ne $target} @groups; 121 $result = $self->ec2->revoke_security_group_ingress( 122 GroupName => $target, 123 SourceSecurityGroupName => $peer_target, 124 SourceSecurityGroupOwnerId => $self->aws_account_id, 125 ); 126 $result == 1 or last; 127 $success++; 128 } 129 $success == 2 or return; 130 for my $target ( @groups ) { 131 $result = $self->ec2->delete_security_group( 132 GroupName => $target, 133 ); 134 $result == 1 or last; 135 $success++; 136 } 137 return $success == 4; 138 } 139 19 140 1; 20 141 … … 31 152 my $hadoop = Net::Amazon::HadoopEC2->new( 32 153 { 154 aws_account_id => 'your_aws_account', 33 155 aws_access_key_id => 'your_key', 34 156 aws_secret_access_key => 'your_secret', … … 38 160 $hadoop->launch_cluster( 39 161 { 40 hadoop_version => '0.18.1',41 group => 'hadoop',162 name => 'hadoop', 163 image_id => 'ami-b0fe1ad9', 42 164 slaves => 2, 43 165 } -
lang/perl/Net-Amazon-HadoopEC2/trunk/t/01_module.t
r20808 r21205 2 2 use warnings; 3 3 use Test::More; 4 use File::Spec; 4 use Digest::MD5 qw(md5_hex); 5 use IO::All; 6 use File::Temp; 7 use File::Basename; 5 8 6 9 BEGIN { 7 for (qw( AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY)) {10 for (qw( AWS_ACCOUNT_ID AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY )) { 8 11 unless ($ENV{$_}) { 9 12 plan skip_all => "set $_ to run this test."; … … 11 14 } 12 15 } 13 plan 'no_plan';16 plan tests => 20; 14 17 use_ok 'Net::Amazon::HadoopEC2'; 15 18 } … … 17 20 my $hadoop = Net::Amazon::HadoopEC2->new( 18 21 { 22 aws_account_id => $ENV{AWS_ACCOUNT_ID}, 19 23 aws_access_key_id => $ENV{AWS_ACCESS_KEY_ID}, 20 24 aws_secret_access_key => $ENV{AWS_SECRET_ACCESS_KEY}, … … 22 26 ); 23 27 isa_ok $hadoop, 'Net::Amazon::HadoopEC2'; 28 isa_ok $hadoop->ec2, 'Net::Amazon::EC2'; 29 is $hadoop->ec2->AWSAccessKeyId, $ENV{AWS_ACCESS_KEY_ID}; 30 is $hadoop->ec2->SecretAccessKey, $ENV{AWS_SECRET_ACCESS_KEY}; 24 31 25 my $cluster = $hadoop->launch_cluster( 32 { 33 my $res; 34 my $group_name = substr(md5_hex($$, time),0,8); 35 ok $hadoop->_assert_group({group => $group_name}); 36 $res = $hadoop->ec2->describe_security_groups(GroupName => [$group_name, "$group_name-master"]); 37 is scalar @{$res}, 2; 38 ok $hadoop->_remove_group({group => $group_name}); 39 $res = $hadoop->ec2->describe_security_groups(GroupName => [$group_name, "$group_name-master"]); 40 isa_ok $res, 'Net::Amazon::EC2::Errors'; 41 is scalar @{$res->errors}, 1; 42 is $res->errors->[0]->code, 'InvalidGroup.NotFound'; 43 } 44 45 { 46 my $cluster = $hadoop->launch_cluster( 47 { 48 name => 'hadoop', 49 image_id => 'ami-b0fe1ad9', # hadoop-ec2 official image 50 slaves => 2, 51 key_name => 'gsg-keypair', 52 key_file => "$ENV{HOME}/.ssh/id_rsa-gsg-keypair", 53 } 54 ); 55 isa_ok $cluster, 'Net::Amazon::HadoopEC2::Cluster'; 56 isa_ok $cluster->master_instance, 'Net::Amazon::EC2::RunningInstances'; 57 my $res = $hadoop->ec2->describe_instances; 58 26 59 { 27 version => '0.18.1', 28 group => 'hadoop', 29 slave => 2, 30 type => 'small', 60 my $file_push = File::Temp->new; 61 print $file_push 'hoge'; 62 close $file_push; 63 ok $cluster->push_files( 64 { 65 files => [$file_push->filename], 66 destination => '/mnt', 67 } 68 ); 69 my $remote = $cluster->execute( { command => 'ls /mnt' } ); 70 ok grep {$_ eq basename($file_push->filename)} split( /\n/, $remote->{stdout} ); 71 my $file_get = File::Temp->new; 72 close $file_get; 73 $cluster->get_files( 74 { 75 files => [ File::Spec->catfile('/mnt', basename($file_push->filename)) ], 76 destination => $file_get->filename, 77 } 78 ); 79 my $content < io $file_get->filename; 80 is $content, 'hoge'; 31 81 } 32 );33 isa_ok $cluster, 'Net::Amazon::HadoopEC2::Cluster';34 82 35 my $file = File::Spec->catfile(qw(t data sample.txt));36 $cluster->push(37 83 { 38 files => [qw($file)], 39 destination => '/mnt', 84 my $command = join ' ', qw( 85 /usr/local/hadoop-0.18.0/bin/hadoop 86 jar 87 /usr/local/hadoop-0.18.0/hadoop-0.18.0-examples.jar 88 pi 10 100 89 ); 90 my $result = $cluster->execute( { command => $command, } ); 91 my ($pi) = grep { /^Estimated value of PI is/} split( /\n/, $result->{stdout} ); 92 ok $pi, $pi; 40 93 } 41 ); 42 my $remote = $cluster->excute('ls /mnt'); 43 ok grep {$_ eq 'sample.txt'} $remote; 94 95 { 96 my $res = $cluster->terminate; 97 isa_ok $res, 'ARRAY'; 98 is scalar @{$res}, 3; 99 isa_ok $res->[0], 'Net::Amazon::EC2::TerminateInstancesResponse'; 100 } 101 }
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)