package EC2TestSupport; use base 'Exporter'; use POSIX 'setsid','setpgid','tcsetpgrp'; our @EXPORT_OK = qw(setup_environment reset_declined reset_cache TEST_IMAGE confirm_payment); our @EXPORT = @EXPORT_OK; use constant TEST_IMAGE => 'ami-4a936a23'; # Nano natty - only 1 GB in size! sub reset_declined { unlink ('.declined','.payment_confirmed','.payment_declined') } sub reset_cache { unlink '.credentials' } sub setup_environment { return if -e '.declined'; unless ($ENV{EC2_ACCESS_KEY} && $ENV{EC2_SECRET_KEY}) { read_from_cache() && return 1; eval { local $SIG{INT} = sub {warn "Interrupted!\n";die "interrupted" }; local $SIG{ALRM} = sub {warn "Timeout!\n" ;die "timeout"}; print STDERR <.declined'; close $f; return; } write_to_cache(); } 1; } sub write_to_cache { open my $f,'>.credentials'; print $f "EC2_ACCESS_KEY=$ENV{EC2_ACCESS_KEY}\n"; print $f "EC2_SECRET_KEY=$ENV{EC2_SECRET_KEY}\n"; close $f; } sub read_from_cache { open my $f,'.credentials' or return; chmod 0600,'.credentials'; while (<$f>) { chomp; my($key,$value) = split '='; $ENV{$key}||=$value; } return 1 if $ENV{EC2_ACCESS_KEY} && $ENV{EC2_SECRET_KEY}; } sub confirm_payment { my $msg = shift; return 1 if -e '.payment_confirmed'; return 0 if -e '.payment_declined'; print STDERR $msg; print STDERR "Do you want to proceed? [Y/n] "; my $result = eval { local $SIG{ALRM} = sub {warn "Timeout!\n"; die 'timeout'}; alarm(15); chomp(my $input = <>); $input ||= 'y'; $input =~ /^[yY]/; }; alarm(0); my $filename = $result ? '.payment_confirmed' : '.payment_declined'; open my $f,">$filename"; close $f; return $result; } sub msg { my $msg = shift; print STDERR $msg; alarm(10); chomp (my $result = <>); $result; } 1;