2
0
mirror of https://github.com/xcat2/confluent.git synced 2025-02-28 08:11:45 +00:00

Refactor apiclient and allow specific host

For getnetcfg, will need to select
spsecific interfaces.  While
tocuhing that anyway, simplify the
v4/v6 paths to be more agnostic.
This commit is contained in:
Jarrod Johnson 2021-10-28 15:48:58 -04:00
parent 5b128238e7
commit 925ea3e95a

View File

@ -168,17 +168,12 @@ def get_net_apikey(nodename, mgr):
return ''
def get_apikey(nodename, mgr, mgr6=None):
def get_apikey(nodename, hosts):
apikey = ""
if os.path.exists('/etc/confluent/confluent.apikey'):
apikey = open('/etc/confluent/confluent.apikey').read().strip()
if apikey:
return apikey
hosts = []
for host in (mgr, mgr6):
if not host:
continue
hosts.append(host)
while not apikey:
for host in hosts:
try:
@ -207,52 +202,46 @@ def get_apikey(nodename, mgr, mgr6=None):
return apikey
class HTTPSClient(client.HTTPConnection, object):
def __init__(self, json=False, port=443):
def __init__(self, usejson=False, port=443, host=None):
self.stdheaders = {}
info = open('/etc/confluent/confluent.info').read().split('\n')
host = None
mgtiface = None
havedefault = '0'
for line in info:
if line.startswith('NODENAME:'):
node = line.split(' ')[1]
self.stdheaders['CONFLUENT_NODENAME'] = node
if line.startswith('MANAGER:') and not host:
host = line.split(' ')[1]
if line.startswith('EXTMGRINFO:'):
extinfo = line.split(' ')[1]
extinfo = extinfo.split('|')
if not mgtiface:
host, mgtiface, havedefault = extinfo[:3]
if havedefault == '0' and extinfo[2] == '1':
host, mgtiface, havedefault = extinfo[:3]
if '%' in host:
ifidx = host.split('%', 1)[1]
with open('/tmp/confluent.ifidx', 'w+') as ifout:
ifout.write(ifidx)
v4srv = None
v6srv = None
if ':' in host:
v6srv = host
else:
v4srv = host
if json:
if usejson:
self.stdheaders['ACCEPT'] = 'application/json'
try:
info = open('/etc/confluent/confluent.deploycfg').read().split('\n')
except Exception:
info = None
if info:
if host:
self.hosts = [host]
else:
self.hosts = []
info = open('/etc/confluent/confluent.info').read().split('\n')
mgtiface = None
havedefault = '0'
for line in info:
if line.startswith('deploy_server: '):
v4srv = line.split(': ', 1)[1]
if line.startswith('deploy_server_v6: '):
v6srv = line.split(': ', 1)[1]
self.stdheaders['CONFLUENT_APIKEY'] = get_apikey(node, v4srv, v6srv)
if line.startswith('NODENAME:'):
node = line.split(' ')[1]
self.stdheaders['CONFLUENT_NODENAME'] = node
if line.startswith('MANAGER:') and not host:
host = line.split(' ')[1]
if line.startswith('EXTMGRINFO:'):
extinfo = line.split(' ')[1]
extinfo = extinfo.split('|')
if not mgtiface:
host, mgtiface, havedefault = extinfo[:3]
if havedefault == '0' and extinfo[2] == '1':
host, mgtiface, havedefault = extinfo[:3]
if '%' in host:
ifidx = host.split('%', 1)[1]
with open('/tmp/confluent.ifidx', 'w+') as ifout:
ifout.write(ifidx)
self.hosts.append(host)
try:
info = open('/etc/confluent/confluent.deploycfg').read().split('\n')
except Exception:
info = None
if info:
for line in info:
if line.startswith('deploy_server: ') or line.startswith('deploy_server_v6: '):
self.hosts.append(line.split(': ', 1)[1])
self.stdheaders['CONFLUENT_APIKEY'] = get_apikey(node, self.hosts)
if mgtiface:
self.stdheaders['CONFLUENT_MGTIFACE'] = mgtiface
self.v4srv = v4srv
self.v6srv = v6srv
self.port = port
self.host = None
self.node = node
@ -266,11 +255,7 @@ class HTTPSClient(client.HTTPConnection, object):
def check_connections(self):
foundsrv = None
hosts = []
if self.v4srv:
hosts.append(self.v4srv)
if self.v6srv:
hosts.append(self.v6srv)
hosts = self.hosts
for timeo in (0.1, 5):
for host in hosts:
try:
@ -348,14 +333,14 @@ class HTTPSClient(client.HTTPConnection, object):
with open('/etc/confluent/confluent.apikey', 'w+') as akfile:
akfile.write('')
self.stdheaders['CONFLUENT_APIKEY'] = get_apikey(
self.node, self.host)
self.node, [self.host])
raise Exception(rsp.read())
if __name__ == '__main__':
data = None
json = False
usejson = False
if '-j' in sys.argv:
json = True
usejson = True
if len(sys.argv) == 1:
HTTPSClient()
sys.exit(0)
@ -389,10 +374,10 @@ if __name__ == '__main__':
if len(sys.argv) > 2 and os.path.exists(sys.argv[-1]):
data = open(sys.argv[-1]).read()
if waitfor:
client = HTTPSClient(json=json)
client = HTTPSClient(usejson)
status = 201
while status != waitfor:
status, rsp = client.grab_url_with_status(sys.argv[1], data)
sys.stdout.write(rsp.decode())
else:
sys.stdout.write(HTTPSClient(json=json).grab_url(sys.argv[1], data).decode())
sys.stdout.write(HTTPSClient(usejson).grab_url(sys.argv[1], data).decode())