-
Notifications
You must be signed in to change notification settings - Fork 6
/
vmcatcher_image
executable file
·260 lines (245 loc) · 10.2 KB
/
vmcatcher_image
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/env python
import sys
if sys.version_info < (2, 4):
print "Your python interpreter is too old. Please consider upgrading."
sys.exit(1)
if sys.version_info < (2, 5):
import site
import os.path
from distutils.sysconfig import get_python_lib
found = False
module_dir = get_python_lib()
for name in os.listdir(module_dir):
lowername = name.lower()
if lowername[0:10] == 'sqlalchemy' and 'egg' in lowername:
sqlalchemy_dir = os.path.join(module_dir, name)
if os.path.isdir(sqlalchemy_dir):
site.addsitedir(sqlalchemy_dir)
found = True
break
if not found:
print "Could not find SQLAlchemy installed."
sys.exit(1)
import vmcatcher.databaseDefinition as model
import os
import logging
import optparse
from smimeX509validation import TrustStore, LoadDirChainOfTrust,smimeX509validation, smimeX509ValidationError
from vmcatcher.__version__ import version
import vmcatcher
import urllib2
import urllib
import hashlib
import datetime
from hepixvmitrust.vmitrustlib import VMimageListDecoder as VMimageListDecoder
from hepixvmitrust.vmitrustlib import time_format_definition as time_format_definition
from vmcatcher.vmcatcher_image.controler import db_controler
from vmcatcher.listutils import pairsNnot
def main():
"""Runs program and handles command line options"""
p = optparse.OptionParser(version = "%prog " + version)
p.add_option('-l', '--list', action ='store_true',help='list all images.')
p.add_option('-d', '--database', action ='store', help='Database Initiation string')
p.add_option('-c', '--cert-dir', action ='store',help='Certificate directory.', metavar='INPUTDIR',
default='/etc/grid-security/certificates/')
p.add_option('-a', '--add', action ='store_true',help='Add image to Cache.')
p.add_option('-r', '--remove', action ='store_true',help='Remove image from Cache.')
p.add_option('-s', '--sha512', action ='append',help='Select images by sha512.', metavar='SHA512')
p.add_option('-u', '--uuid', action ='append',help='Select images by uuid.', metavar='UUID')
p.add_option('-i', '--info', action ='store_true',help='Info on selected images')
p.add_option('-f', '--format', action ='store',help='Sets the output format')
p.add_option('-o', '--output', action ='append',help='Export File.', metavar='OUTPUTFILE')
p.add_option('-L', '--logfile', action ='store',help='Logfile configuration file.', metavar='LOGFILE')
p.add_option('-v', '--verbose', action ='count',help='Change global log level, increasing log output.')
p.add_option('-q', '--quiet', action ='count',help='Change global log level, decreasing log output.')
p.add_option('--log-config', action ='store',help='Logfile configuration file, (overrides command line).', metavar='LOG_CONFIG')
p.add_option('--log-sql-info', action ='store_true',help='Echo all SQL commands.')
outputformats = set(['SMIME','message','lines','json'])
output_format_selected = 'lines'
options, arguments = p.parse_args()
anchor_needed = False
format_needed = False
anchor = None
actions = set([])
images_selected = []
messages_path = []
subscription_url_list = []
outputfiles = []
input_format_selected = set([])
actionsrequiring_selections = set(['output','delete','info','add','remove'])
inputformats = set(['uuid','sha512'])
input_format_selected = set([])
databaseConnectionString = None
certDir = None
logFile = None
debugSqlEcho = False
if 'VMCATCHER_RDBMS' in os.environ:
databaseConnectionString = os.environ['VMCATCHER_RDBMS']
if 'VMCATCHER_LOG_CONF' in os.environ:
logFile = os.environ['VMCATCHER_LOG_CONF']
if 'VMCATCHER_DIR_CERT' in os.environ:
certDir = os.environ['VMCATCHER_DIR_CERT']
if 'VMCATCHER_OUTPUT_FORMAT' in os.environ:
output_format_selected = os.environ['VMCATCHER_OUTPUT_FORMAT']
# Set up log file
LoggingLevel = logging.WARNING
LoggingLevelCounter = 2
if options.verbose:
LoggingLevelCounter = LoggingLevelCounter - options.verbose
if options.verbose == 1:
LoggingLevel = logging.INFO
if options.verbose == 2:
LoggingLevel = logging.DEBUG
if options.quiet:
LoggingLevelCounter = LoggingLevelCounter + options.quiet
if LoggingLevelCounter <= 0:
LoggingLevel = logging.DEBUG
if LoggingLevelCounter == 1:
LoggingLevel = logging.INFO
if LoggingLevelCounter == 2:
LoggingLevel = logging.WARNING
if LoggingLevelCounter == 3:
LoggingLevel = logging.ERROR
if LoggingLevelCounter == 4:
LoggingLevel = logging.FATAL
if LoggingLevelCounter >= 5:
LoggingLevel = logging.CRITICAL
if options.log_config:
logFile = str(options.log_config)
if logFile != None:
if os.path.isfile(logFile):
logging.config.fileConfig(logFile)
else:
logging.basicConfig(level=LoggingLevel)
log = logging.getLogger("main")
log.error("Logfile configuration file '%s' was not found." % (options.log_config))
sys.exit(1)
else:
logging.basicConfig(level=LoggingLevel)
log = logging.getLogger("main")
# Now logging is set up process other options
if options.log_sql_info:
debugSqlEcho = True
if options.cert_dir:
certDir = options.cert_dir
if options.list:
actions.add('list')
if options.sha512:
images_selected = options.sha512
input_format_selected.add('sha512')
if options.uuid:
images_selected = options.uuid
input_format_selected.add('uuid')
if options.info:
actions.add('info')
if options.output:
format_needed = True
outputfiles = options.output
if options.format:
output_format_selected = options.format
if options.add:
actions.add('add')
if options.remove:
actions.add('remove')
if options.database:
databaseConnectionString = options.database
# 1 So we have some command line validation
if databaseConnectionString == None:
databaseConnectionString = 'sqlite:///vmcatcher.db'
log.info("Defaulting DB connection to '%s'" % (databaseConnectionString))
if len(actions) == 0:
log.error("No actions selected")
sys.exit(1)
if len(actions) > 1:
log.error("More than one action selected.")
sys.exit(1)
if actions.issubset(actionsrequiring_selections) and len(images_selected) == 0:
action_askedfor = actions.pop()
log.error("Action '%s' requires an image to be selected." % action_askedfor)
sys.exit(1)
if not output_format_selected in outputformats:
log.error("Invalid format '%s' allowed formats are '%s'" % (output_format_selected,outputformats))
sys.exit(1)
# 1 So we have some actions to process
if output_format_selected == "message" and "info" in actions:
anchor_needed = True
# 1.1 Initate DB
database = db_controler(databaseConnectionString,debugSqlEcho)
# 2 Initate CA's to manage files
if anchor_needed:
if certDir == None:
certDir = "/etc/grid-security/certificates/"
log.info("Defaulting Certificate directory to '%s'" % (certDir))
if not os.path.isdir(certDir):
log.error('Invalid x509 trust store directory: %s.' % (certDir))
sys.exit(1)
database.setup_trust_anchor(certDir)
# Handle conflicting identifiers
selectors_types = inputformats.intersection(input_format_selected)
selectors_types_len = len(selectors_types)
if selectors_types_len > 1:
log.error('Conflicting selectors.')
sys.exit(1)
selector_str = 'uuid'
if selectors_types_len == 1:
selector_str = selectors_types.pop()
selectorMapper = {'uuid' : 'image_uuid',
'sha512' : 'image_sha512',
}
if not selector_str in selectorMapper.keys():
log.error('Invalid selector.')
sys.exit(1)
rc = database.set_selector(selectorMapper[selector_str])
# Handle the output_view
database.setup_view_format(output_format_selected)
# Handle actions
if 'subscribe' in actions:
Session = SessionFactory()
db = db_actions(Session)
for uri in subscription_url_list:
db.subscribe_file(anchor,uri)
if 'list' in actions:
database.image_list()
if 'update' in actions:
Session = SessionFactory()
db = db_actions(Session)
db.subscriptions_update(anchor)
if 'info' in actions:
if 'json' in actions:
pairs, extra_uuid ,extra_paths = pairsNnot(images_selected,messages_path)
if len(extra_paths) > 0:
log.warning('Extra paths will be ignored.')
for path in extra_paths:
log.info('ignoring path %s' % (path))
if len(extra_uuid) > 0:
log.warning('sha512 ignored.')
for path in extra_uuid:
log.info('ignoring sha512 %s' % (path))
Session = SessionFactory()
db = db_actions(Session)
for item in pairs:
db.image_by_sha512_writefile_json(anchor,item[0],item[1])
if 'message' in actions:
pairs, extra_uuid ,extra_paths = pairsNnot(images_selected,messages_path)
if len(extra_paths) > 0:
log.warning('Extra paths will be ignored.')
for path in extra_paths:
log.info('ignoring path %s' % (path))
if len(extra_uuid) > 0:
log.warning('sha512 ignored.')
for path in extra_uuid:
log.info('Ignoring sha512 %s' % (path))
Session = SessionFactory()
db = db_actions(Session)
for item in pairs:
db.image_by_sha512_writefile_imagelist(anchor,item[0],item[1])
if 'info' in actions:
if not database.images_info(images_selected,outputfiles):
sys.exit(1)
if 'add' in actions:
database.images_subscribe(images_selected,1)
if 'remove' in actions:
database.images_subscribe(images_selected,0)
if __name__ == "__main__":
main()
sys.exit(0)