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 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 = Xen::Slice.find(:example)
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
- ruby-xen is licenced under the GPL. This means that you can use it in commercial
45
- or open source applications. More details found here:
46
- http://www.gnu.org/licenses/gpl.html
50
+ (The MIT License)
47
51
 
48
- ruby-xen
49
- Copyright (C) 2008 Mike Bailey and Nick Marfleet
52
+ Copyright (c) 2008-2009 Mike Bailey
50
53
 
51
- This program is free software; you can redistribute it and/or
52
- modify it under the terms of the GNU General Public License
53
- as published by the Free Software Foundation; either version 2
54
- of the License, or (at your option) any later version.
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
- This program is distributed in the hope that it will be useful,
57
- but WITHOUT ANY WARRANTY; without even the implied warranty of
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
- You should have received a copy of the GNU General Public License
62
- along with this program; if not, write to the Free Software
63
- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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
@@ -20,7 +20,7 @@ module Xen
20
20
  CONFIG_FILE_EXTENSION = '.cfg'
21
21
 
22
22
  # Directory for backups of system images
23
- BACKUP_DIR='/var/xen_images'
23
+ BACKUP_DIR='/var/backups/xen'
24
24
 
25
25
  # File extension for backups
26
26
  BACKUP_FILE_EXT = '.tar'
@@ -0,0 +1,2 @@
1
+ ./proc
2
+ ./etc/udev/rules.d/70-persistent-net.rules
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}.#{backup_file_ext}"
16
+ archive_name="#{name}-#{version}#{backup_file_ext}"
17
17
 
18
18
  slice = Xen::Slice.find(name) # XXX test for failure
19
- slice.stop
20
- sleep 10
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
- # Creating archive at $ARCHIVE_DIR/$ARCHIVE_NAME ...
26
- `tar --create --exclude=/proc --exclude=/etc/udev/rules.d/70-persistent-net.rules --directory #{temp_mount} --file #{backup_dir}/#{archive_name} .`
27
- # XXX test for failure
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}.#{backup_file_ext}"
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 }
@@ -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)}" if !instance_variable_get('@'+key).nil?
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
- # XXX Not needed?
150
- # def path
151
- # "/dev/#{volume_group}/#{name}"
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
- output = Xen::Command.run cmd
19
- result = output.collect { |line|
20
- name, size = line.strip.split(' ')
21
- new name, size
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
- Xen::Command.run cmd
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.0.3
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: 2008-09-21 00:00:00 -07:00
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