Class: CloudInfo

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

Defined Under Namespace

Classes: Instances

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.



8
9
10
11
12
# File 'lib/cloud_info.rb', line 8

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.



7
8
9
# File 'lib/cloud_info.rb', line 7

def env_name
  @env_name
end

Class Method Details

.all_hosts(options = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cloud_info.rb', line 46

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



64
65
66
# File 'lib/cloud_info.rb', line 64

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

.write_yaml(path, hosts) ⇒ Object



59
60
61
62
63
# File 'lib/cloud_info.rb', line 59

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

Instance Method Details

#app_masterObject



39
40
41
# File 'lib/cloud_info.rb', line 39

def app_master
  apps[0][1]
end

#appsObject



36
37
38
# File 'lib/cloud_info.rb', line 36

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

#hosts(use_cache = false) ⇒ Object

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



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cloud_info.rb', line 15

def hosts(use_cache = false)
  # 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
    CloudInfo.write_yaml(@cache_path, all_hosts)
  end
  @hosts
end

#utilsObject



42
43
44
# File 'lib/cloud_info.rb', line 42

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