Module: Veewee::Provider::Kvm::BoxCommand

Included in:
Box
Defined in:
lib/veewee/provider/kvm/box/up.rb,
lib/veewee/provider/kvm/box/halt.rb,
lib/veewee/provider/kvm/box/build.rb,
lib/veewee/provider/kvm/box/create.rb,
lib/veewee/provider/kvm/box/destroy.rb,
lib/veewee/provider/kvm/box/poweroff.rb,
lib/veewee/provider/kvm/box/helper/ip.rb,
lib/veewee/provider/kvm/box/validate_kvm.rb,
lib/veewee/provider/kvm/box/helper/status.rb,
lib/veewee/provider/kvm/box/helper/ssh_options.rb,
lib/veewee/provider/kvm/box/helper/console_type.rb

Instance Method Summary collapse

Instance Method Details

#add_floppyObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/veewee/provider/kvm/box/create.rb', line 93

def add_floppy
  # Get a raw libvirt connection
  c=@connection.raw

  # Retrieve the domain
  domain=c.lookup_domain_by_name(name)

  # Retrieve the existing XML from the domain
  domain_xml=domain.xml_desc

  # Convert the xml nokogiri doc
  domain_doc=Nokogiri::XML(domain_xml)

  # Find the device section
  devices=domain_doc.xpath('/domain/devices').first
  # The floppy xml representation
  floppy_xml="<disk type='file' device='floppy'><driver name='qemu' type='raw'/><source file='"+
  File.join(definitition.path,"virtualfloppy.img") +
  "'/><target dev='fda' bus='fdc'/><address type='drive' controller='0' bus='0' unit='0'/></disk>
  <controller type='fdc' index='0'>"

  # Convert the floppy xml to nokogiri
  floppy_doc=Nokogiri::XML(floppy_xml)

  # Add the floppy to the devices section
  devices.add_child(floppy_doc.root)

  # Get the raw xml of the changed document
  new_xml=domain_doc.to_xml

  # Undefine the existing domain
  s.undefine

  # Re-define the domain
  c.define_domain_xml(new_xml)
end

#build(options) ⇒ Object



5
6
7
# File 'lib/veewee/provider/kvm/box/build.rb', line 5

def build(options)
  super(options)
end

#check_default_networkObject



58
59
60
61
62
63
64
65
# File 'lib/veewee/provider/kvm/box/create.rb', line 58

def check_default_network
  # Nothing specified, we check for default network
  conn = ::Libvirt::open("qemu:///system")
  networks=conn.list_networks
  if networks.count < 1
    raise Veewee::Error, "You need at least one (active) network defined or customize your definition. This needs to be available if you connect to qemu:///system."
  end
end

#check_network(options, attributes) ⇒ Object

Check the network availability of the defined network for kvm



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/veewee/provider/kvm/box/create.rb', line 40

def check_network(options, attributes)
  env.logger.info "Checking network"
  if options.nil? or options.empty? or options["network_type"].nil? or options["network_type"] == "network"
    check_default_network
    attributes[:network_interface_type] = "network"
  elsif options["network_type"] == "bridge"
    options["network_bridge_name"].nil?
    if options["network_bridge_name"].nil?
      raise Veewee::Error, "You need to specify a 'network_bridge_name' if you plan to use 'bridge' as network type"
    else
      attributes[:network_interface_type] = "bridge"
      attributes[:network_bridge_name] = "#{options["network_bridge_name"]}"
    end
  else
    raise Veewee::Error, "You specified a 'network_type' that isn't known (#{options["network_type"]})"
  end
end

#check_pool(options, attributes) ⇒ Object

Check the given pool and append to attributes Note: volume_pool_name is not working in fog library version 1.7.0.

This should be fixed in the next release.


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/veewee/provider/kvm/box/create.rb', line 70

def check_pool(options, attributes)
  env.logger.info "Checking pool storage"
  if not options.nil? and not options.empty? and not options["pool_name"].nil?
    conn = ::Libvirt::open("qemu:///system")
    # Checking if the pool exists and if it is active
    begin
      storage = conn.lookup_storage_pool_by_name(options["pool_name"])
    rescue Libvirt::RetrieveError
      raise Veewee::Error, "You've specified an non-existent storage pool (#{options["pool_name"]})"
    end
    if storage.active?
      attributes[:volume_pool_name] = options["pool_name"]
    else
      raise Veewee::Error, "The storage pool '#{options["pool_name"]}' is not started. Start it and restart the creation process"
    end
  end
end

#console_type(sequence, type_options = {}) ⇒ Object

Type on the console



10
11
12
13
14
15
# File 'lib/veewee/provider/kvm/box/helper/console_type.rb', line 10

def console_type(sequence,type_options={})
  tcp_port=@connection.servers.all(:name => name).first.display[:port]
  display_port=tcp_port.to_i - 5900
  ui.success "Sending keystrokes to VNC port :#{display_port} - TCP port: #{tcp_port}"
  vnc_type(sequence,"127.0.0.1",display_port)
end

#create(options = {}) ⇒ Object

Create a new vm



8
9
10
11
12
13
14
15
# File 'lib/veewee/provider/kvm/box/create.rb', line 8

def create(options={})
  # Assemble the Virtualmachine and set all the memory and other stuff"


  create_server(options)
  create_volume(options)
  self.create_floppy("virtualfloppy.img")
end

#create_server(options) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/veewee/provider/kvm/box/create.rb', line 17

def create_server(options)
  #memory_size,cpu_count, volume_size
  # verify some stuff before trying to create the server
  kvm_options = definition.kvm[:vm_options][0]
  # Create the "server"
  attributes = {
    :name => name,
    :memory_size => definition.memory_size.to_i*1024,
    :cpus => definition.cpu_count.to_i,
    :volume_capacity => "#{definition.disk_size}M",
    :domain_type => options['use_emulation'] ? 'qemu': 'kvm',
    :iso_file => definition.iso_file,
    :arch => definition.os_type_id.end_with?("_64") ? "x86_64" : "i686",
    :iso_dir => env.config.veewee.iso_dir
  }
  # Check for network stuff (default, bridge)
  check_network(kvm_options, attributes)
  # Check for pool (storage)
  check_pool(kvm_options, attributes)
  s=@connection.servers.create(attributes)
end

#create_volume(options) ⇒ Object

Create the volume of a new vm



89
90
91
# File 'lib/veewee/provider/kvm/box/create.rb', line 89

def create_volume(options)
  # Creating the volume is part of the server creation
end

#destroy(options = {}) ⇒ Object

Destroy a vm



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/veewee/provider/kvm/box/destroy.rb', line 6

def destroy(options={})
  if @connection.servers.all(:name => name).nil?
    env.ui.error "Error:: You tried to destroy a non-existing box '#{name}'"
    raise Veewee::Error,"Error:: You tried to destroy a non-existing box '#{name}'"
  end

  self.poweroff if running?
  destroy_vm if exists_vm?

  vol_exists=!@connection.volumes.all(:name => "#{name}.img").nil?
  env.logger.info "Volume exists? : #{vol_exists}"
  destroy_volume if exists_volume?
end

#destroy_vmObject



20
21
22
23
# File 'lib/veewee/provider/kvm/box/destroy.rb', line 20

def destroy_vm
  matched_servers=@connection.servers.all(:name => name)
  matched_servers.first.destroy() unless matched_servers.nil?
end

#destroy_volumeObject



25
26
27
28
# File 'lib/veewee/provider/kvm/box/destroy.rb', line 25

def destroy_volume
  vol=@connection.volumes.all(:name => "#{name}.img").first
  vol.destroy
end

#exists?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/veewee/provider/kvm/box/helper/status.rb', line 13

def exists?
  exists_volume? || exists_vm?
end

#exists_vm?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/veewee/provider/kvm/box/helper/status.rb', line 21

def exists_vm?
  @connection.list_domains.find { |d| d[:name] == name }
end

#exists_volume?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/veewee/provider/kvm/box/helper/status.rb', line 17

def exists_volume?
  @connection.list_volumes.find { |v| v[:name] == "#{name}.img" }
end

#halt(options = {}) ⇒ Object



6
7
8
9
# File 'lib/veewee/provider/kvm/box/halt.rb', line 6

def halt(options={})
  matched_servers=@connection.servers.all(:name => name)
  matched_servers.first.halt unless matched_servers.nil?
end

#ip_addressObject



5
6
7
8
9
# File 'lib/veewee/provider/kvm/box/helper/ip.rb', line 5

def ip_address
  ip=@connection.servers.all(:name => "#{name}").first.public_ip_address
  return [*ip].first unless ip.nil?
  return ip
end

#poweroff(options = {}) ⇒ Object



6
7
8
9
# File 'lib/veewee/provider/kvm/box/poweroff.rb', line 6

def poweroff(options={})
  matched_servers=@connection.servers.all(:name => name)
  matched_servers.first.stop unless matched_servers.nil?
end

#running?Boolean

Returns:

  • (Boolean)


5
6
7
8
9
10
11
# File 'lib/veewee/provider/kvm/box/helper/status.rb', line 5

def running?
  if exists_vm?
    @connection.servers.all(:name => name).first.ready?
  else
    false
  end
end

#ssh_optionsObject



6
7
8
9
10
11
12
13
14
# File 'lib/veewee/provider/kvm/box/helper/ssh_options.rb', line 6

def ssh_options
  ssh_options={
    :user => definition.ssh_user,
    :port => 22,
    :password => definition.ssh_password,
    :timeout => definition..to_i
  }
  return ssh_options
end

#stop(options = {}) ⇒ Object



11
12
13
14
# File 'lib/veewee/provider/kvm/box/halt.rb', line 11

def stop(options={})
  matched_servers=@connection.servers.all(:name => name)
  matched_servers.first.stop unless matched_servers.nil?
end

#up(options = {}) ⇒ Object



6
7
8
9
# File 'lib/veewee/provider/kvm/box/up.rb', line 6

def up(options={})
  matched_servers=@connection.servers.all(:name => name)
  matched_servers.first.start unless matched_servers.nil?
end

#validate_kvm(options) ⇒ Object



6
7
8
9
10
# File 'lib/veewee/provider/kvm/box/validate_kvm.rb', line 6

def validate_kvm(options)

  validate_tags( [ 'vbox', 'puppet', 'chef'],options)

end