Create makefile

This commit is contained in:
Vinicius Silva 2023-05-16 20:55:20 -03:00
parent b0ebb5d503
commit ca88d352e2
5 changed files with 75 additions and 19 deletions

View File

@ -1,4 +1,5 @@
import uuid, os, settings, app import uuid, os, settings, app
from sh import du
from settings import MAIN_REPO from settings import MAIN_REPO
from time import sleep from time import sleep
@ -12,11 +13,21 @@ class Danix():
def rm(filesystem_name): def rm(filesystem_name):
return os.system(f"rm -r {MAIN_REPO}{filesystem_name}") return os.system(f"rm -r {MAIN_REPO}{filesystem_name}")
@staticmethod
def get_size(environment_name, snapshot_name):
try:
if snapshot_name is None:
return du(f'{MAIN_REPO}{environment_name}/','-ch').split('\n')[-2].split('\t')[0]
else:
return du(f'{MAIN_REPO}.snapshots/{snapshot_name}/{environment_name}.tar.gz','-ch').split('\n')[-2].split('\t')[0]
except Exception:
return "0M"
@staticmethod @staticmethod
def make_snapshot(filesystem_name, snapshot_name): def make_snapshot(filesystem_name, snapshot_name):
os.system(f"mkdir {MAIN_REPO}.snapshots/{snapshot_name}") os.system(f"mkdir {MAIN_REPO}.snapshots/{snapshot_name} >/dev/null 2>&1")
resp = os.system(f"tar -czf {MAIN_REPO}.snapshots/{snapshot_name}/{filesystem_name}.tar.gz {MAIN_REPO}{filesystem_name}/") resp = os.system(f"tar czf {MAIN_REPO}.snapshots/{snapshot_name}/{filesystem_name}.tar.gz {MAIN_REPO}{filesystem_name}/ >/dev/null 2>&1")
return resp return resp
@ -24,7 +35,7 @@ class Danix():
def back_snapshot(filesystem_name, snapshot_name): def back_snapshot(filesystem_name, snapshot_name):
os.system(f"rm -r {MAIN_REPO}{filesystem_name} > /dev/null") os.system(f"rm -r {MAIN_REPO}{filesystem_name} > /dev/null")
resp1 = os.system(f"tar -xf {MAIN_REPO}.snapshots/{snapshot_name}/{filesystem_name}.tar.gz -C {MAIN_REPO}") resp1 = os.system(f"tar -xf {MAIN_REPO}.snapshots/{snapshot_name}/{filesystem_name}.tar.gz -C {MAIN_REPO} >/dev/null 2>&1")
resp2 = os.system(f"mv {MAIN_REPO}/opt/danix/{filesystem_name} {MAIN_REPO}") resp2 = os.system(f"mv {MAIN_REPO}/opt/danix/{filesystem_name} {MAIN_REPO}")
resp3 = os.system(f"rm -r {MAIN_REPO}opt") resp3 = os.system(f"rm -r {MAIN_REPO}opt")

View File

@ -18,7 +18,7 @@ class Environment(models.Model):
environment = Environment.objects.filter(filesystem_name=filesystem_name) environment = Environment.objects.filter(filesystem_name=filesystem_name)
if environment.count() == 0: if environment.count() == 0:
print("Environment does exist!") print("Environment does not exist!")
exit(1) exit(1)
else: else:
if environment.first().status: if environment.first().status:
@ -33,7 +33,7 @@ class Environment(models.Model):
exit(1) exit(1)
except ValidationError as error: except ValidationError as error:
print("Environment doenst exist!") print("Environment doenst not exist!")
exit(1) exit(1)
@staticmethod @staticmethod
@ -98,9 +98,9 @@ class Environment(models.Model):
def list_environments(): def list_environments():
environments = Environment.objects.all() environments = Environment.objects.all()
print("============================================================================================================================================") print("===============================================================================================================================================")
print("| ENVIRONMENT NAME | TEMPLATE | CREATED | SUBSYSTEM NAME | IMAGE | STATUS | SIZE |") print("| ENVIRONMENT NAME | TEMPLATE | CREATED | SUBSYSTEM NAME | IMAGE | STATUS | SIZE |")
print("=============================================================================================================================================") print("|=============================================================================================================================================|")
if environments.count() > 0: if environments.count() > 0:
for environment in environments: for environment in environments:
@ -112,9 +112,10 @@ class Environment(models.Model):
repeat_template = (6-len(template)) * ' ' repeat_template = (6-len(template)) * ' '
print(f" {name[0:11]}{repeat} {template}{repeat_template} {environment.created} {environment.filesystem_name} Alpine {status_icon} 211 MB") size = Danix.get_size(environment.filesystem_name, None)
print(f"| {name[0:11]}{repeat} {template}{repeat_template} {environment.created} {environment.filesystem_name} Alpine {status_icon} {size}B |")
print("=============================================================================================================================================") print("===============================================================================================================================================")
class Meta: class Meta:
@ -195,12 +196,17 @@ class Snapshot(models.Model):
snapshot_name = uuid.uuid4() snapshot_name = uuid.uuid4()
print('Wait a minute! Taking snapshot')
snapshot = Snapshot.objects.create(snapshot_name=snapshot_name,environment_id=environment).save() snapshot = Snapshot.objects.create(snapshot_name=snapshot_name,environment_id=environment).save()
resp = Danix.make_snapshot(subsystem_name, snapshot_name) resp = Danix.make_snapshot(subsystem_name, snapshot_name)
if resp == 0: if resp == 0:
print("Snapshot created successfully") print("Snapshot created successfully")
print(f"Snapshot name {snapshot_name}") print(f"Snapshot name {snapshot_name}\n")
print(f"======================================")
print(f"Environment size: {Danix.get_size(subsystem_name, None)}B")
print(f"Snapshot size: {Danix.get_size(subsystem_name, snapshot_name)}B")
print(f"======================================")
exit(0) exit(0)
else: else:
@ -217,9 +223,9 @@ class Snapshot(models.Model):
snapshots = Snapshot.objects.all() snapshots = Snapshot.objects.all()
print("==========================================================================================================================================") print("===========================================================================================================================================")
print("| SNAPSHOT NAME | ENVIRONMENT NAME | CREATED | LAST SNAPSHOT | SIZE |") print("| SNAPSHOT NAME | ENVIRONMENT NAME | CREATED | LAST SNAPSHOT | SIZE |")
print("==========================================================================================================================================") print("|==========================================================================================================================================|")
if snapshots.count() > 0: if snapshots.count() > 0:
for snapshot in snapshots: for snapshot in snapshots:
@ -228,13 +234,17 @@ class Snapshot(models.Model):
lastsnapshot_icon = "🟢 Yes" if snapshot.last else "🟠 No " lastsnapshot_icon = "🟢 Yes" if snapshot.last else "🟠 No "
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)
else: else:
repeated = 14*' ' repeated = 14*' '
environment_name = f'Environment Removed 🔴{repeated}' environment_name = f'Environment Removed 🔴{repeated}'
size = "---"
print(f" {name} {environment_name} {snapshot.created} {lastsnapshot_icon} 73,5 MB ") print(f"| {name} {environment_name} {snapshot.created} {lastsnapshot_icon} {size}B |")
print("==========================================================================================================================================") print("===========================================================================================================================================")
class Meta: class Meta:
db_table = "snapshot" db_table = "snapshot"

6
danix/main.py Normal file → Executable file
View File

@ -1,3 +1,4 @@
#!./venv/bin/python
import argparse, os import argparse, os
import app import app
from utils import is_root from utils import is_root
@ -55,7 +56,8 @@ if args.rm:
for environment in environments: for environment in environments:
Environment.rm_environment(environment) Environment.rm_environment(environment)
exit(0)
exit(0)
print("[Danix]: System abort!") print("[Danix]: System abort!")
if args.stop: if args.stop:
@ -90,6 +92,6 @@ if args.snapshotremove:
for snapshot in snapshots: for snapshot in snapshots:
Snapshot.rm_snapshot(snapshot) Snapshot.rm_snapshot(snapshot)
exit(0) exit(0)
print("[Danix]: System abort!") print("[Danix]: System abort!")

32
danix/makefile Normal file
View File

@ -0,0 +1,32 @@
PIP=pip
PYTHON=python
SNAPSHOT_LIMIT=3
DB_NAME=db.sqlite3
MAIN_REPO=/opt/danix/
ROOT_FS=danixfs.tar.gz
DANIX_PATH=$(shell pwd)
REPO_NAME=https://silvavinicius.com.br/danixfs/
config:
@mkdir $(MAIN_REPO) > /dev/null 2>&1
@mkdir $(MAIN_REPO)/.snapshot > /dev/null 2>&1
@echo REPO_NAME =$(REPO_NAME) >> $(DANIX_PATH)/.env
@echo MAIN_REPO =$(MAIN_REPO) >> $(DANIX_PATH)/.env
@echo ROOT_FS =$(ROOT_FS) >> $(DANIX_PATH)/.env
@echo SNAPSHOT_LIMIT =$(SNAPSHOT_LIMIT) >> $(DANIX_PATH)/.env
@$(PIP) install -r $(DANIX_PATH)/requirements.txt > /dev/null 2>&1
$(PYTHON) $(DANIX_PATH)/manage.py migrate
alias danix="sudo main.py"
echo Danix configured successfully! Please run danix -h!
clean:
@rm -r $(MAIN_REPO) > /dev/null 2>&1
@rm $(DANIX_PATH)/$(DB_NAME) > /dev/null 2>&1
echo Danix cleaned!

View File

@ -1,3 +1,4 @@
django django
argparse argparse
django-environ django-environ
sh