bbrowning-deltacloud-core 0.0.4-java → 0.0.6-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -310,6 +310,15 @@ class EC2Driver < Deltacloud::BaseDriver
310
310
  end
311
311
  end
312
312
 
313
+ def valid_credentials?(credentials)
314
+ client = new_client(credentials)
315
+ # FIXME: We need to do this call to determine if
316
+ # EC2 is working with given credentials. There is no
317
+ # other way to check, if given credentials are valid or not.
318
+ realms = client.describe_availability_zones rescue false
319
+ return realms ? true : false
320
+ end
321
+
313
322
  private
314
323
 
315
324
  def new_client(credentials)
@@ -192,6 +192,15 @@ class GogridDriver < Deltacloud::BaseDriver
192
192
  return creds
193
193
  end
194
194
 
195
+ def valid_credentials?(credentials)
196
+ client = new_client(credentials)
197
+ # FIXME: We need to do this call to determine if
198
+ # GoGrid is working with given credentials. There is no
199
+ # other way to check, if given credentials are valid or not.
200
+ return false unless new_client(credentials).request('common/lookup/list', { 'lookup' => 'ip.datacenter' })
201
+ true
202
+ end
203
+
195
204
  define_instance_states do
196
205
  start.to( :pending ) .automatically
197
206
  pending.to( :running ) .automatically
@@ -257,6 +257,15 @@ class MockDriver < Deltacloud::BaseDriver
257
257
  snapshots
258
258
  end
259
259
 
260
+ def valid_credentials?(credentials)
261
+ begin
262
+ check_credentials(credentials)
263
+ return true
264
+ rescue Deltacloud::AuthException
265
+ end
266
+ return false
267
+ end
268
+
260
269
  private
261
270
 
262
271
  def check_credentials(credentials)
@@ -131,6 +131,15 @@ class RackspaceDriver < Deltacloud::BaseDriver
131
131
  instances
132
132
  end
133
133
 
134
+ def valid_credentials?(credentials)
135
+ begin
136
+ new_client(credentials)
137
+ rescue
138
+ return false
139
+ end
140
+ true
141
+ end
142
+
134
143
 
135
144
  def convert_srv_to_instance(srv)
136
145
  inst = Instance.new(:id => srv["id"].to_s,
@@ -191,6 +191,15 @@ def destroy_instance(credentials, id)
191
191
  end
192
192
  end
193
193
 
194
+ def valid_credentials?(credentials)
195
+ begin
196
+ new_client(credentials)
197
+ rescue Deltacloud::AuthException
198
+ return false
199
+ end
200
+ true
201
+ end
202
+
194
203
  #--
195
204
  # PRIVATE METHODS:
196
205
  #--
@@ -0,0 +1,253 @@
1
+ #
2
+ # Copyright (C) 2009 Red Hat, Inc.
3
+ #
4
+ # Licensed to the Apache Software Foundation (ASF) under one or more
5
+ # contributor license agreements. See the NOTICE file distributed with
6
+ # this work for additional information regarding copyright ownership. The
7
+ # ASF licenses this file to you under the Apache License, Version 2.0 (the
8
+ # "License"); you may not use this file except in compliance with the
9
+ # License. You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16
+ # License for the specific language governing permissions and limitations
17
+ # under the License.
18
+
19
+ # INSTALLATION:
20
+ # 1. You need VirtualBox and VBoxManage tool installed
21
+ # 2. You need to setup some images manually inside VirtualBox
22
+ # 3. You need to install 'Guest Additions' to this images for metrics
23
+ # 4. You need a lot of hard drive space ;-)
24
+
25
+ # NETWORKING:
26
+ # For now, the VM is always started with bridged networking. The NIC
27
+ # it uses defaults to eth0, but may be overriden with the VIRTUALBOX_NIC
28
+ # environment variable. This should be the NIC name as expected by Virtualbox.
29
+ # For example, on my Macbook Pro this is 'en1: AirPort'
30
+
31
+ require 'deltacloud/base_driver'
32
+ require 'virtualbox'
33
+
34
+ module Deltacloud
35
+ module Drivers
36
+ module Virtualbox
37
+ class VirtualboxDriver < Deltacloud::BaseDriver
38
+
39
+ ( REALMS = [
40
+ Realm.new({
41
+ :id => 'local',
42
+ :name => 'localhost',
43
+ :limit => 100,
44
+ :state => 'AVAILABLE'
45
+ })
46
+ ] ) unless defined?( REALMS )
47
+
48
+ define_hardware_profile 'small' do
49
+ cpu 1
50
+ memory 0.5
51
+ storage 1
52
+ architecture `uname -m`.strip
53
+ end
54
+
55
+ define_hardware_profile 'medium' do
56
+ cpu 1
57
+ memory 1
58
+ storage 1
59
+ architecture `uname -m`.strip
60
+ end
61
+
62
+ define_hardware_profile 'large' do
63
+ cpu 2
64
+ memory 2
65
+ storage 1
66
+ architecture `uname -m`.strip
67
+ end
68
+
69
+ define_instance_states do
70
+ start.to( :pending ) .on( :create )
71
+ pending.to( :running ) .automatically
72
+ running.to( :stopped ) .on( :stop )
73
+ stopped.to( :running ) .on( :start )
74
+ stopped.to( :finish ) .on( :destroy )
75
+ end
76
+
77
+ def realms(credentials, opts = nil)
78
+ return REALMS if ( opts.nil? )
79
+ results = REALMS
80
+ results = filter_on( results, :id, opts )
81
+ results
82
+ end
83
+
84
+ def images(credentials, opts = nil)
85
+ images = convert_images(VirtualBox::VM.all)
86
+ images = filter_on( images, :id, opts )
87
+ images = filter_on( images, :architecture, opts )
88
+ images.sort_by{|e| [e.owner_id, e.description]}
89
+ end
90
+
91
+ def instances(credentials, opts = nil)
92
+ instances = convert_instances(VirtualBox::VM.all)
93
+ instances = filter_on( instances, :id, opts )
94
+ instances = filter_on( instances, :state, opts )
95
+ instances = filter_on( instances, :image_id, opts )
96
+ instances
97
+ end
98
+
99
+ def create_instance(credentials, image_id, opts)
100
+ image = images(credentials, { :id => image_id }).first
101
+
102
+ # Choose a name for the new vm unless one was given
103
+ name = opts[:name]
104
+ if name.nil? or name.empty?
105
+ # random uniqueish name w/o having to pull in UUID gem
106
+ name = "#{image.name} - #{(Time.now.to_f * 1000).to_i}#{rand(1000)}"
107
+ end
108
+
109
+ hwp = find_hardware_profile(credentials, opts[:hwp_id], image_id)
110
+ parent_vm = VirtualBox::VM.find(image.id)
111
+
112
+ # Create new virtual machine
113
+ vm = VirtualBox::VM.create(name, parent_vm.os_type_id)
114
+ new_uid = vm.uuid
115
+
116
+ # Add Hardware profile to this machine
117
+ vm.memory_size = ((hwp.memory.value*1.024)*1000).to_i
118
+ vm.cpu_count = hwp.cpu.value.to_i
119
+ vm.vram_size = 16
120
+
121
+ # Copy the network adapter settings from the parent vm
122
+ vm.network_adapters[0].enabled = true
123
+ vm.network_adapters[0].attachment_type = parent_vm.network_adapters[0].attachment_type
124
+ vm.network_adapters[0].host_interface = parent_vm.network_adapters[0].host_interface
125
+
126
+ # Store some metadata using extra data fields
127
+ vm.extra_data['deltacloud_hwp_id'] = hwp.name
128
+ vm.extra_data['deltacloud_image_id'] = image_id
129
+
130
+ vm.save
131
+
132
+ # Clone the disk image in a separate thread because it can take a long time
133
+ Thread.new do
134
+ # Reload the vm objects because they probably aren't safe to
135
+ # reuse across threads
136
+ parent_vm = VirtualBox::VM.find(image.id)
137
+ vm = VirtualBox::VM.find(new_uid)
138
+
139
+ # Add storage
140
+ # This will 'reuse' existing image
141
+ parent_hdd = hard_disk(parent_vm)
142
+ new_location = File.join(File.dirname(parent_hdd.location), "#{name}.vdi")
143
+
144
+ new_hd = parent_hdd.clone(new_location, "VDI")
145
+ vm.add_storage_controller('IDE Controller', :ide, :piix4)
146
+ vm.attach_storage('IDE Controller', 0, 0, :hard_disk, new_hd.uuid)
147
+ vm.start
148
+ if opts[:user_data]
149
+ user_data = opts[:user_data].gsub("\n", '') # remove newlines from base64 encoded text
150
+ vm.guest_property["/Deltacloud/UserData"] = user_data
151
+ end
152
+ end
153
+ instances(credentials, :id => new_uid).first
154
+ end
155
+
156
+ def reboot_instance(credentials, id)
157
+ vm = VirtualBox::VM.find(id)
158
+ vm.control(:reset)
159
+ end
160
+
161
+ def stop_instance(credentials, id)
162
+ vm = VirtualBox::VM.find(id)
163
+ unless vm.shutdown
164
+ vm.stop
165
+ end
166
+ end
167
+
168
+ def start_instance(credentials, id)
169
+ vm = VirtualBox::VM.find(id)
170
+ vm.start
171
+ end
172
+
173
+ def destroy_instance(credentials, id)
174
+ vm = VirtualBox::VM.find(id)
175
+ vm.destroy(:destroy_medium => :delete)
176
+ end
177
+
178
+ private
179
+
180
+ def convert_instances(instances)
181
+ vms = []
182
+ instances.each do |instance|
183
+ volume = convert_volume(instance)
184
+ state = convert_state(instance.state, volume)
185
+ ip = vbox_get_ip(instance)
186
+ hwp_name = instance.extra_data['deltacloud_hwp_id'] || 'small'
187
+ image_id = instance.extra_data['deltacloud_image_id'] || ''
188
+ vms << Instance.new(:id => instance.uuid,
189
+ :image_id => image_id,
190
+ :name => instance.name,
191
+ :state => state,
192
+ :owner_id => ENV['USER'] || ENV['USERNAME'] || 'nobody',
193
+ :realm_id => 'local',
194
+ :public_addresses => ip,
195
+ :private_addresses => ip,
196
+ :actions => instance_actions_for(state),
197
+ :instance_profile => InstanceProfile.new(hwp_name))
198
+ end
199
+ vms
200
+ end
201
+
202
+ # Warning: You need VirtualHost guest additions for this
203
+ def vbox_get_ip(instance)
204
+ ip = instance.guest_property["/VirtualBox/GuestInfo/Net/0/V4/IP"]
205
+ ip.nil? ? [] : [ip]
206
+ end
207
+
208
+ def convert_state(state, volume)
209
+ return 'PENDING' if volume.nil?
210
+ state = state.to_s.strip.upcase
211
+ case state
212
+ when 'POWEROFF' then 'STOPPED'
213
+ when 'POWERED_OFF' then 'STOPPED'
214
+ else
215
+ state
216
+ end
217
+ end
218
+
219
+ def convert_volume(vm)
220
+ hdd = hard_disk(vm)
221
+ return nil if hdd.nil?
222
+ StorageVolume.new(:id => hdd.uuid,
223
+ :created => Time.now,
224
+ :state => 'AVAILABLE',
225
+ :capacity => hdd.logical_size,
226
+ :instance_id => vm.uuid,
227
+ :device => hdd.type)
228
+ end
229
+
230
+ def hard_disk(vm)
231
+ attachment = vm.medium_attachments.select { |ma| ma.type == :hard_disk }.first
232
+ attachment.nil? ? nil : attachment.medium
233
+ end
234
+
235
+ def convert_images(images)
236
+ vms = []
237
+ images.each do |image|
238
+ hdd = hard_disk(image)
239
+ next unless hdd
240
+ capacity = ", #{hdd.logical_size} MBytes HDD"
241
+ vms << Image.new(:id => image.uuid,
242
+ :name => image.name,
243
+ :description => "#{image.memory_size} MB RAM, #{image.cpu_count} CPU#{capacity}",
244
+ :owner_id => ENV['USER'] || ENV['USERNAME'] || 'nobody',
245
+ :architecture => `uname -m`.strip)
246
+ end
247
+ vms
248
+ end
249
+
250
+ end
251
+ end
252
+ end
253
+ end
data/lib/drivers.rb CHANGED
@@ -6,6 +6,7 @@ DRIVERS = {
6
6
  :rimuhosting => { :name => "RimuHosting"},
7
7
  :opennebula => { :name => "Opennebula", :class => "OpennebulaDriver" },
8
8
  :terremark => { :name => "Terremark"},
9
+ :virtualbox => { :name => "Virtualbox" },
9
10
  :mock => { :name => "Mock" }
10
11
  }
11
12
 
data/server.rb CHANGED
@@ -42,6 +42,9 @@ get '/' do redirect url_for('/api'); end
42
42
 
43
43
  get '/api\/?' do
44
44
  @version = 0.1
45
+ if params[:force_auth]
46
+ return [401, 'Authentication failed'] unless driver.valid_credentials?(credentials)
47
+ end
45
48
  respond_to do |format|
46
49
  format.xml { haml :"api/show" }
47
50
  format.json do
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 4
9
- version: 0.0.4
8
+ - 6
9
+ version: 0.0.6
10
10
  platform: java
11
11
  authors:
12
12
  - Red Hat, Inc.
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-09-01 00:00:00 -04:00
17
+ date: 2010-09-19 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -197,6 +197,7 @@ files:
197
197
  - lib/deltacloud/drivers/rimuhosting/rimuhosting_client.rb
198
198
  - lib/deltacloud/drivers/rimuhosting/rimuhosting_driver.rb
199
199
  - lib/deltacloud/drivers/terremark/terremark_driver.rb
200
+ - lib/deltacloud/drivers/virtualbox/virtualbox_driver.rb
200
201
  - lib/deltacloud/helpers/application_helper.rb
201
202
  - lib/deltacloud/helpers/conversion_helper.rb
202
203
  - lib/deltacloud/helpers/hardware_profiles_helper.rb