Skip to content

Commit dbdee8a

Browse files
committed
better config handling in sharding script
1 parent 4f297b8 commit dbdee8a

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

sharding/simple-setup.py

+20-9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
# BEGIN CONFIGURATION
1616

17+
# some settings can also be set on command line. start with --help to see options
18+
1719
BASE_DATA_PATH='/data/db/sharding/' #warning: gets wiped every time you run this
1820
MONGO_PATH=os.getenv( "MONGO_HOME" , os.path.expanduser('~/10gen/mongo/') )
1921
print( "MONGO_PATH: " + MONGO_PATH )
@@ -32,26 +34,35 @@
3234
MONGOD_COLOR=36 #cyan
3335
BOLD=True
3436

35-
TEST_FOO_SHARD_KEY = "_id";
37+
# defaults -- can change on command line
38+
COLLECTION_KEYS = {'foo' : '_id', 'bar': 'key'}
3639

3740
for x in sys.argv[1:]:
38-
opt = x.split("=")
39-
if len(opt) != 2:
41+
opt = x.split("=", 1)
42+
if opt[0] != '--help' and len(opt) != 2:
4043
raise Exception("bad arg: " + x )
4144

42-
if opt[0] == "chunkSize":
43-
CHUNK_SIZE = int(opt[1])
44-
elif opt[0] == "foo":
45-
TEST_FOO_SHARD_KEY = opt[1]
45+
if opt[0].startswith('--'):
46+
opt[0] = opt[0][2:].lower()
47+
if opt[0] == 'help':
48+
print sys.argv[0], '[--help] [--chunksize=200] [--port=27017] [collection=key]'
49+
sys.exit()
50+
elif opt[0] == 'chunksize':
51+
CHUNK_SIZE = int(opt[1])
52+
elif opt[0] == 'port':
53+
MONGOS_PORT = int(opt[1])
54+
else:
55+
raise( Exception("unknown option: " + opt[0] ) )
4656
else:
47-
raise( Exception("unknown arg: " + opt[0] ) )
57+
COLLECTION_KEYS[opt[0]] = opt[1]
4858

4959
def AFTER_SETUP():
5060
# feel free to change any of this
5161
# admin and conn are both defined globaly
5262
admin.command('enablesharding', 'test')
5363

54-
admin.command('shardcollection', 'test.foo', key={ TEST_FOO_SHARD_KEY : 1 })
64+
for (collection, key) in COLLECTION_KEYS.iteritems():
65+
admin.command('shardcollection', 'test.'+collection, key={ key : 1 })
5566

5667
admin.command('shardcollection', 'test.fs.files', key={'_id':1})
5768
admin.command('shardcollection', 'test.fs.chunks', key={'files_id':1})

0 commit comments

Comments
 (0)