feat: pass template configuration, map VM sizes, generate secrets

return gitea passwd, git pull before writing and push after add/rm
pull/1/head
Aravinth Manivannan 2022-06-25 18:02:03 +05:30
parent 9af5361f63
commit beb4b29c49
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88
1 changed files with 51 additions and 11 deletions

View File

@ -16,6 +16,7 @@ import os
import shutil import shutil
from pathlib import Path from pathlib import Path
from django.utils.crypto import get_random_string
from django.template.loader import render_to_string from django.template.loader import render_to_string
from django.conf import settings from django.conf import settings
from git import Repo from git import Repo
@ -31,6 +32,7 @@ class Infra:
def __init__(self): def __init__(self):
conf = settings.HOSTEA["INFRA"]["HOSTEA_REPO"] conf = settings.HOSTEA["INFRA"]["HOSTEA_REPO"]
self.hostea_domain = settings.HOSTEA["INFRA"]["HOSTEA_DOMAIN"]
self.repo_path = Path(conf["PATH"]) self.repo_path = Path(conf["PATH"])
if not self.repo_path.exists(): if not self.repo_path.exists():
os.makedirs(self.repo_path) os.makedirs(self.repo_path)
@ -92,9 +94,9 @@ class Infra:
""" """
Add all relevant files of a VM Add all relevant files of a VM
""" """
self.repo.index.add(self._host_vars_dir(subdomain=subdomain)) self.repo.git.add(str(self._host_vars_dir(subdomain=subdomain)))
self.repo.index.add(self._backup_path(subdomain=subdomain)) self.repo.git.add(str(self._backup_path(subdomain=subdomain)))
self.repo.index.add(self._hostscript_path(subdomain=subdomain)) self.repo.git.add(str(self._hostscript_path(subdomain=subdomain)))
def _commit(self, action: str, subdomain: str): def _commit(self, action: str, subdomain: str):
""" """
@ -103,13 +105,18 @@ class Infra:
self._add_files(subdomain=subdomain) self._add_files(subdomain=subdomain)
self.repo.git.commit( self.repo.git.commit(
f"{action} VM {subdomain}", author="bot@dashboard.hostea.org" message=f"{action} VM {subdomain}",
author="Dashboard Bot <bot@dashboard.hostea.org>",
) )
def add_vm(self, instance: Instance): def add_vm(self, instance: Instance) -> str:
""" """
Add new VM to infrastructure repository Add new VM to infrastructure repository
The gitea user password is returned
""" """
self.repo.git.pull()
subdomain = instance.name subdomain = instance.name
host_vars_dir = self._host_vars_dir(subdomain) host_vars_dir = self._host_vars_dir(subdomain)
@ -120,9 +127,26 @@ class Infra:
if not hostscript_path.exists(): if not hostscript_path.exists():
os.makedirs(hostscript_path) os.makedirs(hostscript_path)
woodpecker_agent_secret = get_random_string(64)
gitea_password = get_random_string(20)
ctx = {
"woodpecker_agent_secret": woodpecker_agent_secret,
"woodpecker_hostname": f"{subdomain}-ci",
"woodpecker_admins": f"{instance.owned_by.username}",
"gitea_email": instance.owned_by.email,
"gitea_password": gitea_password,
"subdomain": subdomain,
}
gitea = self._gitea_path(subdomain) gitea = self._gitea_path(subdomain)
gitea_template = "./templates/infrastructure/yml/gitea.yml" with open(gitea, "w", encoding="utf-8") as f:
shutil.copy(gitea_template, gitea) f.write(
render_to_string(
"infrastructure/yml/gitea.yml",
context=ctx,
)
)
# provision_template = "./templates/infrastructure/yml/provision.yml" # provision_template = "./templates/infrastructure/yml/provision.yml"
provision = self._provision_path(subdomain) provision = self._provision_path(subdomain)
@ -131,11 +155,21 @@ class Infra:
# openstack_flavor: {{ openstack_flavor_medium }} * openstack_flavor: {{ openstack_flavor_large }} # openstack_flavor: {{ openstack_flavor_medium }} * openstack_flavor: {{ openstack_flavor_large }}
# ``` # ```
# check with @dachary about this # check with @dachary about this
size = None
if instance.configuration_id.name == "s1-2":
size = "openstack_flavor_small"
elif instance.configuration_id.name == "s1-4":
size = "openstack_flavor_medium"
elif instance.configuration_id.name == "s1-8":
size = "openstack_flavor_large"
else:
size = instance.configuration_id.name
with open(provision, "w", encoding="utf-8") as f: with open(provision, "w", encoding="utf-8") as f:
f.write( f.write(
render_to_string( render_to_string(
"infrastructure/yml/provision.yml", "infrastructure/yml/provision.yml", context={"vm_size": size}
context={"vm_size": instance.instance_id.name},
) )
) )
@ -154,16 +188,20 @@ class Infra:
self.write_hostscript( self.write_hostscript(
subdomain=subdomain, subdomain=subdomain,
content=render_to_string( content=render_to_string(
"infrastructure/sh/create.sh", context={"subdomain": subdomain} "infrastructure/sh/hostscripts/create.sh",
context={"subdomain": subdomain, "hostea_domain": self.hostea_domain},
), ),
) )
self._commit(action="add", subdomain=subdomain) self._commit(action="add", subdomain=subdomain)
self.repo.git.push()
return gitea_password
def remove_vm(self, instance: Instance): def remove_vm(self, instance: Instance):
""" """
Remove a VM from infrastructure repository Remove a VM from infrastructure repository
""" """
self.repo.git.pull()
subdomain = instance.name subdomain = instance.name
host_vars_dir = self._host_vars_dir(subdomain) host_vars_dir = self._host_vars_dir(subdomain)
@ -175,7 +213,9 @@ class Infra:
self.write_hostscript( self.write_hostscript(
subdomain=subdomain, subdomain=subdomain,
content=render_to_string( content=render_to_string(
"infrastructure/sh/rm.sh", context={"subdomain": subdomain} "infrastructure/sh/hostscripts/create.sh",
context={"subdomain": subdomain, "hostea_domain": self.hostea_domain},
), ),
) )
self._commit(action="rm", subdomain=subdomain) self._commit(action="rm", subdomain=subdomain)
self.repo.git.push()