Refactoring template.py code
This commit is contained in:
parent
e2fa6afacb
commit
a105eea090
|
|
@ -5,7 +5,7 @@ import settings
|
||||||
from danixfs import Danix
|
from danixfs import Danix
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from settings import SNAPSHOT_LIMIT
|
from settings import SNAPSHOT_LIMIT
|
||||||
from utils import is_unique_database_tuple, get_message, check_equal_sentence, check_not_equal_sentence
|
from utils import print_snapshot_list_header, print_footer, print_environment_list_header, is_unique_database_tuple, get_message, check_equal_sentence, check_not_equal_sentence
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
|
|
||||||
def get_queryset_filtered(model, sub_attribute):
|
def get_queryset_filtered(model, sub_attribute):
|
||||||
|
|
@ -184,9 +184,7 @@ class Environment(models.Model):
|
||||||
environments = Environment.objects.all()
|
environments = Environment.objects.all()
|
||||||
environment_counter = environments.count()
|
environment_counter = environments.count()
|
||||||
|
|
||||||
print("===============================================================================================================================================")
|
print_environment_list_header()
|
||||||
print("| ENVIRONMENT NAME | TEMPLATE | CREATED | SUBSYSTEM NAME | IMAGE | STATUS | SIZE |")
|
|
||||||
print("|=============================================================================================================================================|")
|
|
||||||
|
|
||||||
if environment_counter > 0:
|
if environment_counter > 0:
|
||||||
|
|
||||||
|
|
@ -197,11 +195,16 @@ class Environment(models.Model):
|
||||||
status_icon = "🟢 Running" if environment.status else "🔴 Stopped"
|
status_icon = "🟢 Running" if environment.status else "🔴 Stopped"
|
||||||
template = str(environment.template)
|
template = str(environment.template)
|
||||||
|
|
||||||
size = Danix.get_size(environment.filesystem_name, None)
|
size_str = str(Danix.get_size(environment.filesystem_name, None))
|
||||||
|
|
||||||
print(f"| {name[0:11]}{(11-len(name)) * '.'} {template}{(6-len(template)) * ' '} {environment.created} {environment.filesystem_name} Alpine {status_icon} {size}B |")
|
size = int(size_str.replace("M",""))
|
||||||
|
|
||||||
print("===============================================================================================================================================")
|
if size >= 1000:
|
||||||
|
size_str = f"{round(size/1000, 1)}G"
|
||||||
|
|
||||||
|
print(f"| {name[0:11]}{(11-len(name)) * '.'} {template}{(6-len(template)) * ' '} {environment.created} {environment.filesystem_name} Alpine {status_icon} {size_str}B |")
|
||||||
|
|
||||||
|
print_footer()
|
||||||
|
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
|
|
@ -339,9 +342,7 @@ class Snapshot(models.Model):
|
||||||
|
|
||||||
snapshots = Snapshot.objects.all()
|
snapshots = Snapshot.objects.all()
|
||||||
|
|
||||||
print("===========================================================================================================================================")
|
print_snapshot_list_header()
|
||||||
print("| SNAPSHOT NAME | ENVIRONMENT NAME | CREATED | LAST SNAPSHOT | SIZE |")
|
|
||||||
print("|==========================================================================================================================================|")
|
|
||||||
|
|
||||||
if snapshots.count() > 0:
|
if snapshots.count() > 0:
|
||||||
for snapshot in snapshots:
|
for snapshot in snapshots:
|
||||||
|
|
@ -352,14 +353,22 @@ class Snapshot(models.Model):
|
||||||
if snapshot.environment_id:
|
if snapshot.environment_id:
|
||||||
|
|
||||||
environment_name = Environment.objects.filter(id=snapshot.environment_id.id).first().filesystem_name
|
environment_name = Environment.objects.filter(id=snapshot.environment_id.id).first().filesystem_name
|
||||||
size = Danix.get_size(environment_name, name)
|
size_str = str(Danix.get_size(environment_name, name))
|
||||||
|
|
||||||
|
size = int(size_str.replace("M",""))
|
||||||
|
|
||||||
|
if size >= 1000:
|
||||||
|
size_str = f"{round(size/1000, 1)}G"
|
||||||
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
||||||
environment_name = f'Environment Removed 🔴{14*" "}'
|
environment_name = f'Environment Removed 🔴{14*" "}'
|
||||||
size = "---"
|
size = "---"
|
||||||
|
|
||||||
print(f"| {name} {environment_name} {snapshot.created} {lastsnapshot_icon} {size}B |")
|
print(f"| {name} {environment_name} {snapshot.created} {lastsnapshot_icon} {size_str}B |")
|
||||||
print("===========================================================================================================================================")
|
|
||||||
|
print_footer()
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
db_table = "snapshot"
|
db_table = "snapshot"
|
||||||
|
|
|
||||||
|
|
@ -1,103 +1,102 @@
|
||||||
import uuid, app
|
import uuid, app
|
||||||
from db.models import Environment
|
from db.models import Environment
|
||||||
from danixfs import Danix
|
from danixfs import Danix
|
||||||
|
from utils import check_equal_sentence
|
||||||
class Essentials():
|
class Essentials():
|
||||||
|
|
||||||
packages = ["vim", "nano", "micro", "git"]
|
packages = ["build-base", "vim", "nano", "micro", "git"]
|
||||||
|
|
||||||
class Languanges():
|
|
||||||
|
|
||||||
|
class Template():
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def install(packages, environment_name, template):
|
||||||
|
|
||||||
|
filesystem_name = uuid.uuid4()
|
||||||
|
|
||||||
|
environment_count = Environment.objects.filter(
|
||||||
|
filesystem_name=filesystem_name
|
||||||
|
).count()
|
||||||
|
|
||||||
|
if check_equal_sentence(environment_count, 0):
|
||||||
|
|
||||||
|
Environment.objects.create(
|
||||||
|
filesystem_name=filesystem_name,
|
||||||
|
template=template,
|
||||||
|
name=environment_name).save()
|
||||||
|
|
||||||
|
joined_packages = Template.menu(packages)
|
||||||
|
|
||||||
|
Environment.set_active(filesystem_name)
|
||||||
|
Danix.build_environment(joined_packages, filesystem_name)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def languange_menu(packages):
|
||||||
|
i = 1
|
||||||
|
|
||||||
|
for package in packages:
|
||||||
|
print(f"[{i}] - Package {package}")
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
print('Select essentials packages:')
|
||||||
|
print(f'Example: 1 2 to install {packages[0]}, {packages[1]}')
|
||||||
|
packages_selected = input()
|
||||||
|
|
||||||
|
list_selected_packages = []
|
||||||
|
|
||||||
|
for pos in packages_selected.split(" "):
|
||||||
|
list_selected_packages.append(packages[int(pos)-1])
|
||||||
|
|
||||||
|
return list_selected_packages
|
||||||
|
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def essentials_menu():
|
||||||
|
essentials_packages = Essentials().packages
|
||||||
|
|
||||||
|
i = 1
|
||||||
|
|
||||||
|
for essential in essentials_packages:
|
||||||
|
|
||||||
|
print(f'[{i}] - Package {essential}')
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
print('Select essentials packages:')
|
||||||
|
print(f'Example: 1 2 3 to install {essentials_packages[0]}, {essentials_packages[1]}, and {essentials_packages[2]}')
|
||||||
|
essentials_packages_selected = input()
|
||||||
|
|
||||||
|
list_essentials_packages = []
|
||||||
|
|
||||||
|
for pos in essentials_packages_selected.split(" "):
|
||||||
|
list_essentials_packages.append(essentials_packages[int(pos)-1])
|
||||||
|
|
||||||
|
return list_essentials_packages
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def menu(packages):
|
||||||
|
return Template.essentials_menu() + Template.languange_menu(packages)
|
||||||
|
|
||||||
|
class Languanges():
|
||||||
class Python():
|
class Python():
|
||||||
|
|
||||||
packages = ["python3", "py3-pip"]
|
packages = ["python3", "py3-pip"]
|
||||||
|
|
||||||
def install(self, environment_name, template):
|
def install(self, environment_name, template):
|
||||||
|
|
||||||
packages = input("")
|
Template.install(self.packages, environment_name, template)
|
||||||
filesystem_name = uuid.uuid4()
|
|
||||||
|
|
||||||
environment_count = Environment.objects.filter(filesystem_name=filesystem_name).count()
|
|
||||||
|
|
||||||
if environment_count == 0:
|
|
||||||
Environment.objects.create(filesystem_name=filesystem_name, template=template, name=environment_name).save()
|
|
||||||
|
|
||||||
joined_packages = self.packages + Essentials().packages
|
|
||||||
|
|
||||||
Environment.set_active(filesystem_name)
|
|
||||||
Danix.build_environment(joined_packages, filesystem_name)
|
|
||||||
|
|
||||||
class CLike():
|
class CLike():
|
||||||
|
|
||||||
packages = ["gcc", "g++", "clang", "rust cargo"]
|
packages = ["gcc", "g++", "clang", "rust cargo"]
|
||||||
|
|
||||||
def select_packages(self):
|
|
||||||
|
|
||||||
essentials_packages = Essentials().packages
|
|
||||||
|
|
||||||
i = 1
|
|
||||||
|
|
||||||
for essential in essentials_packages:
|
|
||||||
|
|
||||||
print(f'[{i}] - Package {essential}')
|
|
||||||
i += 1
|
|
||||||
|
|
||||||
print('Select essentials packages:')
|
|
||||||
print('Example: 1 2 3 to install vim, nano, and git')
|
|
||||||
essentials_packages_selected = input()
|
|
||||||
|
|
||||||
list_essentials_packages = []
|
|
||||||
|
|
||||||
for pos in essentials_packages_selected.split(" "):
|
|
||||||
list_essentials_packages.append(essentials_packages[int(pos)-1])
|
|
||||||
|
|
||||||
i = 1
|
|
||||||
|
|
||||||
for package in self.packages:
|
|
||||||
print(f"[{i}] - Package {package}")
|
|
||||||
i += 1
|
|
||||||
|
|
||||||
print('Select essentials packages:')
|
|
||||||
print('Example: 1 2 to install gcc, g++')
|
|
||||||
clike_packages_selected = input()
|
|
||||||
|
|
||||||
list_clike_packages = []
|
|
||||||
|
|
||||||
for pos in clike_packages_selected.split(" "):
|
|
||||||
list_clike_packages.append(self.packages[int(pos)-1])
|
|
||||||
|
|
||||||
return list_essentials_packages + list_clike_packages
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def install(self, environment_name, template):
|
def install(self, environment_name, template):
|
||||||
filesystem_name = uuid.uuid4()
|
Template.install(self.packages, environment_name, template)
|
||||||
|
|
||||||
environment_count = Environment.objects.filter(filesystem_name=filesystem_name).count()
|
|
||||||
|
|
||||||
if environment_count == 0:
|
|
||||||
|
|
||||||
Environment.objects.create(filesystem_name=filesystem_name, template=template, name=environment_name).save()
|
|
||||||
|
|
||||||
joined_packages = self.select_packages()
|
|
||||||
|
|
||||||
Environment.set_active(filesystem_name)
|
|
||||||
Danix.build_environment(joined_packages, filesystem_name)
|
|
||||||
|
|
||||||
class Java():
|
class Java():
|
||||||
|
|
||||||
packages = ["openjdk8", "openjdk11", "openjdk17"]
|
packages = ["openjdk8", "openjdk11", "openjdk17"]
|
||||||
|
|
||||||
def install(self, environment_name, template):
|
def install(self, environment_name, template):
|
||||||
filesystem_name = uuid.uuid4()
|
Template.install(self.packages, environment_name, template)
|
||||||
|
|
||||||
environment_count = Environment.objects.filter(filesystem_name=filesystem_name).count()
|
|
||||||
|
|
||||||
if environment_count == 0:
|
|
||||||
|
|
||||||
Environment.objects.create(filesystem_name=filesystem_name, template=template, name=environment_name).save()
|
|
||||||
|
|
||||||
joined_packages = self.packages + Essentials().packages
|
|
||||||
|
|
||||||
Environment.set_active(filesystem_name)
|
|
||||||
Danix.build_environment(joined_packages, filesystem_name)
|
|
||||||
|
|
@ -1,5 +1,22 @@
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def print_footer():
|
||||||
|
print("================================================================================================================================================")
|
||||||
|
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def print_snapshot_list_header():
|
||||||
|
print("================================================================================================================================================")
|
||||||
|
print("| SNAPSHOT NAME | ENVIRONMENT NAME | CREATED | LAST SNAPSHOT | SIZE |")
|
||||||
|
print("|==============================================================================================================================================|")
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def print_environment_list_header():
|
||||||
|
print("================================================================================================================================================")
|
||||||
|
print("| ENVIRONMENT NAME | TEMPLATE | CREATED | SUBSYSTEM NAME | IMAGE | STATUS | SIZE |")
|
||||||
|
print("|==============================================================================================================================================|")
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def check_equal_sentence(left_expression, right_expression):
|
def check_equal_sentence(left_expression, right_expression):
|
||||||
return left_expression == right_expression
|
return left_expression == right_expression
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue