Skip to content

Commit dd723bc

Browse files
committedSep 29, 2021
handle exceptions from IPAddressPool and exit with failure as appropriate
1 parent bfc4c7d commit dd723bc

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed
 

‎ip_pool/cli.py

+24-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import sys
12
from argparse import ArgumentParser
23

3-
from .ip_pool import IPAddressPool
4+
from .ip_pool import IPAddressPool, IPAddressPoolException
45

56

67
def parse_args():
@@ -32,23 +33,28 @@ def parse_args():
3233

3334
def ip_pool_main():
3435
options = parse_args()
35-
pool = IPAddressPool(options.pool_db_json)
36-
if options.initialize:
37-
pool.initialize(options.initialize)
38-
39-
if options.release_address:
40-
hostname = options.release_address
41-
pool.release_address(hostname)
42-
43-
if options.new_address:
44-
hostname = options.new_address
45-
addr = pool.new_address(hostname)
46-
print(addr)
47-
48-
if options.address_for:
49-
hostname = options.address_for
50-
addr = pool.address_for(hostname)
51-
print(addr)
36+
try:
37+
pool = IPAddressPool(options.pool_db_json)
38+
if options.initialize:
39+
pool.initialize(options.initialize)
40+
41+
if options.release_address:
42+
hostname = options.release_address
43+
pool.release_address(hostname)
44+
45+
if options.new_address:
46+
hostname = options.new_address
47+
addr = pool.new_address(hostname)
48+
print(addr)
49+
50+
if options.address_for:
51+
hostname = options.address_for
52+
addr = pool.address_for(hostname)
53+
print(addr)
54+
except IPAddressPoolException as e:
55+
print(e, file=sys.stderr)
56+
return -1
57+
return 0
5258

5359

5460
if __name__ == "__main__":

0 commit comments

Comments
 (0)