Class: EyInfo::Hosts

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Hosts

Returns a new instance of Hosts.



136
137
138
139
140
141
142
143
144
145
# File 'lib/ey_info.rb', line 136

def initialize(options = {})
  path = File.expand_path("~/.eyrc")
  if File.exist?(path)
    @api_token = YAML.load_file(File.expand_path("~/.eyrc"))["api_token"]
  else
    raise "~/.eyrc file does not exist, need to run 'engineyard environments' at least once and log in"
  end
  @api = EY::API.new(@api_token)
  @envs = @api.environments.to_a
end

Instance Method Details

#all_hostsObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/ey_info.rb', line 147

def all_hosts
  hosts = {}
  @envs.map{|x| x.name}.sort.uniq.each do |env_name, index|
    env_name = env_name.to_sym
    arr = []
    app_count = 0
    db_count  = 0

    instances = @api.environments.match_one!(env_name.to_s).instances.
      select {|x| x.hostname}.
      sort_by {|x| x.hostname}
    instances.each do |i, idx|
      # if i.role == 'app_master'
      #   key = "app0"
      # elsif i.role == 'app'
      #   app_count += 1
      #   key = "app#{app_count}"
      # elsif i.role == 'db_master'
      #   key = "db0"
      # elsif i.role == 'db_slave'
      #   db_count += 1
      #   key = "db#{db_count}"
      # elsif i.role == 'util'
      #   key = "#{i.name}"
      # elsif i.role == 'solo'
      #   key = "solo"
      # end
      
      key = i.role == 'util' ? i.name : i.role
      if key == 'app'
        app_count += 1
        key = "app#{app_count}"
      elsif key == 'db_slave'
        db_count += 1
        key = "db_slave#{db_count}"
      end
      
      arr << {
        :ssh_key => "#{env_name}_#{key}",
        :hostname => i.hostname,
        :role => i.role,
        :name => i.name
      }
    end
    
    hosts[env_name] = arr
  end
  hosts
end

#hosts(env_name) ⇒ Object



197
198
199
# File 'lib/ey_info.rb', line 197

def hosts(env_name)
  all_hosts[env_name.to_sym]
end