Class: Backup

Inherits:
QuartzPlugin show all
Defined in:
lib/plugins/backup.rb

Constant Summary collapse

@@version_major =
0
@@version_minor =
0
@@version_revision =
1

Instance Method Summary collapse

Methods inherited from QuartzPlugin

#get_version, #initialize, #payload, #run_result, #run_shell

Constructor Details

This class inherits a constructor from QuartzPlugin

Instance Method Details

#infoObject



11
12
13
# File 'lib/plugins/backup.rb', line 11

def info
	{ :uid => "64cec894b98b43ce9e6047a9284a4b7d", :name => "Backup", :version => get_version }
end

#run(message) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
# File 'lib/plugins/backup.rb', line 15

def run(message)
	
	pl = payload(message)

	job_name = pl['job_name']
	backup_script = "#Script generated by Cloud66 job '#{job_name}'\n\n" + pl['script']
	script_name = pl['script_name']

	random_file = Tempfile.new('cloud66').path + '.rb'
   	File.open(random_file, 'w') do |f|  
     		f.puts backup_script
   	end
	
	backup_root_directory = File.expand_path('~/.cloud66/backup')
	FileUtils.mkdir_p(backup_root_directory)
	
	command = "backup perform --trigger #{script_name} --root-path #{backup_root_directory} --config_file #{random_file} 2>&1"
	@log.info "Shell command '#{command}'"

	begin
		result = run_shell("#{command}")
		data = 	result[:message]
	
		#regex to identify all the "error" lines
		errorRegex = Regexp.new('(?<line>\[\d{4}\/\d{2}\/\d{2}\s\d{2}\:\d{2}\:\d{2}\]\[\\e\[31merror\\e\[0m\].*?)(\\n|$)')
		errors = data.scan(errorRegex)
		
		if errors.size > 0
			
			#log all the errors
			@log.info errors

			#get rid of the backtrace
			removeRegex = Regexp.new('(Backtrace\:|\d{2}\:\d{2}\]\[\\e\[31merror\\e\[0m\]\s*\/)')

			#subregex to get rid of nasty timestamp and [\e[31merror\e[0m]
			replaceRegex = Regexp.new('^.*\[\\e\[31merror\\e\[0m\]')
			
			errorResult = []
			errors.each do |errorArr|
				error = errorArr[0]
				errorResult << error.gsub(replaceRegex,'').chomp unless error =~ removeRegex
			end

			run_result(false, errorResult.join("\n"))
		else
			if (result[:ok])
				run_result(true, "Backup completed successfully!")
			else
				run_result(true, "An error occurred!")
			end
		end

	rescue => ex
		run_result(false, "Failed to execute backup due to #{ex}")
	ensure 
		File.delete random_file if File.exists? random_file
	end
end