class Deltacloud::Drivers::Digitalocean::DigitaloceanDriver

Public Instance Methods

create_instance(credentials, image_id, opts={}) click to toggle source
# File lib/deltacloud/drivers/digitalocean/digitalocean_driver.rb, line 134
def create_instance(credentials, image_id, opts={})
  safely do
    client = new_client(credentials)
    args = { :image_id => image_id }
    # Defaults to first realm if realm_id not set
    opts[:realm_id] ||= '1'
    args.merge!(:region_id => opts[:realm_id])
    # Defaults to first size if hwp_id not set
    opts[:hwp_id] ||= '66'
    args.merge!(:size_id => opts[:hwp_id])
    # Default to 'inst-timestamp if name is not set'
    opts[:name] ||= "inst-#{Time.now.to_i}"
    args.merge!(:name => opts[:name])
    args.merge!(:ssh_key_ids => opts[:keyname]) if opts[:keyname]
    convert_instance(
      credentials.user,
      client.get("droplets/new", args)['droplet']
    )
  end
end
create_key(credentials, opts={}) click to toggle source
# File lib/deltacloud/drivers/digitalocean/digitalocean_driver.rb, line 205
def create_key(credentials, opts={})
  client = new_client(credentials)
  convert_key(
    client.get(
      "ssh_keys/new",
      :name => opts[:key_name],
      :ssh_pub_key => opts[:public_key])['ssh_key']
  )
end
destroy_image(credentials, image_id) click to toggle source

You can only destroy images you own.

# File lib/deltacloud/drivers/digitalocean/digitalocean_driver.rb, line 110
def destroy_image(credentials, image_id)
  safely do
    new_client(credentials).get('images/%s/destroy', image_id)
  end
end
destroy_instance(credentials, instance_id) click to toggle source
# File lib/deltacloud/drivers/digitalocean/digitalocean_driver.rb, line 155
def destroy_instance(credentials, instance_id)
  safely do
    new_client(credentials).get("droplets/#{instance_id}/destroy/")
  end
end
destroy_key(credentials, opts={}) click to toggle source
# File lib/deltacloud/drivers/digitalocean/digitalocean_driver.rb, line 195
def destroy_key(credentials, opts={})
  client = new_client(credentials)
  original_key = key(credentials, opts)
  safely do
    client.get("ssh_keys/#{opts[:id]}/destroy")
    original_key.state = 'deleted'
    original_key
  end
end
hardware_profile_ids(credentials) click to toggle source
# File lib/deltacloud/drivers/digitalocean/digitalocean_driver.rb, line 58
def hardware_profile_ids(credentials)
  do_client = new_client(credentials)
  hwps = []
  safely do
    do_client.get("sizes")["sizes"].each do |s|
      hwps << HardwareProfile.new(s["id"].to_s)
    end
  end
  hwps
end
hardware_profiles(credentials, opts={}) click to toggle source
# File lib/deltacloud/drivers/digitalocean/digitalocean_driver.rb, line 41
def hardware_profiles(credentials, opts={})
  do_client = new_client(credentials)
  results = []
  safely do
    if opts[:id]
      size = do_client.get("sizes/#{opts[:id]}")["size"]
      results << hardware_profile_from(size)
    else
      do_client.get("sizes")["sizes"].each do |s|
        size = do_client.get("sizes/#{s['id']}")["size"]
        results << hardware_profile_from(size)
      end
    end
    filter_hardware_profiles(results, opts)
  end
end
images(credentials, opts={}) click to toggle source

By default images will return list of 'all' images available to launch. With 'owner_id' you can filter them using 'global' and 'my_images' values to get less images.

# File lib/deltacloud/drivers/digitalocean/digitalocean_driver.rb, line 88
def images(credentials, opts={})
  hwps = hardware_profile_ids(credentials)
  unless opts[:id]
    filter = opts[:owner_id] ? { :filter => "my_images" } : {}
    img_arr = safely do
      new_client(credentials).get('images', filter)['images'].map do |i|
        convert_image(hwps, i)
      end
    end
    filter_on( img_arr, :architecture, opts )
  else
    safely do
      [convert_image(
        hwps,
        new_client(credentials).get('images/%s' % opts[:id])['image']
      )]
    end
  end
end
instance(credentials, opts={}) click to toggle source
# File lib/deltacloud/drivers/digitalocean/digitalocean_driver.rb, line 125
def instance(credentials, opts={})
  safely do
    convert_instance(
      credentials.user,
      new_client(credentials).get("droplets/#{opts[:id]}")["droplet"]
    )
  end
end
instances(credentials, opts={}) click to toggle source
# File lib/deltacloud/drivers/digitalocean/digitalocean_driver.rb, line 116
def instances(credentials, opts={})
  inst_arr = safely do
    new_client(credentials).get('droplets')['droplets'].map do |i|
      convert_instance(credentials.user, i)
    end
  end
  filter_on inst_arr, :state, opts
end
key(credentials, opts={}) click to toggle source
# File lib/deltacloud/drivers/digitalocean/digitalocean_driver.rb, line 188
def key(credentials, opts={})
  client = new_client(credentials)
  safely do
    convert_key(client.get("ssh_keys/#{opts[:id]}")["ssh_key"])
  end
end
keys(credentials, opts={}) click to toggle source
# File lib/deltacloud/drivers/digitalocean/digitalocean_driver.rb, line 179
def keys(credentials, opts={})
  client = new_client(credentials)
  safely do
    client.get('ssh_keys')['ssh_keys'].map do |k|
      convert_key(k)
    end
  end
end
realms(credentials, opts={}) click to toggle source
# File lib/deltacloud/drivers/digitalocean/digitalocean_driver.rb, line 69
def realms(credentials, opts={})
  safely do
    realms = new_client(credentials).get('regions')['regions'].map do |r|
      Realm.new(
        :id => r['id'].to_s,
        :name => r['name'],
        :state => 'AVAILABLE',
        :limit => :unlimited
      )
    end
    filter_on(realms, opts, :id)
  end
end
reboot_instance(credentials, instance_id) click to toggle source
# File lib/deltacloud/drivers/digitalocean/digitalocean_driver.rb, line 173
def reboot_instance(credentials, instance_id)
  safely do
    new_client(credentials).get("droplets/#{instance_id}/reboot/")
  end
end
start_instance(credentials, instance_id) click to toggle source
# File lib/deltacloud/drivers/digitalocean/digitalocean_driver.rb, line 167
def start_instance(credentials, instance_id)
  safely do
    new_client(credentials).get("droplets/#{instance_id}/power_on/")
  end
end
stop_instance(credentials, instance_id) click to toggle source
# File lib/deltacloud/drivers/digitalocean/digitalocean_driver.rb, line 161
def stop_instance(credentials, instance_id)
  safely do
    new_client(credentials).get("droplets/#{instance_id}/shutdown")
  end
end
valid_credentials?(credentials) click to toggle source
# File lib/deltacloud/drivers/digitalocean/digitalocean_driver.rb, line 235
def valid_credentials?(credentials)
  begin
    hardware_profile_ids(credentials)
  rescue  Deltacloud::Exceptions::AuthenticationFailure
    return false
  rescue => e
    safely { raise e }
  end
  true
end