Skip to content

Instantly share code, notes, and snippets.

@pingali
Created April 19, 2009 19:44
Show Gist options
  • Save pingali/98185 to your computer and use it in GitHub Desktop.
Save pingali/98185 to your computer and use it in GitHub Desktop.
http://ec2dream.blogspot.com/search/label/Networking%20Multi-Tier%20Applications
#!/usr/bin/ruby
require 'rubygems'
require 'right_aws'
AMAZON_PUBLIC_KEY=<public key>
AMAZON_PRIVATE_KEY=<private key>
#
# update hosts file from S3 copy
#
print "Getting /etc/hosts file from S3\n"
s3= RightAws::S3Interface.new(AMAZON_PUBLIC_KEY, AMAZON_PRIVATE_KEY)
print "Getting /etc/hosts file from S3"
hosts = File.new('/etc/hosts', 'wb')
rhdr = s3.get('', 'hosts') do |chunk|
hosts.write(chunk)
end
hosts.close
#
# match ip address with hosts file and write a local_hostname file to the
# /root folder
#
print "Setting /root/local_hostname file\n"
begin
File.delete('/root/local_hostname')
rescue Errno::ENOENT
print ""
end
ip_address = %x(hostname -i)
print "ip address " + ip_address
ip_address[ip_address.length-1] = ""
hostFile = "/etc/hosts"
File.open(hostFile).each_line{ |s|
a = s.split(' ')
if a[0] == ip_address
print "hostname is " + a[1] + "\n"
File.open('/root/local_hostname.sh', 'w') {|f| f.write("export LOCAL_HOSTNAME="+a[1]) }
system('chmod 700 /root/local_hostname.sh')
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment