Created
June 6, 2012 10:16
-
-
Save hSATAC/2881131 to your computer and use it in GitHub Desktop.
generate ec2 /etc/hosts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| # coding: utf-8 | |
| require 'rubygems' | |
| require 'aws-sdk' | |
| config = {:access_key_id => '', | |
| :secret_access_key => ''} | |
| AWS.config(config) | |
| ec2 = AWS::EC2.new | |
| ec2 = ec2.regions['ap-northeast-1'] | |
| hosts = [] | |
| hosts << "127.0.0.1\tlocalhost localhost.localdomain" | |
| ec2.instances.each do |instance| | |
| next unless instance.status == :running | |
| tags = instance.tags.to_h | |
| hosts << "#{instance.private_ip_address}\t#{tags["Name"]}" | |
| end | |
| `cp -p /etc/hosts /etc/hosts.bak` | |
| open("/etc/hosts.new", "w") {|f| f.write hosts.join("\n")+"\n"} | |
| is_same = system("diff -q /etc/hosts.new /etc/hosts > /dev/null 2>/dev/null") | |
| unless is_same | |
| `cp -p /etc/hosts.new /etc/hosts` | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment