Verified Commit b83324eb authored by Aleksey Shirokih's avatar Aleksey Shirokih
Browse files

Minor refactor

parent fd02727d
Pipeline #21054 passed with stages
in 3 minutes and 31 seconds
......@@ -176,32 +176,7 @@ class Environment(Model):
has_cert = False
certificate = {}
for s in srv_descr:
if "require_cert" in srv_descr[s] and srv_descr[s]["require_cert"]:
srs = Service.select().where(Service.service == s)
for line in srs:
ln = json.loads(line.config)
if not ln["cert"]:
if line.present:
need_cert.append(line)
else:
has_cert = True
certificate[s] = {
"key": ln["cert_key"],
"cert": ln["cert"]
}
if not has_cert and need_cert:
key, cert = self.generate_certificate()
certificate[s] = {
"key": key,
"cert": cert
}
for n in need_cert:
conf = json.loads(n.config)
conf["cert"] = certificate[s]["cert"]
conf["cert_key"] = certificate[s]["key"]
n.config = json.dumps(conf.decode("utf-8"), sort_keys=True)
n.save()
self.update_certs(certificate, has_cert, need_cert, s, srv_descr)
for srv in self.get_service_config():
# do not work with stale or old services
......@@ -258,40 +233,73 @@ class Environment(Model):
r["svc-%s-read" % dep]["hosts"].append(srv['node'])
# Generate tower.yml
if "category" in srv_descr[srv['service']] and srv_descr[srv['service']]["category"] == "internal":
node_noc_config = "noc-config-%s" % srv['node']
if node_noc_config not in r:
r[node_noc_config] = {
"hosts": [srv['node']],
"vars": {
"noc_services": []
}
self.generate_tower_inventory(pool_name, r, srv, srv_descr)
return r
def generate_tower_inventory(self, pool_name, r, srv, srv_descr):
if "category" in srv_descr[srv['service']] and srv_descr[srv['service']]["category"] == "internal":
node_noc_config = "noc-config-%s" % srv['node']
if node_noc_config not in r:
r[node_noc_config] = {
"hosts": [srv['node']],
"vars": {
"noc_services": []
}
line = {
"name": srv['service'],
"config": srv['config'],
"pool": pool_name,
"environment": srv_descr[srv['service']]["environment"].copy()
}
# append pool configuration file to config string
if srv_descr[srv['service']]["level"] == "pool":
order = self.config_order.split(",")
for conf in order:
if 'yaml://' in conf:
path = urlparse(conf).path
basepath = os.path.dirname(path)
pool_config_path = "yaml://" + str(os.path.join(basepath, 'pool-%s.yml' % srv['pool']))
order.insert(-1, pool_config_path)
break
pooled_order = ",".join(order)
line["config_order"] = pooled_order
line = {
"name": srv['service'],
"config": srv['config'],
"pool": pool_name,
"environment": srv_descr[srv['service']]["environment"].copy()
}
# append pool configuration file to config string
if srv_descr[srv['service']]["level"] == "pool":
order = self.config_order.split(",")
for conf in order:
if 'yaml://' in conf:
path = urlparse(conf).path
basepath = os.path.dirname(path)
pool_config_path = "yaml://" + str(os.path.join(basepath, 'pool-%s.yml' % srv['pool']))
order.insert(-1, pool_config_path)
break
pooled_order = ",".join(order)
line["config_order"] = pooled_order
else:
line["config_order"] = self.config_order
if "description" in line["environment"]:
del line["environment"]["description"]
r[node_noc_config]["vars"]["noc_services"].append(line)
def update_certs(self, certificate, has_cert, need_cert, s, srv_descr):
from .service import Service
if "require_cert" in srv_descr[s] and srv_descr[s]["require_cert"]:
srs = Service.select().where(Service.service == s)
for line in srs:
ln = json.loads(line.config)
if not ln["cert"]:
if line.present:
need_cert.append(line)
else:
line["config_order"] = self.config_order
if "description" in line["environment"]:
del line["environment"]["description"]
r[node_noc_config]["vars"]["noc_services"].append(line)
has_cert = True
certificate[s] = {
"key": ln["cert_key"],
"cert": ln["cert"]
}
if not has_cert and need_cert:
key, cert = self.generate_certificate()
certificate[s] = {
"key": key,
"cert": cert
}
return r
for n in need_cert:
conf = json.loads(n.config)
conf["cert"] = certificate[s]["cert"]
conf["cert_key"] = certificate[s]["key"]
n.config = json.dumps(conf.decode("utf-8"), sort_keys=True)
n.save()
@staticmethod
def name_config(config, service):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment