Class: ChefWorkflow::VM::EC2Provisioner
- Inherits:
-
Object
- Object
- ChefWorkflow::VM::EC2Provisioner
show all
- Includes:
- DebugSupport
- Defined in:
- lib/chef-workflow/support/vm/ec2.rb
Constant Summary
DebugSupport::CHEF_WORKFLOW_DEBUG_DEFAULT
Instance Attribute Summary collapse
Instance Method Summary
collapse
#if_debug
Constructor Details
#initialize(name, number_of_servers) ⇒ EC2Provisioner
10
11
12
13
14
15
16
17
18
19
|
# File 'lib/chef-workflow/support/vm/ec2.rb', line 10
def initialize(name, number_of_servers)
require 'chef-workflow/support/ec2'
require 'chef-workflow/support/ip'
require 'net/ssh'
require 'timeout'
@name = name
@number_of_servers = number_of_servers
init_instance_ids
end
|
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
8
9
10
|
# File 'lib/chef-workflow/support/vm/ec2.rb', line 8
def name
@name
end
|
Instance Method Details
#init_instance_ids ⇒ Object
21
22
23
|
# File 'lib/chef-workflow/support/vm/ec2.rb', line 21
def init_instance_ids
@instance_ids = ChefWorkflow::DatabaseSupport::Set.new("vm_ec2_instances", name)
end
|
#report ⇒ Object
169
170
171
172
|
# File 'lib/chef-workflow/support/vm/ec2.rb', line 169
def report
init_instance_ids
["#{@number_of_servers} servers; instance ids: #{@instance_ids.to_a.join(" ")}"]
end
|
#shutdown ⇒ Object
156
157
158
159
160
161
162
163
164
165
166
167
|
# File 'lib/chef-workflow/support/vm/ec2.rb', line 156
def shutdown
aws_ec2 = ec2.ec2_obj
@instance_ids.each do |instance_id|
aws_ec2.instances[instance_id].terminate
end
@instance_ids.clear
ChefWorkflow::IPSupport.delete_role(name)
return true
end
|
#ssh_connection_check(ip) ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/chef-workflow/support/vm/ec2.rb', line 25
def ssh_connection_check(ip)
Net::SSH.start(ip, ChefWorkflow::KnifeSupport.ssh_user, { :keys => [ChefWorkflow::KnifeSupport.ssh_identity_file] }) do |ssh|
ssh.open_channel do |ch|
ch.on_open_failed do
return false
end
ch.exec("exit 0") do
return true
end
end
ssh.loop
end
rescue Exception => e
return false
end
|
#startup(*args) ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
# File 'lib/chef-workflow/support/vm/ec2.rb', line 46
def startup(*args)
aws_ec2 = ec2.ec2_obj
ec2.assert_security_groups
instances = aws_ec2.instances.create(
:count => @number_of_servers,
:image_id => ec2.ami,
:security_groups => ec2.security_groups,
:key_name => ec2.ssh_key,
:instance_type => ec2.instance_type
)
if instances.kind_of?(Array)
new_instances = []
instances.each do |instance|
new_instances.push(instance)
end
instances = new_instances
else
instances = [instances]
end
unresolved_instances = instances.dup
until unresolved_instances.empty?
instance = unresolved_instances.shift
unless (instance.status rescue nil)
if_debug(3) do
$stderr.puts "API server doesn't think #{instance.id} exists yet."
end
sleep 0.3
unresolved_instances.push(instance)
end
end
ip_addresses = []
instances.each do |instance|
@instance_ids.add(instance.id)
end
ipaddress_mutex = Mutex.new
debug_mutex = Mutex.new
begin
Timeout.timeout(ec2.provision_wait) do
instances.map do |instance|
Thread.new do
loop do
ready = false
if instance.status == :running
ready = ssh_connection_check(instance.ip_address)
unless ready
if_debug(3) do
debug_mutex.synchronize do
$stderr.puts "Instance #{instance.id} running, but ssh isn't up yet."
end
end
end
else
if_debug(3) do
debug_mutex.synchronize do
$stderr.puts "#{instance.id} isn't running yet -- scheduling for re-check"
end
end
end
if ready
ipaddress_mutex.synchronize do
ip_addresses.push(instance.ip_address)
end
break
end
sleep 2
end
end
end.each(&:join)
end
rescue TimeoutError
raise "instances timed out waiting for ec2"
end
ip_addresses.each do |ipaddr|
ChefWorkflow::IPSupport.assign_role_ip(name, ipaddr)
end
return ip_addresses
end
|