Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- _form.html.erb
- <% form_for @hypervisor do |f| %>
- <%= base_errors_for @hypervisor %>
- <%= text_f f, :name %>
- <%= text_f f, :uri, :label => "URI", :class =>"xxlarge" %>
- <%= text_f f, :hyperuser, :label => "Username", :class => "xxlarge" %>
- <%= password_f f, :hyperpass, :label => "Password" %>>
- <%= selectable_f f, :kind, Hypervisor::KINDS %>
- <%= submit_or_cancel f %>
- <% end %>
- hypervisor.rb model
- # only line that references hyperuser and hyperpass
- def connect
- return true if Rails.env == "test"
- logger.debug "trying to contact Hypervisor #{name}"
- Timeout::timeout(10, StandardError) { @host = Virt.connect(uri, hyperuser, hyperpass).host }
- rescue => e
- logger.warn "Failed to connect to hypervisor #{name} - #{e}"
- @host = nil
- raise
- end
- new migration
- class AddHypervisorCredentials < ActiveRecord::Migration
- def self.up
- add_column :hypervisors, :hyperuser, :string
- add_column :hypervisors, :hyperpass, :string
- end
- def self.down
- remove_column :hypervisors, :hyperuser
- remove_column :hypervisors, :hyperpass
- end
- end
- virt.rb
- require 'virt/util'
- require 'virt/connection'
- require 'virt/host'
- require 'virt/guest'
- require 'virt/pool'
- require 'virt/volume'
- require 'virt/interface'
- module Virt
- class << self
- def connect( uri, hyperuser, hyperpass)
- #hyperuser = "root"
- #uri = 'esx://192.168.1.19?no_verify=1'
- #hyperpass = "changeme"
- @connection = Virt::Connection.new(uri, hyperuser, hyperpass)
- puts @connection.version
- end
- def connection
- return @connection if @connection and !@connection.closed?
- raise "No Connection or connection has been closed"
- end
- end
- end
- ### Connection.rb from virt lib ####
- require 'libvirt'
- module Virt
- class Connection
- attr_reader :connection
- def initialize(uri, hyperuser, hyperpass)
- raise("Must provide a guest to connect to") unless uri
- @connection = Libvirt::open_auth(uri,
- [Libvirt::CRED_AUTHNAME, Libvirt::CRED_PASSPHRASE],
- "my data") do |cred|
- if cred["type"] == Libvirt::CRED_AUTHNAME
- res = hyperuser
- elsif cred["type"] == Libvirt::CRED_PASSPHRASE
- res = hyperpass
- else
- raise "Unsupported credential #{cred['type']}"
- end
- end
- puts @connection.version
- # @connection = Libvirt::open uri
- end
- def closed?
- connection.closed?
- end
- def secure?
- connection.encrypted?
- end
- def version
- connection.libversion
- end
- def disconnect
- connection.close
- end
- def host
- Host.new
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement