Module: Chef::Knife::KVMBase

Included in:
KvmVmCreate, KvmVmDelete, KvmVmList
Defined in:
lib/chef/knife/kvm_base.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(includer) ⇒ Object

:nodoc: Would prefer to do this in a rational way, but can’t be done b/c of Mixlib::CLI’s design :(



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/chef/knife/kvm_base.rb', line 32

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 :kvm_password,
      :long => "--kvm-password PASSWORD",
      :description => "Your KVM password",
      :proc => Proc.new { |key| Chef::Config[:knife][:kvm_password] = key }

    option :kvm_username,
      :long => "--kvm-username USERNAME",
      :default => "root",
      :description => "Your KVM username (default 'root')",
      :proc => Proc.new { |username| Chef::Config[:knife][:kvm_username] = (username || 'root') }

    option :kvm_host,
      :long => "--kvm-host ADDRESS",
      :description => "Your KVM host address",
      :default => "127.0.0.1",
      :proc => Proc.new { |host| Chef::Config[:knife][:kvm_host] = host }
    
    option :libvirt_protocol,
      :long => "--libvirt-protocol PROTO",
      :description => "Libvirt connection protocol (default SSH)",
      :default => "ssh"
  end
end

Instance Method Details

#connectionObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/chef/knife/kvm_base.rb', line 66

def connection
  Chef::Config[:knife][:kvm_username] = 'root' if not Chef::Config[:knife][:kvm_username]
  if not @connection
    host = Chef::Config[:knife][:kvm_host] || '127.0.0.1'
    username = Chef::Config[:knife][:kvm_username] 
    password = Chef::Config[:knife][:kvm_password]
    libvirt_uri = "qemu+#{config[:libvirt_protocol]}://#{username}@#{host}/system"
    ui.info "#{ui.color("Connecting to KVM host #{config[:kvm_host]} (#{config[:libvirt_protocol]})... ", :magenta)}"
    @connection = ::Fog::Compute.new :provider => 'libvirt',
                                   :libvirt_uri => libvirt_uri,
                                   :libvirt_ip_command => "virt-cat -d $server_name /tmp/ip-info 2> /dev/null |grep -v 127.0.0.1"
  else
    @connection
  end
end

#locate_config_value(key) ⇒ Object



95
96
97
98
# File 'lib/chef/knife/kvm_base.rb', line 95

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

#upload_file(source, dest, print_progress = true) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/chef/knife/kvm_base.rb', line 82

def upload_file(source, dest, print_progress = true)
  Net::SSH.start(config[:kvm_host], config[:kvm_username], :password => config[:kvm_password]) do |ssh|
    puts "Uploading file... (#{File.basename(source)})"
    ssh.scp.upload!(source, dest) do |ch, name, sent, total|
      if print_progress
        print "\rProgress: #{(sent.to_f * 100 / total.to_f).to_i}% completed"
      end
    end
  end
  puts if print_progress
end