Task #9611: Add support for writing DNS entries matrix.ungleich.cloud and matrix.0co2.cloud

This commit is contained in:
amalelshihaby 2021-08-09 09:43:11 +02:00
commit 7986b825a7
31 changed files with 626 additions and 478 deletions

View file

@ -1,9 +1,10 @@
import logging
import uuid
import os
import json
import sys
import gitlab
from jinja2 import Environment, FileSystemLoader
import yaml
from django.db import models
from django.conf import settings
@ -21,7 +22,11 @@ class VMInstance(models.Model):
on_delete=models.CASCADE,
editable=True)
vm_id = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
vm_name = models.CharField(max_length=253, editable=False, unique=True)
homeserver_domain = models.CharField(max_length=253, unique=True, blank=True)
webclient_domain = models.CharField(max_length=253, unique=True, blank=True)
config = models.JSONField(null=False, blank=False)
@ -32,24 +37,18 @@ class VMInstance(models.Model):
termination_date = models.DateTimeField(blank=True, null=True)
def save(self, *args, **kwargs):
# Read the deployment yaml file and render the template
# Then save it as new yaml file and push it to github repo
# Save it as new yaml file and push it to github repo
if 'test' in sys.argv:
return super().save(*args, **kwargs)
template_dir = os.path.join(os.path.dirname(__file__), 'yaml')
env = Environment(loader = FileSystemLoader(template_dir),autoescape = True)
tmpl = env.get_template('deployment.yaml.tmpl')
result = tmpl.render(
name=self.vm_id
)
result = yaml.dump(self.config)
gl = gitlab.Gitlab(settings.GITLAB_SERVER, oauth_token=settings.GITLAB_OAUTH_TOKEN)
project = gl.projects.get(settings.GITLAB_PROJECT_ID)
project.files.create({'file_path': settings.GITLAB_YAML_DIR + f'{self.vm_id}.yaml',
project.files.create({'file_path': settings.GITLAB_YAML_DIR + f'matrix-{self.vm_name}.yaml',
'branch': 'master',
'content': result,
'author_email': settings.GITLAB_AUTHOR_EMAIL,
'author_name': settings.GITLAB_AUTHOR_NAME,
'commit_message': f'Add New Deployment for {self.vm_id}'})
'commit_message': f'Add New Deployment for matrix-{self.vm_name}'})
super().save(*args, **kwargs)
def delete(self, *args, **kwargs):
@ -59,11 +58,11 @@ class VMInstance(models.Model):
return super().delete(*args, **kwargs)
gl = gitlab.Gitlab(settings.GITLAB_SERVER, oauth_token=settings.GITLAB_OAUTH_TOKEN)
project = gl.projects.get(settings.GITLAB_PROJECT_ID)
f_path = settings.GITLAB_YAML_DIR + f'{self.vm_id}.yaml'
f_path = settings.GITLAB_YAML_DIR + f'matrix-{self.vm_name}.yaml'
file = project.files.get(file_path=f_path, ref='master')
if file:
project.files.delete(file_path=f_path,
commit_message=f'Delete {self.vm_id}', branch='master',
commit_message=f'Delete matrix-{self.vm_name}', branch='master',
author_email=settings.GITLAB_AUTHOR_EMAIL,
author_name=settings.GITLAB_AUTHOR_NAME)