├── .gitignore ├── tests └── 00_basic_include.pp ├── templates ├── qemu.conf.erb └── libvirtd.conf.erb ├── Modulefile ├── Rakefile ├── LICENSE ├── manifests ├── qemu_config.pp ├── libvirtd_config.pp ├── params.pp └── init.pp ├── files └── mc-plugins │ ├── agent │ ├── libvirt.ddl │ └── libvirt.rb │ └── application │ └── libvirt.rb └── README.markdown /.gitignore: -------------------------------------------------------------------------------- 1 | pkg/ 2 | -------------------------------------------------------------------------------- /tests/00_basic_include.pp: -------------------------------------------------------------------------------- 1 | class { 'libvirt': } 2 | -------------------------------------------------------------------------------- /templates/qemu.conf.erb: -------------------------------------------------------------------------------- 1 | <%= name %>=<% if value.kind_of?(Array) %>["<%= value.join("\",\"") %>"]<% elsif value.match(/^[1-9]\d*/) %><%=value%><% else %>"<%= value %>"<% end %><%= "\n" %> -------------------------------------------------------------------------------- /templates/libvirtd.conf.erb: -------------------------------------------------------------------------------- 1 | <%= name %>=<% if value.kind_of?(Array) %>["<%= value.join("\",\"") %>"]<% elsif value.match(/^[1-9]\d*/) %><%=value%><% else %>"<%= value %>"<% end %><%= "\n" %> -------------------------------------------------------------------------------- /Modulefile: -------------------------------------------------------------------------------- 1 | name 'puppetlabs-libvirt' 2 | version '0.0.1' 3 | source 'git://github.com/puppetlabs/puppetlabs-libvirt.git' 4 | author 'puppetlabs' 5 | license 'ASL 2.0' 6 | summary 'libvirt Module' 7 | description 'Manages and configures libvirt' 8 | project_page 'https://github.com/puppetlabs/puppetlabs-libvirt' 9 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'puppetlabs_spec_helper/rake_tasks' 2 | require 'puppet-lint/tasks/puppet-lint' 3 | 4 | PuppetLint.configuration.ignore_paths = ["spec/**/*.pp", "vendor/**/*.pp"] 5 | PuppetLint.configuration.log_format = '%{path}:%{linenumber}:%{KIND}: %{message}' 6 | PuppetLint.configuration.send("disable_80chars") 7 | PuppetLint.configuration.send("disable_autoloader_layout") 8 | PuppetLint.configuration.send("disable_class_inherits_from_params_class") 9 | 10 | task :default => [:spec, :lint] 11 | 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Puppet OpenNebula Module - Puppet module for managing OpenNebula 2 | 3 | Copyright (C) 2011 Puppet Labs Inc 4 | 5 | Puppet Labs can be contacted at: info@puppetlabs.com 6 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | -------------------------------------------------------------------------------- /manifests/qemu_config.pp: -------------------------------------------------------------------------------- 1 | # This resource sets configuration items within qemu.conf. 2 | # 3 | # == Parameters 4 | # 5 | # [namevar] 6 | # This is the parameter you wish to change. For example 'vnc_listen'. 7 | # [value] 8 | # The value you wish to set the parameter to. 9 | # 10 | # == Examples 11 | # 12 | # libvirt::qemu_config { "vnc_listen": 13 | # value => "0.0.0.0", 14 | # } 15 | # 16 | # == Authors 17 | # 18 | # Ken Barber 19 | # 20 | # == Copyright 21 | # 22 | # Copyright 2011 Puppetlabs Inc, unless otherwise noted. 23 | # 24 | define libvirt::qemu_config( 25 | 26 | $value 27 | 28 | ) { 29 | 30 | file { "${libvirt::params::libvirt_config_dir}/qemu.d/${name}": 31 | content => template("${module_name}/qemu.conf.erb"), 32 | notify => Exec['create_qemu_conf'], 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /manifests/libvirtd_config.pp: -------------------------------------------------------------------------------- 1 | # This resource sets configuration items within libvirtd.conf. 2 | # 3 | # == Parameters 4 | # 5 | # [namevar] 6 | # This is the parameter you wish to change. For example 'unix_sock_group'. 7 | # [value] 8 | # The value you wish to set the parameter to. 9 | # 10 | # == Examples 11 | # 12 | # libvirt::libvirtd_config { "unix_sock_group": 13 | # value => "sasl", 14 | # } 15 | # 16 | # == Authors 17 | # 18 | # Ken Barber 19 | # 20 | # == Copyright 21 | # 22 | # Copyright 2011 Puppetlabs Inc, unless otherwise noted. 23 | # 24 | define libvirt::libvirtd_config( 25 | 26 | $value 27 | 28 | ) { 29 | 30 | file { "/etc/libvirt/libvirtd.d/${name}": 31 | content => template("${module_name}/libvirtd.conf.erb"), 32 | notify => Exec['create_libvirtd_conf'], 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /manifests/params.pp: -------------------------------------------------------------------------------- 1 | # Libvirt parameter class. Not to be used directly. 2 | # 3 | # == OS Support 4 | # 5 | # * Debian 7.0 (wheezy) 6 | # * Ubuntu 7 | # 8 | # == Variables 9 | # 10 | # This is a list of variables that must be set for each operating system. 11 | # 12 | # [libvirt_package] 13 | # Package(s) for installing libvirt. 14 | # [libvirt_version] 15 | # Version for libvirt package. 16 | # [libvirt_service] 17 | # Service for libvirt. 18 | # [libvirt_user] 19 | # User for libvirt. This is for process and for socket permissions. 20 | # [libvirt_group] 21 | # Group for libvirt. This is for process and for socket permissions. 22 | # [libvirt_config_dir] 23 | # Path to configuration directory for libvirt. 24 | # [libvirtd_config_file] 25 | # Path to libvirtd.conf. 26 | # [qemu_config_file] 27 | # Path to qemu.conf. 28 | # 29 | # == Authors 30 | # 31 | # Ken Barber 32 | # 33 | # == Copyright 34 | # 35 | # Copyright 2011 Puppetlabs Inc, unless otherwise noted. 36 | # 37 | class libvirt::params { 38 | 39 | case $::operatingsystem { 40 | 'ubuntu', 'debian': { 41 | $libvirt_package = 'libvirt-bin' 42 | $libvirt_version = 'installed' 43 | $libvirt_service = 'libvirt-bin' 44 | $libvirt_user = 'libvirt' 45 | $libvirt_group = 'libvirt' 46 | $libvirt_config_dir = '/etc/libvirt' 47 | $libvirtd_config_file = "${libvirt_config_dir}/libvirtd.conf" 48 | $qemu_config_file = "${libvirt_config_dir}/qemu.conf" 49 | } 50 | 'Fedora', 'CentOS': { 51 | $libvirt_package = 'libvirt' 52 | $libvirt_version = 'installed' 53 | $libvirt_service = 'libvirtd' 54 | $libvirt_user = 'libvirt' 55 | $libvirt_group = 'libvirt' 56 | $libvirt_config_dir = '/etc/libvirt' 57 | $libvirtd_config_file = "${libvirt_config_dir}/libvirtd.conf" 58 | $qemu_config_file = "${libvirt_config_dir}/qemu.conf" 59 | } 60 | default: { 61 | fail("Operating system ${::operatingsystem} is not supported") 62 | } 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /files/mc-plugins/agent/libvirt.ddl: -------------------------------------------------------------------------------- 1 | metadata :name => "SimpleRPC Agent For Libvirt Management", 2 | :description => "Agent To Manage Libvirt", 3 | :author => "Ken Barber", 4 | :license => "ASLv2", 5 | :version => "0.0.1", 6 | :url => "http://github.com/puppetlabs/puppetlabs-libvirt", 7 | :timeout => 180 8 | 9 | action "domain_list", :description => "List domains" do 10 | display :always 11 | 12 | output :domain_list, 13 | :description => "Hash with small amount of information per domain", 14 | :display_as => "Domain List" 15 | 16 | output :status, 17 | :description => "Status of action", 18 | :display_as => "Status" 19 | end 20 | 21 | action "domain_detail", :description => "List domains" do 22 | display :always 23 | 24 | input :uuid, 25 | :prompt => "UUID of domain", 26 | :description => "UUID of domain", 27 | :type => :string, 28 | :validation => '.', 29 | :optional => false, 30 | :maxlength => 250 31 | 32 | output :domain_detail, 33 | :description => "Hash with detailed information on a domain", 34 | :display_as => "Domain Detail" 35 | 36 | output :status, 37 | :description => "Status of action", 38 | :display_as => "Status" 39 | end 40 | 41 | action "domain_shutdown", :description => "Shutdown domain" do 42 | display :always 43 | 44 | input :uuid, 45 | :prompt => "UUID of domain", 46 | :description => "UUID of domain", 47 | :type => :string, 48 | :validation => '.', 49 | :optional => false, 50 | :maxlength => 250 51 | 52 | output :status, 53 | :description => "Status of action", 54 | :display_as => "Status" 55 | end 56 | 57 | action "domain_destroy", :description => "Destroy domain" do 58 | display :always 59 | 60 | input :uuid, 61 | :prompt => "UUID of domain", 62 | :description => "UUID of domain", 63 | :type => :string, 64 | :validation => '.', 65 | :optional => false, 66 | :maxlength => 250 67 | 68 | output :status, 69 | :description => "Status of action", 70 | :display_as => "Status" 71 | end 72 | 73 | action "domain_create", :description => "Create domain" do 74 | display :always 75 | 76 | input :name, 77 | :prompt => "Name of domain", 78 | :description => "Name of domain", 79 | :type => :string, 80 | :validation => '.', 81 | :optional => false, 82 | :maxlength => 100 83 | 84 | output :status, 85 | :description => "Status of action", 86 | :display_as => "Status" 87 | end 88 | -------------------------------------------------------------------------------- /files/mc-plugins/application/libvirt.rb: -------------------------------------------------------------------------------- 1 | require 'pp' 2 | 3 | class MCollective::Application::Libvirt options) 34 | 35 | action = configuration[:action] 36 | 37 | data = {} 38 | mc.send(action, configuration[:arguments]).each do |resp| 39 | if resp[:statuscode] == 0 40 | data[resp[:sender]] ||= {} 41 | case action 42 | when "domain_list" 43 | data[resp[:sender]][:domain_list] = resp[:data]["domain_list"] 44 | data[resp[:sender]][:status] = resp[:data]["status"] 45 | when "domain_detail" 46 | data[resp[:sender]][:domain_detail] = resp[:data]["domain_detail"] 47 | data[resp[:sender]][:status] = resp[:data]["status"] 48 | else 49 | data[resp[:sender]][:status] = resp[:data]["status"] 50 | end 51 | else 52 | printf("%-40s error = %s\n", resp[:sender], resp[:statusmsg]) 53 | end 54 | end 55 | 56 | # summarize 57 | case action 58 | when "domain_list" 59 | puts sprintf("%-36s %-4s %-8s %-4s %-4s %-6s %-15s", "UUID", "ID", "NAME", "STATUS", "VCPU", "MEM", "NODE") 60 | data.each do |sender,value| 61 | value[:domain_list].each do |uuid,domain_info| 62 | # Basic conversion to megabytes - but probably should have a nicer way of doing 63 | # this for other unit sizes 64 | memory = (domain_info["memory"].to_i/1024).floor.to_s + "M" 65 | status = domain_info["status"].to_i ? "running" : "stopped" 66 | puts sprintf("%-36.36s %-4.4s %-8.8s %-6.6s %-4.4s %-6.6s %-15.15s", uuid, domain_info["id"], domain_info["name"], status, domain_info["num_vcpus"], memory, sender) 67 | end 68 | end 69 | else 70 | data.each do |sender,data| 71 | puts "#{sender}:" 72 | pp data 73 | end 74 | end 75 | end 76 | end 77 | # vi:tabstop=4:expandtab:ai 78 | -------------------------------------------------------------------------------- /README.markdown: -------------------------------------------------------------------------------- 1 | # libvirt Module 2 | 3 | This module manages software to install and configure libvirt from within 4 | Puppet. 5 | 6 | ### Overview 7 | 8 | This is the Puppet libvirt module. Here we are providing capability within 9 | Puppet to install and configure libvirt on a host machine. 10 | 11 | It does not take care of the creation of virtual instances - if you wish to 12 | manage instances directly in Puppet use: 13 | 14 | https://github.com/carlasouza/puppet-virt 15 | 16 | ### Disclaimer 17 | 18 | Warning! While this software is written in the best interest of quality it has 19 | not been formally tested by our QA teams. Use at your own risk, but feel free 20 | to enjoy and perhaps improve it while you do. 21 | 22 | Please see the included Apache Software License for more legal details 23 | regarding warranty. 24 | 25 | ### Requirements 26 | 27 | So this module was predominantly tested on: 28 | 29 | * Debian Wheezy/libvirt 0.9.0/Puppet 2.7.0rc4 30 | 31 | This model is known to work on: 32 | 33 | * Fedora 16/libvirt 0.9.6/Puppet 2.7.6 34 | 35 | Other combinations may work, and we are happy to obviously take patches to 36 | support other stacks. 37 | 38 | # Installation 39 | 40 | As with most modules, its best to download this module from the forge: 41 | 42 | http://forge.puppetlabs.com/puppetlabs/libvirt 43 | 44 | If you want the bleeding edge (and potentially broken) version from github, 45 | download the module into your modulepath on your Puppetmaster. If you are not 46 | sure where your module path is try this command: 47 | 48 | puppet --configprint modulepath 49 | 50 | Depending on the version of Puppet, you may need to restart the puppetmasterd 51 | (or Apache) process before the functions will work. 52 | 53 | # Quick Start 54 | 55 | Setup libvirt. 56 | 57 | node "kvm1" { 58 | class { "libvirt": } 59 | } 60 | 61 | Setup and configure libvirt: 62 | 63 | node "kvm1" { 64 | class { libvirt: 65 | libvirtd_config => { 66 | max_clients => { value => 10 }, 67 | }, 68 | qemu_config => { 69 | vnc_listen => { value => "0.0.0.0" }, 70 | }, 71 | } 72 | } 73 | 74 | # Detailed Usage 75 | 76 | ## Classes 77 | 78 | ### libvirt 79 | 80 | The libvirt class is responsible for installing and setting up libvirt on a 81 | host. 82 | 83 | #### Parameters 84 | 85 | ##### libvirtd_config 86 | 87 | This parameter allows you to pass a hash that is passed to the 88 | libvirt::libvirtd_config resource. 89 | 90 | ##### qemu_config 91 | 92 | This parameter allows you to pass a hash that is passed to the 93 | libvirt::qemu_config resource. 94 | 95 | #### Examples 96 | 97 | Basic example: 98 | 99 | class { "libvirt": } 100 | 101 | Example with libvirtd_config parameters for configuring your libvirtd.conf 102 | file: 103 | 104 | class { "libvirt": 105 | libvirtd_config => { 106 | max_clients => { value => 5 }, 107 | tcp_port => { value => "16666" }, 108 | } 109 | } 110 | 111 | Example with qemu_config parameters for configuring your qemu.conf file: 112 | 113 | class { "libvirt": 114 | qemu_config => { 115 | vnc_port_listen => { value => "0.0.0.0" }, 116 | } 117 | } 118 | 119 | ## Resources 120 | 121 | ### libvirt::libvirtd_config 122 | 123 | This resource can be used to configure libvirt by populating items in the 124 | libvirtd.conf file. 125 | 126 | ### libvirt::qemu_config 127 | 128 | This resource can be used to configure libvirt by populating items in the 129 | qemu.conf file. 130 | -------------------------------------------------------------------------------- /manifests/init.pp: -------------------------------------------------------------------------------- 1 | # Install and configure libvirt. 2 | # 3 | # == Parameters 4 | # 5 | # These two parameters are used for configuring various configuration files 6 | # that libvirt uses. 7 | # 8 | # [libvirtd_config] 9 | # *Optional* A hash for creating libvirt::libvirtd_config resources. 10 | # [qemu_config] 11 | # *Optional* A hash for creating libvirt::qemu_config resources. 12 | # 13 | # These parameters are usually worked out automatically and can usually be 14 | # left as they are. 15 | # 16 | # [package] 17 | # *Optional* Package(s) for installing libvirt. 18 | # [version] 19 | # *Optional* Version for the libvirt package. 20 | # [service] 21 | # *Optional* Service(s) for stopping and starting the libvirtd process. 22 | # [user] 23 | # *Optional* User that libvirtd runs as. 24 | # [group] 25 | # *Optional* Group that libvirtd runs as. 26 | # [config_dir] 27 | # *Optional* Path to libvirt configuration. 28 | # [libvirtd_config_file] 29 | # *Optional* Path to the libvirtd configuration file. 30 | # [qemu_config_file] 31 | # *Optional* Path to the qemu configuration file for libvirtd. 32 | # 33 | # == Variables 34 | # 35 | # N/A 36 | # 37 | # == Examples 38 | # 39 | # Default configuration: 40 | # 41 | # class { "libvirt": } 42 | # 43 | # Custom libvirtd configuration: 44 | # 45 | # class { "libvirt": 46 | # libvirtd_config => { 47 | # max_clients => { value => 10 }, 48 | # }, 49 | # qemu_config => { 50 | # vnc_listen => { value => $ipaddress }, 51 | # }, 52 | # } 53 | # 54 | # == Authors 55 | # 56 | # Ken Barber 57 | # 58 | # == Copyright 59 | # 60 | # Copyright 2011 Puppetlabs Inc, unless otherwise noted. 61 | # 62 | class libvirt ( 63 | 64 | $package = $libvirt::params::libvirt_package, 65 | $version = $libvirt::params::libvirt_version, 66 | $service = $libvirt::params::libvirt_service, 67 | $user = $libvirt::params::libvirt_user, 68 | $group = $libvirt::params::libvirt_group, 69 | $libvirtd_config = undef, 70 | $config_dir = $libvirt::params::libvirt_config_dir, 71 | $libvirtd_config_file = $libvirt::params::libvirtd_config_file, 72 | $qemu_config_file = $libvirt::params::qemu_config_file, 73 | $qemu_config = undef 74 | 75 | ) inherits libvirt::params { 76 | 77 | ############################## 78 | # Base packages and service # 79 | ############################## 80 | package { $package: 81 | ensure => $version 82 | } 83 | service { $service: 84 | ensure => running, 85 | enable => true, 86 | hasstatus => true, 87 | hasrestart => true, 88 | } 89 | 90 | ##################### 91 | # Users and groups. # 92 | ##################### 93 | 94 | group { $group: 95 | ensure => present, 96 | system => true, 97 | require => Package[$package], 98 | } 99 | 100 | ######################## 101 | # libvirtd.conf Config # 102 | ######################## 103 | file { "${config_dir}/libvirtd.d": 104 | ensure => directory, 105 | purge => true, 106 | recurse => true, 107 | require => Package[$package], 108 | notify => Exec['create_libvirtd_conf'], 109 | } 110 | file { "${config_dir}/libvirtd.d/00-header": 111 | content => "# Managed by puppet\n", 112 | require => Package[$package], 113 | notify => Exec['create_libvirtd_conf'], 114 | } 115 | exec { 'create_libvirtd_conf': 116 | command => "/bin/cat ${config_dir}/libvirtd.d/* > ${libvirtd_config_file}", 117 | refreshonly => true, 118 | require => [ Package[$package], File["${config_dir}/libvirtd.d"] ], 119 | notify => Service[$service], 120 | } 121 | file { $libvirtd_config_file: 122 | ensure => file, 123 | owner => root, 124 | group => root, 125 | mode => '0644', 126 | require => Package[$package], 127 | notify => Exec['create_libvirtd_conf'], 128 | } 129 | create_resources('libvirt::libvirtd_config', $libvirtd_config) 130 | 131 | # Some minor defaults. These may need to differ per OS in the future. 132 | libvirt::libvirtd_config { ['auth_unix_ro', 'auth_unix_rw']: value => 'none' } 133 | libvirt::libvirtd_config { 'unix_sock_group': value => $group } 134 | libvirt::libvirtd_config { 'unix_sock_rw_perms': value => '0770' } 135 | 136 | #################### 137 | # qemu.conf Config # 138 | #################### 139 | file { "${config_dir}/qemu.d": 140 | ensure => directory, 141 | purge => true, 142 | recurse => true, 143 | require => Package[$package], 144 | notify => Exec['create_qemu_conf'], 145 | } 146 | file { "${config_dir}/qemu.d/00-header": 147 | content => "# Managed by puppet\n", 148 | require => Package[$package], 149 | notify => Exec['create_qemu_conf'], 150 | } 151 | exec { 'create_qemu_conf': 152 | command => "/bin/cat ${config_dir}/qemu.d/* > ${qemu_config_file}", 153 | refreshonly => true, 154 | require => [ Package[$package], File["${config_dir}/qemu.d"] ], 155 | } 156 | file { $qemu_config_file: 157 | ensure => file, 158 | owner => root, 159 | group => root, 160 | mode => '0644', 161 | require => Package[$package], 162 | notify => Exec['create_qemu_conf'], 163 | } 164 | create_resources('libvirt::qemu_config', $qemu_config) 165 | 166 | } 167 | 168 | -------------------------------------------------------------------------------- /files/mc-plugins/agent/libvirt.rb: -------------------------------------------------------------------------------- 1 | require 'libvirt' 2 | require 'xmlsimple' 3 | 4 | module MCollective 5 | module Agent 6 | # An agent that interacts with libvirt. 7 | # 8 | # See http://github.com/puppetlabs/puppetlabs-libvirt/ 9 | # 10 | # Released under the terms of ASL 2.0, same as Puppet 11 | class Libvirt "SimpleRPC Libvirt Agent", 13 | :description => "Agent to interact with libvirt", 14 | :author => "Ken Barber", 15 | :license => "ASLv2", 16 | :version => "0.0.1", 17 | :url => "http://github.com/puppetlabs/puppetlabs-libvirt/", 18 | :timeout => 60 19 | 20 | # This is a convenience wrapper around opening and closing the connection to 21 | # libvirt. 22 | def libvirt_transaction(uri = "qemu:///system") 23 | conn = ::Libvirt::open(uri) 24 | 25 | yield conn 26 | 27 | conn.close 28 | end 29 | 30 | # This action returns a short list of domains for each hypervisor. 31 | action "domain_list" do 32 | begin 33 | Log.instance.debug("Getting domain_detail for libvirt") 34 | 35 | libvirt_transaction do |conn| 36 | domains = conn.list_domains 37 | 38 | reply["domain_list"] ||= {} 39 | domains.each do |id| 40 | domain = conn.lookup_domain_by_id(id) 41 | reply["domain_list"][domain.uuid] = { 42 | "name" => domain.name, 43 | "id" => domain.id, 44 | "num_vcpus" => domain.info.nr_virt_cpu || 0, 45 | "memory" => domain.info.memory || 0, 46 | "state" => domain.info.state, 47 | } 48 | end 49 | end 50 | 51 | reply["status"] = "ok" 52 | rescue Exception => e 53 | reply.fail "#{e}" 54 | end 55 | end 56 | 57 | # This action returns detailed information gathered from 58 | # libvirt for a particulur domain. 59 | action "domain_detail" do 60 | validate :uuid, String 61 | uuid = request[:uuid] 62 | 63 | begin 64 | Log.instance.debug("Getting domain_detail for libvirt") 65 | 66 | libvirt_transaction do |conn| 67 | domain = conn.lookup_domain_by_uuid(uuid) 68 | 69 | # Grab XML data and turn it into a hash 70 | xml_desc = XmlSimple.xml_in(domain.xml_desc, {}) 71 | 72 | # The interface for information is a bit 73 | # haphazard, so I'm cherrypicking to populate 74 | # this hash. 75 | reply["domain_detail"] = { 76 | "uuid" => uuid, 77 | "cpu_time" => domain.info.cpu_time, 78 | "state" => domain.info.state, 79 | "os_type" => domain.os_type, 80 | "xml_desc" => xml_desc, 81 | } 82 | end 83 | rescue Exception => e 84 | reply.fail "#{e}" 85 | end 86 | end 87 | 88 | action "domain_shutdown" do 89 | validate :uuid, String 90 | uuid = request[:uuid] 91 | 92 | begin 93 | Log.instance.debug("Doing shutdown_domain for libvirt") 94 | 95 | libvirt_transaction do |conn| 96 | domain = conn.lookup_domain_by_uuid(uuid) 97 | domain.shutdown 98 | end 99 | 100 | reply["status"] = ["ok",uuid.to_s] 101 | rescue Exception => e 102 | reply.fail "#{e}" 103 | end 104 | 105 | end 106 | 107 | # Destroy a domain 108 | action "domain_destroy" do 109 | validate :uuid, String 110 | uuid = request[:uuid] 111 | 112 | begin 113 | Log.instance.debug("Doing domain_destroy for libvirt") 114 | 115 | libvirt_transaction do |conn| 116 | domain = conn.lookup_domain_by_uuid(uuid) 117 | domain.destroy 118 | end 119 | 120 | reply["status"] = ["ok",uuid.to_s] 121 | rescue Exception => e 122 | reply.fail "#{e}" 123 | end 124 | 125 | end 126 | 127 | # This action attempts to start a domain that exists. 128 | action "domain_start" do 129 | validate :uuid, String 130 | uuid = request[:uuid] 131 | 132 | begin 133 | Log.instance.debug("Doing domain_start for libvirt") 134 | 135 | libvirt_transaction do |conn| 136 | domain = conn.lookup_domain_by_uuid(uuid) 137 | domain.start 138 | end 139 | 140 | reply["status"] = ["ok", uuid.to_s] 141 | rescue Exception => e 142 | reply.fail "#{e}" 143 | end 144 | 145 | end 146 | 147 | # This action creates a domain. 148 | action "domain_create" do 149 | validate :name, String 150 | name = request[:name] 151 | 152 | # start by creating a disk 153 | # TODO: this is very much inserted just to make this 'work' for now 154 | `qemu-img create -f qcow2 /srv/virt/virtuals/#{name}.img.disk.0 10000000000` 155 | 156 | # TODO: most basic way to create a template obviously. 157 | xml_template = <<-EOS 158 | 159 | #{name} 160 | 256000 161 | 1 162 | 163 | hvm 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | destroy 173 | restart 174 | restart 175 | 176 | /usr/bin/kvm 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 205 | 206 | 207 | 208 | 209 | 210 | EOS 211 | 212 | begin 213 | Log.instance.debug("Doing list_nodedevices for libvirt") 214 | 215 | libvirt_transaction do |conn| 216 | domain = conn.define_domain_xml(xml_template) 217 | domain.create 218 | end 219 | 220 | reply["status"] = "ok" 221 | rescue Exception => e 222 | reply.fail "#{e}: #{e.libvirt_message}" 223 | end 224 | 225 | end 226 | 227 | end 228 | end 229 | end 230 | 231 | # vi:tabstop=4:expandtab:ai:filetype=ruby 232 | --------------------------------------------------------------------------------