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]
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.info errors
removeRegex = Regexp.new('(Backtrace\:|\d{2}\:\d{2}\]\[\\e\[31merror\\e\[0m\]\s*\/)')
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
|