This commit is contained in:
cooler
2023-06-19 10:42:25 +03:00
parent da99902e99
commit bc9856052b

24
main.py
View File

@@ -56,8 +56,12 @@ if os.getenv('CONSUL') != None:
else: else:
CONSUL=get_default_gateway_linux() CONSUL=get_default_gateway_linux()
if os.getenv('HOSTNAME') != None:
HOSTNAME = os.getenv('HOSTNAME')
else:
HOSTNAME=get_ip()
BASE_CONSUL_URL = 'http://' + CONSUL + ':8500' BASE_CONSUL_URL = 'http://' + CONSUL + ':8500'
HOSTNAME = os.getenv('HOSTNAME')
config = {'default':'config'} config = {'default':'config'}
prev_config = {'default':'config'} prev_config = {'default':'config'}
@@ -65,7 +69,7 @@ changed = 0
SERVICE_ADDRESS = get_ip() SERVICE_ADDRESS = get_ip()
PORT = 8080 PORT = 8080
UUID=str(uuid.uuid5(uuid.NAMESPACE_DNS, str(SERVICE_ADDRESS) + ":" + str(PORT) + ":" + HOSTNAME)) UUID=str(uuid.uuid5(uuid.NAMESPACE_DNS, f'{SERVICE_ADDRESS}:{PORT}:{HOSTNAME}'))
c = consul.Consul(host=CONSUL) c = consul.Consul(host=CONSUL)
@@ -107,7 +111,7 @@ def hello_world():
@app.route('/register') @app.route('/register')
def register(): def register():
url = BASE_CONSUL_URL + '/v1/agent/service/register' url = f'{BASE_CONSUL_URL}/v1/agent/service/register'
data = { data = {
'Name': 'PythonApp', 'Name': 'PythonApp',
'ID': UUID, 'ID': UUID,
@@ -115,20 +119,21 @@ def register():
'Address': SERVICE_ADDRESS, 'Address': SERVICE_ADDRESS,
'Port': PORT, 'Port': PORT,
'Check': { 'Check': {
'http': 'http://{address}:{port}/health'.format(address=SERVICE_ADDRESS, port=PORT), 'http': f'http://{SERVICE_ADDRESS}:{PORT}/health',
'interval': '10s' 'interval': '10s'
} }
} }
res = requests.put( res = requests.put(
url, url
data=json.dumps(data) , data=json.dumps(data)
, timeout=15
) )
return res return res
def cleanup(): def cleanup():
try: try:
sleep(int(random.randrange(1,5))) sleep(int(random.randrange(1,5)))
url = BASE_CONSUL_URL + '/v1/agent/service/deregister/' + UUID url = f'{BASE_CONSUL_URL}/v1/agent/service/deregister/{UUID}'
data = { data = {
'service_id': UUID, 'service_id': UUID,
} }
@@ -148,9 +153,10 @@ if __name__ == '__main__':
status = "" status = ""
while status != 200: while status != 200:
try: try:
app.logger.debug(f'Registering on consul') print(f'Registering on consul')
status = register().status_code status = register().status_code
sleep(int(random.randrange(1,5))) sleep(int(random.randrange(1,5)))
except Exception as e: except Exception as e:
app.logger.debug(f'ERROR::::: {e}') print(f'ERROR::::: {e}')
sleep(int(random.randrange(1,5)))
app.run(debug=True, host="0.0.0.0", port=PORT) app.run(debug=True, host="0.0.0.0", port=PORT)