Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
  • 3 commits
  • 5 files changed
  • 0 commit comments
  • 1 contributor
Showing with 54 additions and 43 deletions.
  1. +9 −2 README.md
  2. +38 −40 app/mvmc.rb
  3. +1 −0 config/locales/en.yml
  4. +1 −0 views/vms/form.haml
  5. +5 −1 views/vms/index.haml
View
11 README.md
@@ -8,11 +8,18 @@ That is both good and bad, but we have our reasons!
## Installation
-You'll need Ruby `1.9.3-p194` or greater, it was built against Ruby `2.0.0-p0.
+You'll need the `libvirt-dev` package for your platform, as well as `libvirt``
+and `virsh`:
+
+ # apt-get install -y libvirt-dev libvirt virsh
+ # apt-get install -y
+
+You'll also need Ruby `1.9.3-p194` or greater, it was built against Ruby
+`2.0.0-p0.
$ git clone https://github.com/protonet/mvmc protonet-mvmc
$ cd protonet-mvmc
- $ bundle
+ $ protonet_bundle
## Starting
View
78 app/mvmc.rb
@@ -4,14 +4,16 @@
require 'sinatra/content_for'
require 'sinatra/i18n'
-VIRSH_HOST = "protonet@cebit.local"
-VIRSH_URI = "qemu+ssh://#{VIRSH_HOST}/system?socket=/var/run/libvirt/libvirt-sock"
+VIRSH_URI = "qemu:///system"
+VIRSH_POOL_DIR = "/var/lib/libvirt/images"
-VMS_DIR = File.expand_path(File.join(File.dirname(__FILE__), '../vms/'))
-ISOS_DIR = File.expand_path(File.join(File.dirname(__FILE__), '../isos/'))
+VMS_DIR = File.expand_path(File.join(File.dirname(__FILE__), '../vms/'))
+ISOS_DIR = File.expand_path(File.join(File.dirname(__FILE__), '../isos/'))
ISO = Struct.new(:basename, :mtime, :size)
+$libvirt = Libvirt::open(VIRSH_URI)
+
module Virsh
class StoragePool
@@ -34,21 +36,21 @@ def create_volume(name, capacity)
class << self
def find(uuid)
- pool = libvirt.lookup_storage_pool_by_uuid(uuid)
+ pool = $libvirt.lookup_storage_pool_by_uuid(uuid)
StoragePool.new.tap do |sp|
sp.uuid = uuid
end
end
def create_default
- libvirt.define_storage_pool_xml(create_pool_xml).tap do |pool|
+ $libvirt.define_storage_pool_xml(create_pool_xml).tap do |pool|
pool.build
pool.create
pool.autostart = true
end
end
def all
- libvirt.list_storage_pools.collect do |pool_name|
- pool = libvirt.lookup_storage_pool_by_name(pool_name)
+ $libvirt.list_storage_pools.collect do |pool_name|
+ pool = $libvirt.lookup_storage_pool_by_name(pool_name)
StoragePool.new.tap do |sp|
sp.name = pool.name
sp.uuid = pool.uuid
@@ -56,10 +58,6 @@ def all
end
end
- def libvirt
- @_libvirt ||= Libvirt::open(VIRSH_URI)
- end
-
private
def create_pool_xml
@@ -68,7 +66,7 @@ def create_pool_xml
xml.pool type: :dir do |pool|
pool.name "virsh-images"
pool.target do |target|
- target.path "/root/virsh-images"
+ target.path VIRSH_POOL_DIR
end
end
warn xml.target!
@@ -86,11 +84,7 @@ def create_volume_xml_string(name, capacity)
end
def pool
- libvirt.lookup_storage_pool_by_uuid(uuid)
- end
-
- def libvirt
- self.class.libvirt
+ $libvirt.lookup_storage_pool_by_uuid(uuid)
end
end
@@ -109,8 +103,12 @@ def start
domain.create
end
- def stop
- domain.stop
+ def destroy
+ domain.destroy
+ end
+
+ def undefine
+ domain.undefine
end
def shutdown
@@ -121,12 +119,11 @@ def state
domain.state
end
- def vnc_address_with_port
+ def vnc_port
doc = REXML::Document.new(domain.xml_desc)
elements = doc.elements.to_a('/domain/devices/graphics')
ports = elements.collect { |e| e.attribute(:port).value }
- port = ports.first
- "vnc://#{VIRSH_HOST}:#{port}"
+ ports.first
end
class << self
@@ -136,8 +133,8 @@ def all
end
def defined
- libvirt.list_defined_domains.collect do |domain_name|
- domain = libvirt.lookup_domain_by_name(domain_name)
+ $libvirt.list_defined_domains.collect do |domain_name|
+ domain = $libvirt.lookup_domain_by_name(domain_name)
VM.new.tap do |vm|
vm.id = domain.id rescue nil
vm.uuid = domain.uuid
@@ -147,8 +144,8 @@ def defined
end
def running
- libvirt.list_domains.collect do |domain_id|
- domain = libvirt.lookup_domain_by_id(domain_id)
+ $libvirt.list_domains.collect do |domain_id|
+ domain = $libvirt.lookup_domain_by_id(domain_id)
VM.new.tap do |vm|
vm.id = domain_id
vm.uuid = domain.uuid
@@ -157,12 +154,8 @@ def running
end
end
- def libvirt
- @_libvirt ||= Libvirt::open(VIRSH_URI)
- end
-
def create(name, cdiso_paths, volume_paths)
- domain = libvirt.define_domain_xml(create_xml(name, cdiso_paths, volume_paths))
+ domain = $libvirt.define_domain_xml(create_xml(name, cdiso_paths, volume_paths))
domain.tap do |d|
domain.autostart = true
domain.create
@@ -239,7 +232,9 @@ def create_xml(name, cdisos, volume_paths)
video.address type: :pci, domain: '0x0000', bus: '0x00', slot: '0x02', function: '0x0'
end
- devices.graphics type: :vnc, port: -1
+ devices.graphics type: :vnc, autostart: :no, listen: '0.0.0.0' do |graphics|
+ graphics.listen type: :address, address: '0.0.0.0'
+ end
end
@@ -251,12 +246,8 @@ def create_xml(name, cdisos, volume_paths)
private
- def libvirt
- self.class.libvirt
- end
-
def domain
- libvirt.lookup_domain_by_uuid(uuid)
+ $libvirt.lookup_domain_by_uuid(uuid)
end
end
@@ -278,7 +269,7 @@ class Mvmc < Sinatra::Base
end
get '/dashboard' do
- haml :'dashboard/index'
+ redirect to('/vms'), 303
end
get '/vms' do
@@ -319,10 +310,17 @@ class Mvmc < Sinatra::Base
redirect '/vms'
end
+ get '/vms/:uuid/undefine' do |uuid|
+ VM.new.tap do |vm|
+ vm.uuid = uuid
+ end.undefine
+ redirect '/vms'
+ end
+
get '/vms/:uuid/stop' do |uuid|
VM.new.tap do |vm|
vm.uuid = uuid
- end.stop
+ end.destroy
redirect '/vms'
end
View
1 config/locales/en.yml
@@ -47,6 +47,7 @@ en:
vnc: "VNC"
state: "State"
button:
+ undefine: "Destroy"
start: "Start"
stop: "Stop"
shutdown: "Shutdown"
View
1 views/vms/form.haml
@@ -12,6 +12,7 @@
%label.control-label{ for: "vm_name[cdisos][#{num}]" }= t(:"vms.form.cdisos.#{num}")
.controls
%select{ id: "vm_cdisos_#{num}", name: "vm[cdisos][#{num}]" }
+ %option
- @isos.each do |file|
%option{value: File.join(ISOS_DIR, file.basename)}= file.basename
%fieldset
View
6 views/vms/index.haml
@@ -16,7 +16,8 @@
= vm.state
%td
- if vm.running?
- %a{href: vm.vnc_address_with_port}= vm.vnc_address_with_port
+ - vnc_address_with_port = sprintf("vnc://%s:%d", request.host, vm.vnc_port)
+ %a{href: vnc_address_with_port}= vnc_address_with_port
%td
- if vm.running?
%a.btn.btn-mini{href: "/vms/#{vm.uuid}/shutdown"}
@@ -29,6 +30,9 @@
%a.btn.btn-mini{href: "/vms/#{vm.uuid}/start"}
%i.icon-play
= t(:'vms.table.button.start')
+ %a.btn.btn-mini.btn-danger{href: "/vms/#{vm.uuid}/undefine"}
+ %i.icon-trash.icon-white
+ = t(:'vms.table.button.undefine')
.span6
= haml :'vms/form'

No commit comments for this range

Something went wrong with that request. Please try again.