2021-07-19 14:36:10 +00:00
|
|
|
import logging
|
|
|
|
import uuid
|
|
|
|
import os
|
2021-08-09 07:43:11 +00:00
|
|
|
import json
|
2021-07-19 14:36:10 +00:00
|
|
|
import sys
|
|
|
|
import gitlab
|
2021-08-09 07:43:11 +00:00
|
|
|
import yaml
|
2021-07-19 14:36:10 +00:00
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
from django.conf import settings
|
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
from django.template.loader import render_to_string
|
|
|
|
|
|
|
|
from uncloud_pay.models import Order
|
|
|
|
|
|
|
|
|
|
|
|
# Initialize logger.
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
class VMInstance(models.Model):
|
|
|
|
owner = models.ForeignKey(get_user_model(),
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
editable=True)
|
|
|
|
|
2021-08-09 07:43:11 +00:00
|
|
|
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)
|
2021-07-19 14:36:10 +00:00
|
|
|
|
|
|
|
config = models.JSONField(null=False, blank=False)
|
|
|
|
|
|
|
|
order = models.OneToOneField(Order, on_delete=models.CASCADE, related_name='instance_id')
|
|
|
|
|
|
|
|
creation_date = models.DateTimeField(auto_now_add=True)
|
|
|
|
|
|
|
|
termination_date = models.DateTimeField(blank=True, null=True)
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
2021-08-09 07:43:11 +00:00
|
|
|
# Save it as new yaml file and push it to github repo
|
2021-07-19 14:36:10 +00:00
|
|
|
if 'test' in sys.argv:
|
|
|
|
return super().save(*args, **kwargs)
|
2021-08-09 07:43:11 +00:00
|
|
|
result = yaml.dump(self.config)
|
2021-07-19 14:36:10 +00:00
|
|
|
gl = gitlab.Gitlab(settings.GITLAB_SERVER, oauth_token=settings.GITLAB_OAUTH_TOKEN)
|
|
|
|
project = gl.projects.get(settings.GITLAB_PROJECT_ID)
|
2021-08-09 07:43:11 +00:00
|
|
|
project.files.create({'file_path': settings.GITLAB_YAML_DIR + f'matrix-{self.vm_name}.yaml',
|
2021-07-19 14:36:10 +00:00
|
|
|
'branch': 'master',
|
|
|
|
'content': result,
|
|
|
|
'author_email': settings.GITLAB_AUTHOR_EMAIL,
|
|
|
|
'author_name': settings.GITLAB_AUTHOR_NAME,
|
2021-08-09 07:43:11 +00:00
|
|
|
'commit_message': f'Add New Deployment for matrix-{self.vm_name}'})
|
2021-07-19 14:36:10 +00:00
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
|
|
|
def delete(self, *args, **kwargs):
|
|
|
|
# Delete the deployment yaml file first then
|
|
|
|
# Then delete it
|
|
|
|
if 'test' in sys.argv:
|
|
|
|
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)
|
2021-08-09 07:43:11 +00:00
|
|
|
f_path = settings.GITLAB_YAML_DIR + f'matrix-{self.vm_name}.yaml'
|
2021-07-19 14:36:10 +00:00
|
|
|
file = project.files.get(file_path=f_path, ref='master')
|
|
|
|
if file:
|
|
|
|
project.files.delete(file_path=f_path,
|
2021-08-09 07:43:11 +00:00
|
|
|
commit_message=f'Delete matrix-{self.vm_name}', branch='master',
|
2021-07-19 14:36:10 +00:00
|
|
|
author_email=settings.GITLAB_AUTHOR_EMAIL,
|
|
|
|
author_name=settings.GITLAB_AUTHOR_NAME)
|
|
|
|
|
|
|
|
super().delete(*args, **kwargs)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return f"{self.id}-{self.order}"
|
|
|
|
|
|
|
|
def delete_for_bill(self, bill):
|
|
|
|
#TODO delete related instances
|
|
|
|
return True
|