mbailey-ruby-xen 0.0.3 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +25 -17
- data/lib/ruby-xen.rb +1 -1
- data/lib/templates/exclude_from_backups +2 -0
- data/lib/xen/backup.rb +19 -11
- data/lib/xen/command.rb +6 -0
- data/lib/xen/config_file.rb +5 -6
- data/lib/xen/host.rb +37 -0
- data/lib/xen/lvm.rb +14 -7
- data/lib/xen/slice.rb +5 -0
- metadata +5 -2
data/README.rdoc
CHANGED
@@ -22,13 +22,19 @@ ruby-xen can also be used by ruby code or from irb.
|
|
22
22
|
|
23
23
|
require 'rubygems'
|
24
24
|
require 'ruby-xen'
|
25
|
+
include Xen
|
25
26
|
|
26
|
-
slice =
|
27
|
+
slice = Slice.find :example
|
27
28
|
slice.running? # true
|
28
29
|
slice.stop
|
29
30
|
slice.running? # false
|
30
31
|
slice.start
|
31
32
|
slice.running? # true
|
33
|
+
|
34
|
+
slice.backups # => [#<Xen::Backup:0xb7a96520 @name=:example, @version="20090118">]
|
35
|
+
s.create_backup # => #<Xen::Backup:0xb7a922a4 @name=:example, @version="20090123">
|
36
|
+
|
37
|
+
|
32
38
|
|
33
39
|
== REQUIREMENTS:
|
34
40
|
|
@@ -41,23 +47,25 @@ sudo gem install ruby-xen
|
|
41
47
|
|
42
48
|
== LICENSE:
|
43
49
|
|
44
|
-
|
45
|
-
or open source applications. More details found here:
|
46
|
-
http://www.gnu.org/licenses/gpl.html
|
50
|
+
(The MIT License)
|
47
51
|
|
48
|
-
|
49
|
-
Copyright (C) 2008 Mike Bailey and Nick Marfleet
|
52
|
+
Copyright (c) 2008-2009 Mike Bailey
|
50
53
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
54
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
55
|
+
a copy of this software and associated documentation files (the
|
56
|
+
'Software'), to deal in the Software without restriction, including
|
57
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
58
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
59
|
+
permit persons to whom the Software is furnished to do so, subject to
|
60
|
+
the following conditions:
|
55
61
|
|
56
|
-
|
57
|
-
|
58
|
-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
59
|
-
GNU General Public License for more details.
|
62
|
+
The above copyright notice and this permission notice shall be
|
63
|
+
included in all copies or substantial portions of the Software.
|
60
64
|
|
61
|
-
|
62
|
-
|
63
|
-
|
65
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
66
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
67
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
68
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
69
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
70
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
71
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/lib/ruby-xen.rb
CHANGED
data/lib/xen/backup.rb
CHANGED
@@ -8,34 +8,42 @@ module Xen
|
|
8
8
|
options = args.extract_options!
|
9
9
|
name = args.first
|
10
10
|
|
11
|
-
options = {}
|
12
|
-
name = 'foo' # XXX replace with real value
|
11
|
+
# options = {}
|
12
|
+
# name = 'foo' # XXX replace with real value
|
13
13
|
version = options[:version] || Time.now.strftime('%Y%m%d')
|
14
14
|
backup_dir = options[:backup_dir] || Xen::BACKUP_DIR
|
15
15
|
backup_file_ext = options[:backup_file_ext] || Xen::BACKUP_FILE_EXT
|
16
|
-
archive_name="#{name}-#{version}
|
16
|
+
archive_name="#{name}-#{version}#{backup_file_ext}"
|
17
17
|
|
18
18
|
slice = Xen::Slice.find(name) # XXX test for failure
|
19
|
-
slice.
|
20
|
-
|
19
|
+
if slice.running?
|
20
|
+
slice.stop
|
21
|
+
sleep 10
|
22
|
+
restart_slice = true
|
23
|
+
end
|
21
24
|
|
22
25
|
temp_mount = `mktemp -d -p /mnt #{name}-XXXXX`.chomp # XXX test for failure
|
23
26
|
`mount #{slice.root_disk.path} #{temp_mount}` # XXX test for failure
|
27
|
+
|
28
|
+
|
24
29
|
|
25
|
-
|
26
|
-
|
27
|
-
#
|
30
|
+
FileUtils.mkdir_p backup_dir
|
31
|
+
|
32
|
+
# Creating archive at backup_dir/archive_name ...
|
33
|
+
excludes_file = File.join(File.dirname(__FILE__),'..','templates','exclude_from_backups')
|
34
|
+
temp_tarball = `mktemp -p #{backup_dir} #{name}-XXXXX`.chomp # XXX test for failure
|
35
|
+
`tar --create --exclude-from=#{excludes_file} --directory #{temp_mount} --file #{temp_tarball} . && mv #{temp_tarball} #{backup_dir}/#{archive_name}`
|
28
36
|
|
29
37
|
# Unmounting image
|
30
38
|
`umount #{temp_mount}`
|
31
39
|
Dir.delete(temp_mount)
|
32
40
|
|
33
41
|
# Creating symlink from new backup to filename without version number
|
34
|
-
last_backup = "#{backup_dir}/#{name}
|
35
|
-
File.delete(last_backup) if File.symlink(last_backup)
|
42
|
+
last_backup = "#{backup_dir}/#{name}#{backup_file_ext}"
|
43
|
+
File.delete(last_backup) if File.symlink?(last_backup)
|
36
44
|
`ln -sf #{backup_dir}/#{archive_name} #{last_backup}`
|
37
45
|
|
38
|
-
slice.start
|
46
|
+
slice.start if restart_slice == true
|
39
47
|
|
40
48
|
new(:name => name, :version => version)
|
41
49
|
end
|
data/lib/xen/command.rb
CHANGED
@@ -93,6 +93,12 @@ module Xen
|
|
93
93
|
system(cmd)
|
94
94
|
end
|
95
95
|
|
96
|
+
def self.create_backup(*args)
|
97
|
+
name = args.shift
|
98
|
+
slice = Xen::Slice.find(name)
|
99
|
+
slice.create_backup
|
100
|
+
end
|
101
|
+
|
96
102
|
def self.xm_info
|
97
103
|
result = `/usr/sbin/xm info`
|
98
104
|
result.scan(/(\S+)\s*:\s*([^\n]+)/).inject({}){ |m, (i,j)| m[i.to_sym] = j; m }
|
data/lib/xen/config_file.rb
CHANGED
@@ -30,7 +30,7 @@ module Xen
|
|
30
30
|
end
|
31
31
|
|
32
32
|
def self.all
|
33
|
-
config_files = Dir.glob("#{Xen::XEN_DOMU_CONFIG_DIR}/*#{Xen::CONFIG_FILE_EXTENSION}")
|
33
|
+
config_files = Dir.glob("#{Xen::XEN_DOMU_CONFIG_DIR}/*#{Xen::CONFIG_FILE_EXTENSION}").sort
|
34
34
|
config_files.collect do |filename|
|
35
35
|
create_from_config_file(File.read(filename))
|
36
36
|
end
|
@@ -113,7 +113,7 @@ module Xen
|
|
113
113
|
|
114
114
|
def to_str
|
115
115
|
%w(ip mac bridge vifname).collect { |key|
|
116
|
-
"#{key}=#{instance_variable_get('@' + key)}"
|
116
|
+
"#{key}=#{instance_variable_get('@' + key)}" unless instance_variable_get('@'+key) == ''
|
117
117
|
}.compact.join(',')
|
118
118
|
end
|
119
119
|
end
|
@@ -146,10 +146,9 @@ module Xen
|
|
146
146
|
Xen::Command.lv_size(@volume_group, @name)
|
147
147
|
end
|
148
148
|
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
# end
|
149
|
+
def path
|
150
|
+
"/dev/#{volume_group}/#{name}"
|
151
|
+
end
|
153
152
|
|
154
153
|
def to_str
|
155
154
|
"phy:#{volume_group}/#{name},#{domu},#{mode}"
|
data/lib/xen/host.rb
CHANGED
@@ -24,4 +24,41 @@ module Xen
|
|
24
24
|
|
25
25
|
end
|
26
26
|
|
27
|
+
|
28
|
+
# XXX Move this somewhere else!
|
29
|
+
|
30
|
+
# Network Bridge Script looks like this.
|
31
|
+
#
|
32
|
+
# #!/bin/sh
|
33
|
+
# /etc/xen/scripts/network-bridge $1 netdev=eth0 bridge=xenbr0 vifnum=0 antispoof=no
|
34
|
+
# /etc/xen/scripts/network-bridge $1 netdev=eth1 bridge=xenbr1 vifnum=1 antispoof=no
|
35
|
+
|
36
|
+
class Bridges
|
37
|
+
NETWORK_BRIDGE_WRAPPER = '/etc/xen/scripts/network-bridge-wrapper'
|
38
|
+
|
39
|
+
def self.find
|
40
|
+
f = File.readlines(NETWORK_BRIDGE_WRAPPER).collect { |line|
|
41
|
+
if (m = line.match /netdev=(.*) bridge=(.*) vifnum=(.*) antispoof=(.*)/)
|
42
|
+
Xen::Bridge.new :netdev => m[1], :bridge => m[2], :vifnum => m[3], :antispoof => m[4]
|
43
|
+
end
|
44
|
+
}.compact
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.save
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
class Bridge
|
53
|
+
attr_accessor :netdev, :bridge, :vifnum, :antispoof
|
54
|
+
|
55
|
+
def initialize(*args)
|
56
|
+
options = args.extract_options!
|
57
|
+
@netdev = options[:netdev]
|
58
|
+
@bridge = options[:bridge]
|
59
|
+
@vifnum = options[:vifnum]
|
60
|
+
@antispoof = options[:antispoof]
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
27
64
|
end
|
data/lib/xen/lvm.rb
CHANGED
@@ -15,19 +15,26 @@ module Xen
|
|
15
15
|
name ||= nil
|
16
16
|
# XXX deal with not found error
|
17
17
|
cmd = "vgs --options=vg_name,vg_size --separator=' ' --noheadings --units=g --nosuffix #{name}"
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
18
|
+
begin
|
19
|
+
output = Xen::Command.run cmd
|
20
|
+
result = output.collect { |line|
|
21
|
+
name, size = line.strip.split(' ')
|
22
|
+
new name, size
|
23
|
+
}
|
24
|
+
rescue # don't die if `vgs` command missing
|
25
|
+
end
|
23
26
|
name ? result[0] : result
|
24
27
|
end
|
25
28
|
|
26
29
|
def free
|
27
30
|
cmd = "vgs --options=vg_free --separator=' ' --noheadings --units=g --nosuffix #{@name}"
|
28
|
-
|
31
|
+
begin
|
32
|
+
result = Xen::Command.run cmd
|
33
|
+
rescue # don't die if `vgs` command missing
|
34
|
+
end
|
35
|
+
result
|
29
36
|
end
|
30
37
|
|
31
38
|
end
|
32
39
|
|
33
|
-
end
|
40
|
+
end
|
data/lib/xen/slice.rb
CHANGED
@@ -34,6 +34,7 @@ module Xen
|
|
34
34
|
# Set some derived options
|
35
35
|
options.reverse_merge! 'hostname' => name # Name host after this slice
|
36
36
|
options['dhcp'] = true unless options['ip']
|
37
|
+
options['accounts'].to_i == 1 ? options['accounts'] = true : options.delete('accounts')
|
37
38
|
options['swap'] ||= options['memory'].to_i * 2
|
38
39
|
if options['root_pass']
|
39
40
|
options['role'] = 'passwd'
|
@@ -91,6 +92,10 @@ module Xen
|
|
91
92
|
self.instance ? :running : :stopped
|
92
93
|
end
|
93
94
|
|
95
|
+
def first_ip
|
96
|
+
config_file.vifs.first.ip if config_file and config_file.vifs
|
97
|
+
end
|
98
|
+
|
94
99
|
def running?
|
95
100
|
self.instance ? true : false
|
96
101
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mbailey-ruby-xen
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Bailey
|
@@ -10,11 +10,12 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date:
|
13
|
+
date: 2009-02-27 00:00:00 -08:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: activesupport
|
18
|
+
type: :runtime
|
18
19
|
version_requirement:
|
19
20
|
version_requirements: !ruby/object:Gem::Requirement
|
20
21
|
requirements:
|
@@ -24,6 +25,7 @@ dependencies:
|
|
24
25
|
version:
|
25
26
|
- !ruby/object:Gem::Dependency
|
26
27
|
name: open4
|
28
|
+
type: :runtime
|
27
29
|
version_requirement:
|
28
30
|
version_requirements: !ruby/object:Gem::Requirement
|
29
31
|
requirements:
|
@@ -58,6 +60,7 @@ files:
|
|
58
60
|
- lib/xen/slice.rb
|
59
61
|
- lib/xen/xen_tools_conf.rb
|
60
62
|
- lib/templates/domu.cfg.erb
|
63
|
+
- lib/templates/exclude_from_backups
|
61
64
|
- lib/templates/xen-tools.conf.erb
|
62
65
|
has_rdoc: true
|
63
66
|
homepage: http://github.com/mbailey/ruby-xen
|