1
- #!/usr/bin/python -uO
1
+ #! /usr/bin/env python
2
2
# Copyright (c) 2010-2011 OpenStack, LLC.
3
3
#
4
4
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -28,9 +28,9 @@ from swift.common.ring import RingBuilder
28
28
29
29
30
30
MAJOR_VERSION = 1
31
- MINOR_VERSION = 2
32
- EXIT_RING_CHANGED = 0
33
- EXIT_RING_UNCHANGED = 1
31
+ MINOR_VERSION = 3
32
+ EXIT_SUCCESS = 0
33
+ EXIT_WARNING = 1
34
34
EXIT_ERROR = 2
35
35
36
36
@@ -146,7 +146,7 @@ swift-ring-builder <builder_file> create <part_power> <replicas>
146
146
"""
147
147
if len (argv ) < 6 :
148
148
print Commands .create .__doc__ .strip ()
149
- exit (EXIT_RING_UNCHANGED )
149
+ exit (EXIT_ERROR )
150
150
builder = RingBuilder (int (argv [3 ]), int (argv [4 ]), int (argv [5 ]))
151
151
backup_dir = pathjoin (dirname (argv [1 ]), 'backups' )
152
152
try :
@@ -157,7 +157,7 @@ swift-ring-builder <builder_file> create <part_power> <replicas>
157
157
pickle .dump (builder .to_dict (), open (pathjoin (backup_dir ,
158
158
'%d.' % time () + basename (argv [1 ])), 'wb' ), protocol = 2 )
159
159
pickle .dump (builder .to_dict (), open (argv [1 ], 'wb' ), protocol = 2 )
160
- exit (EXIT_RING_CHANGED )
160
+ exit (EXIT_SUCCESS )
161
161
162
162
def default ():
163
163
"""
@@ -195,7 +195,7 @@ swift-ring-builder <builder_file>
195
195
(dev ['id' ], dev ['zone' ], dev ['ip' ], dev ['port' ],
196
196
dev ['device' ], dev ['weight' ], dev ['parts' ], balance ,
197
197
dev ['meta' ])
198
- exit (EXIT_RING_UNCHANGED )
198
+ exit (EXIT_SUCCESS )
199
199
200
200
def search ():
201
201
"""
@@ -206,7 +206,7 @@ swift-ring-builder <builder_file> search <search-value>
206
206
print Commands .search .__doc__ .strip ()
207
207
print
208
208
print search_devs .__doc__ .strip ()
209
- exit (EXIT_RING_UNCHANGED )
209
+ exit (EXIT_ERROR )
210
210
devs = search_devs (builder , argv [3 ])
211
211
if not devs :
212
212
print 'No matching devices found'
@@ -228,7 +228,7 @@ swift-ring-builder <builder_file> search <search-value>
228
228
(dev ['id' ], dev ['zone' ], dev ['ip' ], dev ['port' ],
229
229
dev ['device' ], dev ['weight' ], dev ['parts' ], balance ,
230
230
dev ['meta' ])
231
- exit (EXIT_RING_UNCHANGED )
231
+ exit (EXIT_SUCCESS )
232
232
233
233
def list_parts ():
234
234
"""
@@ -244,7 +244,7 @@ swift-ring-builder <builder_file> list_parts <search-value> [<search-value>] ..
244
244
print Commands .list_parts .__doc__ .strip ()
245
245
print
246
246
print search_devs .__doc__ .strip ()
247
- exit (EXIT_RING_UNCHANGED )
247
+ exit (EXIT_ERROR )
248
248
devs = []
249
249
for arg in argv [3 :]:
250
250
devs .extend (search_devs (builder , arg ) or [])
@@ -262,7 +262,7 @@ swift-ring-builder <builder_file> list_parts <search-value> [<search-value>] ..
262
262
for index , parts in enumerate (matches ):
263
263
for part in parts :
264
264
print '%9d %7d' % (part , builder .replicas - index )
265
- exit (EXIT_RING_UNCHANGED )
265
+ exit (EXIT_SUCCESS )
266
266
267
267
def add ():
268
268
"""
@@ -274,7 +274,7 @@ swift-ring-builder <builder_file> add z<zone>-<ip>:<port>/<device_name>_<meta>
274
274
"""
275
275
if len (argv ) < 5 :
276
276
print Commands .add .__doc__ .strip ()
277
- exit (EXIT_RING_UNCHANGED )
277
+ exit (EXIT_ERROR )
278
278
279
279
if not argv [3 ].startswith ('z' ):
280
280
print 'Invalid add value: %s' % argv [3 ]
@@ -348,7 +348,7 @@ swift-ring-builder <builder_file> add z<zone>-<ip>:<port>/<device_name>_<meta>
348
348
print 'Device z%s-%s:%s/%s_"%s" with %s weight got id %s' % \
349
349
(zone , ip , port , device_name , meta , weight , next_dev_id )
350
350
pickle .dump (builder .to_dict (), open (argv [1 ], 'wb' ), protocol = 2 )
351
- exit (EXIT_RING_UNCHANGED )
351
+ exit (EXIT_SUCCESS )
352
352
353
353
def set_weight ():
354
354
"""
@@ -361,7 +361,7 @@ swift-ring-builder <builder_file> set_weight <search-value> <weight>
361
361
print Commands .set_weight .__doc__ .strip ()
362
362
print
363
363
print search_devs .__doc__ .strip ()
364
- exit (EXIT_RING_UNCHANGED )
364
+ exit (EXIT_ERROR )
365
365
devs = search_devs (builder , argv [3 ])
366
366
weight = float (argv [4 ])
367
367
if not devs :
@@ -381,7 +381,7 @@ swift-ring-builder <builder_file> set_weight <search-value> <weight>
381
381
print 'd%(id)sz%(zone)s-%(ip)s:%(port)s/%(device)s_"%(meta)s" ' \
382
382
'weight set to %(weight)s' % dev
383
383
pickle .dump (builder .to_dict (), open (argv [1 ], 'wb' ), protocol = 2 )
384
- exit (EXIT_RING_UNCHANGED )
384
+ exit (EXIT_SUCCESS )
385
385
386
386
def set_info ():
387
387
"""
@@ -398,7 +398,7 @@ swift-ring-builder <builder_file> set_info <search-value>
398
398
print Commands .set_info .__doc__ .strip ()
399
399
print
400
400
print search_devs .__doc__ .strip ()
401
- exit (EXIT_RING_UNCHANGED )
401
+ exit (EXIT_ERROR )
402
402
devs = search_devs (builder , argv [3 ])
403
403
change_value = argv [4 ]
404
404
change = []
@@ -463,7 +463,7 @@ swift-ring-builder <builder_file> set_info <search-value>
463
463
dev [key ] = value
464
464
print 'Device %s is now %s' % (orig_dev_string , format_device (dev ))
465
465
pickle .dump (builder .to_dict (), open (argv [1 ], 'wb' ), protocol = 2 )
466
- exit (EXIT_RING_UNCHANGED )
466
+ exit (EXIT_SUCCESS )
467
467
468
468
def remove ():
469
469
"""
@@ -479,7 +479,7 @@ swift-ring-builder <builder_file> remove <search-value>
479
479
print Commands .remove .__doc__ .strip ()
480
480
print
481
481
print search_devs .__doc__ .strip ()
482
- exit (EXIT_RING_UNCHANGED )
482
+ exit (EXIT_ERROR )
483
483
devs = search_devs (builder , argv [3 ])
484
484
if not devs :
485
485
print 'No matching devices found'
@@ -499,7 +499,7 @@ swift-ring-builder <builder_file> remove <search-value>
499
499
'marked for removal and will be removed next rebalance.' \
500
500
% dev
501
501
pickle .dump (builder .to_dict (), open (argv [1 ], 'wb' ), protocol = 2 )
502
- exit (EXIT_RING_UNCHANGED )
502
+ exit (EXIT_SUCCESS )
503
503
504
504
def rebalance ():
505
505
"""
@@ -514,21 +514,23 @@ swift-ring-builder <builder_file> rebalance
514
514
print 'No partitions could be reassigned.'
515
515
print 'Either none need to be or none can be due to ' \
516
516
'min_part_hours [%s].' % builder .min_part_hours
517
- exit (EXIT_RING_UNCHANGED )
517
+ exit (EXIT_WARNING )
518
518
if not devs_changed and abs (last_balance - balance ) < 1 :
519
519
print 'Cowardly refusing to save rebalance as it did not change ' \
520
520
'at least 1%.'
521
- exit (EXIT_RING_UNCHANGED )
521
+ exit (EXIT_WARNING )
522
522
builder .validate ()
523
523
print 'Reassigned %d (%.02f%%) partitions. Balance is now %.02f.' % \
524
524
(parts , 100.0 * parts / builder .parts , balance )
525
+ status = EXIT_SUCCESS
525
526
if balance > 5 :
526
527
print '-' * 79
527
528
print 'NOTE: Balance of %.02f indicates you should push this ' % \
528
529
balance
529
530
print ' ring, wait at least %d hours, and rebalance/repush.' \
530
531
% builder .min_part_hours
531
532
print '-' * 79
533
+ status = EXIT_WARNING
532
534
ts = time ()
533
535
pickle .dump (builder .get_ring ().to_dict (),
534
536
GzipFile (pathjoin (backup_dir , '%d.' % ts +
@@ -538,15 +540,15 @@ swift-ring-builder <builder_file> rebalance
538
540
pickle .dump (builder .get_ring ().to_dict (), GzipFile (ring_file , 'wb' ),
539
541
protocol = 2 )
540
542
pickle .dump (builder .to_dict (), open (argv [1 ], 'wb' ), protocol = 2 )
541
- exit (EXIT_RING_CHANGED )
543
+ exit (status )
542
544
543
545
def validate ():
544
546
"""
545
547
swift-ring-builder <builder_file> validate
546
548
Just runs the validation routines on the ring.
547
549
"""
548
550
builder .validate ()
549
- exit (EXIT_RING_UNCHANGED )
551
+ exit (EXIT_SUCCESS )
550
552
551
553
def write_ring ():
552
554
"""
@@ -568,12 +570,12 @@ swift-ring-builder <builder_file> write_ring
568
570
GzipFile (pathjoin (backup_dir , '%d.' % time () +
569
571
basename (ring_file )), 'wb' ), protocol = 2 )
570
572
pickle .dump (ring_data .to_dict (), GzipFile (ring_file , 'wb' ), protocol = 2 )
571
- exit (EXIT_RING_CHANGED )
573
+ exit (EXIT_SUCCESS )
572
574
573
575
def pretend_min_part_hours_passed ():
574
576
builder .pretend_min_part_hours_passed ()
575
577
pickle .dump (builder .to_dict (), open (argv [1 ], 'wb' ), protocol = 2 )
576
- exit (EXIT_RING_UNCHANGED )
578
+ exit (EXIT_SUCCESS )
577
579
578
580
def set_min_part_hours ():
579
581
"""
@@ -584,12 +586,12 @@ swift-ring-builder <builder_file> set_min_part_hours <hours>
584
586
"""
585
587
if len (argv ) < 4 :
586
588
print Commands .set_min_part_hours .__doc__ .strip ()
587
- exit (EXIT_RING_UNCHANGED )
589
+ exit (EXIT_ERROR )
588
590
builder .change_min_part_hours (int (argv [3 ]))
589
591
print 'The minimum number of hours before a partition can be ' \
590
592
'reassigned is now set to %s' % argv [3 ]
591
593
pickle .dump (builder .to_dict (), open (argv [1 ], 'wb' ), protocol = 2 )
592
- exit (EXIT_RING_UNCHANGED )
594
+ exit (EXIT_SUCCESS )
593
595
594
596
595
597
if __name__ == '__main__' :
@@ -609,9 +611,11 @@ if __name__ == '__main__':
609
611
for line in wrap (' ' .join (cmds ), 79 , initial_indent = 'Quick list: ' ,
610
612
subsequent_indent = ' ' ):
611
613
print line
612
- print 'Exit codes: 0 = ring changed, 1 = ring did not change, ' \
613
- '2 = error'
614
- exit (EXIT_RING_UNCHANGED )
614
+ print ('Exit codes: 0 = operation successful\n '
615
+ ' 1 = operation completed with warnings\n ' \
616
+ ' 2 = error'
617
+ )
618
+ exit (EXIT_SUCCESS )
615
619
616
620
if exists (argv [1 ]):
617
621
try :
0 commit comments