Commit 15645bfa authored by Aleksey Shirokih's avatar Aleksey Shirokih

Introduce role_name field

Role_name have to be differed from Service name cause ansible will search role by static name
parent b616b39e
......@@ -220,7 +220,15 @@ class ServiceAPI(API):
# speedup orm
srv_list = db.execute_sql(
'SELECT id,service,pool_id,node_id, config, present FROM service WHERE environment_id=? ORDER BY service',
'SELECT\n'
' s.id,service,pool_id,node_id, config, present\n'
'FROM\n'
' service s\n'
' left JOIN role r on s.service==r.role_name\n'
'WHERE\n'
' s.environment_id=?\n'
' and (r.is_enabled=1 or r.is_enabled is null)\n'
'ORDER BY s.service\n',
env_id)
for srv in srv_list:
try:
......
......@@ -27,5 +27,6 @@ def migrate(migrator):
link = CharField()
environment = ForeignKeyField(Environment, on_delete="RESTRICT")
is_enabled = BooleanField(default=False)
role_name = CharField()
migrator.create_table(Role)
......@@ -31,7 +31,8 @@ DEFAULT_ROLES = [
"name": "Alerta notifications",
"description": "Notifies about deploy to deploy system",
"link": "git+https://code.getnoc.com/ansible-roles/ansible-role-alerta-notifications.git",
"is_enabled": False
"is_enabled": False,
"role_name": "deploy_notifications"
},
{
"name": "Telegraf",
......@@ -67,6 +68,7 @@ def migrate(migrator):
link = CharField()
environment = ForeignKeyField(Environment, on_delete="RESTRICT")
is_enabled = BooleanField(default=False)
role_name = CharField()
class Node(Model):
class Meta:
......@@ -120,7 +122,8 @@ def migrate(migrator):
description=role["description"],
link=role["link"],
environment=env,
is_enabled=role["is_enabled"]
is_enabled=role["is_enabled"],
role_name=role.get("role_name", role["name"].lower())
).save()
if "telegraf" in role["name"].lower():
for n in Node.select().where(Node.environment == env):
......@@ -139,13 +142,3 @@ def migrate(migrator):
}
env.service_config = yaml.dump(config)
env.save()
# for n in Node.select().where(Node.environment == env):
# Service.get_or_create(
# environment=env.id,
# service="noc",
# pool=None,
# node=n.id,
# n_instances=1,
# n_backup_instances=0,
# loglevel="info"
# ).save()
......@@ -310,10 +310,16 @@ class Environment(Model):
pools[p.id] = p.name
srv_list = db.execute_sql(
'SELECT id,service,pool_id,node_id, config '
'FROM service '
'WHERE environment_id=? AND present=1 '
'ORDER BY service ', str(self.id))
'SELECT\n'
' s.id,service,pool_id,node_id, config, present\n'
'FROM\n'
' service s\n'
' left JOIN role r on s.service==r.role_name\n'
'WHERE\n'
' s.environment_id=?\n'
' and (r.is_enabled=1 or r.is_enabled is null)\n'
'ORDER BY s.service\n',
str(self.id))
for srv in srv_list:
try:
r.append({
......@@ -340,22 +346,35 @@ class Environment(Model):
@property
def services_path(self):
from .role import Role
r = glob.glob(
os.path.abspath(
os.path.join(
"var", "tower", "playbooks",
self.name,
"*_roles",
"noc_roles",
"*",
"meta", "tower.yml"
)
)
)
r.extend(
glob.glob(os.path.join(
self.roles_prefix, "*", "meta", "tower.yml"
))
)
r.extend(glob.glob(
os.path.abspath(
os.path.join(
"var", "tower", "playbooks",
self.name,
"system_roles",
"*",
"meta", "tower.yml"
)
)
))
for role in Role.select().where(Role.environment == self, Role.is_enabled == True): # noqa
r.append(
os.path.join(
self.roles_prefix, role.role_name, "meta", "tower.yml"
)
)
return r
@property
......
......@@ -42,6 +42,7 @@ DEFAULT_ROLES = [
"name": "Alerta notifications",
"description": "Notifies about deploy to deploy system",
"link": "git+https://code.getnoc.com/ansible-roles/ansible-role-alerta-notifications.git",
"role_name": "deploy_notification"
},
{
"name": "Telegraf",
......@@ -66,6 +67,7 @@ class Role(Model):
link = CharField()
environment = ForeignKeyField(Environment, on_delete="RESTRICT")
is_enabled = BooleanField(default=False)
role_name = CharField()
def list_item(self):
return {
......@@ -73,7 +75,8 @@ class Role(Model):
"name": self.name,
"description": self.description,
"link": self.link,
"is_enabled": self.is_enabled
"is_enabled": self.is_enabled,
"role_name": self.role_name
}
def reference_item(self):
......@@ -87,18 +90,29 @@ class Role(Model):
shutil.rmtree(self.role_path)
def save(self, *args, **kwargs):
from tower.api.pull import PullAPI
for attr in self.dirty_fields:
if attr.name == 'link':
self.remove_role_dir()
if self.is_enabled:
PullAPI.pull(self.link, self.role_path)
elif attr.name == 'is_enabled' and not self.is_enabled:
self.remove_role_dir()
elif attr.name == 'is_enabled' and self.is_enabled:
PullAPI.pull(self.link, self.role_path)
return super(Role, self).save(*args, **kwargs)
def delete_instance(self, *args, **kwargs):
from tower.models.service import Service
for srv in Service.select().where(Service.environment == self.environment, Service.service == self.name):
srv.delete_instance()
self.remove_role_dir()
return super(Role, self).delete_instance(*args, **kwargs)
@property
def role_path(self):
return os.path.abspath(os.path.join(self.environment.roles_prefix, self.name.lower()))
return os.path.abspath(os.path.join(self.environment.roles_prefix, self.role_name))
@post_save(sender=Environment)
......@@ -110,5 +124,6 @@ def on_save_environment_new(sender, instance, created):
name=role["name"],
description=role["description"],
link=role["link"],
environment=instance
environment=instance,
role_name=role.get("role_name", role["name"].lower()),
).save()
......@@ -30,9 +30,13 @@ var role_list = {
columns: [
{
id: "name",
header: "Role",
width: 100,
sort: "server"
header: "Name",
width: 150
},
{
id: "role_name",
header: "Role Name",
width: 150
},
{
id: "is_enabled",
......@@ -47,7 +51,7 @@ var role_list = {
{
id: "link",
header: "Link",
width: 250,
width: 250
}
],
on: {
......@@ -104,9 +108,18 @@ var role_form = {
name: "name",
label: "Name",
required: true,
placeholder: "Role name",
placeholder: "Name",
invalidMessage: "Cannot be empty"
},
{
view: "text",
name: "role_name",
label: "Role name",
required: true,
placeholder: "Role name",
invalidMessage: "Cannot be empty",
bottomLabel: "Role name that will be known to Ansible"
},
{
view: "checkbox",
name: "is_enabled",
......
Markdown is supported
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