ruby-xen 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ === 0.0.1 / 2008-09-08
2
+
3
+ * Initial import
4
+
@@ -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
@@ -0,0 +1,53 @@
1
+ = ruby-xen
2
+
3
+ * FIX (url)
4
+
5
+ == DESCRIPTION:
6
+
7
+ ruby-xen is a ruby library for managing Xen virtual servers. It currently
8
+ wraps the command line tools provided by Xen (xm) as well as Steve Kemps
9
+ excellent Xen-tools [http://www.xen-tools.org/software/xen-tools/].
10
+
11
+ ruby-xen is packaged as a Rails Gem which means you can require it from
12
+ a Ruby on Rails project and make use of the classes it provides.
13
+
14
+ ruby-xen can also be used by ruby code or from irb.
15
+
16
+ == FEATURES/PROBLEMS:
17
+
18
+ * FIX (list of features or problems)
19
+
20
+ == SYNOPSIS:
21
+
22
+ FIX (code sample of usage)
23
+
24
+ == REQUIREMENTS:
25
+
26
+ xen-tools (http://www.xen-tools.org/software/xen-tools/)
27
+
28
+ == INSTALL:
29
+
30
+ sudo gem install ruby-xen
31
+
32
+ == LICENSE:
33
+
34
+ ruby-xen is licenced under the GPL. This means that you can use it in commercial
35
+ or open source applications. More details found here:
36
+ http://www.gnu.org/licenses/gpl.html
37
+
38
+ ruby-xen
39
+ Copyright (C) 2008 Mike Bailey and Nick Markfleet
40
+
41
+ This program is free software; you can redistribute it and/or
42
+ modify it under the terms of the GNU General Public License
43
+ as published by the Free Software Foundation; either version 2
44
+ of the License, or (at your option) any later version.
45
+
46
+ This program is distributed in the hope that it will be useful,
47
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
48
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
49
+ GNU General Public License for more details.
50
+
51
+ You should have received a copy of the GNU General Public License
52
+ along with this program; if not, write to the Free Software
53
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
@@ -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
File without changes
@@ -0,0 +1,4 @@
1
+ class RubyXen
2
+ VERSION = '1.0.0'
3
+ end
4
+ require "#{File.dirname(__FILE__)}/ruby-xen/domain"
@@ -0,0 +1,229 @@
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
+ class Host
18
+
19
+ attr_reader :host, :machine, :total_memory, :free_memory
20
+ def initialize
21
+ result = `xm info`
22
+ result.scan(/(\S+)\s*:\s*([^\n]+)/).each do |i,j|
23
+ instance_variable_set("@#{i}", j)
24
+ end
25
+ end
26
+
27
+ end
28
+
29
+
30
+ class Domain
31
+
32
+ attr_accessor :name, :image, :config, :instance
33
+
34
+ def initialize(name)
35
+ @name = name
36
+ @config = Xen::Config.find(name)
37
+ @instance = Xen::Instance.find(name)
38
+ @image = Xen::Image.find(name)
39
+ end
40
+
41
+ def self.find(*args)
42
+ options = args.extract_options!
43
+ case args.first
44
+ when :all then Xen::Config.find(:all, options).collect { |config| Xen::Domain.new(config.name) }
45
+ when :running then Xen::Instance.find(:all, options).collect { |instance| Xen::Domain.new(instance.name) }
46
+ # Retrieve a Domain by name
47
+ else Xen::Config.find_by_name(args.first) && self.new(args.first)
48
+ end
49
+ end
50
+
51
+ def running?
52
+ @instance
53
+ end
54
+
55
+ end
56
+
57
+
58
+ class Config
59
+
60
+ attr_reader :name, :memory, :ip
61
+
62
+ def initialize(*args)
63
+ options = args.extract_options!
64
+ @name = args.first
65
+ @memory = options[:memory] || nil
66
+ @ip = options[:ip] || nil
67
+ end
68
+
69
+ def self.find(*args)
70
+ options = args.extract_options!
71
+ case args.first
72
+ when :all then all
73
+ else find_by_name(args.first)
74
+ end
75
+ end
76
+
77
+ def self.all
78
+ result = `xen-list-images`
79
+ configs = result.scan(/Name: (\w+)\nMemory: (\w+)\nIP: (\S+)/)
80
+ configs.collect do |config|
81
+ name, memory, ip = config
82
+ new(name, :memory => memory, :ip => ip)
83
+ end
84
+ end
85
+
86
+ def self.find_by_name(name)
87
+ return new('Domain-0') if name == 'Domain-0'
88
+ all.detect {|config| puts config; config.name == name.to_s}
89
+ end
90
+
91
+ end
92
+
93
+
94
+ class Image
95
+
96
+ attr_accessor :name
97
+
98
+ def initialize(name)
99
+ @name = name
100
+ end
101
+
102
+ def self.find(name)
103
+ new name
104
+ end
105
+
106
+ def find_one(name, options)
107
+ if result = find_every(options).first
108
+ result
109
+ else
110
+ raise RecordNotFound, "Couldn't find domain with name=#{name}"
111
+ end
112
+ end
113
+ end
114
+
115
+
116
+ class Instance
117
+
118
+ attr_reader :name, :domid, :memory, :cpu_time, :vcpus, :state, :start_time
119
+
120
+ def initialize(name, options={})
121
+ @name = name
122
+ @domid = options[:domid] || nil
123
+ @memory = options[:memory] || nil
124
+ @cpu_time = options[:cpu_time] || nil
125
+ @vcpus = options[:vcpus] || nil
126
+ @state = options[:state] || nil
127
+ @start_time = options[:start_time] || nil
128
+ end
129
+
130
+ def self.find(*args)
131
+ options = args.extract_options!
132
+ case args.first
133
+ when :all then all
134
+ else find_by_name(args.first)
135
+ end
136
+ end
137
+
138
+ def self.all
139
+ result = `xm list`
140
+ # XXX check for failed command
141
+ result_array = result.split("\n")
142
+ result_array.shift
143
+ result_array.collect do |domain|
144
+ name, domid, memory, vcpus, state, cpu_time = domain.scan(/[^ ,]+/)
145
+ new(name, :domid => domid, :memory => memory, :cpu_time => cpu_time)
146
+ end
147
+ end
148
+
149
+ def self.find_by_name(name)
150
+ all.detect{|domain| domain.name == name.to_s }
151
+ end
152
+
153
+ # XXX Rails version - we need some error checking!
154
+ #
155
+ # def self.find_by_name(name, options)
156
+ # if result = find_every(options)
157
+ # result.detect{ |domain| domain.name == name }
158
+ # else
159
+ # raise RecordNotFound, "Couldn't find domain with name=#{name}"
160
+ # end
161
+ # end
162
+
163
+ # A convenience wrapper for <tt>find(:dom0)</tt>.</tt>.
164
+ def dom0(*args)
165
+ find_by_name(:dom0)
166
+ end
167
+
168
+ # This is an alias for find(:all). You can pass in all the same arguments to this method as you can
169
+ # to find(:all)
170
+ def all(*args)
171
+ find(:all, *args)
172
+ end
173
+
174
+ def uptime
175
+ start_time ? Time.now - start_time : nil
176
+ end
177
+
178
+ def running?
179
+ output = `xm list #{name}`
180
+ $? == 0 ? true : false
181
+ end
182
+
183
+ def start
184
+ output = `xm create #{name}.cfg`
185
+ $? == 0 ? true : false
186
+ end
187
+
188
+ def shutdown
189
+ output = `xm shutdown #{name}`
190
+ $? == 0 ? true : false
191
+ end
192
+
193
+ def reboot
194
+ `xm reboot #{name}`
195
+ $? == 0 ? true : false
196
+ end
197
+
198
+ def destroy
199
+ end
200
+
201
+ def pause
202
+ end
203
+
204
+ end
205
+
206
+ class Backup
207
+ end
208
+
209
+ # class XenTools
210
+ #
211
+ # def self.xen_list_images
212
+ # puts "returning list of images"
213
+ # end
214
+ #
215
+ # def xen_create_image
216
+ # puts "creating image"
217
+ # end
218
+ #
219
+ # def xen_delete_image
220
+ # puts "creating image"
221
+ # end
222
+ #
223
+ # def xen_archive_image
224
+ # puts "archiving image"
225
+ # end
226
+ #
227
+ # end
228
+
229
+ end
File without changes
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: 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 +10: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/ruby-xen/domain.rb
36
+ has_rdoc: true
37
+ homepage: http://github.com/schacon/grit
38
+ post_install_message:
39
+ rdoc_options:
40
+ - --main
41
+ - README.txt
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ requirements: []
57
+
58
+ rubyforge_project:
59
+ rubygems_version: 1.2.0
60
+ signing_key:
61
+ specification_version: 2
62
+ summary: Ruby library for managing Xen virtual hosts
63
+ test_files: []
64
+