-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvmilisttool
executable file
·203 lines (190 loc) · 7.69 KB
/
vmilisttool
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
#!/usr/bin/env python
import codecs
import optparse
import os
import sys
import hashlib
import os.path
import uuid
# simplejson is included with Python 2.6 and above
# with the name json
if float(sys.version[:3]) >= 2.6:
import json
else:
# python 2.4 or 2.5 can also import simplejson
# as working alternative to the json included.
import simplejson as json
import datetime
from hepixvmitrust import VMListControler
# import below purely to get the version number
# if the command line tool will have different
# version number then the library we can take this
# out
from hepixvmitrust.__version__ import version
import logging, logging.config
# needed for the signing of images.
import M2Crypto.SMIME
import M2Crypto.BIO
# User interface
def pairsNnot(list_a,list_b):
len_generate_list = len(list_a)
len_image_list = len(list_b)
ocupies_generate_list = set(range(len_generate_list))
ocupies_image_list = set(range(len_image_list))
ocupies_pairs = ocupies_image_list.intersection(ocupies_generate_list)
diff_a = ocupies_generate_list.difference(ocupies_image_list)
diff_b = ocupies_image_list.difference(ocupies_generate_list)
arepairs = []
for i in ocupies_pairs:
arepairs.append([list_a[i],list_b[i]])
notpairs_a = []
for i in diff_a:
notpairs_a.append(list_a[i])
notpairs_b = []
for i in diff_b:
notpairs_b.append(list_b[i])
return arepairs,notpairs_a,notpairs_b
def main():
"""Runs program and handles command line options"""
actions = set([])
listcontroler = VMListControler()
p = optparse.OptionParser(version = "%prog " + version)
p.add_option('-j', '--json', action ='store', help='Path of the json output file', metavar='OUTPUTFILE')
p.add_option('-t', '--template', action ='store', help='Path of the json template file', metavar='TEMPLATE')
p.add_option('-m', '--smime-template', action ='store', help='Path of the smime encoded json template file', metavar='TEMPLATE')
p.add_option('-a', '--add', action ='append', help='adds a VM image to the JSON', metavar='IMAGEMETADATA')
p.add_option('-d', '--delete', action ='append', help='del a VM image to the JSON', metavar='IMAGEMETADATA')
p.add_option('-g', '--generate', action ='append', help='generates a VM image metadata for image', metavar='OUTPUTMETADATA')
p.add_option('-i', '--image', action ='append', help='Sets the image to generates a VM image metadata', metavar='IMAGEFILE')
p.add_option('-l', '--list', action ='store_true', help='lists VM images in the JSON')
p.add_option('-k', '--signer_key', action ='store', help='path to signer key', metavar='SIGNERKEY')
p.add_option('-c', '--signer_certificate', action ='store', help='path to signer certificate', metavar='SIGNERCERT')
p.add_option('-s', '--sign', action ='store', help='returns verbose output', metavar='SIGNEDOUTPUT')
p.add_option('-f', '--format', action ='store', help='Set the format valid values are JSON and XML', metavar='FORMAT')
p.add_option('--imagelist_version', action ='store', help='Over write the version in the message', metavar='FORMAT')
options, arguments = p.parse_args()
filename_template = None
smime_template = None
json_output = 'imagelist.json'
generate_list = []
add_image_file = []
del_image_metadata = []
format = None
signer_key = os.environ['HOME'] + '/.globus/userkey.pem'
signer_cert = os.environ['HOME'] + '/.globus/usercert.pem'
signed_message_output = None
list_images = False
imagelist = []
imagelist_version = '0.0.1'
clashingactions_template = set(['loads_smime_template','load_template'])
if options.template:
filename_template = options.template
actions.add('load_template')
if options.smime_template:
smime_template = options.smime_template
actions.add('loads_smime_template')
if options.json:
json_output = options.json
actions.add('save')
if options.add:
add_image_file = options.add
actions.add('image_add')
if options.delete:
del_image_file = options.delete
actions.add('image_del')
if options.list:
list_images = True
actions.add('image_list')
if options.signer_key:
signer_key = options.signer_key
if options.signer_certificate:
signer_cert = options.signer_certificate
if options.sign:
actions.add('verify')
actions.add('sign')
signed_message_output = options.sign
if options.generate:
generate_list = options.generate
actions.add('generate')
if options.format:
format = options.format
actions.add('format')
if options.image:
imagelist = options.image
actions.add('generate')
if options.imagelist_version:
imageListVersion = options.imagelist_version
actions.add('setversion')
temp1 = actions.intersection(clashingactions_template)
temp1_len = len (temp1)
if temp1_len > 1:
print "Cannot support more than one template with this script."
sys.exit(1)
if temp1_len == 1:
filename = filename_template
if filename == None:
filename = smime_template
fp = open(str(filename))
text = fp.read()
if actions.__contains__('loads_smime_template'):
listcontroler.loads_smime(text)
if actions.__contains__('load_template'):
listcontroler.loads(text)
listcontroler.enviroment_default()
if actions.__contains__('setversion'):
listcontroler.model.metadata[u'hv:version'] = imageListVersion
# Now process the actions.
if actions.__contains__('generate'):
pairs, extra_gens ,extra_images = pairsNnot(generate_list,imagelist)
if len(extra_images) > 0:
print "error images and no target"
for paired_items in pairs:
listcontroler.generate(paired_items[0],paired_items[1])
for gen_it in extra_gens:
rc = listcontroler.generate(gen_it)
if not rc:
print "Files '%s'" % (gen_it)
sys.exit(1)
if actions.__contains__('image_add'):
for item in add_image_file:
listcontroler.image_add(item)
if actions.__contains__('image_del'):
for item in del_image_file:
success = listcontroler.image_del(item)
if success == False:
print "Failed to delete image '%s'" % (item)
sys.exit(1)
if actions.__contains__('image_list'):
listcontroler.images_list()
if actions.__contains__('verify'):
success = listcontroler.verify()
if success == False:
print "Failed to verify valid meta data for image."
sys.exit(1)
if actions.__contains__('sign'):
content = listcontroler.dumps()
smime = M2Crypto.SMIME.SMIME()
smime.load_key(signer_key,signer_cert)
buf = M2Crypto.BIO.MemoryBuffer(content)
p7 = smime.sign(buf,M2Crypto.SMIME.PKCS7_DETACHED)
buf = M2Crypto.BIO.MemoryBuffer(content)
out = M2Crypto.BIO.MemoryBuffer()
smime.write(out, p7, buf)
message_signed = str(out.read())
f = open(signed_message_output, 'w')
f.write(message_signed)
if actions.__contains__('save'):
listcontroler.save(json_output)
if actions.__contains__('format'):
if format == 'xml':
# do nothing right now
1
else:
print "Output format type '" + format + "' not supported right now."
print "Currently only supports JSON output RDF XML output may come later"
sys.exit(1)
#test_things5()
#test_things5()
if __name__ == "__main__":
logging.basicConfig(level=logging.WARNING)
main()