eucalyptus.rb

Path: lib/svengali/plugins/eucalyptus.rb
Last Update: Sat Jun 12 15:55:44 +0900 2010

Required files

timeout   yaml  

Methods

Public Instance methods

deregister specified image and delete images on server

[Source]

# File lib/svengali/plugins/eucalyptus.rb, line 106
def delete_image(image_id_str)
  `euca-deregister #{image_id_str}`
  bucket_and_image = get_bucket_and_image_name_by_id(image_id_str)

  `euca-delete-bundle -b #{bucket_and_image[:bucket]} -p #{bucket_and_image[:image]} --clear`

  debug_p "deregisterd and deleted #{image_id_str}"
end

return -> array : like this

                 ["i-xxxx","i-xxxx","i-xxxx",...]

[Source]

# File lib/svengali/plugins/eucalyptus.rb, line 66
def get_all_instance_id()
  ret_arr = `euca-describe-instances`.scan(/\s+(i-\w+)\s+/)
  debug_p ret_arr.inspect()
  return ret_arr
end

return -> symbol : one of values below

                      :running, :pending, :shutdown, :terminated

[Source]

# File lib/svengali/plugins/eucalyptus.rb, line 6
def get_instance_state(instance_id_str)
  exec_result = ""
  while exec_result == ""
    exec_result = `euca-describe-instances #{instance_id_str}`
  end
  
  if exec_result.index("pending")
    return :pending
  elsif exec_result.index("running")
    return :running
  elsif exec_result.index("shutting-down")
    return :shutdown
  elsif exec_result.index("terminated")
    return :terminated
  end
end

return -> string: id of running instance this function..

              blocks until kicked image starts running
              uses c1.medium machine spec

[Source]

# File lib/svengali/plugins/eucalyptus.rb, line 27
def run_image(image_id_str)
  begin
    debug_p "trying kick instance from image #{image_id_str}"
    instance_id = `euca-run-instances #{image_id_str} -t c1.medium`.scan(/\s+(i-\w+)\s+/)[0].to_s
  rescue
    debug_p "retry!"
    retry
  end

  # waits until instance finishes booting
  while(get_instance_state(instance_id) != :running)
  end

  debug_p "instance( #{instance_id} ) has been running successfully."

  return instance_id
end

this method blocks until the instance finishs terminating

[Source]

# File lib/svengali/plugins/eucalyptus.rb, line 46
def terminate_instance(instance_id_str)
  `euca-terminate-instances #{instance_id_str}`

  while(get_instance_state(instance_id_str) != :terminated)
  end

  debug_p "instance( #{instance_id_str} ) has been terminated successfully."
end

this method blocks until the instance finishs terminating

[Source]

# File lib/svengali/plugins/eucalyptus.rb, line 56
def terminate_instance_all()
  instance_id_arr = get_all_instance_id()

  instance_id_arr.each{ |instance_id|
    terminate_instance(instance_id)
  }
end

return -> string: image id when timeout, returns nil

[Source]

# File lib/svengali/plugins/eucalyptus.rb, line 74
def upload_and_register_image(image_path_str,timeout_sec = nil)
  `euca-bundle-image -i #{image_path_str}`
  dir_name = File::dirname(image_path_str)
  if(dir_name == ".")
    bucket_and_image_name = image_path_str.gsub("./","")
  else
    bucket_and_image_name = image_path_str.gsub(dir_name,"")
  end

  begin
    timeout(timeout_sec) do
      `euca-upload-bundle -b #{bucket_and_image_name} -m /tmp/#{bucket_and_image_name}.manifest.xml`
    end
  rescue TimeoutError
    "Too long time elapsed to upload image file. It is highly possible that uploading is missed"
    return nil
  end

  begin
    image_id = `euca-register #{bucket_and_image_name}/#{bucket_and_image_name}.manifest.xml`.scan(/IMAGE\s+(emi\-\w+)\s*/)[0].to_s
  rescue
    debug_p "retry!"
    retry
  end
  
  `rm -f /tmp/#{bucket_and_image_name}*`

  debug_p "uploaded and registerd #{image_path_str}"
  return image_id
end

[Validate]