Skip to content

Commit

Permalink
added snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
geelen committed Jul 23, 2010
1 parent e48c438 commit ffc3b8b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 8 deletions.
6 changes: 5 additions & 1 deletion lib/dollhouse/config_loader.rb
Expand Up @@ -26,12 +26,16 @@ def os o
@os = o
end

def from_latest_snapshot snapshot_name
@snapshot = snapshot_name
end

def first_boot &blk
callbacks[:first_boot] = blk
end

def to_server
Server[name, @instance_type, @os, callbacks]
Server[name, @instance_type, @os, @snapshot, callbacks]
end

def callbacks
Expand Down
2 changes: 1 addition & 1 deletion lib/dollhouse/deployment.rb
Expand Up @@ -5,7 +5,7 @@ def initiate opts
cloud_name = [opts[:prefix], server.name].compact.join("-")
Dollhouse.cloud_adapter.boot_new_server cloud_name,
lambda { server_online(cloud_name, server) },
{:instance_type => server.instance_type, :os => server.os}
{:instance_type => server.instance_type, :os => server.os, :snapshot => server.snapshot}
end
end

Expand Down
4 changes: 4 additions & 0 deletions lib/dollhouse/online_server.rb
Expand Up @@ -42,6 +42,10 @@ def as user, opts = {}, &blk
self.password = old_password
end

def take_snapshot name
Dollhouse.cloud_adapter.take_snapshot(name_in_cloud, name + "-" + Time.now.strftime("%Y%M%d-%H%M%S"))
end

private

def default_opts
Expand Down
27 changes: 22 additions & 5 deletions lib/dollhouse/rackspace_cloud_adapter.rb
Expand Up @@ -14,8 +14,15 @@ def conn
def boot_new_server(name, callback, opts)
flavor_num = FLAVORS[opts[:instance_type]]
raise "Unknown instance_type of #{opts[:instance_type].inspect}. Permitted values are #{FLAVORS.keys.inspect}" unless flavor_num
image_num = IMAGES[opts[:os]]
raise "Unknown os of #{opts[:os].inspect}. Permitted values are #{IMAGES.keys.inspect}" unless image_num
image_num = if IMAGES[opts[:snapshot]]
image = conn.images.select { |i| i.created_at && i.name =~ Regexp.new(Regexp.escape(IMAGES[opts[:snapshot]])) }.sort_by { |i| i.created_at }.last
raise "Snapshot name of #{IMAGES[opts[:snapshot]]} doesn't match any images!" unless image
puts "Booting from image: #{image.inspect}"
image.id
else
raise "Unknown os of #{opts[:os].inspect}. Permitted values are #{IMAGES.keys.inspect}" unless IMAGES[opts[:os]]
IMAGES[opts[:os]]
end

puts "Booting server #{name}"
server = conn.servers.create(:flavor_id => flavor_num, :image_id => image_num, :name => name)
Expand Down Expand Up @@ -49,12 +56,17 @@ def write_file(name, path, content, opts = {})
end

def destroy name
server = conn.servers.find { |s| s.name == name }
server = get_server name
puts "Killing server #{server.inspect}"
server.destroy
puts "Done."
end

def take_snapshot name, snapshot_name
response = conn.create_image get_server(name).id, snapshot_name
puts "Created image: #{response.body['image'].inspect}"
end

private

def ssh_conn(name, opts, &blk)
Expand All @@ -67,14 +79,19 @@ def ssh_conns
@ssh_conns ||= Hash.new { |h, (name, opts)|
puts "Establishing connection to #{name}:"
#change this to use instances.yml, or something
server = conn.servers.find { |s| s.name == name }
raise "Can't find server #{name}" if server.nil?
server = get_server name
host = server.addresses['public'].first
user = opts[:user] || 'root'
puts "Connecting to #{host} as #{user}..."

h[[name, opts]] = RemoteServer.new(Net::SSH.start(host, user, {:forward_agent => true}.merge(opts)))
}
end

def get_server name
server = conn.servers.find { |s| s.name == name }
raise "Can't find server #{name}" if server.nil?
server
end
end
end
2 changes: 1 addition & 1 deletion lib/dollhouse/server.rb
@@ -1,4 +1,4 @@
module Dollhouse
class Server < Struct.new(:name, :instance_type, :os, :callbacks)
class Server < Struct.new(:name, :instance_type, :os, :snapshot, :callbacks)
end
end

0 comments on commit ffc3b8b

Please sign in to comment.