-
-
Save gb-swatanabe/c09cdf3dd0f6c6f1b5c1 to your computer and use it in GitHub Desktop.
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 -*- | |
| begin | |
| require 'aws-sdk-core' | |
| rescue LoadError | |
| abort "please run 'gem install aws-sdk-core'.\n" | |
| end | |
| trap(:INT) { # Ctrl-Cされたら終了 | |
| abort "Interrupted." | |
| } | |
| CFG_ESF = ENV["HOME"] + "/.ssh/esf-config" | |
| #----#----#----#----#----#----#----#----#----#----#----#----#----#----#----#----#----#----# | |
| # IAMアカウント設定をYAMLファイルから読み込み | |
| class ESFConfig | |
| attr_accessor :file, :default | |
| attr_reader :config, :accounts | |
| def initialize(file: "/etc/esf-config", default: "_default") | |
| @config_bare = YAML.load(File.read( file )) | |
| @accounts = @config_bare["iam"].keys | |
| @accounts.delete(default) | |
| # デフォルト値を展開した上で格納 | |
| @config = {"esf"=>{"ssh_config"=>"~/.ssh/config"}} | |
| @config["esf"].merge!(@config_bare["esf"]) | |
| @accounts.each{|a| | |
| @config[a] = Hash.new | |
| @config[a].merge!(@config_bare["iam"][default]) | |
| @config[a].merge!(@config_bare["iam"][a]) | |
| } # end of each | |
| end # end of def initialize | |
| end # end of class ESFConfig | |
| #----#----#----#----#----#----#----#----#----#----#----#----#----#----#----#----#----#----# | |
| # インスタンス情報取得 | |
| class ESFInstance | |
| attr_accessor :region, :access_key_id, :secret_access_key | |
| attr_reader :instances, :name, :public_dns_name | |
| def initialize(region: nil, access_key_id: nil, secret_access_key: nil) | |
| ec2 = Aws::EC2::Client.new( access_key_id: access_key_id, | |
| secret_access_key: secret_access_key, | |
| region: region ) | |
| @instances = Array.new | |
| @name = Hash.new | |
| @public_dns_name = Hash.new | |
| ec2.describe_instances[:reservations].each{|i| | |
| ii = i[:instances][0] | |
| @instances += [ii] | |
| # Nameタグからインスタンス名を取得 | |
| id = ii[:instance_id] | |
| t = ii[:tags] | |
| @name[id] = if t.index{|s| /\Aopsworks:/ =~ s.key } then | |
| # OpsWorks管理下 | |
| sprintf("%s_%s", | |
| t[t.index{|s| s.key == "opsworks:instance" }].value, | |
| t[t.index{|s| s.key == "opsworks:stack" }].value ) | |
| else | |
| t[t.index{|s| s.key == "Name" }].value | |
| end | |
| } # end of each i | |
| end # of def initialize | |
| end # of class ESFInstance | |
| #----#----#----#----#----#----#----#----#----#----#----#----#----#----#----#----#----#----# | |
| # ssh_config出力 | |
| class ESFssh_config | |
| attr_accessor :config | |
| require 'date' | |
| def initialize(config: nil) | |
| @config = config | |
| @progname = File.expand_path($0) | |
| @delimiter = sprintf("= = = %s %s.%d = = =", | |
| File.basename(@progname), | |
| DateTime.now.strftime("%Y%m%d_%H%M%S"), $$) | |
| end | |
| def puts(fh: STDOUT) | |
| fh.printf( "# %s\n# FOLLOWS ARE AUTO-GENERATED BY %s\n\n", | |
| @delimiter, @progname ) | |
| @config.accounts.each{ |a| | |
| fh.printf( "# %s - %s\n", a, @config.config[a]["comment"]) | |
| i = ESFInstance.new( access_key_id: @config.config[a]["access_key_id"], | |
| secret_access_key: @config.config[a]["secret_access_key"], | |
| region: @config.config[a]["region"] ) | |
| i.instances.each{|ii| | |
| id = ii[:instance_id] | |
| destination = nil | |
| @config.config[a]["destinations"].each{|d| | |
| if ii[d.to_sym] != "" then | |
| destination = ii[d.to_sym] | |
| break | |
| end | |
| } | |
| if destination != nil then | |
| fh.printf( "Host %s %s.%s\n\tHostname\t%s\n", | |
| id, i.name[id], a, | |
| destination ) | |
| if @config.config[a].has_key?("ssh_option") then | |
| @config.config[a]["ssh_option"].each{|k,v| | |
| tabs = "\t" * if k.length >= 8 then 1 else 2 end | |
| fh.printf( "\t%s%s%s\n",k,tabs,v ) | |
| } | |
| end | |
| else | |
| fh.printf( "# Host %s %s.%s\n", id, i.name[id], a ) | |
| end | |
| } # end of each n | |
| fh.printf("\n") | |
| } # end of each a | |
| fh.printf( "# AUTO-GENERATED CONFIGURATION END\n# %s\n", @delimiter ) | |
| end | |
| def remove(file: ENV["HOME"] + "/.ssh/config") | |
| end | |
| def update(file: ENV["HOME"] + "/.ssh/config") | |
| end | |
| end | |
| #----#----#----#----#----#----#----#----#----#----#----#----#----#----#----#----#----#----# | |
| # main | |
| #----#----#----#----#----#----#----#----#----#----#----#----#----#----#----#----#----#----# | |
| ec2cfg = ESFConfig.new(file: CFG_ESF) | |
| s = ESFssh_config.new(config: ec2cfg) | |
| s.puts() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment