added option handling and command-line help [fixes #1]
This commit is contained in:
parent
8fccf60d75
commit
09b2a8f5f2
17
hap.py
17
hap.py
|
@ -4,13 +4,16 @@ from kubernetes import client, config
|
||||||
import sys, random
|
import sys, random
|
||||||
|
|
||||||
SVC_TYPE = 'LoadBalancer'
|
SVC_TYPE = 'LoadBalancer'
|
||||||
PORT_COUNT = 1
|
|
||||||
|
|
||||||
if len(sys.argv) > 1:
|
def getopts():
|
||||||
try:
|
"""parse command-line"""
|
||||||
PORT_COUNT = int(sys.argv[-1])
|
from optparse import OptionParser, OptionValueError
|
||||||
except:
|
usage = "%prog [options]"
|
||||||
sys.exit('optional positional parameter must be a number (default: 1)')
|
parser = OptionParser(usage=usage)
|
||||||
|
parser.add_option("-n", "--count", type="int", default=1, dest="port_count", help="number of ports needed (default: %default)")
|
||||||
|
return parser.parse_args()
|
||||||
|
|
||||||
|
options, args = getopts()
|
||||||
|
|
||||||
# load client configuration (kubeconfig)
|
# load client configuration (kubeconfig)
|
||||||
config.load_kube_config()
|
config.load_kube_config()
|
||||||
|
@ -29,4 +32,4 @@ full_port_range = range(49152, 65536) # https://tools.ietf.org/html/rfc6335#sect
|
||||||
available_ports = list(set(full_port_range) - set(used_ports))
|
available_ports = list(set(full_port_range) - set(used_ports))
|
||||||
|
|
||||||
# pick available ports
|
# pick available ports
|
||||||
print(' '.join(map(str, random.sample(available_ports, PORT_COUNT))))
|
print(' '.join(map(str, random.sample(available_ports, options.port_count))))
|
||||||
|
|
Loading…
Reference in New Issue