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
|
# 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[0].groupId 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.disable_api_termination = raw_response["disableApiTermination"] rescue nil
self.instance_initiated_shutdown_behavior = raw_response["instance_initiated_shutdown_behavior"] rescue nil
self.subnet_id = raw_response["subnetId"] rescue nil
super
end
|
Class Method Details
.run!(hsh) ⇒ Object
80
|
# File 'lib/cloud_providers/ec2/ec2_instance.rb', line 80
def self.run!(hsh); new(hsh).run!; end
|
.terminate!(hsh = {}) ⇒ Object
86
|
# File 'lib/cloud_providers/ec2/ec2_instance.rb', line 86
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
# File 'lib/cloud_providers/ec2/ec2_instance.rb', line 129
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
60
61
62
|
# File 'lib/cloud_providers/ec2/ec2_instance.rb', line 60
def in_service?
running?
end
|
#keypair(n = nil) ⇒ Object
39
40
41
|
# File 'lib/cloud_providers/ec2/ec2_instance.rb', line 39
def keypair(n=nil)
@keypair ||= Keypair.new(self.key_name)
end
|
#make_image(opts = {}) ⇒ Object
create an image file and copy this instance to the image file.
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/cloud_providers/ec2/ec2_instance.rb', line 107
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
47
48
49
|
# File 'lib/cloud_providers/ec2/ec2_instance.rb', line 47
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/cloud_providers/ec2/ec2_instance.rb', line 89
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
|
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/cloud_providers/ec2/ec2_instance.rb', line 64
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,
: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
|
#ssh_available? ⇒ Boolean
51
52
53
54
55
56
57
58
|
# File 'lib/cloud_providers/ec2/ec2_instance.rb', line 51
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
82
83
84
85
|
# File 'lib/cloud_providers/ec2/ec2_instance.rb', line 82
def terminate!
cloud_provider.ec2.terminate_instances(:instance_id => [self.instance_id])
cloud_provider.reset!
end
|
43
44
45
|
# File 'lib/cloud_providers/ec2/ec2_instance.rb', line 43
def zone
availability_zones.first
end
|