-
Notifications
You must be signed in to change notification settings - Fork 1
/
dist.py
440 lines (341 loc) · 14.1 KB
/
dist.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
import os
import re
import types
import sys
import logging
import platform
import glob
from paramiko.pkey import PKey
from paramiko.rsakey import RSAKey
from paramiko.sftp_client import SFTPClient
from paramiko.transport import Transport
from yumcommands import YumCommand
from yum.Errors import YumBaseError
from yum.plugins import TYPE_CORE, TYPE_INTERACTIVE
from paramiko.message import Message
from paramiko.sftp import CMD_OPENDIR, CMD_HANDLE, SFTPError, CMD_READDIR, CMD_NAME, CMD_CLOSE
from paramiko.sftp_client import _to_unicode
from paramiko.sftp_attr import SFTPAttributes
from pprint import pprint
requires_api_version = '2.4'
plugin_type = (TYPE_CORE, TYPE_INTERACTIVE)
branch_list = set(["test", "current", "stable"])
debuginfo_list = set(["debug", "debuginfo"])
arch_list = set(["x86-64", "noarch"])
_repository_prefix = None
_repository_server = None
_repository_port = 22
_repository_user = None
_repository_path = None
_identity_file = None
_sftp_client = None
class DistPushError(YumBaseError):
pass
class DistMoveError(YumBaseError):
pass
class DistRemoveError(YumBaseError):
pass
class DistExistsError(YumBaseError):
pass
class DistSFTPClient(SFTPClient):
def listdir_ex(self, path):
path = self._adjust_cwd(path)
basename = os.path.basename(path);
dir = os.path.dirname(path)
t, msg = self._request(CMD_OPENDIR, dir)
if t != CMD_HANDLE:
raise SFTPError('Expected handle')
handle = msg.get_string()
filelist = []
while True:
try:
t, msg = self._request(CMD_READDIR, handle)
except EOFError:
# done with handle
break
if t != CMD_NAME:
raise SFTPError('Expected name response')
count = msg.get_int()
for i in range(count):
filename = _to_unicode(msg.get_string())
longname = _to_unicode(msg.get_string())
attr = SFTPAttributes._from_msg(msg, filename, longname)
if (filename != '.') and (filename != '..') and filename.startswith(basename):
filelist.append(filename)
self._request(CMD_CLOSE, handle)
return filelist
def get_sftp_client():
global _sftp_client, _identity_file, _repository_server, _repository_port, _repository_user
try:
if not _sftp_client:
key = RSAKey.from_private_key_file(_identity_file)
t = Transport((_repository_server, _repository_port))
t.connect(username = _repository_user, pkey = key)
_sftp_client = DistSFTPClient.from_transport(t)
except Exception, e:
print e
return _sftp_client
def create_sftp_dir(sftp_client, path):
if path and path[len(path) - 1] == '/':
path = path[:-1]
dirs_to_create = []
while path:
try :
stat = sftp_client.stat(path)
break
except Exception, e:
dirs_to_create.append(path)
path = os.path.dirname(path)
dirs_to_create.reverse()
try :
for file in dirs_to_create:
sftp_client.mkdir(file)
except Exception, e:
print e
def create_sftp_symlink(sftp_client, path, filename, overwrite):
create_sftp_dir(sftp_client, path)
source_file = sftp_client.normalize(path + "/../../packages/" + filename)
dest_file = path + "/" + filename
if is_file_exists(sftp_client, dest_file, overwrite):
raise DistExistsError, "file %s exists in repository!"%(filename)
sftp_client.chdir(path)
sftp_client.symlink(source_file, filename)
# sftp_client
# format
# branc_list
# remove
def is_file_exists(sftp_client, file, overwrite):
try :
stat = sftp_client.stat(file)
if overwrite:
sftp_client.remove(file)
else:
return True
except Exception:
pass
return False
def guess_rpm_files(sftp_client, osver, arch, branch, file):
global _repository_path
try :
path = "%s/%d/%s/%s/packages/%s"%(_repository_path, osver, arch, branch, file)
return sftp_client.listdir_ex(path)
except Exception, e:
print e
return None
def update_last_modify_time(sftp_client, osver, arch, branch):
global _repository_path
try :
file = "%s/%d/%s/%s/last_modify_time"%(_repository_path, osver, arch, branch)
sftp_client.utime(file, None)
except Exception, e:
obj = sftp_client.open(file, "w", 0)
obj.close()
print e
return True
# yum dist-push
# yum dist-move
# yum dist-remove
class DistPushCommand(YumCommand):
def getNames(self):
return ['dist-push', 'dist-add']
def getUsage(self):
return ("PACKAGE...")
def getSummary(self):
return ("Push the rpm packages to repository via sftp")
def doCheck(self, base, basecmd, extcmds):
pass
def doCommand(self, base, basecmd, extcmds):
logger = logging.getLogger("yum.verbose.main");
opts = base.plugins.cmdline[0]
print "================================\n"
# get the branch
branch = opts.branch
if len(base.plugins.cmdline[1]) <= 1:
raise DistPushError, "Must specify at least one package to be push (i.e yum dist-push --branch <branch> pkg)";
elif not branch or branch not in branch_list:
while branch not in branch_list:
branch = raw_input("Push the packages to which branch? [test] : ");
if branch == "":
branch = "test"
else:
branch = opts.branch
osver = None
if opts.osver:
osver = int(opts.osver)
if (osver < 6) or (osver > 10):
raise DistPushError, "osver must be a number, and between 6 and 10"
if not osver:
osver = platform.dist()[1]
osver = int(osver[0:osver.index(".")])
# test/x86_64/packages
rpm_files = []
cmd_lines = base.plugins.cmdline[1][1:]
for i in cmd_lines:
rpm_files = rpm_files + glob.glob(i)
# filter the duplicate files
rpm_files = list(set(rpm_files))
sftp_client = get_sftp_client()
global _repository_path, _repository_server
print "Try to push rpms to repository..."
for file in rpm_files:
local_file = file
arch = platform.machine()
cur_branch = branch
if local_file.endswith(".src.rpm"):
arch = "SRPMS"
if local_file.find("-debuginfo-") > 0:
cur_branch = "debuginfo"
elif local_file.find("-debug-") > 0:
cur_branch = "debuginfo"
filename = os.path.basename(file)
remote_file = "%s/%d/%s/packages/%s"%(_repository_path, osver, arch, filename)
# check if file exists
if is_file_exists(sftp_client, remote_file, opts.overwrite):
raise DistPushError, "file %s exists in repository, please update the version"%(filename)
# create the path if not exists
create_sftp_dir(sftp_client, os.path.dirname(remote_file))
# push file
try :
attr = sftp_client.put(local_file, remote_file)
except Exception, e:
raise DistPushError, "Push %s to repository failed!"%(local_file)
# setup symlink
try :
# check all the branches
if opts.overwrite:
for b in branch_list:
file = "%s/%d/%s/%s/packages/%s"%(_repository_path, osver, arch, b, filename)
if is_file_exists(sftp_client, file, False):
sftp_client.remove(file)
remote_branch_path = "%s/%d/%s/%s/packages/"%(_repository_path, osver, arch, cur_branch)
create_sftp_symlink(sftp_client, remote_branch_path, filename, opts.overwrite)
update_last_modify_time(sftp_client, osver, arch, cur_branch)
print "Push %s done!"%(local_file)
except IOError, e:
raise DistExistsError, "Push %s to %s branch failed!"%(filename, cur_branch)
return 0, ["All done!"]
class DistMoveCommand(YumCommand):
def getNames(self):
return ['dist-move', 'dist-mv']
def getUsage(self):
return ("PACKAGE...")
def getSummary(self):
return ("Move rpm file from one branch to another")
def doCheck(self, base, basecmd, extcmds):
pass
def doCommand(self, base, basecmd, extcmds):
opts = base.plugins.cmdline[0]
print "================================\n"
if len(base.plugins.cmdline[1]) <= 1:
raise DistPushError, "Must specify at least one package to be move (i.e yum dist-move pkg)";
# get the branch
from_branch = None
to_branch = None
if opts.from_branch in branch_list:
from_branch = opts.from_branch
if opts.to_branch in branch_list:
to_branch = opts.to_branch
if not from_branch:
while from_branch not in branch_list:
from_branch = raw_input("Please enter the old branch [test] : ")
if from_branch == "":
from_branch = "test"
if not to_branch:
while to_branch not in branch_list:
to_branch = raw_input("Please enter the new branch [current] : ")
if to_branch == "":
to_branch = "current"
osver = None
if opts.osver:
osver = int(opts.osver)
if (osver < 6) or (osver > 10):
raise DistPushError, "osver must be a number, and between 6 and 10"
if not osver:
osver = platform.dist()[1]
osver = int(osver[0:osver.index(".")])
# get the os arch
arch = platform.machine()
if opts.arch in arch_list:
arch = opts.arch
search_file = os.path.basename(base.plugins.cmdline[1][1])
sftp_client = get_sftp_client()
rpm_files = guess_rpm_files(sftp_client, osver, arch, from_branch, search_file)
count = 0;
rpm_file = None
if rpm_files:
while True:
print "Which file do you want to move from %s branch to %s branch :\n"%(from_branch, to_branch)
for i in range(len(rpm_files)):
print " %d. %s\n"%(i + 1, rpm_files[i])
idx = raw_input("Please enter the index or [n] for exit : ")
if idx == 'n':
print "You select exit!"
break
try :
idx = int(idx)
except:
idx = -1
to_move = False
if idx > 0 and idx <= len(rpm_files):
to_move = True
rpm_file = rpm_files[idx - 1]
if to_move:
to_path = "%s/%d/%s/%s/packages/"%(_repository_path, osver, arch, to_branch)
from_file = "%s/%d/%s/%s/packages/%s"%(_repository_path, osver, arch, from_branch, rpm_file)
sftp_client.remove(from_file)
count = count + 1
try :
create_sftp_symlink(sftp_client, to_path, rpm_file, opts.overwrite)
del rpm_files[idx - 1]
print "Move %s from %s branch to %s branch success!"%(rpm_file, from_branch, to_branch)
except IOError:
raise DistMoveError, "Move %s from %s branch to %s branch failed!"%(rpm_file, from_branch, to_branch)
else:
print "Cann't find package %s in %s branch"%(search_file, from_branch)
if count > 0:
update_last_modify_time(sftp_client, osver, arch, to_branch)
update_last_modify_time(sftp_client, osver, arch, from_branch)
return 0, [""]
class DistRemoveCommand(YumCommand):
def getNames(self):
return ['dist-remove', 'dist-rm']
def getUsage(self):
return ("PACKAGE...")
def getSummary(self):
return ("Remove rpm package from repository")
def doCheck(self, base, basecmd, extcmds):
pass
def doCommand(self, base, basecmd, extcmds):
print "Not implement!"
return 0, [""]
def config_hook(conduit):
parser = conduit.getOptParser()
parser.add_option('', '--branch', dest='branch',
default='', help="specify the branch")
parser.add_option('', '--arch', dest='arch',
default='', help="specify the arch")
parser.add_option('', '--osver', dest='osver',
default='', help="specify the os version: 6, 7")
parser.add_option('', '--overwrite', dest='overwrite', action="store_true",
default=False, help="overwrite the package")
parser.add_option('', '--from-branch', dest='from_branch',
default='', help="specify the from branch while moving rpm package")
parser.add_option('', '--to-branch', dest='to_branch',
default='', help="specify the to branch while moving rpm package")
conduit.registerCommand(DistPushCommand())
conduit.registerCommand(DistMoveCommand())
conduit.registerCommand(DistRemoveCommand())
global _repository_path, _repository_port, _repository_server, _identity_file, _repository_user
_repository_prefix = conduit.confString("repository", "prefix", "dist")
_repository_server = conduit.confString("repository", "server")
_repository_path = conduit.confString("repository", "path")
_repository_user = conduit.confString("repository", "user", "dist-user")
_repository_port = conduit.confInt("repository", "port", 22)
_identity_file = conduit.confString("repository", "identity_file")
if not _repository_server or not _repository_path or not _identity_file:
raise DistPushError, "Must specify repository_server, repository_path, identity_file"
def args_hook(conduit):
optparser = conduit.getOptParser()
(opts, cmds) = optparser.parse_args(args=conduit.getArgs())
if opts.branch:
conduit.getRepos().enableRepo("%s-%s"%(_repository_prefix, opts.branch))