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



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

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



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

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

.write_yaml(path, hosts) ⇒ Object



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

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

Instance Method Details

#app_masterObject



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

def app_master
  apps[0][1]
end

#appsObject



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

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

#hostsObject

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
35
# File 'lib/cloud_info.rb', line 15

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
    CloudInfo.write_yaml(@cache_path, all_hosts)
  end
  @hosts
end

#utilsObject



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

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