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,
24 Create instances in the Amazon EC2 (Elastic compute cloud). Install
25 Net::Amazon::EC2 perl module. Advisable to set svc_external-skip_manual config
30 $replace_ok_kludge = 0;
32 sub rebless { shift; }
35 my($self, $svc_external) = (shift, shift);
36 $err_or_queue = $self->amazon_ec2_queue( $svc_external->svcnum, 'insert',
37 $svc_external->svcnum,
39 $self->option('keyname'),
41 ref($err_or_queue) ? '' : $err_or_queue;
45 my( $self, $new, $old ) = (shift, shift, shift);
46 return '' if $replace_ok_kludge;
47 return "can't change instance id or IP address";
48 #$err_or_queue = $self->amazon_ec2_queue( $new->svcnum,
49 # 'replace', $new->username, $new->_password );
50 #ref($err_or_queue) ? '' : $err_or_queue;
54 my( $self, $svc_external ) = (shift, shift);
55 my( $instance_id, $ip ) = split(/:/, $svc_external->title );
56 $err_or_queue = $self->amazon_ec2_queue( $svc_external->svcnum, 'delete',
60 ref($err_or_queue) ? '' : $err_or_queue;
63 #these three are optional
64 # fallback for svc_acct will change and restore password
65 #sub _export_suspend {
66 # my( $self, $svc_something ) = (shift, shift);
67 # $err_or_queue = $self->amazon_ec2_queue( $svc_something->svcnum,
68 # 'suspend', $svc_something->username );
69 # ref($err_or_queue) ? '' : $err_or_queue;
72 #sub _export_unsuspend {
73 # my( $self, $svc_something ) = (shift, shift);
74 # $err_or_queue = $self->amazon_ec2_queue( $svc_something->svcnum,
75 # 'unsuspend', $svc_something->username );
76 # ref($err_or_queue) ? '' : $err_or_queue;
80 my($self, $svc_external, $arrayref) = (shift, shift, shift);
81 my( $instance_id, $ip ) = split(/:/, $svc_external->title );
83 push @$arrayref, qq!<A HREF="http://$ip/">http://$ip/</A>!;
89 #a good idea to queue anything that could fail or take any time
90 sub amazon_ec2_queue {
91 my( $self, $svcnum, $method ) = (shift, shift, shift);
92 my $queue = new FS::queue {
94 'job' => "FS::part_export::amazon_ec2::amazon_ec2_$method",
96 $queue->insert( $self->option('access_key'),
97 $self->option('secret_key'),
104 my( $access_key, $secret_key, @rest ) = @_;
106 eval 'use Net::Amazon::EC2;';
109 my $ec2 = new Net::Amazon::EC2 'AWSAccessKeyId' => $access_key,
110 'SecretAccessKey' => $secret_key;
115 sub amazon_ec2_insert { #subroutine, not method
116 my( $ec2, $svcnum, $ami, $keyname ) = amazon_ec2_new(@_);
118 my $reservation_info = $ec2->run_instances( 'ImageId' => $ami,
119 'KeyName' => $keyname,
124 my $instance_id = $reservation_info->instances_set->[0]->instance_id;
126 my $ip = $ec2->allocate_address
127 or die "can't allocate address";
128 $ec2->associate_address('InstanceId' => $instance_id,
131 or die "can't assocate IP address $ip with instance $instance_id";
133 my $svc_external = qsearchs('svc_external', { 'svcnum' => $svcnum } )
134 or die "can't find svc_external.svcnum $svcnum\n";
136 $svc_external->title("$instance_id:$ip");
138 local($replace_ok_kludge) = 1;
139 my $error = $svc_external->replace;
140 die $error if $error;
144 #sub amazon_ec2_replace { #subroutine, not method
147 sub amazon_ec2_delete { #subroutine, not method
148 my( $ec2, $id, $ip ) = amazon_ec2_new(@_);
150 my $instance_id = sprintf('i-%x', $id);
151 $ec2->disassociate_address('PublicIp'=>$ip)
152 or die "can't dissassocate $ip";
154 $ec2->release_address('PublicIp'=>$ip)
155 or die "can't release $ip";
157 my $result = $ec2->terminate_instances('InstanceId'=>$instance_id);
158 #check for instance_id match or something?
162 #sub amazon_ec2_suspend { #subroutine, not method
165 #sub amazon_ec2_unsuspend { #subroutine, not method