mbailey-ruby-xen 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ === 0.0.1 / 2008-09-08
2
+
3
+ * Initial import
4
+
data/Manifest.txt ADDED
@@ -0,0 +1,8 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.rdoc
4
+ Rakefile
5
+ bin/ruby-xen
6
+ lib/ruby-xen.rb
7
+ test/test_ruby-xen.rb
8
+ lib/ruby-xen/domain.rb
data/README.rdoc ADDED
@@ -0,0 +1,66 @@
1
+ = ruby-xen
2
+
3
+ # Warning! Not ready yet - project started Sep 2008
4
+
5
+ http://github.com/mbailey/ruby-xen
6
+
7
+
8
+ == DESCRIPTION:
9
+
10
+ ruby-xen is a ruby library for managing Xen virtual servers. It currently
11
+ wraps the command line tools provided by Xen (xm) as well as Steve Kemps
12
+ excellent Xen-tools [http://www.xen-tools.org/software/xen-tools/].
13
+
14
+ ruby-xen is packaged as a Rails Gem which means you can require it from
15
+ a Ruby on Rails project and make use of the classes it provides.
16
+
17
+ ruby-xen can also be used by ruby code or from irb.
18
+
19
+ == FEATURES/PROBLEMS:
20
+
21
+ * FIX (list of features or problems)
22
+
23
+ == SYNOPSIS:
24
+
25
+ require 'rubygems'
26
+ require 'ruby-xen'
27
+
28
+
29
+ domain = Xen::Domain.find(:example)
30
+ domain.running? # true
31
+ domain.stop
32
+ domain.running? # false
33
+ domain.start
34
+ domain.running? # true
35
+
36
+ == REQUIREMENTS:
37
+
38
+ ruby-xen must be run as root as it uses Xen's 'xm' command.
39
+ xen-tools must be installed (http://www.xen-tools.org/software/xen-tools/)
40
+
41
+ == INSTALL:
42
+
43
+ sudo gem install ruby-xen
44
+
45
+ == LICENSE:
46
+
47
+ ruby-xen is licenced under the GPL. This means that you can use it in commercial
48
+ or open source applications. More details found here:
49
+ http://www.gnu.org/licenses/gpl.html
50
+
51
+ ruby-xen
52
+ Copyright (C) 2008 Mike Bailey and Nick Markfleet
53
+
54
+ This program is free software; you can redistribute it and/or
55
+ modify it under the terms of the GNU General Public License
56
+ as published by the Free Software Foundation; either version 2
57
+ of the License, or (at your option) any later version.
58
+
59
+ This program is distributed in the hope that it will be useful,
60
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
61
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
62
+ GNU General Public License for more details.
63
+
64
+ You should have received a copy of the GNU General Public License
65
+ along with this program; if not, write to the Free Software
66
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+ require './lib/ruby-xen.rb'
6
+
7
+ Hoe.new('ruby-xen', RubyXen::VERSION) do |p|
8
+ # p.rubyforge_name = 'ruby-xenx' # if different than lowercase project name
9
+ p.developer('Mike Bailey', 'mike@bailey.net.au')
10
+ end
11
+
12
+ # vim: syntax=Ruby
data/bin/ruby-xen ADDED
File without changes
data/lib/ruby-xen.rb ADDED
@@ -0,0 +1,50 @@
1
+ class Array #:nodoc:
2
+ # Extracts options from a set of arguments. Removes and returns the last
3
+ # element in the array if it's a hash, otherwise returns a blank hash.
4
+ #
5
+ # def options(*args)
6
+ # args.extract_options!
7
+ # end
8
+ #
9
+ # options(1, 2) # => {}
10
+ # options(1, 2, :a => :b) # => {:a=>:b}
11
+ def extract_options!
12
+ last.is_a?(::Hash) ? pop : {}
13
+ end
14
+ end
15
+
16
+ module Xen
17
+ # Location of Xen config files
18
+ XEN_DOMU_CONFIG_DIR = '/etc/xen'
19
+ # XEN_DOMU_CONFIG_DIR = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '/spec/fixtures/xen_domu_configs'))
20
+
21
+ # We don't want out library to hit Xen too often (premature optimization perhaps?)
22
+ # so we keep information about Xen instances in an object. Specify how long before
23
+ # the object expires.
24
+ INSTANCE_OBJECT_LIFETIME = 1
25
+
26
+ # DRY up some classes (children of Domain) with some module funkiness.
27
+ module Parentable
28
+ # Returns the parent Domain object (d) for a sub-object.
29
+ # We ensure d.instance.object_id == self.object_id
30
+ #
31
+ # ==== Example
32
+ # i = Xen::Instance.all[2]
33
+ # d = i.domain
34
+ # # i.object_id == d.instance.object_id
35
+ #
36
+ def domain
37
+ d = Xen::Domain.new(name)
38
+ d.instance_variable_set("@#{self.class.to_s.sub('Xen::','').downcase}", self)
39
+ d
40
+ end
41
+ end
42
+ end
43
+
44
+ require "#{File.dirname(__FILE__)}/xen/backup"
45
+ require "#{File.dirname(__FILE__)}/xen/command"
46
+ require "#{File.dirname(__FILE__)}/xen/config"
47
+ require "#{File.dirname(__FILE__)}/xen/domain"
48
+ require "#{File.dirname(__FILE__)}/xen/host"
49
+ require "#{File.dirname(__FILE__)}/xen/image"
50
+ require "#{File.dirname(__FILE__)}/xen/instance"
data/lib/xen/backup.rb ADDED
@@ -0,0 +1,3 @@
1
+ class Xen::Backup
2
+ include Xen::Parentable
3
+ end
@@ -0,0 +1,8 @@
1
+ class Xen::Command
2
+ def self.xm_list
3
+ `xm list`
4
+ end
5
+ def self.xm_info
6
+ `xm info`
7
+ end
8
+ end
data/lib/xen/config.rb ADDED
@@ -0,0 +1,52 @@
1
+ # The Xen config files on disk
2
+ class Xen::Config
3
+ include Xen::Parentable
4
+ attr_accessor :name, :kernel, :ramdisk, :memory, :root, :disk, :vif, :on_poweroff, :on_reboot, :on_crash, :extra
5
+
6
+ def initialize(*args)
7
+ options = args.extract_options!
8
+ @name = args.first
9
+ @kernel = options[:kernel]
10
+ @ramdisk = options[:ramdisk]
11
+ @memory = options[:memory]
12
+ @root = options[:root]
13
+ @disk = options[:disk]
14
+ @vif = options[:vif]
15
+ @on_poweroff = options[:on_poweroff]
16
+ @on_reboot = options[:on_reboot]
17
+ @on_crash = options[:on_crash]
18
+ @extra = options[:extra]
19
+ end
20
+
21
+ def self.find(*args)
22
+ options = args.extract_options!
23
+ case args.first
24
+ when :all then all
25
+ else find_by_name(args.first)
26
+ end
27
+ end
28
+
29
+ def self.all
30
+ config_files = Dir.glob("#{Xen::XEN_DOMU_CONFIG_DIR}/*.cfg")
31
+ config_files.collect do |filename|
32
+ create_from_config_file(File.read(filename))
33
+ end
34
+ end
35
+
36
+ def self.find_by_name(name)
37
+ return new('Domain-0') if name == 'Domain-0'
38
+ filename = "#{Xen::XEN_DOMU_CONFIG_DIR}/#{name}.cfg"
39
+ create_from_config_file(File.read(filename))
40
+ end
41
+
42
+ def self.create_from_config_file(config)
43
+ name, kernel, ramdisk, memory, root, disk, vif, on_poweroff, on_reboot, on_crash, extra = nil
44
+ eval(config)
45
+ new(name, :disk => disk, :kernel => kernel, :ramdisk => ramdisk, :memory => memory, :root => root, :disk => disk, :vif => vif, :on_poweroff => on_poweroff, :on_reboot => on_reboot, :on_crash => on_crash, :extra => extra)
46
+ end
47
+
48
+ def save
49
+ puts "I saved the config!"
50
+ end
51
+
52
+ end
data/lib/xen/domain.rb ADDED
@@ -0,0 +1,47 @@
1
+ class Xen::Domain
2
+ attr_accessor :name, :image, :config
3
+
4
+ def initialize(name)
5
+ @name = name
6
+ @config = Xen::Config.find(name)
7
+ @instance = Xen::Instance.find(name)
8
+ @image = Xen::Image.find(name)
9
+ end
10
+
11
+ def instance
12
+ if @instance && @instance.object_expires > Time.now
13
+ @instance
14
+ else
15
+ @instance = Xen::Instance.find(@name)
16
+ end
17
+ end
18
+
19
+ def self.find(*args)
20
+ options = args.extract_options!
21
+ case args.first
22
+ when :all then Xen::Config.find(:all, options).collect { |config| config.domain }
23
+ when :running then Xen::Instance.find(:all, options).collect { |instance| instance.domain }
24
+ # Retrieve a Domain by name
25
+ else Xen::Config.find_by_name(args.first) && self.new(args.first)
26
+ end
27
+ end
28
+
29
+ def all(options={})
30
+ self.find(:all, options)
31
+ end
32
+
33
+ def running?
34
+ self.instance ? true : false
35
+ end
36
+
37
+ def start
38
+ Xen::Instance.create(@name)
39
+ @instance = Xen::Instance.find(@name)
40
+ end
41
+
42
+ def stop
43
+ Xen::Instance.shutdown(@name)
44
+ @instance = Xen::Instance.find(@name)
45
+ end
46
+
47
+ end
data/lib/xen/host.rb ADDED
@@ -0,0 +1,10 @@
1
+ class Xen::Host
2
+ attr_reader :host, :machine, :total_memory, :free_memory
3
+
4
+ def initialize
5
+ result = Xen:Command.xm_info
6
+ result.scan(/(\S+)\s*:\s*([^\n]+)/).each do |i,j|
7
+ instance_variable_set("@#{i}", j)
8
+ end
9
+ end
10
+ end
data/lib/xen/image.rb ADDED
@@ -0,0 +1,12 @@
1
+ class Xen::Image
2
+ include Xen::Parentable
3
+ attr_accessor :name
4
+
5
+ def initialize(name)
6
+ @name = name
7
+ end
8
+
9
+ def self.find(name)
10
+ new name
11
+ end
12
+ end
@@ -0,0 +1,84 @@
1
+ class Xen::Instance
2
+ include Xen::Parentable
3
+ attr_reader :name, :domid, :memory, :cpu_time, :vcpus, :state, :start_time, :object_expires
4
+
5
+ def initialize(name, options={})
6
+ @name = name
7
+ @domid = options[:domid]
8
+ @memory = options[:memory]
9
+ @cpu_time = options[:cpu_time]
10
+ @vcpus = options[:vcpus]
11
+ @state = options[:state]
12
+ @start_time = options[:start_time]
13
+ @object_expires = Time.now + Xen::INSTANCE_OBJECT_LIFETIME
14
+ end
15
+
16
+ def self.find(*args)
17
+ options = args.extract_options!
18
+ case args.first
19
+ when :all then all
20
+ else find_by_name(args.first)
21
+ end
22
+ end
23
+
24
+ def self.all
25
+ result = Xen::Command.xm_list
26
+ # XXX check for failed command
27
+ result_array = result.split("\n")
28
+ result_array.shift
29
+ result_array.collect do |domain|
30
+ name, domid, memory, vcpus, state, cpu_time = domain.scan(/[^ ,]+/)
31
+ new(name, :domid => domid, :memory => memory, :cpu_time => cpu_time)
32
+ end
33
+ end
34
+
35
+ def self.find_by_name(name)
36
+ all.detect{|domain| domain.name == name.to_s }
37
+ end
38
+
39
+ # XXX Rails version - we need some error checking!
40
+ #
41
+ # def self.find_by_name(name, options)
42
+ # if result = find_every(options)
43
+ # result.detect{ |domain| domain.name == name }
44
+ # else
45
+ # raise RecordNotFound, "Couldn't find domain with name=#{name}"
46
+ # end
47
+ # end
48
+
49
+ def self.create(name)
50
+ output = `xm create #{name}.cfg`
51
+ $? == 0 ? true : false
52
+ end
53
+
54
+ def self.shutdown(name)
55
+ output = `xm shutdown #{name}`
56
+ $? == 0 ? true : false
57
+ end
58
+
59
+ # A convenience wrapper for <tt>find(:dom0)</tt>.</tt>.
60
+ def self.dom0(*args)
61
+ find_by_name(:dom0)
62
+ end
63
+
64
+ def uptime
65
+ start_time ? Time.now - start_time : nil
66
+ end
67
+
68
+ def running?
69
+ output = `xm list #{name}`
70
+ $? == 0 ? true : false
71
+ end
72
+
73
+ def reboot
74
+ `xm reboot #{name}`
75
+ $? == 0 ? true : false
76
+ end
77
+
78
+ def destroy
79
+ end
80
+
81
+ def pause
82
+ end
83
+
84
+ end
File without changes
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mbailey-ruby-xen
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Mike Bailey
8
+ - Nick Marfleet
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2008-09-08 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: ruby-xen allows you to manage Xen virtual servers via Ruby. It currently wraps the command line tools provided by Xen (xm) as well as Steve Kemps excellent Xen-tools (http://www.xen-tools.org/software/xen-tools/).
18
+ email: mike@bailey.net.au
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files:
24
+ - History.txt
25
+ - Manifest.txt
26
+ - README.rdoc
27
+ files:
28
+ - History.txt
29
+ - Manifest.txt
30
+ - README.rdoc
31
+ - Rakefile
32
+ - bin/ruby-xen
33
+ - lib/ruby-xen.rb
34
+ - test/test_ruby-xen.rb
35
+ - lib/xen/backup.rb
36
+ - lib/xen/command.rb
37
+ - lib/xen/config.rb
38
+ - lib/xen/domain.rb
39
+ - lib/xen/host.rb
40
+ - lib/xen/image.rb
41
+ - lib/xen/instance.rb
42
+ has_rdoc: true
43
+ homepage: http://github.com/schacon/grit
44
+ post_install_message:
45
+ rdoc_options:
46
+ - --main
47
+ - README.rdoc
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.2.0
66
+ signing_key:
67
+ specification_version: 2
68
+ summary: Ruby library for managing Xen virtual hosts
69
+ test_files: []
70
+