From 0f576cc40a85198bf98fa54187b49ccd7970093b Mon Sep 17 00:00:00 2001 From: Matt Date: Sat, 15 Aug 2015 01:33:12 -0400 Subject: [PATCH 1/2] *added clone function for cloning an existing droplet --- doproxy.rb | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 115 insertions(+), 1 deletion(-) diff --git a/doproxy.rb b/doproxy.rb index 469fc83..96a7ee3 100755 --- a/doproxy.rb +++ b/doproxy.rb @@ -21,6 +21,9 @@ def initialize @region = config['droplet_options']['region'] @size = config['droplet_options']['size'] @image = config['droplet_options']['image'] + @master = config['droplet_options']['master'] + @clone_image = config['droplet_options']['clone_image'] + @snapshot_overwrite = config['droplet_options']['snapshot_overwrite'] @client = DropletKit::Client.new(access_token: config['token']) @backend_count @@ -121,6 +124,115 @@ def reload_haproxy `service haproxy reload` end + def clone_server + hostname = "#{@hostname_prefix}-#{@backend_count}" + userdata = File.open(@userdata_file).read + + mdrop = nil + + @client.droplets.all.each do |droplet| + if droplet.name == @master + puts droplet.id + mdrop = droplet + end + end + + if mdrop.nil? + puts "@master=|"+@master+"|" + puts "Could not find master droplet" + return + end + + image = imageWithName(@clone_image) + + if image.nil? or @snapshot_overwrite + puts "Shutting down master.." + shutdown = @client.droplet_actions.shutdown(droplet_id: mdrop.id) + + if shutdown + if shutdown.try( :status ) + while shutdown.status != "completed" + puts ".." + sleep(2) + shutdown = @client.actions.find(id: shutdown.id) + end + else + if shutdown['id'] + if shutdown['id'] == 'unprocessable_entity' + puts shutdown.message + end + else + puts "Not sure what happened, but here is the response:"+shutdown + return + end + end + else + puts "Not sure what happend, but no response from trying to shutdown the master" + return + end + + puts "Creating snapshot.." + snapshot = @client.droplet_actions.snapshot(droplet_id: mdrop.id, name: @clone_image) + + if snapshot.try( :id ) and snapshot.id == 'unprocessable_entity' + puts snapshot.message + return + end + + + if snapshot and snapshot.try( :status ) + while snapshot.status != "completed" + puts ".." + sleep(2) + snapshot = @client.actions.find(id: snapshot.id) + end + else + puts "Not sure what happened, but here is the response:"+snapshot + return + end + + @client.droplet_actions.power_on(droplet_id: mdrop.id) + + image = imageWithName(@clone_image) + end + + if image.nil? + puts "Image was not created" + return + end + + puts "Creating Droplet from snapshot.." + + droplet = DropletKit::Droplet.new(name: hostname, region: @region, size: @size, image: image.id, private_networking: true, ssh_keys: @ssh_key_ids, user_data: userdata) + created = @client.droplets.create(droplet) + droplet_id = created.id + + if (created.status == 'new') + while created.status != 'active' + sleep(15) # wait for droplet to become active before checking again + created = @client.droplets.find(id: droplet_id) + end + # droplet status is now 'active' + backend_inventory = File.open(@inventory_file, 'a') + backend_inventory.write("#{droplet_id}\n") + backend_inventory.close + @backend_count += 1 + @droplets.push(created) # add droplet to array so it gets included in haproxy.cfg + reload_haproxy + puts "Success: #{droplet_id} created and added to backend." + else + puts "Some error has occurred on droplet create (status was not 'new')" + end + end + + def imageWithName(name) + @client.images.all(public: false).each do |image| + if image.name == name + return image + end + end + return nil + end end @@ -144,6 +256,8 @@ def print_usage proxy.print_inventory when "create" proxy.create_server + when "clone" + proxy.clone_server when "delete" if ARGV[1] != nil proxy.delete_server(ARGV[1].to_i) @@ -155,4 +269,4 @@ def print_usage when "generate" proxy.generate_haproxy_cfg end -end \ No newline at end of file +end From 69bcd2cee830a9b3c9134461548dc8b5ab70e179 Mon Sep 17 00:00:00 2001 From: Matt Date: Sat, 15 Aug 2015 01:40:45 -0400 Subject: [PATCH 2/2] *fixed some of the error checking --- doproxy.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/doproxy.rb b/doproxy.rb index 96a7ee3..d8f24ec 100755 --- a/doproxy.rb +++ b/doproxy.rb @@ -150,7 +150,7 @@ def clone_server shutdown = @client.droplet_actions.shutdown(droplet_id: mdrop.id) if shutdown - if shutdown.try( :status ) + if shutdown['status'] while shutdown.status != "completed" puts ".." sleep(2) @@ -174,13 +174,13 @@ def clone_server puts "Creating snapshot.." snapshot = @client.droplet_actions.snapshot(droplet_id: mdrop.id, name: @clone_image) - if snapshot.try( :id ) and snapshot.id == 'unprocessable_entity' + if snapshot['id'] and snapshot.id == 'unprocessable_entity' puts snapshot.message return end - if snapshot and snapshot.try( :status ) + if snapshot and snapshot['status'] while snapshot.status != "completed" puts ".." sleep(2)