Class: EyInfo::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/ey_info.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ CLI

Returns a new instance of CLI.



24
25
26
# File 'lib/ey_info.rb', line 24

def initialize(args)
  @args = args.dup
end

Instance Attribute Details

#argsObject (readonly)

The array of (unparsed) command-line options



20
21
22
# File 'lib/ey_info.rb', line 20

def args
  @args
end

#optionsObject (readonly)

The hash of (parsed) command-line options



22
23
24
# File 'lib/ey_info.rb', line 22

def options
  @options
end

Class Method Details

.run(args) ⇒ Object



13
14
15
16
17
# File 'lib/ey_info.rb', line 13

def self.run(args)
  cli = new(args)
  cli.parse_options!
  cli.run
end

Instance Method Details

#extract_environment_variables!Object

Extracts name=value pairs from the remaining command-line arguments and assigns them as environment variables.



78
79
80
81
82
83
# File 'lib/ey_info.rb', line 78

def extract_environment_variables! #:nodoc:
  args.delete_if do |arg|
    next unless arg.match(/^(\w+)=(.*)$/)
    ENV[$1] = $2
  end
end

#option_parserObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ey_info.rb', line 28

def option_parser
  # @logger = Logger.new
  @option_parser ||= OptionParser.new do |opts|
    opts.banner = "Usage: #{File.basename($0)} ssh_config"

    opts.on("-i", "--identity-file [IDENTITY_FILE]", "ssh key default id_rsa.") do |value|
      options[:ssh_key] = value
    end

    opts.on("-u", "--user [USER]", "ssh key default root.") do |value|
      options[:user] = value
    end

    opts.on("-t", "--template [TEMPLATE]", "path to template.") do |value|
      options[:template] = value
    end

    opts.on("-h", "--help", "Display this help message.") do
      puts opts
      exit
    end

    opts.on("-V", "--version",
      "Display the ey_info version, and exit."
    ) do
      puts "EyInfo Version #{EyInfo::Version}"
      exit
    end

  end
end

#parse_options!Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ey_info.rb', line 60

def parse_options!
  # defaults
  @options = {:actions => [], :user => 'root', :ssh_key => 'id_rsa'}

  if args.empty?
    warn "Please specifiy an action to execute."
    warn option_parser
    exit 1
  end

  option_parser.parse!(args)
  extract_environment_variables!

  options[:actions].concat(args)
end

#runObject



85
86
87
88
# File 'lib/ey_info.rb', line 85

def run
  write_ssh_config
  puts "Your #{ENV['HOME']}/.ssh/config now has shortcuts your ey cloud servers."
end

#ssh_serversObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/ey_info.rb', line 118

def ssh_servers
  @hosts = {}
  @info = EyInfo::Hosts.new
  all_hosts = @info.all_hosts
  all_hosts.keys.each do |env_name|
    all_hosts[env_name].each do |server|
      @hosts["#{server[:ssh_key]}"] = server[:hostname]
    end
  end
  path = options[:template] || File.expand_path("../templates/default_ssh_config.erb", __FILE__)
  content = IO.readlines(path).join("")
  template = ERB.new(content)
  template.result(binding)
end

#write_ssh_configObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/ey_info.rb', line 90

def write_ssh_config
  @ssh_config = "#{ENV['HOME']}/.ssh/config"
  @info = EyInfo::Hosts.new
  
  # clear out old known_hosts
  `grep -v 'compute-1.amazonaws.com' ~/.ssh/known_hosts > ~/.ssh/known_hosts.tmp`
  `mv ~/.ssh/known_hosts.tmp ~/.ssh/known_hosts`
  
  # for erb template
  @identify_file = "~/.ssh/#{options[:ssh_key]}"
  @user = options[:user]
  
  if File.exist?(@ssh_config)
    injector = TextInjector.new(
      :file => @ssh_config,
      :update => true,
      :content => ssh_servers
    )
  else
    injector = TextInjector.new(
      :file => @ssh_config,
      :write => true,
      :content => ssh_servers
    )
  end
  injector.run
end