Skip to content

Instantly share code, notes, and snippets.

@AstraSerg
Created August 20, 2019 14:29
Show Gist options
  • Save AstraSerg/8220d4ae52053b728c0447f8b4af26d4 to your computer and use it in GitHub Desktop.
Save AstraSerg/8220d4ae52053b728c0447f8b4af26d4 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# coding: utf-8
import consul as cons
'''
Cs = {
'redmine_db_ip': '',
'mongo_conn_string': '',
'mongo_un': 'root',
'mongo_db_name': 'aaastat',
'mongo_info_coll_name': 'aaastat_tmp',
'country_codes': ''
}
'''
def get_consul_data(Cs):
# selecting working consul
consul_hosts = ['10.10.3.96', '10.10.3.95', '10.10.3.244', '10.10.3.245', '10.10.3.254']
for c_host in consul_hosts:
consul = cons.Consul(host=c_host)
try:
# for connection test
ind,data = consul.kv.get('mongo_info_coll_name')
break
except:
continue
for const in Cs:
if isinstance(Cs[const], dict):
# if constant is dict will get value
# from consul catalog (service discovery)
d = Cs[const]
ind,res = consul.catalog.service(d['service_name'])
Cs[const] = res[0][d['param_name']]
elif Cs[const] == '':
# constant set as empty string
ind,data = consul.kv.get(const)
Cs[const] = data['Value'].decode('utf-8')
return Cs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment