Class: Deltacloud::Drivers::Digitalocean::DigitaloceanDriver

Inherits:
BaseDriver
  • Object
show all
Defined in:
lib/deltacloud/drivers/digitalocean/digitalocean_driver.rb

Defined Under Namespace

Classes: Client

Constant Summary

Constants inherited from BaseDriver

BaseDriver::MEMBER_SHOW_METHODS, BaseDriver::STATE_MACHINE_OPTS

Instance Method Summary collapse

Methods inherited from BaseDriver

#address, #api_provider, #blob, #bucket, #catched_exceptions_list, #configured_providers, constraints, define_hardware_profile, define_instance_states, driver_name, feature, features, #filter_hardware_profiles, #filter_on, #find_hardware_profile, #firewall, #hardware_profile, hardware_profiles, #has_capability?, #has_feature?, has_feature?, #image, #instance_actions_for, instance_state_machine, #instance_state_machine, #key, #name, #realm, #storage_snapshot, #storage_volume, #supported_collections

Methods included from Exceptions

exception_from_status, exceptions, included, logger, #safely

Instance Method Details

#create_instance(credentials, image_id, opts = {}) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/deltacloud/drivers/digitalocean/digitalocean_driver.rb', line 132

def create_instance(credentials, image_id, opts={})
  safely do
    client = new_client(credentials)
    args = { :image_id => image_id }
    args.merge!(:region_id => opts[:realm_id]) if opts[:realm_id]
    args.merge!(:size_id => opts[:hwp_id]) if opts[:hwp_id]
    args.merge!(:name => opts[:name] || "inst#{Time.now.to_i}")
    convert_instance(
      credentials.user,
      client.get("droplets/new", args)['droplet']
    )
  end
end

#destroy_image(credentials, image_id) ⇒ Object

You can only destroy images you own.



108
109
110
111
112
# File 'lib/deltacloud/drivers/digitalocean/digitalocean_driver.rb', line 108

def destroy_image(credentials, image_id)
  safely do
    new_client(credentials).get('images/%s/destroy', image_id)
  end
end

#destroy_instance(credentials, instance_id) ⇒ Object



146
147
148
149
150
# File 'lib/deltacloud/drivers/digitalocean/digitalocean_driver.rb', line 146

def destroy_instance(credentials, instance_id)
  safely do
    new_client(credentials).get("droplets/#{instance_id}/destroy/")
  end
end

#hardware_profile_ids(credentials) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/deltacloud/drivers/digitalocean/digitalocean_driver.rb', line 57

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 = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/deltacloud/drivers/digitalocean/digitalocean_driver.rb', line 40

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
      sizes = 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 = {}) ⇒ Object

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.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/deltacloud/drivers/digitalocean/digitalocean_driver.rb', line 86

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 = {}) ⇒ Object



123
124
125
126
127
128
129
130
# File 'lib/deltacloud/drivers/digitalocean/digitalocean_driver.rb', line 123

def instance(credentials, opts={})
  safely do
    convert_instance(
      credentials.user,
      new_client(credentials).get("droplets/#{opts[:id]}")["droplet"]
    )
  end
end

#instances(credentials, opts = {}) ⇒ Object



114
115
116
117
118
119
120
121
# File 'lib/deltacloud/drivers/digitalocean/digitalocean_driver.rb', line 114

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

#realms(credentials, opts = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/deltacloud/drivers/digitalocean/digitalocean_driver.rb', line 68

def realms(credentials, opts={})
  safely do
    new_client(credentials).get('regions')['regions'].map do |r|
      Realm.new(
        :id => r['id'],
        :name => r['name'],
        :state => 'AVAILABLE',
        :limit => :unlimited
      )
    end
  end
end

#reboot_instance(credentials, instance_id) ⇒ Object



164
165
166
167
168
# File 'lib/deltacloud/drivers/digitalocean/digitalocean_driver.rb', line 164

def reboot_instance(credentials, instance_id)
  safely do
    new_client(credentials).get("droplets/#{instance_id}/reboot/")
  end
end

#start_instance(credentials, instance_id) ⇒ Object



158
159
160
161
162
# File 'lib/deltacloud/drivers/digitalocean/digitalocean_driver.rb', line 158

def start_instance(credentials, instance_id)
  safely do
    new_client(credentials).get("droplets/#{instance_id}/power_on/")
  end
end

#stop_instance(credentials, instance_id) ⇒ Object



152
153
154
155
156
# File 'lib/deltacloud/drivers/digitalocean/digitalocean_driver.rb', line 152

def stop_instance(credentials, instance_id)
  safely do
    new_client(credentials).get("droplets/#{instance_id}/shutdown")
  end
end