Skip to content

Instantly share code, notes, and snippets.

@taka328w
Created January 15, 2012 14:02

Revisions

  1. taka328w revised this gist Jan 24, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # -*- encoding: utf-8 -*-
    # coding: utf-8
    require 'rubygems'
    require 'aws-sdk'

  2. taka328w created this gist Jan 15, 2012.
    23 changes: 23 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    # -*- encoding: utf-8 -*-
    require 'rubygems'
    require 'aws-sdk'

    config = {:access_key_id => 'アクセスキー ID',
    :secret_access_key => 'シークレットアクセスキー'}
    AWS.config(config)

    hosts = []
    hosts << "127.0.0.1\tlocalhost localhost.localdomain"
    AWS::EC2.new.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