Class: CloudProviders::Ec2Instance
Instance Attribute Summary
#init_opts, #name, #raw_response
Class Method Summary
collapse
Instance Method Summary
collapse
#[], #[]=, #accessible?, #after_initialized, #bootstrap_chef!, #chef_bootstrapped?, #default_keypair_path, #each, #elapsed_runtime, #has_key?, #keys, #pending?, #rsync_dir, #run, #run_chef!, #running?, #terminated?, #terminating?, #to_hash, #values
#host, #ping_port, #rsync, #run, #scp, #shell_escape, #ssh, #ssh_cleanup_known_hosts!, #ssh_options
Constructor Details
#initialize(raw_response = {}) ⇒ Ec2Instance
Returns a new instance of Ec2Instance.
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/cloud_providers/ec2/ec2_instance.rb', line 19
def initialize(raw_response={})
@raw_response = raw_response
self.instance_id = raw_response["instanceId"] rescue nil
self.security_groups = raw_response.groupSet.item.map{|sg| sg.groupId }.sort rescue nil
self.image_id = raw_response["imageId"] rescue nil
self.private_ip = raw_response["privateIpAddress"] rescue nil
self.dns_name = raw_response["dnsName"] rescue nil
self.instance_type = raw_response["instanceType"] rescue nil
self.public_ip = raw_response["ipAddress"] rescue nil
self.key_name = raw_response["keyName"] rescue nil
self.launch_time = raw_response["launchTime"] rescue nil
self.availability_zones = raw_response["placement"]["availabilityZone"] rescue nil
self.status = raw_response["instanceState"]["name"] rescue nil
self.block_device_mapping = raw_response["blockDeviceMapping"] rescue nil
self.subnet_id = raw_response["subnetId"] rescue nil
self.disable_api_termination = raw_response["disableApiTermination"] rescue nil
self.instance_initiated_shutdown_behavior = raw_response["instanceInitiatedShutdownBehavior"] rescue nil
super
end
|
Class Method Details
.run!(hsh) ⇒ Object
89
|
# File 'lib/cloud_providers/ec2/ec2_instance.rb', line 89
def self.run!(hsh); new(hsh).run!; end
|
.terminate!(hsh = {}) ⇒ Object
95
|
# File 'lib/cloud_providers/ec2/ec2_instance.rb', line 95
def self.terminate!(hsh={}); new(hsh).terminate!; end
|
Instance Method Details
#bundle_and_register(img = nil, opts = {}) ⇒ Object
TODO: WIP: bundle up the instance and register it as a new ami. An image of the running node will be creatd, or if a path to an image file on the remote node is given, that will be used
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
# File 'lib/cloud_providers/ec2/ec2_instance.rb', line 139
def bundle_and_register(img=nil, opts={})
opts = {:cert => cert,
:bucket => nil,
:prefix => image_id,
:kernel => kernel_id,
:ramdisk => ramdisk_id,
:ec2cert => cloud_cert
}.merge(opts)
raise "You must specify a bucket to bundle to" if opts[:bucket].nil?
scp ec2cert, "/mnt/bundle/"
scp cert, "/mnt/bundle/"
arch = self[:instanceType].match(/m1\.small|c1\.medium/) ? 'i386' : 'x86_64'
image = img ? img : make_image(opts)
ssh "ec2-bundle-image #{image} -d /mnt/bundle -u #{self[:ownerId]} -k /mnt/bundle/pk-*.pem -c /tmp/cert-*.pem"
manifest = "/mnt/bundle/#{opts[:prefix]}.manifest.xml"
ssh "ec2-upload-bundle -a #{access_key} -s #{secret_access_key} -m #{manifest}"
ami_str = ssh "ec2-register-bundle"
ami = ami_str.grep(/ami-\w*/).first
return ami
end
|
#in_service? ⇒ Boolean
66
67
68
|
# File 'lib/cloud_providers/ec2/ec2_instance.rb', line 66
def in_service?
running?
end
|
#keypair(n = nil) ⇒ Object
40
41
42
43
|
# File 'lib/cloud_providers/ec2/ec2_instance.rb', line 40
def keypair(n=nil)
return @keypair if @keypair
@keypair = (cloud.keypair.basename == self.key_name) ? cloud.keypair : Keypair.new(self.key_name, cloud.keypair.)
end
|
#make_image(opts = {}) ⇒ Object
create an image file and copy this instance to the image file.
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/cloud_providers/ec2/ec2_instance.rb', line 117
def make_image(opts={})
opts = {:volume => '/',
:size => 6000,
:destination => '/mnt/bundle',
:exclude => nil
}.merge(opts)
image_file = File.join(opts[:destination], opts[:prefix] )
cmds = ["mkdir -p #{opts[:destination]}"]
cmds << "dd if=/dev/zero of=#{image_file} bs=1M count=#{opts[:size]}"
cmds << "mkfs.ext3 -F -j #{image_file}"
cmds << "mkdir -p #{opts[:destination]}/loop"
cmds << "mount -o loop #{image_file} #{opts[:destination]}/loop"
cmds << "rsync -ax #{rsync_excludes(opts[:exclude])} #{opts[:volume]}/ #{opts[:destination]}/loop/"
cmds << "if [[ -f /etc/init.d/ec2-ssh-host-key-gen ]]; then chmod u+x /etc/init.d/ec2-ssh-host-key-gen ;fi"
cmds << "umount #{opts[:destination]}/loop"
self.ssh cmds
image_file
end
|
#reachable? ⇒ Boolean
53
54
55
|
# File 'lib/cloud_providers/ec2/ec2_instance.rb', line 53
def reachable?
ping_port self.public_ip, 22
end
|
#rsync_excludes(array_of_abs_paths_to_exclude = nil) ⇒ Object
list of directories and files to exclude when bundling an instance
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/cloud_providers/ec2/ec2_instance.rb', line 98
def rsync_excludes(array_of_abs_paths_to_exclude=nil)
array_of_abs_paths_to_exclude ||= %w[
/sys
/proc
/dev/pts
/dev
/media
/mnt
/proc
/sys
/etc/ssh/ssh_host_*
/etc/ssh/moduli
/etc/udev/rules.d/70-persistent-net.rules
/etc/udev/rules.d/z25_persistent-net.rules
]
array_of_abs_paths_to_exclude.inject(''){|str, path| str << "--exclude=#{path}" ; str}
end
|
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/cloud_providers/ec2/ec2_instance.rb', line 70
def run!
r = cloud_provider.ec2.run_instances(
:image_id => image_id,
:min_count => min_count,
:max_count => max_count,
:key_name => keypair.basename,
:security_group => cloud.security_group_names,
:user_data => user_data,
:instance_type => instance_type,
:availability_zone => availability_zone,
:block_device_mapping => block_device_mapping,
:disable_api_termination => disable_api_termination,
:instance_initiated_shutdown_behavior => instance_initiated_shutdown_behavior,
:base64_encoded => true)
r.instancesSet.item.map do |i|
inst_options = i.merge(r.merge(:cloud => cloud)).merge(cloud.cloud_provider.dsl_options)
Ec2Instance.new(inst_options)
end.first
end
|
#security_group_names ⇒ Object
45
46
47
|
# File 'lib/cloud_providers/ec2/ec2_instance.rb', line 45
def security_group_names
security_groups.map{|a| a.to_s }
end
|
#ssh_available? ⇒ Boolean
57
58
59
60
61
62
63
64
|
# File 'lib/cloud_providers/ec2/ec2_instance.rb', line 57
def ssh_available?
cloud.security_groups.map {|a|
a.authorizes.map {|t| t.from_port.to_i }.flatten
}.flatten.include?(22) and
reachable? and
in_service? and
keypair and keypair.exists?
end
|
#terminate! ⇒ Object
91
92
93
94
|
# File 'lib/cloud_providers/ec2/ec2_instance.rb', line 91
def terminate!
cloud_provider.ec2.terminate_instances(:instance_id => [self.instance_id])
cloud_provider.reset!
end
|
49
50
51
|
# File 'lib/cloud_providers/ec2/ec2_instance.rb', line 49
def zone
availability_zones.first
end
|