Module: Chef::Knife::XenserverBase

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(includer) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/chef/knife/xenserver_base.rb', line 28

def self.included(includer)
  includer.class_eval do

    deps do
      require 'net/ssh/multi'
      require 'readline'
      require 'chef/json_compat'
      require 'terminal-table/import'
    end

    option :xenserver_password,
      :long => "--xenserver-password PASSWORD",
      :description => "Your XenServer password"

    option :xenserver_username,
      :long => "--xenserver-username USERNAME",
      :default => "root",
      :description => "Your XenServer username (default 'root')"

    option :xenserver_host,
      :long => "--xenserver-host ADDRESS",
      :description => "Your XenServer host address"
  end
end

Instance Method Details

#bytes_to_megabytes(bytes) ⇒ Object



94
95
96
# File 'lib/chef/knife/xenserver_base.rb', line 94

def bytes_to_megabytes(bytes)
  (bytes.to_i / (1024.0 * 1024.0)).round
end

#connectionObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/chef/knife/xenserver_base.rb', line 53

def connection
  if not @connection
    host = config[:xenserver_host] || Chef::Config[:knife][:xenserver_host]
    if host.nil?
      ui.error "XenServer host not defined." 
      ui.error "Use --xenserver-host or add it to the knife config file." 
      exit 1
    end
    username = config[:xenserver_username] || Chef::Config[:knife][:xenserver_username]
    password = config[:xenserver_password] || Chef::Config[:knife][:xenserver_password]
    ui.info "Connecting to XenServer host #{host.yellow}..."
    begin
      @connection = Fog::Compute.new({
        :provider => 'XenServer',
        :xenserver_url => host,
        :xenserver_username => username,
        :xenserver_password => password,
      })
    rescue SocketError => e
      ui.error "Error connecting to the hypervisor: #{host}" 
      exit 1
    rescue Fog::XenServer::InvalidLogin => e
      ui.error "Error connecting to the hypervisor: #{host}" 
      ui.error "Check the username and password."
      exit 1
    rescue => e
      ui.error "Error connecting to the hypervisor" 
      ui.error "#{e.class} #{e.message}" 
      exit 1
    end

  else
    @connection
  end
end

#locate_config_value(key) ⇒ Object



89
90
91
92
# File 'lib/chef/knife/xenserver_base.rb', line 89

def locate_config_value(key)
  key = key.to_sym
  Chef::Config[:knife][key] || config[key]
end