Class: Server
Constant Summary
collapse
- TEN_SECONDS =
10
- SIXTY_SECONDS =
60
- TWO_MINUTES =
120
- THREE_MINUTES =
180
- RUNNING_SERVER_STATES =
%w{pending running}
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from FogModel
#id, #initialize, #method_missing
Constructor Details
This class inherits a constructor from FogModel
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
in the class FogModel
Class Method Details
.create!(ami, size, keypair, groups) ⇒ Object
8
9
10
|
# File 'lib/dew/models/server.rb', line 8
def self.create! ami, size, keypair, groups
new(Cloud.compute.servers.create(:image_id => ami, :flavor_id => size, :key_name => keypair, :groups => groups))
end
|
.find(tag_name, tag_value) ⇒ Object
12
13
14
|
# File 'lib/dew/models/server.rb', line 12
def self.find tag_name, tag_value
Cloud.compute.servers.all("tag:#{tag_name}" => tag_value).select{|s| RUNNING_SERVER_STATES.include?(s.state)}.map {|s| new s }
end
|
Instance Method Details
#add_tag(key, val) ⇒ Object
20
21
22
23
24
|
# File 'lib/dew/models/server.rb', line 20
def add_tag key, val
try_for(SIXTY_SECONDS) {
Cloud.compute.tags.create(:resource_id => id, :key => key, :value => val)
}
end
|
26
27
28
29
|
# File 'lib/dew/models/server.rb', line 26
def configure_for_database database, password
ssh.write(database.db_environment_file(password), '/tmp/envfile')
ssh.run('sudo mv /tmp/envfile /etc/environment')
end
|
#create_ami(ami_name) ⇒ Object
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/dew/models/server.rb', line 50
def create_ami ami_name
image_id = Cloud.compute.create_image(id, ami_name, "Created by #{ENV['USER']} on #{Time.now.strftime("%Y-%m-%d")}").body['imageId']
Inform.debug("Created image at %{id}, waiting for AWS to recognize it...", :id => image_id)
image = Timeout::timeout(SIXTY_SECONDS) do
image = nil
while image == nil
image = Cloud.compute.images.get(image_id)
end
image
end
Inform.debug("Image recognized, waiting for it to become available...")
image.wait_for { state == 'available' }
Inform.debug("Image available, sharing with other accounts...")
Account.user_ids.each do |user_id|
Inform.debug("Sharing %{id} with %{user_id}", :id => image_id, :user_id => user_id)
Cloud.compute.modify_image_attributes(image_id, 'launchPermission', 'add', 'UserId' => user_id)
end
image_id
end
|
#creator ⇒ Object
16
17
18
|
# File 'lib/dew/models/server.rb', line 16
def creator
@creator ||= fog_object.tags["Creator"]
end
|
#credentials ⇒ Object
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/dew/models/server.rb', line 74
def credentials
@credentials ||= if key_name
keyfile_path = Cloud.keyfile_path(key_name)
sanitize_key_file(key_name, keyfile_path)
"-i #{keyfile_path} -o Port=#{ssh_port} -o StrictHostKeyChecking=no #{username}@#{public_ip_address}"
else
Inform.warning("Server %{id} has no key and therefore can not be accessed.", :id => id)
false
end
end
|
#ssh ⇒ Object
39
40
41
|
# File 'lib/dew/models/server.rb', line 39
def ssh
Gofer::Host.new(public_ip_address, username, :port => ssh_port, :key_data => [File.read(Cloud.keyfile_path(key_name))], :paranoid => false, :quiet => true)
end
|
#ssh_port ⇒ Object
35
36
37
|
# File 'lib/dew/models/server.rb', line 35
def ssh_port
fog_object.tags['SSHPort'] || '22'
end
|
#username ⇒ Object
31
32
33
|
# File 'lib/dew/models/server.rb', line 31
def username
fog_object.tags['Username'] || 'ubuntu'
end
|
#wait_until_ready(ssh_timeout = THREE_MINUTES) ⇒ Object
43
44
45
46
47
48
|
# File 'lib/dew/models/server.rb', line 43
def wait_until_ready ssh_timeout=THREE_MINUTES
super()
Inform.debug("%{id} online at %{ip}, waiting for SSH connection...", :id => id, :ip => public_ip_address)
wait_for_ssh ssh_timeout
Inform.debug("Connected to %{id} via SSH successfully", :id => id)
end
|