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, rotation) ⇒ Domain

Returns a new instance of Domain.



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

def initialize(name, rotation)
	@name = name
	@rotation = rotation
	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.



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

def name
  @name
end

#rotationObject (readonly)

Returns the value of attribute rotation.



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

def rotation
  @rotation
end

#vmhd_pathesObject (readonly)

Returns the value of attribute vmhd_pathes.



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

def vmhd_pathes
  @vmhd_pathes
end

Instance Method Details

#backup(target_path) ⇒ Object



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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/vamboo/domainlist.rb', line 41

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("Copy vmhd")
	threads = @vmhd_pathes.map do |vmhd_path|
		Thread.new do
			name = File.basename(vmhd_path)
			FileUtils.cp("#{vmhd_path}", "#{tmp_path}/#{name}")
		end
	end

	threads.each do |t|
		t.join
	end

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

	log.info("Compress vmhd")
	@vmhd_pathes.each do |vmhd_path|
		name = File.basename(vmhd_path)
		tmp = "#{tmp_path}/#{name}"
		File.open(tmp, "rb") do |vmhd|
			Zlib::GzipWriter.open("#{tmp}.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
		FileUtils.rm(tmp, {:force => true})
	end

	log.info("Archive")
	file_name = "#{@name}.#{Date.today.strftime("%Y%m%d")}.tar.gz"
	Zlib::GzipWriter.open("#{target_path}/#{file_name}") 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}")
	
	if @rotation > 0 
		old = Date.today - @rotation
		old_file = "#{@name}.#{old}.tar.gz"
		log.info("Remove old file #{target_path}/#{old}")
		FileUtils.rm_rf("#{target_path}/#{old}")
	end
	log.info("Complete backup")
end

#isDefined?Boolean

Returns:

  • (Boolean)


122
123
124
125
126
127
128
129
130
131
# File 'lib/vamboo/domainlist.rb', line 122

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

#verifyObject



139
140
141
# File 'lib/vamboo/domainlist.rb', line 139

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

#vmhdIsExist?Boolean

Returns:

  • (Boolean)


133
134
135
136
137
# File 'lib/vamboo/domainlist.rb', line 133

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