Class: Deltacloud::Drivers::Digitalocean::DigitaloceanDriver
- Inherits:
-
BaseDriver
- Object
- BaseDriver
- Deltacloud::Drivers::Digitalocean::DigitaloceanDriver
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
Constants included
from Deltacloud
API_VERSION, CIMI_API_VERSION
Instance Method Summary
collapse
-
#create_instance(credentials, image_id, opts = {}) ⇒ Object
-
#create_key(credentials, opts = {}) ⇒ Object
-
#destroy_image(credentials, image_id) ⇒ Object
You can only destroy images you own.
-
#destroy_instance(credentials, instance_id) ⇒ Object
-
#destroy_key(credentials, opts = {}) ⇒ Object
-
#hardware_profile_ids(credentials) ⇒ Object
-
#hardware_profiles(credentials, opts = {}) ⇒ Object
-
#images(credentials, opts = {}) ⇒ Object
By default images will return list of ‘all’ images available to launch.
-
#instance(credentials, opts = {}) ⇒ Object
-
#instances(credentials, opts = {}) ⇒ Object
-
#key(credentials, opts = {}) ⇒ Object
-
#keys(credentials, opts = {}) ⇒ Object
-
#realms(credentials, opts = {}) ⇒ Object
-
#reboot_instance(credentials, instance_id) ⇒ Object
-
#start_instance(credentials, instance_id) ⇒ Object
-
#stop_instance(credentials, instance_id) ⇒ Object
-
#valid_credentials?(credentials) ⇒ Boolean
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, #name, #realm, #storage_snapshot, #storage_volume, #supported_collections
Methods included from Deltacloud
[], config, configure, connect, database, default_frontend, drivers, enabled_frontends, frontend_required?, frontends, generate_routes, initialize_database, need_database?, new, require_frontend!
#collection_exists?, #collection_names, #collections
Methods included from Exceptions
exception_from_status, exceptions, included, logger, #safely
Instance Method Details
#create_instance(credentials, image_id, opts = {}) ⇒ Object
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
# 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 }
opts[:realm_id] ||= '1'
args.merge!(:region_id => opts[:realm_id])
opts[:hwp_id] ||= '66'
args.merge!(:size_id => opts[:hwp_id])
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 = {}) ⇒ Object
205
206
207
208
209
210
211
212
213
|
# 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) ⇒ Object
You can only destroy images you own.
110
111
112
113
114
|
# 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) ⇒ Object
155
156
157
158
159
|
# 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 = {}) ⇒ Object
195
196
197
198
199
200
201
202
203
|
# 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) ⇒ Object
58
59
60
61
62
63
64
65
66
67
|
# 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 = {}) ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# 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 = {}) ⇒ 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.
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
# 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 = {}) ⇒ Object
125
126
127
128
129
130
131
132
|
# 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 = {}) ⇒ Object
116
117
118
119
120
121
122
123
|
# 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 = {}) ⇒ Object
188
189
190
191
192
193
|
# 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 = {}) ⇒ Object
179
180
181
182
183
184
185
186
|
# 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 = {}) ⇒ Object
69
70
71
72
73
74
75
76
77
78
79
80
81
|
# 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) ⇒ Object
173
174
175
176
177
|
# 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) ⇒ Object
167
168
169
170
171
|
# 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) ⇒ Object
161
162
163
164
165
|
# 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) ⇒ Boolean
235
236
237
238
239
240
241
242
243
244
|
# 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
|