wtnabe-vbox_config_admin 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog ADDED
@@ -0,0 +1,4 @@
1
+ == 0.0.1 / 2009-08-11
2
+
3
+ * initial release
4
+
data/README ADDED
@@ -0,0 +1,60 @@
1
+
2
+ = vbox_config_admin
3
+
4
+ http://github.com/wtnabe/vbox_config_admin/tree/master
5
+
6
+ == Description
7
+
8
+ VirtualBox config xml file manager written in Ruby and Rake
9
+
10
+ == Requirements
11
+
12
+ * Ruby ( 1.8 or later )
13
+ * Rake
14
+
15
+ == Installation
16
+
17
+ === Gem Installation
18
+
19
+ gem install vbox_config_admin
20
+
21
+ If you use windows, type like below:
22
+
23
+ C:\Ruby\bin> copy ..\lib\ruby\gems\1.8\gems\vbox_config_admin-X.X.X\bin\vbox_config_admin.bat .
24
+
25
+ Remember the last period(.) !
26
+
27
+ Note: I've tested it on MacOSX and Windows host. But Linux host hasn't been tried yet.
28
+
29
+ === Clone
30
+
31
+ git clone git://github.com/wtnabe/vbox_config_admin.git
32
+
33
+ == Features/Problems
34
+
35
+ * create backup for both whole and each machines' config xml
36
+ * check and delete backup files
37
+ * open real config and backup file
38
+
39
+ Problem: On Windows host, you will not open XXXX.xml config file. But you can open backup files. I don't know why.
40
+
41
+ == Synopsis
42
+
43
+ Just type as:
44
+
45
+ vbox_config_admin
46
+
47
+ You can see available tasks, if you've already installed VirtualBox.
48
+
49
+ == Copyright
50
+
51
+ Author:: wtnabe <wtnabe@gmail.com>
52
+ Copyright:: Copyright (c) 2009 wtnabe
53
+ License:: Two-clause BSD
54
+
55
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
56
+
57
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
58
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
59
+
60
+ THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/Rakefile ADDED
@@ -0,0 +1,146 @@
1
+ # -*- mode: ruby -*-
2
+ require 'rubygems'
3
+ require 'rake'
4
+ require 'rake/clean'
5
+ require 'rake/testtask'
6
+ require 'rake/packagetask'
7
+ require 'rake/gempackagetask'
8
+ require 'rake/rdoctask'
9
+ require 'rake/contrib/rubyforgepublisher'
10
+ require 'rake/contrib/sshpublisher'
11
+ require 'fileutils'
12
+ require 'lib/vbox_config_admin'
13
+ include FileUtils
14
+
15
+ NAME = "vbox_config_admin"
16
+ AUTHOR = "wtnabe"
17
+ EMAIL = "wtnabe@gmail.com"
18
+ DESCRIPTION = "Create and delete backups for VirtualBox configuration, and open config & backups"
19
+ RUBYFORGE_PROJECT = ""
20
+ HOMEPATH = ""
21
+ BIN_FILES = %w( vbox_config_admin )
22
+
23
+ VERS = VboxConfigAdmin::VERSION
24
+ REV = File.read(".svn/entries")[/committed-rev="(d+)"/, 1] rescue nil
25
+ CLEAN.include ['**/.*.sw?', '*.gem', '.config']
26
+ RDOC_OPTS = [
27
+ '--title', "#{NAME} documentation",
28
+ "--charset", "utf-8",
29
+ "--opname", "index.html",
30
+ "--line-numbers",
31
+ "--main", "README",
32
+ "--inline-source",
33
+ ]
34
+
35
+ task :default => [:test]
36
+ task :package => [:clean]
37
+
38
+ Rake::TestTask.new("test") do |t|
39
+ t.libs << "test"
40
+ t.pattern = "test/**/*_test.rb"
41
+ t.verbose = true
42
+ end
43
+
44
+ spec = Gem::Specification.new do |s|
45
+ s.name = NAME
46
+ s.version = VERS
47
+ s.platform = Gem::Platform::RUBY
48
+ s.has_rdoc = true
49
+ s.extra_rdoc_files = ["README", "ChangeLog"]
50
+ s.rdoc_options += RDOC_OPTS + ['--exclude', '^(examples|extras)/']
51
+ s.summary = DESCRIPTION
52
+ s.description = DESCRIPTION
53
+ s.author = AUTHOR
54
+ s.email = EMAIL
55
+ s.homepage = HOMEPATH
56
+ s.executables = BIN_FILES
57
+ s.rubyforge_project = RUBYFORGE_PROJECT
58
+ s.bindir = "bin"
59
+ s.require_path = "lib"
60
+ #s.autorequire = ""
61
+ s.test_files = Dir["test/*_test.rb"]
62
+
63
+ s.add_dependency('rake', '>= 0')
64
+ s.requirements << 'rake'
65
+ #s.required_ruby_version = '>= 1.8.2'
66
+
67
+ s.files = %w(README ChangeLog Rakefile) +
68
+ Dir.glob("{bin,doc,test,lib,templates,generator,extras,website,script}/**/*") +
69
+ Dir.glob("ext/**/*.{h,c,rb}") +
70
+ Dir.glob("examples/**/*.rb") +
71
+ Dir.glob("tools/*.rb") +
72
+ Dir.glob("rails/*.rb")
73
+
74
+ s.extensions = FileList["ext/**/extconf.rb"].to_a
75
+ end
76
+
77
+ Rake::GemPackageTask.new(spec) do |p|
78
+ p.need_tar = true
79
+ p.gem_spec = spec
80
+ end
81
+
82
+ task :install do
83
+ name = "#{NAME}-#{VERS}.gem"
84
+ sh %{rake package}
85
+ sh %{sudo gem install pkg/#{name}}
86
+ end
87
+
88
+ task :uninstall => [:clean] do
89
+ sh %{sudo gem uninstall #{NAME}}
90
+ end
91
+
92
+
93
+ Rake::RDocTask.new do |rdoc|
94
+ rdoc.rdoc_dir = 'html'
95
+ rdoc.options += RDOC_OPTS
96
+ rdoc.template = "resh"
97
+ #rdoc.template = "#{ENV['template']}.rb" if ENV['template']
98
+ if ENV['DOC_FILES']
99
+ rdoc.rdoc_files.include(ENV['DOC_FILES'].split(/,\s*/))
100
+ else
101
+ rdoc.rdoc_files.include('README', 'ChangeLog')
102
+ rdoc.rdoc_files.include('lib/**/*.rb')
103
+ rdoc.rdoc_files.include('ext/**/*.c')
104
+ end
105
+ end
106
+
107
+ desc "Publish to RubyForge"
108
+ task :rubyforge => [:rdoc, :package] do
109
+ require 'rubyforge'
110
+ Rake::RubyForgePublisher.new(RUBYFORGE_PROJECT, 'wtnabe').upload
111
+ end
112
+
113
+ desc 'Package and upload the release to rubyforge.'
114
+ task :release => [:clean, :package] do |t|
115
+ v = ENV["VERSION"] or abort "Must supply VERSION=x.y.z"
116
+ abort "Versions don't match #{v} vs #{VERS}" unless v == VERS
117
+ pkg = "pkg/#{NAME}-#{VERS}"
118
+
119
+ require 'rubyforge'
120
+ rf = RubyForge.new.configure
121
+ puts "Logging in"
122
+ rf.login
123
+
124
+ c = rf.userconfig
125
+ # c["release_notes"] = description if description
126
+ # c["release_changes"] = changes if changes
127
+ c["preformatted"] = true
128
+
129
+ files = [
130
+ "#{pkg}.tgz",
131
+ "#{pkg}.gem"
132
+ ].compact
133
+
134
+ puts "Releasing #{NAME} v. #{VERS}"
135
+ rf.add_release RUBYFORGE_PROJECT, NAME, VERS, *files
136
+ end
137
+
138
+ desc 'Show information about the gem.'
139
+ task :debug_gem do
140
+ puts spec.to_ruby
141
+ end
142
+
143
+ desc 'Update gem spec'
144
+ task :gemspec do
145
+ open("#{NAME}.gemspec", 'w').write spec.to_ruby
146
+ end
@@ -0,0 +1,90 @@
1
+ #! /usr/bin/env rake -f
2
+ # -*- mode: ruby; coding: utf-8 -*-
3
+
4
+ unless ( ENV['PATH'].split( File::PATH_SEPARATOR ).include?( File.dirname( __FILE__ ) ) )
5
+ $LOAD_PATH << File.expand_path("../lib", File.dirname(__FILE__))
6
+ end
7
+ require 'vbox_config_admin'
8
+
9
+ #
10
+ # global tasks
11
+ #
12
+ namespace :vbox do
13
+ desc "show VirtualBox version number"
14
+ task :version do
15
+ puts VboxConfigAdmin.new.version
16
+ end
17
+
18
+ desc "backup all config files"
19
+ task :backup_configs do
20
+ v = VboxConfigAdmin.new
21
+ v.machines.each { |m|
22
+ v.create_backup( m )
23
+ puts "Backup completed #{m}"
24
+ }
25
+ v.create_backup
26
+ puts "Backup completed #{File.basename( v.config_file )}"
27
+ end
28
+
29
+ desc "clear all backup files"
30
+ task :clear_backups do
31
+ v = VboxConfigAdmin.new
32
+ v.machines.each { |m|
33
+ v.clear_backups( m )
34
+ puts "Backup files for #{m} are deleted."
35
+ }
36
+ v.clear_backups
37
+ puts "Backup files for #{File.basename( v.config_file )}"
38
+ end
39
+ end
40
+
41
+ #
42
+ # tasks for each machines
43
+ #
44
+ VboxConfigAdmin.new.machines.each { |m|
45
+ namespace m do
46
+ desc "getextradata from #{m}"
47
+ task :getextradata do
48
+ puts VboxConfigAdmin.new.getextradata( m )
49
+ end
50
+
51
+ desc "backup files of #{m}"
52
+ task :backup_files do
53
+ puts VboxConfigAdmin.new.backup_files( m )
54
+ end
55
+
56
+ desc "create backup for #{m}"
57
+ task :create_backup do
58
+ VboxConfigAdmin.new.create_backup( m )
59
+ puts "backup created."
60
+ end
61
+
62
+ desc "clear backup files for #{m}"
63
+ task :clear_backups do
64
+ VboxConfigAdmin.new.clear_backups( m )
65
+ puts "Backup files are deleted."
66
+ end
67
+
68
+ desc "open config file for #{m}"
69
+ task :open_config do
70
+ VboxConfigAdmin.new.open_config( m )
71
+ end
72
+
73
+ desc "open latest backup for #{m}"
74
+ task :open_latest_backup do
75
+ VboxConfigAdmin.new.open_latest_backup( m )
76
+ end
77
+ end
78
+ }
79
+
80
+ #
81
+ # default task
82
+ #
83
+ task :default do
84
+ app = Rake.application
85
+ app.instance_eval {
86
+ @name = File.basename( __FILE__ )
87
+ }
88
+ app.options.show_task_pattern = Regexp.new('')
89
+ app.display_tasks_and_comments
90
+ end
@@ -0,0 +1,6 @@
1
+ @ECHO OFF
2
+ IF NOT "%~f0" == "~f0" GOTO :WinNT
3
+ @"C:\Ruby\bin\rake.bat" "vbox_config_admin" %1 %2 %3 %4 %5 %6 %7 %8 %9
4
+ GOTO :EOF
5
+ :WinNT
6
+ @"C:\Ruby\bin\rake.bat" -f "%~d0%~p0%~n0" %*
@@ -0,0 +1,160 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'fileutils'
4
+
5
+ #
6
+ # require VBoxManage executable binary
7
+ #
8
+
9
+ class VboxConfigAdmin
10
+ VERSION = '0.0.2'
11
+
12
+ def version
13
+ cmdline = "#{manager} --version"
14
+ `#{cmdline}`.strip
15
+ end
16
+
17
+ def machine_base
18
+ return File.join( vm_root, 'Machines' )
19
+ end
20
+
21
+ def vm_root
22
+ home = ENV['HOME']
23
+ ['Library/VirtualBox', '.VirtualBox'].each { |dir|
24
+ d = File.join( home, dir )
25
+ if ( File.exist?( d ) and File.directory?( d ) )
26
+ return d
27
+ end
28
+ }
29
+ end
30
+
31
+ def machines
32
+ cmdline = "#{manager} list vms"
33
+ `#{cmdline}`.split( /(\r\n|[\r\n])/ ).map { |e|
34
+ e.strip.match( /\A"(.+)" \{[0-9a-z-]+\}\z/ )
35
+ $1
36
+ }.compact.sort
37
+ end
38
+
39
+ #
40
+ # VBoxManage binary
41
+ #
42
+ def manager
43
+ @manager
44
+
45
+ if ( !@manager )
46
+ if ( RUBY_PLATFORM =~ /win/ and RUBY_PLATFORM !~ /darwin/ )
47
+ @manager = which( 'VBoxManage.exe',
48
+ 'C:\Program Files\Sun\xVM VirtualBox' )
49
+ else
50
+ @manager = 'VBoxManage'
51
+ end
52
+ end
53
+
54
+ return @manager
55
+ end
56
+
57
+ def config_file( machine = nil )
58
+ if ( machine )
59
+ file = File.join( machine_base, machine, "#{machine}.xml" )
60
+ else
61
+ file = File.join( vm_root, 'VirtualBox.xml' )
62
+ end
63
+
64
+ return ( File.exist?( file ) and File.file?( file ) ) ? file : nil
65
+ end
66
+
67
+ def create_backup( machine = nil )
68
+ return FileUtils.cp( config_file( machine ), backup_file( machine ) )
69
+ end
70
+
71
+ def backup_file( machine = nil )
72
+ return config_file( machine ) +
73
+ '.bak' + Time.now.strftime( "%Y%m%d%H%M%S" ) + '.txt'
74
+ end
75
+
76
+ def backup_files( machine = nil )
77
+ path = File.dirname( config_file( machine ) )
78
+ return Dir.chdir( path ) {
79
+ Dir.glob( File.basename( config_file( machine ) ) +
80
+ '.bak[0-9]*.txt' ).map { |f|
81
+ File.join( path, f )
82
+ }
83
+ }
84
+ end
85
+
86
+ def clear_backups( machine = nil )
87
+ if ( machine )
88
+ backup_files( machine ).map { |e|
89
+ FileUtils.rm( e )
90
+ }
91
+ else
92
+ backup_files.map { |e|
93
+ FileUtils.rm( File.join( vm_root, e ) )
94
+ }
95
+ end
96
+ end
97
+
98
+ def open( file )
99
+ cmd = open_cmd
100
+ if ( cmd )
101
+ system( cmd, file )
102
+ else
103
+ # for Windows. Cannot open #{Machine}.xml, why ?
104
+ system( 'cmd.exe /c "' + file + '"' )
105
+ end
106
+ end
107
+
108
+ def open_config( machine )
109
+ open( config_file( machine ) )
110
+ end
111
+
112
+ def open_latest_backup( machine )
113
+ file = backup_files( machine ).sort.last
114
+ if ( file )
115
+ open( file )
116
+ else
117
+ puts "No backup."
118
+ end
119
+ end
120
+
121
+ def getextradata( machine )
122
+ call4vm( machine, 'getextradata', 'enumerate' )
123
+ end
124
+
125
+ private
126
+ def call4vm( machine, subcommand, *params )
127
+ if ( machines.include?( machine ) )
128
+ machine = '"' + machine + '"' if ( machine.include?(' ') )
129
+ cmdline = "#{manager} #{subcommand} #{machine} #{params.join(' ')}"
130
+ puts cmdline
131
+ `#{cmdline}`
132
+ end
133
+ end
134
+
135
+ def open_cmd
136
+ cmd = nil
137
+
138
+ if ( ENV['EDITOR'] )
139
+ cmd = ENV['EDITOR']
140
+ else
141
+ cmd = %w( gnome-open open start.com ).map { |cmd|
142
+ ( which( cmd ) ) ? cmd : nil
143
+ }.compact.first
144
+ end
145
+
146
+ return cmd
147
+ end
148
+ end
149
+
150
+ def which( cmd, *additional )
151
+ paths = ENV['PATH'].split( File::PATH_SEPARATOR )
152
+ if ( additional )
153
+ paths += additional
154
+ end
155
+
156
+ return paths.map { |path|
157
+ file = File.join( path, cmd )
158
+ ( File.exist?( file ) ) ? file : nil
159
+ }.compact.first
160
+ end
@@ -0,0 +1,3 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/vbox_config_admin'
3
+
@@ -0,0 +1,83 @@
1
+ #! /usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+
4
+ require 'test/unit'
5
+ require File.dirname( __FILE__ ) + '/../lib/vbox_config_admin'
6
+
7
+ class Test_VboxConfigAdmin < Test::Unit::TestCase
8
+ def setup
9
+ @obj = VboxConfigAdmin.new
10
+ end
11
+
12
+ def test_version
13
+ assert( @obj.version.size > 0 )
14
+ end
15
+
16
+ def test_machine_base
17
+ assert( @obj.machine_base.size > 0 )
18
+ end
19
+
20
+ def test_vm_root
21
+ assert( @obj.vm_root.class == String )
22
+ end
23
+
24
+ def test_machines
25
+ assert( @obj.machines.class == Array )
26
+ end
27
+
28
+ def test_manager
29
+ assert( @obj.manager.size > 0 )
30
+ end
31
+
32
+ def test_config_file
33
+ @obj.machines.each { |m|
34
+ assert( @obj.config_file( m ).size > 0 )
35
+ }
36
+ assert( @obj.config_file.size > 0 )
37
+ end
38
+
39
+ def test_create_backup
40
+ @obj.machines.each { |m|
41
+ assert( @obj.backup_files( m ) == [] )
42
+ @obj.create_backup( m )
43
+ assert( @obj.backup_files( m ).size > 0 )
44
+ @obj.clear_backups( m )
45
+ assert( @obj.backup_files( m ) == [] )
46
+ }
47
+ end
48
+
49
+ def test_backup_file
50
+ @obj.machines.each { |m|
51
+ assert( @obj.backup_file( m ).size > 0 )
52
+ }
53
+ assert( @obj.backup_file.size > 0 )
54
+ end
55
+
56
+ def test_backup_files
57
+ # already tested in test_create_backup
58
+ end
59
+
60
+ def test_clear_backups
61
+ # already tested in test_clear_backups
62
+ end
63
+
64
+ def test_open
65
+ # don't know how to test
66
+ end
67
+
68
+ def test_open_config
69
+ # don't know how to test
70
+ end
71
+
72
+ def test_open_latest_backup
73
+ # don't know how to test
74
+ end
75
+
76
+ def
77
+
78
+ def test_getextradata
79
+ @obj.machines.each { |m|
80
+ assert( @obj.getextradata( m ).size > 0 )
81
+ }
82
+ end
83
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wtnabe-vbox_config_admin
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - wtnabe
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-08-12 00:00:00 -07:00
13
+ default_executable: vbox_config_admin
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rake
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: Create and delete backups for VirtualBox configuration, and open config & backups
26
+ email: wtnabe@gmail.com
27
+ executables:
28
+ - vbox_config_admin
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README
33
+ - ChangeLog
34
+ files:
35
+ - README
36
+ - ChangeLog
37
+ - Rakefile
38
+ - bin/vbox_config_admin
39
+ - bin/vbox_config_admin.bat
40
+ - test/test_helper.rb
41
+ - test/vbox_config_admin_test.rb
42
+ - lib/vbox_config_admin.rb
43
+ has_rdoc: false
44
+ homepage: ""
45
+ licenses:
46
+ post_install_message:
47
+ rdoc_options:
48
+ - --title
49
+ - vbox_config_admin documentation
50
+ - --charset
51
+ - utf-8
52
+ - --opname
53
+ - index.html
54
+ - --line-numbers
55
+ - --main
56
+ - README
57
+ - --inline-source
58
+ - --exclude
59
+ - ^(examples|extras)/
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ version:
74
+ requirements:
75
+ - rake
76
+ rubyforge_project: ""
77
+ rubygems_version: 1.3.5
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: Create and delete backups for VirtualBox configuration, and open config & backups
81
+ test_files:
82
+ - test/vbox_config_admin_test.rb