Project

General

Profile

0001-Feature-955-vmware-provisioning-support.patch

Corey Osman, 11/15/2011 03:52 PM

View differences:

app/controllers/hypervisors/guests_controller.rb
34 34
  end
35 35

  
36 36
  def destroy
37
    if @guest.volume.destroy and @guest.destroy
37
    #if @guest.volume.destroy and @guest.destroy
38
    if @guest.destroy
38 39
      process_success({:success_redirect => hypervisor_guests_path(@hypervisor)})
39 40
    else
40 41
      process_error({:redirect => hypervisor_guests_path(@hypervisor)})
app/helpers/hosts_and_hostgroups_helper.rb
36 36

  
37 37
  def volume_size item
38 38
    return item.disk_size if item.try(:disk_size)
39
    return nil if @guest.volume.nil?
39 40
    return @guest.volume.size if @guest
40 41
  end
41 42

  
app/helpers/hypervisors/guests_helper.rb
8 8
    "class='label #{s ? "success" : ""}'"
9 9
  end
10 10

  
11
  def power_action guest
11
  def power_action(guest, staticstatus=false)
12 12
    opts = hash_for_power_hypervisor_guest_path(:hypervisor_id => @hypervisor, :id => guest)
13
    html =  guest.running? ?  { :confirm => 'Are you sure?', :class => "label important" } : { :class => "label notice" }
13
    html =  guest.running?(staticstatus) ?  { :confirm => 'Are you sure?', :class => "label important" } : { :class => "label notice" }
14 14

  
15
    display_link_if_authorized "Power#{state(guest.running?)}" , opts, html.merge(:method => :put)
15
    display_link_if_authorized "Power#{state(guest.running?(staticstatus))}" , opts, html.merge(:method => :put)
16 16
  end
17 17
end
app/models/hypervisor.rb
2 2
require 'timeout'
3 3

  
4 4
class Hypervisor < ActiveRecord::Base
5
  attr_accessible :name, :uri, :kind
5
  attr_accessible :name, :kind, :uri, :hyperpass, :hyperuser
6 6
  attr_reader :host
7 7

  
8 8
  validates_presence_of :name, :uri, :kind
......
49 49
  def connect
50 50
    return true if Rails.env == "test"
51 51
    logger.debug "trying to contact Hypervisor #{name}"
52
    Timeout::timeout(10, StandardError) { @host = Virt.connect(uri).host }
52
    Timeout::timeout(10, StandardError) { @host = Virt.connect(uri, {:username => hyperuser, :password => hyperpass}).host }
53 53
  rescue => e
54 54
    logger.warn "Failed to connect to hypervisor #{name} - #{e}"
55 55
    @host = nil
app/models/hypervisor/guest.rb
17 17
  end
18 18

  
19 19
  def as_json opts
20
    {to_s => {:memory => memory, :vcpu => vcpu, :running => running?, :volume => {:size => volume.size, :pool => volume.pool.name}}}
20
    {to_s => {:memory => memory, :vcpu => vcpu, :running => running?(true), :volume => {:size => volume.size, :pool => volume.pool.name}}}
21 21
  end
22 22

  
23 23
end
app/models/orchestration/libvirt.rb
18 18
    protected
19 19
    def initialize_libvirt
20 20
      return unless libvirt?
21
      (@hypervisor = Hypervisor.find(hypervisor_id)).connect
22
      @guest = Virt::Guest.new({:name => name, :memory => memory, :arch => architecture.name,
23
                               :vcpu => vcpu, :pool => storage_pool, :size => disk_size,
24
                               :device => interface, :type => network_type})
25
    rescue => e
26
      failure "Failed to initialize the Libvirt connection: #{e}"
21
      @hypervisor = Hypervisor.find(hypervisor_id).connect
22
        
23
        @guest = @hypervisor.create_guest({:name => name, :memory => memory, :arch => architecture.name,
24
                                 :vcpu => vcpu, :pool => storage_pool, :size => disk_size,
25
                                 :device => interface, :type => network_type})
26
        rescue => e
27
      failure "Failed to initialize the Libvirt connection: #{e} #{@hypervisor}"
27 28
    end
28 29

  
29 30
    def queue_libvirt
......
49 50
      return unless errors.empty?
50 51
      return unless libvirt? or (hostgroup and hostgroup.hypervisor?)
51 52
      @hypervisor ||= hostgroup.hypervisor.connect
53
        # Does the below line even work?  Shouldn't this be @hypervisor.find_guest_by_name(name)
52 54
      @guest ||= Virt::Guest.find(name) rescue nil
53 55
      return if @guest.nil?
54 56
      queue.create(:name => "Removing libvirt instance #{self}", :priority => 1,
......
57 59
                   :action => [self, :delLibvirtVolume])
58 60
    end
59 61

  
62
    #TODO: Find a better way to handle the volume exists error since the virt::guest object
63
      #      creates a volume during save which is called after this method
64
      # need to handle for when : volume already esists, can't save
65
      # This should just all be moved into the virt lib
66
      # For vmware virt already handles creating the storage before the guest
60 67
    def setLibvirtVolume
61
      logger.info "Adding Libvirt instance storage for #{name}"
62
      @guest.volume.save
68
        logger.info "Adding Libvirt instance storage for #{name}"
69
        #@guest.volume.save 
63 70
    rescue => e
64 71
      failure "Failed to create Storage for Libirt instance #{name}: #{e}"
65 72
    end
app/views/hypervisors/_form.html.erb
2 2
  <%= base_errors_for @hypervisor %>
3 3
  <%= text_f f, :name %>
4 4
  <%= text_f f, :uri, :label => "URI", :class =>"xxlarge" %>
5
  <%= text_f f, :hyperuser, :label => "Username" %>
6
  <%= password_f f, :hyperpass, :label => "Password" %>
5 7
  <%= selectable_f f, :kind, Hypervisor::KINDS %>
6 8
  <%= submit_or_cancel f %>
7 9
<% end %>
app/views/hypervisors/guests/index.html.erb
17 17
      <td> <%= guest.interface.device rescue "N/A" %> </td>
18 18
      <td> <%= guest.volume.size %> GB </td>
19 19
      <td> <%= guest.volume.pool %> </td>
20
      <td <%= power_class(guest.running?)%>> <%= state(!guest.running?) %> </td>
20
      <td <%= power_class(guest.running?(true))%>> <%= state(!guest.running?(true)) %> </td> 
21 21
      <td>
22
        <%= power_action guest %>
23
        <%= display_link_if_authorized "Destroy", hash_for_hypervisor_guest_path(:hypervisor_id => @hypervisor, :id => guest).merge(:auth_action => :destroy), :confirm => 'Are you sure?', :method => :delete, :class => "label important" %>
22
        <%= power_action(guest, true) %>
23
        <%= display_link_if_authorized "Destroy", hash_for_hypervisor_guest_path(:hypervisor_id => @hypervisor,
24
        :id => guest).merge(:auth_action => :destroy), :confirm => 'Are you sure?', :method => :delete, :class => "label important" %>
24 25
      </td>
25
    </tr>
26
    </tr> 
26 27
  <% end -%>
27 28
</table>
28

  
29 29
<p>
30
Total of <%= number_to_human_size @guests.delete_if{|g| !g.running?}.sum {|g| g.memory*1024} %> / <%= number_to_human_size @hypervisor.memory * 1024 %> memory used
30
Total of <%= number_to_human_size @guests.delete_if{|g| !g.running?(true)}.sum {|g| g.memory*1024} %> / <%= number_to_human_size @hypervisor.memory * 1024 %> memory used
31 31
</p>
32 32

  
33 33
<%= page_entries_info @guests %>
34
<%= will_paginate     @guests %>
34
<%= will_paginate     @guests %> 
db/migrate/20111024010236_add_hypervisor_credentials.rb
1
class AddHypervisorCredentials < ActiveRecord::Migration
2
  def self.up
3
	add_column :hypervisors, :hyperuser,  :string 
4
	add_column :hypervisors, :hyperpass, :string 
5

  
6
  end
7

  
8
  def self.down
9
	remove_column :hypervisors, :hyperuser
10
	remove_column :hypervisors, :hyperpass
11
  end
12
end
0
-