Class: CloudInfo::Instances

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

Defined Under Namespace

Modules: Aws

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Aws

#aws_group_for_instance_id, #aws_groups, #aws_instances

Constructor Details

#initialize(cloud_info, options = {}) ⇒ Instances

Returns a new instance of Instances.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/cloud_info/instances.rb', line 10

def initialize(cloud_info, options = {})
  @cloud_info = cloud_info
  @home_dir = ENV['HOME']
  @user = "root"  # TODO: move out
  @private_key = options[:private_key] # TODO: move out
  
  @ey_cloud = "#{@home_dir}/.ey-cloud.yml"
  unless File.exist?(@ey_cloud)
    warn("You need to have an ~/.ey-cloud.yml file")
    exit(1)
  end
  @config = YAML.load(IO.read(@ey_cloud))
  
  @servers = {}
  @sessions = []
  @ec2 = setup_ec2
end

Instance Attribute Details

#private_keyObject

Returns the value of attribute private_key.



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

def private_key
  @private_key
end

#serversObject

Returns the value of attribute servers.



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

def servers
  @servers
end

#sessionsObject

Returns the value of attribute sessions.



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

def sessions
  @sessions
end

#userObject

Returns the value of attribute user.



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

def user
  @user
end

Class Method Details

.ey_environmentsObject



139
140
141
142
# File 'lib/cloud_info/instances.rb', line 139

def self.ey_environments
  out = ey_recipes
  environments = out.split("\n").grep(/env/).collect {|x| x =~ /env\: (\w+) / ; $1 }
end

.ey_recipesObject



132
133
134
# File 'lib/cloud_info/instances.rb', line 132

def self.ey_recipes
  @@ey_recipes ||= `ey-recipes`
end

Instance Method Details

#build_server_infosObject



65
66
67
68
69
70
71
72
73
# File 'lib/cloud_info/instances.rb', line 65

def build_server_infos
  return if @built
  execute_on_servers do |ssh|
    @servers[ssh.host] ||= {}
    dna_json = ssh.exec!("cat /etc/chef/dna.json")
    @servers[ssh.host]["dna"] = JSON.parse(dna_json)
  end
  @built = true
end

#connect_to_serversObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cloud_info/instances.rb', line 48

def connect_to_servers
  thread = Thread.current
  hosts_with_blanks = instances.select{|i| i[:aws_state] == 'running'}.collect {|i| i[:dns_name]}
  hosts = hosts_with_blanks.select{|i| i[:dns_name] != ""}
  if hosts_with_blanks.size != hosts.size
    puts "WARNING: some hosts do not have dns_name's yet, amazon is not yet ready"
  end
  
  threads = hosts.collect do |host|
    Thread.new {
      @sessions << Net::SSH.start(host, @user, {:keys => @private_key})
      @sessions
    }
  end
  threads.collect {|t| t.join}
end

#execute_on_serversObject



75
76
77
78
79
80
81
82
83
84
# File 'lib/cloud_info/instances.rb', line 75

def execute_on_servers
  connect_to_servers
  threads = []
  sessions.each do |ssh|
    threads << Thread.new {
      yield(ssh)
    }
  end
  threads.collect{|t| t.join}
end

#ey_environmentsObject



143
144
145
# File 'lib/cloud_info/instances.rb', line 143

def ey_environments
  self.class.ey_environments
end

#ey_instances(env_name = nil) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/cloud_info/instances.rb', line 147

def ey_instances(env_name = nil)
  instance_ids = []
  out = ey_recipes
  lines = out.split("\n")
  line_with_instances = false
  lines.each do |line|
    if line_with_instances
      md = line.match(/\[(.*)\]/)
      list = md[1]
      instance_ids = list.split(",").collect{|x| x.gsub('"','').strip}
      break
    end
    if line =~ Regexp.new(env_name)
      line_with_instances = true # next line will have the instances info on it
    end
  end
  instance_ids
end

#ey_recipesObject



135
136
137
# File 'lib/cloud_info/instances.rb', line 135

def ey_recipes
  self.class.ey_recipes
end

#hostsObject

builds up the hosts hash TODO: move out looks like this:

"prod_br_util0"=>"ec2-123.456.789.123.compute-1.amazonaws.com",
"prod_br_memcached0"=>"ec2-123.456.789.123.compute-1.amazonaws.com",
"prod_br_util1"=>"ec2-123.456.789.123.compute-1.amazonaws.com",
"prod_br_memcached1"=>"ec2-123.456.789.123.compute-1.amazonaws.com",
"prod_br_app1"=>"ec2-123.456.789.123.compute-1.amazonaws.com",
"prod_br_app2"=>"ec2-123.456.789.123.compute-1.amazonaws.com",
"prod_br_app3"=>"ec2-123.456.789.123.compute-1.amazonaws.com",
"prod_br_db0"=>"ec2-123.456.789.123.compute-1.amazonaws.com",
"prod_br_db1"=>"ec2-123.456.789.123.compute-1.amazonaws.com"


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/cloud_info/instances.rb', line 99

def hosts
  build_server_infos
  h = {}
  counters = Hash.new(0)
  servers.each do |host, v|
    node = v["dna"]
    instance_role = node["instance_role"]
    env_name = @cloud_info.env_name
    
    case instance_role
    when "app_master"
      h["#{env_name}_app0"] = node["master_app_server"]["public_ip"]
    when "solo"
      h["#{env_name}_app0"] = host
    when "app"
      h["#{env_name}_app#{counters[:app] += 1}"] = host
    when "util"
      if node["name"] =~ /util/
        h["#{env_name}_#{node["name"]}"] = host
        counters[:util] += 1
      elsif
        h["#{env_name}_#{node["name"]}"] = host
        counters[:memcached] += 1
      end
    when "db_master"
      h["#{env_name}_db0"] = host
    when "db_slave"
      h["#{env_name}_db#{counters[:db] += 1}"] = host
    end
  end
  h
end

#instancesObject

only the instances for the env



37
38
39
40
41
42
43
44
45
46
# File 'lib/cloud_info/instances.rb', line 37

def instances
  instance_ids = ey_instances(@cloud_info.env_name)
  if instance_ids.empty?
    instances = []
  else
    group_id = aws_group_for_instance_id(instance_ids.first)
    instances = instances_in_group(group_id)
    instances
  end
end

#instances_in_group(group_id) ⇒ Object



32
33
34
# File 'lib/cloud_info/instances.rb', line 32

def instances_in_group(group_id)
  aws_instances.select {|x| x[:aws_groups].include?(group_id) }
end

#setup_ec2Object



28
29
30
# File 'lib/cloud_info/instances.rb', line 28

def setup_ec2
  RightAws::Ec2.new(@config[:aws_secret_id], @config[:aws_secret_key])
end