2022-06-24 15:04:11 +00:00
|
|
|
# Copyright © 2022 Aravinth Manivannan <realaravinth@batsense.net>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
from django.shortcuts import render, redirect, get_object_or_404
|
|
|
|
from django.utils.http import urlencode
|
|
|
|
from django.contrib.auth import authenticate, login, logout
|
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
from django.http import HttpResponse
|
2022-07-04 09:27:50 +00:00
|
|
|
from django.template.loader import render_to_string
|
2022-06-24 15:04:11 +00:00
|
|
|
from django.views.decorators.csrf import csrf_protect
|
2022-06-28 19:19:58 +00:00
|
|
|
from django.core.mail import send_mail
|
2022-07-04 09:27:50 +00:00
|
|
|
from django.conf import settings
|
2022-06-24 15:04:11 +00:00
|
|
|
from django.urls import reverse
|
|
|
|
|
|
|
|
from accounts.decorators import confirm_access
|
|
|
|
from dash.models import Instance
|
2022-06-25 12:33:04 +00:00
|
|
|
from billing.utils import payment_fullfilled
|
2022-06-24 15:04:11 +00:00
|
|
|
|
2022-07-01 14:24:20 +00:00
|
|
|
from .utils import create_vm_if_not_exists, Infra, delete_vm
|
2022-06-24 15:04:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
def default_ctx(title: str, username: str):
|
|
|
|
"""
|
|
|
|
Default context for all dashboard pages
|
|
|
|
"""
|
|
|
|
return {
|
|
|
|
"title": title,
|
|
|
|
"username": username,
|
|
|
|
"open_instances": "open",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def create_instance(request, instance_name: str):
|
|
|
|
"""
|
|
|
|
Dashboard homepage view
|
|
|
|
"""
|
|
|
|
instance = get_object_or_404(Instance, name=instance_name, owned_by=request.user)
|
2022-06-25 12:33:04 +00:00
|
|
|
if not payment_fullfilled(instance=instance):
|
|
|
|
return redirect(reverse("billing.invoice.generate", args=(instance_name,)))
|
|
|
|
|
2022-06-28 19:19:58 +00:00
|
|
|
res = create_vm_if_not_exists(instance=instance)
|
|
|
|
if res is not None:
|
|
|
|
(gitea_password, commit) = res
|
|
|
|
|
|
|
|
ctx = {
|
2022-07-04 09:27:50 +00:00
|
|
|
"username": request.user.username,
|
2022-06-28 19:19:58 +00:00
|
|
|
"gitea_password": gitea_password,
|
2022-06-29 05:35:33 +00:00
|
|
|
"gitea_uri": Infra.get_gitea_uri(instance=instance),
|
2022-06-28 19:19:58 +00:00
|
|
|
}
|
2022-07-04 09:27:50 +00:00
|
|
|
|
|
|
|
body = render_to_string(
|
|
|
|
"infrastructure/emails/gitea-creds.txt",
|
|
|
|
context=ctx,
|
|
|
|
)
|
|
|
|
|
2022-07-04 10:37:30 +00:00
|
|
|
sender = settings.DEFAULT_FROM_EMAIL
|
2022-07-04 09:27:50 +00:00
|
|
|
|
|
|
|
send_mail(
|
2022-09-12 11:06:29 +00:00
|
|
|
subject="[Gna!] Gitea admin credentials",
|
2022-07-04 09:27:50 +00:00
|
|
|
message=body,
|
2022-09-12 11:06:29 +00:00
|
|
|
from_email=f"No reply Gna!<{sender}>", # TODO read from settings.py
|
2022-07-04 09:27:50 +00:00
|
|
|
recipient_list=[request.user.email],
|
|
|
|
)
|
|
|
|
|
2022-06-28 19:19:58 +00:00
|
|
|
return render(request, "infrastructure/html/create.html", ctx)
|
2022-06-24 15:04:11 +00:00
|
|
|
return HttpResponse()
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
@confirm_access
|
|
|
|
def delete_instance(request, instance_name: str):
|
|
|
|
"""
|
|
|
|
Dashboard homepage view
|
|
|
|
"""
|
|
|
|
instance = get_object_or_404(Instance, name=instance_name, owned_by=request.user)
|
2022-07-01 14:24:20 +00:00
|
|
|
delete_vm(instance=instance)
|
|
|
|
return redirect(reverse("dash.instances.list"))
|