Advertisement
Guest User

Untitled

a guest
Oct 26th, 2011
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 2.95 KB | None | 0 0
  1. _form.html.erb
  2.  
  3. <% form_for @hypervisor do |f| %>
  4.   <%= base_errors_for @hypervisor %>
  5.   <%= text_f f, :name %>
  6.   <%= text_f f, :uri, :label => "URI", :class =>"xxlarge" %>
  7.   <%= text_f f, :hyperuser, :label => "Username", :class => "xxlarge" %>
  8.   <%= password_f f, :hyperpass, :label => "Password" %>>
  9.   <%= selectable_f f, :kind, Hypervisor::KINDS %>
  10.   <%= submit_or_cancel f %>
  11. <% end %>
  12.  
  13.  
  14. hypervisor.rb model
  15.  
  16. # only line that references hyperuser and hyperpass
  17. def connect
  18.     return true if Rails.env == "test"
  19.     logger.debug "trying to contact Hypervisor #{name}"
  20.     Timeout::timeout(10, StandardError) { @host = Virt.connect(uri, hyperuser, hyperpass).host }
  21.   rescue => e
  22.     logger.warn "Failed to connect to hypervisor #{name} - #{e}"
  23.     @host = nil
  24.     raise
  25.   end
  26.  
  27.  
  28. new migration
  29.  
  30. class AddHypervisorCredentials < ActiveRecord::Migration
  31.   def self.up
  32.         add_column :hypervisors, :hyperuser,  :string
  33.         add_column :hypervisors, :hyperpass, :string
  34.  
  35.   end
  36.  
  37.   def self.down
  38.         remove_column :hypervisors, :hyperuser
  39.         remove_column :hypervisors, :hyperpass
  40.   end
  41. end
  42.  
  43.  
  44. virt.rb
  45.  
  46. require 'virt/util'
  47. require 'virt/connection'
  48. require 'virt/host'
  49. require 'virt/guest'
  50. require 'virt/pool'
  51. require 'virt/volume'
  52. require 'virt/interface'
  53. module Virt
  54.  
  55.   class << self
  56.  
  57.     def connect( uri, hyperuser, hyperpass)
  58.       #hyperuser = "root"
  59.       #uri = 'esx://192.168.1.19?no_verify=1'
  60.       #hyperpass = "changeme"
  61.       @connection = Virt::Connection.new(uri, hyperuser, hyperpass)
  62.       puts @connection.version
  63.     end
  64.  
  65.     def connection
  66.       return @connection if @connection and !@connection.closed?
  67.       raise "No Connection or connection has been closed"
  68.     end
  69.  
  70.   end
  71.  
  72. end
  73.  
  74.  
  75. ### Connection.rb from virt lib ####
  76. require 'libvirt'
  77. module Virt
  78.     class Connection
  79.         attr_reader :connection
  80.  
  81.         def initialize(uri, hyperuser, hyperpass)
  82.             raise("Must provide a guest to connect to") unless uri
  83.             @connection = Libvirt::open_auth(uri,
  84.                                              [Libvirt::CRED_AUTHNAME, Libvirt::CRED_PASSPHRASE],
  85.                                              "my data") do |cred|
  86.  
  87.                 if cred["type"] == Libvirt::CRED_AUTHNAME
  88.                     res = hyperuser
  89.                 elsif cred["type"] == Libvirt::CRED_PASSPHRASE
  90.                     res = hyperpass
  91.                 else
  92.                     raise "Unsupported credential #{cred['type']}"
  93.                 end
  94.  
  95.             end
  96.         puts @connection.version
  97.  
  98.             #  @connection = Libvirt::open uri
  99.         end
  100.  
  101.         def closed?
  102.             connection.closed?
  103.         end
  104.  
  105.         def secure?
  106.             connection.encrypted?
  107.         end
  108.  
  109.         def version
  110.             connection.libversion
  111.         end
  112.  
  113.         def disconnect
  114.             connection.close
  115.         end
  116.  
  117.         def host
  118.             Host.new
  119.         end
  120.     end
  121. end
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement