Class: Domain

Inherits:
Object
  • Object
show all
Defined in:
lib/vamboo/domainlist.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Domain

Returns a new instance of Domain.



28
29
30
31
32
33
34
35
36
37
# File 'lib/vamboo/domainlist.rb', line 28

def initialize(name)
	@name = name
	conn = Libvirt::open("qemu:///system")
	domain = conn.lookup_domain_by_name(@name)
	xml = REXML::Document.new(domain.xml_desc)
	@vmhd_pathes = xml.elements.to_a("domain/devices/disk[@type='file']").map do |dev|
		dev.elements['source'].attributes['file']
	end
	conn.close
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



26
27
28
# File 'lib/vamboo/domainlist.rb', line 26

def name
  @name
end

#vmhd_pathesObject (readonly)

Returns the value of attribute vmhd_pathes.



26
27
28
# File 'lib/vamboo/domainlist.rb', line 26

def vmhd_pathes
  @vmhd_pathes
end

Instance Method Details

#backup(target_path) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/vamboo/domainlist.rb', line 39

def backup(target_path)
	log = Logger.new("#{target_path}/#{@name}.log")
	log.formatter = proc do |severity, datetime, progname, msg|
  				"#{@name}:#{datetime}: #{msg}\n"
	end


	conn = Libvirt::open("qemu:///system")
	domain = conn.lookup_domain_by_name(@name)
	tmp_path = "#{Vamboo.default_vamboo_home}/#{@name}"
	
	log.info("Start backup")
	active = domain.active?
	if active
		log.info("Shutdown")
		domain.shutdown
		sleep(10) while domain.active?
	end

	log.info("Dump xml")
	FileUtils.mkdir_p(tmp_path)
	File.open("#{tmp_path}/#{@name}.xml", "w") do |file|
		xml = domain.xml_desc
		file.write(xml)
	end

	log.info("Compress vmhd")
	@vmhd_pathes.each do |vmhd_path|
		File.open("#{vmhd_path}", "rb") do |vmhd|
			name = File.basename(vmhd)
			Zlib::GzipWriter.open("#{tmp_path}/#{name}.gz", Zlib::BEST_COMPRESSION) do |gz|
				offset = 0
				length = 1024
				while offset < vmhd.size
					gz.print(IO.binread(vmhd.path, length, offset))
					offset += length
				end
			end
		end
	end


	if active
		log.info("Start")
		domain.create
		sleep(10) until domain.active?
	end

	log.info("Archive")
	Zlib::GzipWriter.open("#{target_path}/#{@name}.tar.gz") do |archive|
		out = Archive::Tar::Minitar::Output.new(archive)
		Find.find("#{tmp_path}") do |file|
			Archive::Tar::Minitar::pack_file(file, out)
		end
		out.close
	end	
	FileUtils.rm_rf("#{tmp_path}")

	log.info("Complete backup")
end

#isDefined?Boolean

Returns:

  • (Boolean)


100
101
102
103
104
105
106
107
108
109
# File 'lib/vamboo/domainlist.rb', line 100

def isDefined?
	retval = false
	begin
		Libvirt::open("qemu:///system").lookup_domain_by_name(@name)
		retval = true
	rescue  => e
		retval = false
	end
	retval
end

#verifyObject



117
118
119
# File 'lib/vamboo/domainlist.rb', line 117

def verify
	"#{@name}	#{@vmhd_pathes}"
end

#vmhdIsExist?Boolean

Returns:

  • (Boolean)


111
112
113
114
115
# File 'lib/vamboo/domainlist.rb', line 111

def vmhdIsExist?
	@vmhd_pathes.all? do |vmhd_path|
		File.exist?(vmhd_path)
	end
end