1 package FS::part_export::amazon_ec2;
3 use base qw( FS::part_export );
5 use vars qw(@ISA %info $replace_ok_kludge);
7 use FS::Record qw( qsearchs );
10 tie my %options, 'Tie::IxHash',
11 'access_key' => { label => 'AWS access key', },
12 'secret_key' => { label => 'AWS secret key', },
13 'ami' => { label => 'AMI', 'default' => 'ami-ff46a796', },
14 'keyname' => { label => 'Keypair name', },
15 'region' => { label => 'Region', },
16 'InstanceType' => { label => 'Instance Type', },
17 #option to turn off (or on) ip address allocation
21 'svc' => 'svc_external',
23 'Export to Amazon EC2',
24 'options' => \%options,
27 Create instances in the Amazon EC2 (Elastic compute cloud). Install
28 Net::Amazon::EC2 perl module. Advisable to set svc_external-skip_manual config
33 $replace_ok_kludge = 0;
35 sub rebless { shift; }
38 my($self, $svc_external) = (shift, shift);
39 $err_or_queue = $self->amazon_ec2_queue( $svc_external->svcnum, 'insert',
40 $svc_external->svcnum,
42 $self->option('keyname'),
43 $self->option('InstanceType'),
45 ref($err_or_queue) ? '' : $err_or_queue;
49 my( $self, $new, $old ) = (shift, shift, shift);
50 return '' if $replace_ok_kludge;
51 return "can't change instance id or IP address";
52 #$err_or_queue = $self->amazon_ec2_queue( $new->svcnum,
53 # 'replace', $new->username, $new->_password );
54 #ref($err_or_queue) ? '' : $err_or_queue;
58 my( $self, $svc_external ) = (shift, shift);
59 my( $instance_id, $ip ) = split(/:/, $svc_external->title );
60 $err_or_queue = $self->amazon_ec2_queue( $svc_external->svcnum, 'delete',
64 ref($err_or_queue) ? '' : $err_or_queue;
67 #these three are optional
68 # fallback for svc_acct will change and restore password
69 #sub _export_suspend {
70 # my( $self, $svc_something ) = (shift, shift);
71 # $err_or_queue = $self->amazon_ec2_queue( $svc_something->svcnum,
72 # 'suspend', $svc_something->username );
73 # ref($err_or_queue) ? '' : $err_or_queue;
76 #sub _export_unsuspend {
77 # my( $self, $svc_something ) = (shift, shift);
78 # $err_or_queue = $self->amazon_ec2_queue( $svc_something->svcnum,
79 # 'unsuspend', $svc_something->username );
80 # ref($err_or_queue) ? '' : $err_or_queue;
84 my($self, $svc_external, $arrayref) = (shift, shift, shift);
85 my( $instance_id, $ip ) = split(/:/, $svc_external->title );
87 push @$arrayref, qq!<A HREF="http://$ip/">http://$ip/</A>!;
93 #a good idea to queue anything that could fail or take any time
94 sub amazon_ec2_queue {
95 my( $self, $svcnum, $method ) = (shift, shift, shift);
96 my $queue = new FS::queue {
98 'job' => "FS::part_export::amazon_ec2::amazon_ec2_$method",
100 $queue->insert( $self->option('access_key'),
101 $self->option('secret_key'),
102 $self->option('region'),
109 my( $access_key, $secret_key, $region, @rest ) = @_;
111 eval 'use Net::Amazon::EC2;';
114 my $ec2 = new Net::Amazon::EC2 'AWSAccessKeyId' => $access_key,
115 'SecretAccessKey' => $secret_key,
116 'region' => $region || 'us-east-1',
121 sub amazon_ec2_insert { #subroutine, not method
122 my( $ec2, $svcnum, $ami, $keyname, $InstanceType ) = amazon_ec2_new(@_);
124 my $reservation_info = $ec2->run_instances(
126 'KeyName' => $keyname,
127 'InstanceType' => $InstanceType || 'm1.small',
132 my $instance_id = $reservation_info->instances_set->[0]->instance_id;
134 my $ip = $ec2->allocate_address
135 or die "can't allocate address";
136 $ec2->associate_address('InstanceId' => $instance_id,
139 or die "can't assocate IP address $ip with instance $instance_id";
141 my $svc_external = qsearchs('svc_external', { 'svcnum' => $svcnum } )
142 or die "can't find svc_external.svcnum $svcnum\n";
144 $svc_external->title("$instance_id:$ip");
146 local($replace_ok_kludge) = 1;
147 my $error = $svc_external->replace;
148 die $error if $error;
152 #sub amazon_ec2_replace { #subroutine, not method
155 sub amazon_ec2_delete { #subroutine, not method
156 my( $ec2, $id, $ip ) = amazon_ec2_new(@_);
158 my $instance_id = sprintf('i-%x', $id);
159 $ec2->disassociate_address('PublicIp'=>$ip)
160 or die "can't dissassocate $ip";
162 $ec2->release_address('PublicIp'=>$ip)
163 or die "can't release $ip";
165 my $result = $ec2->terminate_instances('InstanceId'=>$instance_id);
166 #check for instance_id match or something?
170 #sub amazon_ec2_suspend { #subroutine, not method
173 #sub amazon_ec2_unsuspend { #subroutine, not method