This repository has been archived by the owner on Dec 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
packages.py
304 lines (241 loc) · 10.1 KB
/
packages.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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#
# Licensed under the GNU General Public License Version 3
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import xmlrpclib
import shlex
from getpass import getpass
from optparse import Option
from rpathcmd.utils import *
import time
import urllib
import urllib2
import httplib2
from pprint import pprint
from genshi.template import TextTemplate
from xobj import xobj
import epdb
def help_packages_list(self):
print 'packages_list: list projects'
print 'usage: packagess_list projectshortname branch stage'
def do_packages_list(self, options):
h2 = httplib2.Http("~/.rpathcmd/.cache")
h2.disable_ssl_certificate_validation = True
h2.add_credentials(self.options.username, self.options.password)
print "not yet implemented"
def __get_querysetid_by_name(self, name):
# /api/v1/query_sets;filter_by=[query_set.name,EQUAL,All%20projects]
h2 = httplib2.Http("~/.rpathcmd/.cache")
h2.disable_ssl_certificate_validation = True
h2.add_credentials(self.options.username, self.options.password)
#epdb.st()
filterterm = urllib.quote(name)
#print "#%s" % filterterm
tmpxml = h2.request('http://' + self.options.server +
'/api/v1/query_sets;filter_by=[query_set.name,EQUAL,' +
filterterm + ']')
tmpdata = xobj.parse(tmpxml[1])
#epdb.st()
print "#%s == %s" % (filterterm, int(tmpdata.query_sets.query_set.query_set_id))
return int(tmpdata.query_sets.query_set.query_set_id)
def help_package_spfimport(self):
print 'package_spfimport: import an spf package from a url'
print 'usage package_spfimport projectshortname branchname spfurl spfname spfversion spfextractdir'
def do_package_spfimport(self, args):
maxAttempts = 540
requestDelay = 1
# define XMLRPC session
xmlrpc_endpoint = "https://%s:%s@%s/xmlrpc-private" % (self.options.username, self.options.password, self.options.server)
self.proxy = xmlrpclib.ServerProxy(xmlrpc_endpoint)
# set options
(args, options) = parse_arguments(args)
projectshortname = args[0]
proj_id = int(__projectshortname_to_id(self, projectshortname))
branchname = args[1]
branch_id = int(__branchname_to_id(self, projectshortname, branchname))
rebuild = False
stage_label = str(__branchname_to_devlabel(self, projectshortname, branchname))
stage_label = stage_label + '-devel'
spfurl = args[2]
#spfname = args[3]
#spfversion = args[4]
#spfextractdir = args[5]
# startPackageCreatorSession(self, projectId, prodVer, namespace, troveName, label):
# start pcreator/appcreator session
start_appcreator_session_rsp = self.proxy.startApplianceCreatorSession(
int(proj_id),
int(branch_id),
True,
str(stage_label) )
#epdb.st()
# output session info
#pp.pprint(start_appcreator_session_rsp)
pprint(start_appcreator_session_rsp)
print "session: %s" % start_appcreator_session_rsp[1][0]
print "isApplianceCreatorManaged: %s" % start_appcreator_session_rsp[1][1]['isApplianceCreatorManaged']
sessionToken = start_appcreator_session_rsp[1][0]
#create temp dir on the rbuilderto push the spf file into
#epdb.st()
create_temp_package_dir = self.proxy.createPackageTmpDir()
print "Temp Package Dir: %s" % create_temp_package_dir
print "Package URL: %s" % spfurl
# invoke CGI script with file url and the tempdir ID
# file will land in ... /srv/rbuilder/tmp/rb-pc-upload-$ID/tmp_$ID.ccs
url = 'http://'+self.options.server+'/cgi-bin/urldownloader.cgi?fileUrl='+spfurl+'&uploadId='+create_temp_package_dir[1]
print "cgiurl: %s" % url
#upload_file_rsp = restutil.rawGet(url)
# GET the url, so the cgi runs
h = httplib2.Http(".cache")
upload_file_rsp = h.request(url, "GET")
#epdb.st()
pprint(upload_file_rsp)
# fail if not Ok
#assert upload_file_rsp == 'Ok\n'
assert upload_file_rsp[1] == 'Ok\n'
# get all meta about the imported file
package_factory_rsp = self.proxy.getPackageFactories(int(proj_id),
create_temp_package_dir[1],
int(branch_id),
'',
'',
str(stage_label))
#epdb.st()
# parse session
session_Token = package_factory_rsp [1][0]
# get factory type
factoryHandle = package_factory_rsp[1][1][0][0]
# get contents of defaults.txt
smartformdetails = package_factory_rsp[1][1][0][1]
#epdb.st()
packagemeta = xobj.parse(smartformdetails)
spfname = ''
spfversion = ''
spflocation = ''
spftype = ''
spfdestdir = ''
spfconfigdescriptor = ''
for field in packagemeta.factory.dataFields.field:
#epdb.st()
#print field.name
'''
*name --required
*version --required
license
summary
description
*location --required
config_descriptor
'''
# required fields, will have <default> elements if set in defaults.txt
print field.name
if field.name == 'name':
spfname = str(field.default)
#epdb.st()
if field.name == 'version':
spfversion = str(field.default)
if field.name == 'location':
spflocation = str(field.default)
if field.name == 'destdir':
spfdestdir = str(field.default)
if field.name == 'applicationType':
spftype = str(field.default)
if field.name == 'config_descriptor':
spfconfigdescriptor = str(field.default)
#epdb.st()
#print package_factory_rsp[1][1][0][1]
configdata = { 'name': spfname,
'version': spfversion,
'applicationType': spftype,
'location': spflocation,
'destdir': spfdestdir,
'config_descriptor': spfconfigdescriptor }
pprint(configdata)
#epdb.st()
# getPackageCreateRecipe is "supposed" to return whatever recipe
# is in the spf. A default override recipe is returned if
# the spf does not include recipe.txt
packageCreatorRecipeRsp = self.proxy.getPackageCreatorRecipe(session_Token)
# set the recipe to whatever pcreator returns
recipe = packageCreatorRecipeRsp[1][1]
# send back whatever recipe/configdata we have
"""
Store a package creator recipe. using an empty string for recipeData
will return recipe to default.
"""
save_package_response = self.proxy.savePackage(session_Token,
factoryHandle,
configdata,
True,
recipe)
pprint(save_package_response)
# poll job till finished, failure or thresholds met
for i in range(maxAttempts):
# fetch status
package_build_status = self.proxy.getPackageBuildStatus(session_Token)
now = time.strftime('%Y%m%d%H%M%S')
print "%s - %s" % (now, package_build_status[1][2])
if package_build_status[1][0]:
print "Package build completed"
print "Package build status: %s" % package_build_status[1][2]
break
time.sleep(requestDelay)
else:
asserts.fail("Committed state Not reached in :%s seconds" % \
(maxAttempts * requestDelay))
def __projectshortname_to_id (self, projectshortname):
#create session
h2 = httplib2.Http("~/import_spf/.cache")
h2.disable_ssl_certificate_validation = True
h2.add_credentials(self.options.username, self.options.password)
# request queryset by NAME
tmpxml = h2.request('http://' + self.options.server + '/api/v1/projects')
# find the queryset URL
tmpdata = xobj.parse(tmpxml[1])
fullcollectionurl = tmpdata.projects.full_collection
# filter for specific project name
fullcollectionurl = fullcollectionurl + ';filter_by=[project.short_name,EQUAL,' + projectshortname + ']'
fullxml = h2.request(fullcollectionurl)
fulldata = xobj.parse(fullxml[1])
# grep the project ID
project_id = fulldata.projects.project.project_id
return project_id
def __branchname_to_id (self, projectshortname, branchname):
#create session
h2 = httplib2.Http("~/import_spf/.cache")
h2.disable_ssl_certificate_validation = True
h2.add_credentials(self.options.username, self.options.password)
# api/v1/projects/jt-pcreator/project_branches;filter_by=[name,EQUAL,trunk]
# request queryset by NAME
tmpxml = h2.request('http://' + self.options.server +
'/api/v1/projects/' + projectshortname +
'/project_branches;filter_by=[name,EQUAL,' + branchname + ']' )
tmpdata = xobj.parse(tmpxml[1])
#epdb.st()
branch_id = tmpdata.project_branches.project_branch.branch_id
return branch_id
def __branchname_to_devlabel (self, projectshortname, branchname):
#create session
h2 = httplib2.Http("~/import_spf/.cache")
h2.disable_ssl_certificate_validation = True
h2.add_credentials(self.options.username, self.options.password)
# api/v1/projects/jt-pcreator/project_branches;filter_by=[name,EQUAL,trunk]
# request queryset by NAME
tmpxml = h2.request('http://' + self.options.server +
'/api/v1/projects/' + projectshortname +
'/project_branches;filter_by=[name,EQUAL,' + branchname + ']' )
tmpdata = xobj.parse(tmpxml[1])
#epdb.st()
label = tmpdata.project_branches.project_branch.label
return label