-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretrieve_files.py
110 lines (88 loc) · 3.41 KB
/
retrieve_files.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
#############################
import sys
import datetime
import os
import subprocess
#############################
class FileCollector(object):
""" """
def __init__(self, zenhome, files, output_filename = ''):
""" """
self._zenhome = zenhome
self._files = files
self._output_filename = output_filename
@staticmethod
def get_timestamp():
""" """
now = datetime.datetime.now()
timestamp = now.strftime("%Y%m%d_%H%M%S")
return timestamp
def _execute_command(self, command):
"""
Params: command to execute
Return: tuple containing the stout and stderr of the command execution
"""
#print 'Executing ....' + command
proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = proc.communicate()
return (stdout, stderr)
def _get_existing_files(self):
""" """
files_to_tar = []
for f in self._files:
file_path = self._zenhome + "/" + f
if not os.path.isfile(file_path):
print 'Skipping {0}. File not found.'.format(file_path)
else:
files_to_tar.append(f)
return files_to_tar
def _tar_files(self, files_to_tar):
""" """
tar_filename = 'files_{0}_{1}.tar'.format(self._output_filename, FileCollector.get_timestamp())
tar_file = '/tmp/{0}'.format(tar_filename)
command = "cd {0}; tar cvf {1} {2}".format(self._zenhome, tar_file, " ".join(files_to_tar))
stdout, stderr = self._execute_command(command)
value_to_return = {}
value_to_return ['tar_file'] = tar_file
value_to_return ['stdout'] = stdout
value_to_return ['stderr'] = stderr
return value_to_return
def get_files(self):
""" """
files_to_tar = self._get_existing_files()
if len(files_to_tar) > 0:
output = self._tar_files(files_to_tar)
stderr = output.get('stderr', '')
if len(stderr) > 0:
print 'Errors found executing the tar command.\n{0}\n'.format(stderr)
else:
print 'Files tar\'ed in {0}'.format(output.get('tar_file', 'ERROR'))
else:
print 'ERROR: Empty set of files to tar.'
"""
#------------------------------------------------------------------------------------
# MAIN
#------------------------------------------------------------------------------------
"""
if __name__ == '__main__':
""" """
zen_home = os.getenv('ZENHOME', '/opt/zenoss')
if os.path.isdir(zen_home):
files = ['Products/ZenHub/PBDaemon.py', \
'Products/ZenHub/services/EventService.py', \
'Products/ZenHub/zenhub.py', \
'Products/ZenEvents/zensyslog.py', \
'Products/ZenEvents/zentrap.py', \
'Products/ZenHub/interfaces.py', \
'Products/ZenHub/configure.zcml', \
]
output_filename = ''
if len(sys.argv) > 1:
output_filename = sys.argv[1]
file_collector = FileCollector(zen_home, files, output_filename)
file_collector.get_files()
else:
print '\n\nERROR: Enviroment variable ZENHOME is not defined! \n\n'
"""
#------------------------------------------------------------------------------------
"""