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 #option to turn off (or on) ip address allocation
19 'svc' => 'svc_external',
21 'Export to Amazon EC2',
22 'options' => \%options,
25 Create instances in the Amazon EC2 (Elastic compute cloud). Install
26 Net::Amazon::EC2 perl module. Advisable to set svc_external-skip_manual config
31 $replace_ok_kludge = 0;
33 sub rebless { shift; }
36 my($self, $svc_external) = (shift, shift);
37 $err_or_queue = $self->amazon_ec2_queue( $svc_external->svcnum, 'insert',
38 $svc_external->svcnum,
40 $self->option('keyname'),
42 ref($err_or_queue) ? '' : $err_or_queue;
46 my( $self, $new, $old ) = (shift, shift, shift);
47 return '' if $replace_ok_kludge;
48 return "can't change instance id or IP address";
49 #$err_or_queue = $self->amazon_ec2_queue( $new->svcnum,
50 # 'replace', $new->username, $new->_password );
51 #ref($err_or_queue) ? '' : $err_or_queue;
55 my( $self, $svc_external ) = (shift, shift);
56 my( $instance_id, $ip ) = split(/:/, $svc_external->title );
57 $err_or_queue = $self->amazon_ec2_queue( $svc_external->svcnum, 'delete',
61 ref($err_or_queue) ? '' : $err_or_queue;
64 #these three are optional
65 # fallback for svc_acct will change and restore password
66 #sub _export_suspend {
67 # my( $self, $svc_something ) = (shift, shift);
68 # $err_or_queue = $self->amazon_ec2_queue( $svc_something->svcnum,
69 # 'suspend', $svc_something->username );
70 # ref($err_or_queue) ? '' : $err_or_queue;
73 #sub _export_unsuspend {
74 # my( $self, $svc_something ) = (shift, shift);
75 # $err_or_queue = $self->amazon_ec2_queue( $svc_something->svcnum,
76 # 'unsuspend', $svc_something->username );
77 # ref($err_or_queue) ? '' : $err_or_queue;
81 my($self, $svc_external, $arrayref) = (shift, shift, shift);
82 my( $instance_id, $ip ) = split(/:/, $svc_external->title );
84 push @$arrayref, qq!<A HREF="http://$ip/">http://$ip/</A>!;
90 #a good idea to queue anything that could fail or take any time
91 sub amazon_ec2_queue {
92 my( $self, $svcnum, $method ) = (shift, shift, shift);
93 my $queue = new FS::queue {
95 'job' => "FS::part_export::amazon_ec2::amazon_ec2_$method",
97 $queue->insert( $self->option('access_key'),
98 $self->option('secret_key'),
105 my( $access_key, $secret_key, @rest ) = @_;
107 eval 'use Net::Amazon::EC2;';
110 my $ec2 = new Net::Amazon::EC2 'AWSAccessKeyId' => $access_key,
111 'SecretAccessKey' => $secret_key;
116 sub amazon_ec2_insert { #subroutine, not method
117 my( $ec2, $svcnum, $ami, $keyname ) = amazon_ec2_new(@_);
119 my $reservation_info = $ec2->run_instances( 'ImageId' => $ami,
120 'KeyName' => $keyname,
125 my $instance_id = $reservation_info->instances_set->[0]->instance_id;
127 my $ip = $ec2->allocate_address
128 or die "can't allocate address";
129 $ec2->associate_address('InstanceId' => $instance_id,
132 or die "can't assocate IP address $ip with instance $instance_id";
134 my $svc_external = qsearchs('svc_external', { 'svcnum' => $svcnum } )
135 or die "can't find svc_external.svcnum $svcnum\n";
137 $svc_external->title("$instance_id:$ip");
139 local($replace_ok_kludge) = 1;
140 my $error = $svc_external->replace;
141 die $error if $error;
145 #sub amazon_ec2_replace { #subroutine, not method
148 sub amazon_ec2_delete { #subroutine, not method
149 my( $ec2, $id, $ip ) = amazon_ec2_new(@_);
151 my $instance_id = sprintf('i-%x', $id);
152 $ec2->disassociate_address('PublicIp'=>$ip)
153 or die "can't dissassocate $ip";
155 $ec2->release_address('PublicIp'=>$ip)
156 or die "can't release $ip";
158 my $result = $ec2->terminate_instances('InstanceId'=>$instance_id);
159 #check for instance_id match or something?
163 #sub amazon_ec2_suspend { #subroutine, not method
166 #sub amazon_ec2_unsuspend { #subroutine, not method