Class: Provisional::ImageOperations
- Inherits:
-
Object
- Object
- Provisional::ImageOperations
- Defined in:
- lib/provisional/image_operations.rb
Constant Summary collapse
- SSH_OPTIONS =
"-o PasswordAuthentication=no -o StrictHostKeyChecking=no -o CheckHostIP=no -o VisualHostKey=no"
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #all_images ⇒ Object
- #build(image_name) ⇒ Object
- #build_image_from_server(name, id) ⇒ Object
- #custom_images ⇒ Object
-
#initialize(options = {}) ⇒ ImageOperations
constructor
A new instance of ImageOperations.
- #list ⇒ Object
- #run_scripts_on_server(image_name, server) ⇒ Object
- #transfer_files_to_server(image_name, server) ⇒ Object
- #wait_until_ssh_responds(ip_address) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ ImageOperations
Returns a new instance of ImageOperations.
10 11 12 |
# File 'lib/provisional/image_operations.rb', line 10 def initialize( = {}) @options = end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/provisional/image_operations.rb', line 8 def @options end |
Instance Method Details
#all_images ⇒ Object
77 78 79 |
# File 'lib/provisional/image_operations.rb', line 77 def all_images @all_images ||= Provisional.digital_ocean.images.all.select{|image| image.type == "snapshot"} end |
#build(image_name) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/provisional/image_operations.rb', line 23 def build(image_name) puts "Base image for '#{image_name}' is '#{base_image_for(image_name)}'" base_image = Provisional::Image.find(name: base_image_for(image_name)) name = "#{image_name}-#{Time.now.utc.strftime("%Y%m%d%H%M%S")}" server = Provisional::Server.create(name: name, image: base_image) transfer_files_to_server(image_name, server) run_scripts_on_server(image_name, server) Provisional::Server.stop(server) build_image_from_server(name, server.id) Provisional::Server.delete(server) end |
#build_image_from_server(name, id) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/provisional/image_operations.rb', line 58 def build_image_from_server(name, id) action = Provisional.digital_ocean.droplet_actions.snapshot(droplet_id: id, name: name) if action.is_a?(Hash) && action.has_key?("message") raise "Error building image: #{}" end action_id = action.id print "Building '#{name}' image from server '#{id}'." until Provisional.digital_ocean.actions.find(id: action_id).status == "completed" putc(".") $stdout.flush sleep 5 end puts "DONE" end |
#custom_images ⇒ Object
73 74 75 |
# File 'lib/provisional/image_operations.rb', line 73 def custom_images @custom_images ||= all_images.select{|image| !image.public && image.name =~ /-\d{14}$/} end |
#list ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/provisional/image_operations.rb', line 14 def list os_images.each do |image| display(image) end custom_images.each do |image| display(image) end end |
#run_scripts_on_server(image_name, server) ⇒ Object
45 46 47 48 49 |
# File 'lib/provisional/image_operations.rb', line 45 def run_scripts_on_server(image_name, server) ip_address = server.public_ip wait_until_ssh_responds(ip_address) %x(ssh #{SSH_OPTIONS} root@#{ip_address} run-parts --regex \\'.*\\' /var/tmp/provisional/scripts/) end |
#transfer_files_to_server(image_name, server) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/provisional/image_operations.rb', line 35 def transfer_files_to_server(image_name, server) ip_address = server.public_ip wait_until_ssh_responds(ip_address) %x(ssh #{SSH_OPTIONS} root@#{ip_address} mkdir -p /var/tmp/provisional) %x(scp #{SSH_OPTIONS} -r #{Provisional::CONFIG_DIRECTORY}/#{image_name}/files/ root@#{ip_address}:/var/tmp/provisional/files/) if Dir.exists?("#{Provisional::CONFIG_DIRECTORY}/#{image_name}/files") %x(scp #{SSH_OPTIONS} -r #{Provisional::CONFIG_DIRECTORY}/#{image_name}/scripts/ root@#{ip_address}:/var/tmp/provisional/scripts/) if Dir.exists?("#{Provisional::CONFIG_DIRECTORY}/#{image_name}/scripts") %x(ssh #{SSH_OPTIONS} root@#{ip_address} chmod --silent 600 /var/tmp/provisional/files/*) %x(ssh #{SSH_OPTIONS} root@#{ip_address} chmod --silent 700 /var/tmp/provisional/scripts/*) end |
#wait_until_ssh_responds(ip_address) ⇒ Object
51 52 53 54 55 56 |
# File 'lib/provisional/image_operations.rb', line 51 def wait_until_ssh_responds(ip_address) until %x(ssh #{SSH_OPTIONS} root@#{ip_address} "ls -d1 /") == "/\n" puts "Waiting for SSH on #{ip_address} to respond" sleep 1 end end |