#!/usr/bin/perl use Net::Amazon::EC2; ##-------------------------------------------------------------- ## This must be set to something valid ... my $MY_AWS_ACCESS_KEY_ID = ""; my $MY_AWS_SECRET_ACCESS_KEY = ""; my $EXTERNAL_DNS_SERVER = ""; ##-------------------------------------------------------------- ## ## This is a rough script for making EC2 server changes necessary ## to allow a full automatic installation of UniCluster Express ## to occur. Use at your own risk. ## ## This is the summary of changes that are made: ## ## (1) Replace existing /etc/hosts with one that explicitly ## lists the public and private hostnames of the EC2 instance ## ## (2) Change the hostname of the EC2 instance from the private name ## to the public "...amazonaws.com" name ## ## (3) EC2 instances only have a private internal IP that gets NATed ## when communicating outside of the cloud. In order to configure ## Globus GridFTP to work properly we need to capture the IP address ## used as a source for outbound traffic from this node. This info is ## captured via a DNS lookup on the public hostname and then written ## into a file at /etc/sysconfig/EC2-public-IPaddr so that it can be ## grabbed and used by a post-install script that refines the GridFTP ## server configuration located in /etc/xinetd.d/unicluster-gridftp ## ## Chris Dagdigian / BioTeam / chris@bioteam.net ## my $hosttype = shift @ARGV; print "(########) 1 \n"; # Learn our own internal hostname (which is the AWS internal name) my $hostname = `/bin/hostname` ; chomp $hostname; $sh_hostname = $hostname; $hostname .= ".ec2.internal"; my $reservation_hosts = {}; my $host_reservations = {}; #my $aws_access_key_id = $ENV{'AWS_ACCESS_KEY_ID'}; #my $aws_secret_access_key = $ENV{'AWS_SECRET_ACCESS_KEY'}; my $ec2 = Net::Amazon::EC2->new( AWSAccessKeyId => $MY_AWS_ACCESS_KEY_ID, SecretAccessKey => $MY_AWS_SECRET_ACCESS_KEY ); print "(########) 2 \n"; my $instances = $ec2->describe_instances; if (ref($instances) eq "Net::Amazon::EC2::Errors") { print "\nAWS ERROR!\n"; print "request_id: $instances->{request_id}\n"; foreach $error (@{$instances->{errors}}) { print "code: $error->{code}\n"; print "message: $error->{message}\n"; } } foreach my $reservation (@$instances) { foreach my $instance ($reservation->instances_set) { $instance_hostname = $instance->private_dns_name; $public_hostname = $instance->dns_name; ($sh_public_hostname,$junk) = split(/\./,$public_hostname); print "(########) Comparing: $instance_hostname vs. $hostname \n"; if ($hostname eq $instance_hostname) { # If we get here than we are working our own instance print "My internal hostname: $hostname\n"; print "My instance hostname: $instance_hostname\n"; print "My public hostname : $public_hostname\n"; $dnsAnswer = `host $public_hostname $EXTERNAL_DNS_SERVER`; chomp($dnsAnswer); ($junk,$dnsreply) = split(/has\saddress\s+/,$dnsAnswer); print "My public IP : $dnsreply\n"; $privateIPAnswer = `/usr/bin/host $sh_hostname`; chomp($privateIPAnswer); ($junk,$privateIP) = split(/has address\s+/,$privateIPAnswer); print "---- System Changes to be made ... ---\n"; print "[1] Adding the following to /etc/hosts on this node:\n"; print "\t $dnsreply\t\t$sh_public_hostname $public_hostname\n"; print "\t $privateIP\t\t$sh_hostname $hostname\n"; open(FH,">/etc/hosts") or die "can't open write to /etc/hosts"; print FH "## Modified by utility script \n"; print FH "127.0.0.1 localhost localhost.localdomain\n"; print FH "$dnsreply\t\t$public_hostname $sh_public_hostname\n"; print FH "$privateIP\t\t$hostname $sh_hostname\n"; close(FH); print "\n[2] Will change hostname of this node to $public_hostname\n\n"; `echo $sh_hostname > /etc/sysconfig/original_hostname`; `/bin/hostname $public_hostname`; print "\n[3] Write our public IP address to /etc/sysconfig/EC2-public-IPaddr (GridFTP)\n\n"; `echo $dnsreply > /etc/sysconfig/EC2-public-IPaddr`; } } }