Class: CloudInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/cloud_info.rb,
lib/cloud_info/cli.rb,
lib/cloud_info/version.rb,
lib/cloud_info/instances.rb

Overview

usage: require “cloud_info” irb(main):003:0> cloud_info = CloudInfo.new(“prod”,:private_key => “~/.ssh/id_rsa-private-key”) irb(main):005:0> pp cloud_info.apps [[“prod_app0”, “xxx.xxx.xxx.xxx”],

["prod_app1", "xxx.xxx.xxx.xxx"],
["prod_app2", "xxx.xxx.xxx.xxx"],
["prod_app3", "xxx.xxx.xxx.xxx"]]

Defined Under Namespace

Classes: CLI, Instances

Constant Summary collapse

Version =
"0.1.6"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env_name, options = {}) ⇒ CloudInfo

Returns a new instance of CloudInfo.



19
20
21
22
23
# File 'lib/cloud_info.rb', line 19

def initialize(env_name, options = {})
  @env_name = env_name
  @options = options
  @cache_path = options[:cache_path] || 'config/cloud_info.yml'
end

Instance Attribute Details

#env_nameObject (readonly)

Returns the value of attribute env_name.



18
19
20
# File 'lib/cloud_info.rb', line 18

def env_name
  @env_name
end

Class Method Details

.all_hosts(options = {}) ⇒ Object



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

def self.all_hosts(options = {})
  all_hosts = {}
  environments = Instances.ey_environments
  environments.each do |env|
    info = CloudInfo.new(env, options)
    hosts = info.hosts
    # pp env
    # pp hosts
    all_hosts.merge!(hosts)
  end
  all_hosts
end

.read_yaml(path) ⇒ Object



79
80
81
# File 'lib/cloud_info.rb', line 79

def self.read_yaml(path)
  YAML.load(IO.read(path))
end

.write_yaml(path, hosts) ⇒ Object



74
75
76
77
78
# File 'lib/cloud_info.rb', line 74

def self.write_yaml(path, hosts)
  File.open(path, 'w') do |out|
    out.write(hosts.to_yaml)
  end
end

Instance Method Details

#app_masterObject



51
52
53
# File 'lib/cloud_info.rb', line 51

def app_master
  apps[0][1]
end

#appsObject



48
49
50
# File 'lib/cloud_info.rb', line 48

def apps
  apps = hosts.select{|k,v| k =~ Regexp.new("#{@env_name}_app")}.sort {|a,b| a[0] <=> b[0] }
end

#djsObject



57
58
59
# File 'lib/cloud_info.rb', line 57

def djs
  utils = hosts.select{|k,v| k =~ Regexp.new("#{@env_name}_dj")}.sort {|a,b| a[0] <=> b[0] }
end

#hostsObject

if a cache exist it uses it instead of finding the servers



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cloud_info.rb', line 26

def hosts
  use_cache = @options[:use_cache]
  # check yaml file first
  if use_cache and File.exist?(@cache_path)
    @hosts = (CloudInfo.read_yaml(@cache_path) || {})[env_name]
  end
  
  unless @hosts
    @instances = Instances.new(self, @options)
    @hosts = @instances.hosts
    
    if File.exist?(@cache_path)
      all_hosts = (CloudInfo.read_yaml(@cache_path) || {})
    else
      all_hosts = {}
    end
    all_hosts[env_name] = @hosts unless @hosts.empty? # do not replace cached version if its empty
    CloudInfo.write_yaml(@cache_path, all_hosts)
  end
  @hosts
end

#utilsObject



54
55
56
# File 'lib/cloud_info.rb', line 54

def utils
  utils = hosts.select{|k,v| k =~ Regexp.new("#{@env_name}_util")}.sort {|a,b| a[0] <=> b[0] }
end