vamboo 1.0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in vamboo.gemspec
4
+ gem 'thor'
5
+ gem 'ruby-libvirt'
6
+ gem 'archive-tar-minitar'
7
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 funabiki.t
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ # Vamboo
2
+
3
+ 仮想マシンをお手軽にバックアップするためのツール、`virtual machine backup util`それが`vamboo`
4
+
5
+ ## インストール
6
+
7
+ 次のコマンドからアプリケーションをインストールできます
8
+
9
+ $ gem instal vamboo
10
+
11
+ ソースコードからビルドする場合は
12
+
13
+ $ rake build
14
+ $ rake install
15
+
16
+
17
+ ## 使い方
18
+
19
+ ### 設定ファイルの記述
20
+
21
+ まず、設定ファイルの配置を行います。
22
+
23
+ vamboo init
24
+
25
+ デフォルトで`/usr/local/etc/vamboo`に`Vamboofile`が生成されます。また環境変数`VAMBOO_HOME`を定義していれば、その直下に`Vamboofile`が生成されます。
26
+
27
+ `Vamboofile`にはバックアップ対象となる仮想マシンの情報を入力します。
28
+
29
+
30
+ ```ruby
31
+ require "vamboo/domainlist"
32
+
33
+ DomainList.define do
34
+ #add("仮想マシンのドメイン名", "仮想ハードディスクのパス")
35
+ add("my_domay", "/path/to/domain/hd")
36
+ end
37
+ ```
38
+ ### バックアップ実行
39
+
40
+ `Vamboofile`に記述された仮想マシンを全てバックアップを行うには、次のコマンドから行います。
41
+
42
+ vamboo full_backup [backup destination path]
43
+
44
+ ### 仮想マシンの復元
45
+
46
+ バックアップをとったファイルをそれぞれ次ディレクトリに再設置します。
47
+
48
+ - `<domain_name>.xml` -> `/etc/libvirt/qemu`
49
+ - `<domain_name>.img` -> `/var/lib/libvirt/images`
50
+
51
+ 次のコマンドを実行します
52
+
53
+ ```sh
54
+ virsh define /etc/libvirt/qemu/<domain_name>.xml
55
+ virsh start <domain_name>
56
+ ```
57
+
58
+
59
+ この手順で、仮想マシンの復元と起動ができます。
60
+ ## TODO
61
+
62
+ - 特定の仮想マシンのバックアップ機能の実装
63
+ - 仮想マシン定義のXMLから、仮想ハードディスクのパスを特定する
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/vamboo ADDED
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+ require "vamboo"
4
+ require "vamboo/vamboofile"
5
+ require "thor"
6
+
7
+ class VambooCLI < Thor
8
+ desc "init", "Create configuration files at /usr/local/etc/vamboo or VAMBOO_HOME"
9
+ def init
10
+ vamboo_home = Vamboo.default_vamboo_home
11
+ VambooFile.createAt(vamboo_home)
12
+ end
13
+
14
+ desc "full_backup","Backup all virtual machines there are written in ${VAMBOO_HOME}/vm_glass.list"
15
+ def full_backup(dest_dir)
16
+ vamboo = VambooKernel.new(Vamboo.default_vamboo_home)
17
+ threads = vamboo.domains.map do |domain|
18
+ Thread.new do
19
+ domain.backup(dest_dir)
20
+ end
21
+ end
22
+ threads.each do |thread|
23
+ thread.join
24
+ end
25
+ end
26
+
27
+ desc "backup NAME VMHD_PATH","Back up specified virtual machine there are VM_NAME, VMHD_PATH"
28
+ def backup(name, vmhd_path)
29
+ #vamboo = Vamboo.new(Vamboo.default_vamboo_home)
30
+ #domain = vamboo.findDomain(name, vmhd_path)
31
+ #domain.backup
32
+ end
33
+
34
+ desc "verify","show virtual machines, that target of backup"
35
+ def verify
36
+ vamboo = VambooKernel.new(Vamboo.default_vamboo_home)
37
+ vamboo.domains.each do |domain|
38
+ puts domain.verify
39
+ end
40
+ end
41
+ end
42
+
43
+ VambooCLI.start
@@ -0,0 +1,6 @@
1
+ require "vamboo/domainlist"
2
+ DomainList.define do
3
+ #define your vm
4
+ #exsamble
5
+ #add("my_domay")
6
+ end
@@ -0,0 +1,120 @@
1
+ require "libvirt"
2
+ require "zlib"
3
+ require "logger"
4
+ require "archive/tar/minitar"
5
+ require "rexml/document"
6
+
7
+ class DomainList
8
+ attr_reader :list
9
+ def self.define(&block)
10
+ domainList = DomainList.new(&block)
11
+ list = domainList.list
12
+ list
13
+ end
14
+
15
+ def initialize(&block)
16
+ @list = []
17
+ instance_eval(&block)
18
+ end
19
+
20
+ def add(name)
21
+ @list.push(Domain.new(name))
22
+ end
23
+ end
24
+
25
+ class Domain
26
+ attr_reader :name, :vmhd_pathes
27
+
28
+ def initialize(name)
29
+ @name = name
30
+ conn = Libvirt::open("qemu:///system")
31
+ domain = conn.lookup_domain_by_name(@name)
32
+ xml = REXML::Document.new(domain.xml_desc)
33
+ @vmhd_pathes = xml.elements.to_a("domain/devices/disk[@type='file']").map do |dev|
34
+ dev.elements['source'].attributes['file']
35
+ end
36
+ conn.close
37
+ end
38
+
39
+ def backup(target_path)
40
+ log = Logger.new("#{target_path}/#{@name}.log")
41
+ log.formatter = proc do |severity, datetime, progname, msg|
42
+ "#{@name}:#{datetime}: #{msg}\n"
43
+ end
44
+
45
+
46
+ conn = Libvirt::open("qemu:///system")
47
+ domain = conn.lookup_domain_by_name(@name)
48
+ tmp_path = "#{Vamboo.default_vamboo_home}/#{@name}"
49
+
50
+ log.info("Start backup")
51
+ active = domain.active?
52
+ if active
53
+ log.info("Shutdown")
54
+ domain.shutdown
55
+ sleep(10) while domain.active?
56
+ end
57
+
58
+ log.info("Dump xml")
59
+ FileUtils.mkdir_p(tmp_path)
60
+ File.open("#{tmp_path}/#{@name}.xml", "w") do |file|
61
+ xml = domain.xml_desc
62
+ file.write(xml)
63
+ end
64
+
65
+ log.info("Compress vmhd")
66
+ @vmhd_pathes.each do |vmhd_path|
67
+ File.open("#{vmhd_path}", "rb") do |vmhd|
68
+ name = File.basename(vmhd)
69
+ Zlib::GzipWriter.open("#{tmp_path}/#{name}.gz", Zlib::BEST_COMPRESSION) do |gz|
70
+ offset = 0
71
+ length = 1024
72
+ while offset < vmhd.size
73
+ gz.print(IO.binread(vmhd.path, length, offset))
74
+ offset += length
75
+ end
76
+ end
77
+ end
78
+ end
79
+
80
+
81
+ if active
82
+ log.info("Start")
83
+ domain.create
84
+ sleep(10) until domain.active?
85
+ end
86
+
87
+ log.info("Archive")
88
+ Zlib::GzipWriter.open("#{target_path}/#{@name}.tar.gz") do |archive|
89
+ out = Archive::Tar::Minitar::Output.new(archive)
90
+ Find.find("#{tmp_path}") do |file|
91
+ Archive::Tar::Minitar::pack_file(file, out)
92
+ end
93
+ out.close
94
+ end
95
+ FileUtils.rm_rf("#{tmp_path}")
96
+
97
+ log.info("Complete backup")
98
+ end
99
+
100
+ def isDefined?
101
+ retval = false
102
+ begin
103
+ Libvirt::open("qemu:///system").lookup_domain_by_name(@name)
104
+ retval = true
105
+ rescue => e
106
+ retval = false
107
+ end
108
+ retval
109
+ end
110
+
111
+ def vmhdIsExist?
112
+ @vmhd_pathes.all? do |vmhd_path|
113
+ File.exist?(vmhd_path)
114
+ end
115
+ end
116
+
117
+ def verify
118
+ "#{@name} #{@vmhd_pathes}"
119
+ end
120
+ end
@@ -0,0 +1,29 @@
1
+ class VambooFile
2
+ Vamboofile = "Vamboofile"
3
+ attr_reader :vamboo_home
4
+
5
+ def self.createAt(vamboo_home)
6
+ FileUtils.mkdir_p(vamboo_home)
7
+ if File.exist?("#{vamboo_home}/#{Vamboofile}")
8
+ return
9
+ end
10
+ FileUtils.copy("#{File.dirname(__FILE__)}/#{Vamboofile}.org", "#{vamboo_home}/#{Vamboofile}")
11
+ end
12
+
13
+ def initialize(vamboo_home)
14
+ @vamboo_home = vamboo_home
15
+ end
16
+
17
+ def loadDomains
18
+ domains = eval(File.read("#{@vamboo_home}/#{Vamboofile}"))
19
+ domains.each do |domain|
20
+ unless domain.isDefined?
21
+ raise "#{domain.name} is not defined!!"
22
+ end
23
+ unless domain.vmhdIsExist?
24
+ raise "#{domain.name}'s vmhd is not exist!!"
25
+ end
26
+ end
27
+ domains
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module Vamboo
2
+ VERSION = "1.0.3.1"
3
+ end
data/lib/vamboo.rb ADDED
@@ -0,0 +1,28 @@
1
+ require "vamboo/version"
2
+
3
+ module Vamboo
4
+ attr_reader :vamboo_home
5
+
6
+ def self.default_vamboo_home
7
+ vamboo_home = ENV.fetch('VAMBOO_HOME', '/usr/local/etc/vamboo')
8
+ vamboo_home
9
+ end
10
+
11
+ def initialize(vamboo_home)
12
+ @vamboo_home = vamboo_home
13
+ end
14
+
15
+ def domains
16
+ vambooFile = VambooFile.new(vamboo_home)
17
+ domains = vambooFile.loadDomains
18
+ domains
19
+ end
20
+
21
+ def findDomain(name, vmhd_path)
22
+ end
23
+
24
+ end
25
+
26
+ class VambooKernel
27
+ include Vamboo
28
+ end
data/vamboo.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'vamboo/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "vamboo"
8
+ spec.version = Vamboo::VERSION
9
+ spec.authors = ["funabiki.t"]
10
+ spec.email = ["funabiki.t@isoroot.jp"]
11
+ spec.description = "virtual machine backup util"
12
+ spec.summary = ""
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "libvirt"
24
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vamboo
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.3.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - funabiki.t
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-01-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: libvirt
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: virtual machine backup util
63
+ email:
64
+ - funabiki.t@isoroot.jp
65
+ executables:
66
+ - vamboo
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - .gitignore
71
+ - Gemfile
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - bin/vamboo
76
+ - lib/vamboo.rb
77
+ - lib/vamboo/Vamboofile.org
78
+ - lib/vamboo/domainlist.rb
79
+ - lib/vamboo/vamboofile.rb
80
+ - lib/vamboo/version.rb
81
+ - vamboo.gemspec
82
+ homepage: ''
83
+ licenses:
84
+ - MIT
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 1.8.23
104
+ signing_key:
105
+ specification_version: 3
106
+ summary: ''
107
+ test_files: []