-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
12985dd
commit 0a9ca17
Showing
19 changed files
with
236 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
recursive-include ansible-webui/* | ||
recursive-include src/ansible-webui/* | ||
include requirements.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import subprocess | ||
|
||
from aw.settings import BASE_DIR | ||
|
||
|
||
def process(cmd: list, timeout_sec: int = None, shell: bool = False) -> dict: | ||
try: | ||
with subprocess.Popen( | ||
cmd, | ||
shell=shell, | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
cwd=BASE_DIR, | ||
) as p: | ||
b_stdout, b_stderr = p.communicate(timeout=timeout_sec) | ||
stdout, stderr, rc = b_stdout.decode('utf-8').strip(), b_stderr.decode('utf-8').strip(), p.returncode | ||
|
||
except (subprocess.TimeoutExpired, subprocess.SubprocessError, subprocess.CalledProcessError, | ||
OSError, IOError) as error: | ||
stdout, stderr, rc = None, str(error), 1 | ||
|
||
return dict( | ||
stdout=stdout, | ||
stderr=stderr, | ||
rc=rc, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
from pathlib import Path | ||
from shutil import copy | ||
from datetime import datetime | ||
from sys import exit as sys_exit | ||
from os import environ | ||
from secrets import choice as random_choice | ||
from string import digits, ascii_letters | ||
|
||
from aw.settings import DB_FILE | ||
from aw.utils.subps import process | ||
from aw.utils.debug import log, log_error, log_warn | ||
|
||
|
||
ENV_KEY_INIT_ADMIN_NAME = 'AW_ADMIN' | ||
ENV_KEY_INIT_ADMIN_PWD = 'AW_ADMIN_PWD' | ||
|
||
|
||
def install_or_migrate_db(): | ||
if not Path(DB_FILE).is_file(): | ||
return install() | ||
|
||
return migrate() | ||
|
||
|
||
def _manage_db(action: str, cmd: list, backup: str = None): | ||
cmd2 = ['python3', 'manage.py'] | ||
cmd2.extend(cmd) | ||
|
||
log(msg=f"Executing DB-management command: '{cmd2}'", level=6) | ||
result = process(cmd=cmd2) | ||
|
||
if result['rc'] != 0: | ||
log_error(f'Database {action} failed!') | ||
log(msg=f"Error:\n{result['stderr']}", level=1) | ||
log(msg=f"Output:\n{result['stdout']}", level=3) | ||
|
||
if backup is not None: | ||
log_warn( | ||
msg=f"Trying to restore database from automatic backup: {backup} => {DB_FILE}", | ||
_stderr=True, | ||
) | ||
copy(src=DB_FILE, dst=f'{backup}.failed') | ||
copy(src=backup, dst=DB_FILE) | ||
|
||
else: | ||
sys_exit(1) | ||
|
||
|
||
def install(): | ||
log(msg=f"Initializing database {DB_FILE}..", level=3) | ||
_make_migrations() | ||
_manage_db(action='initialization', cmd=['migrate']) | ||
|
||
|
||
def migrate(): | ||
_make_migrations() | ||
|
||
backup = f"{DB_FILE}.{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.bak" | ||
log(msg=f"Creating database backup: '{backup}'", level=6) | ||
copy(src=DB_FILE, dst=backup) | ||
|
||
if 'AW_DB_MIGRATE' not in environ: | ||
log(msg=f"Migrating database {DB_FILE}..", level=3) | ||
_manage_db(action='migration', cmd=['migrate'], backup=backup) | ||
|
||
|
||
def _make_migrations(): | ||
_manage_db(action='schema-creation', cmd=['makemigrations']) | ||
_manage_db(action='schema-creation', cmd=['makemigrations', 'aw']) | ||
|
||
|
||
def create_first_superuser(): | ||
from django.contrib.auth.models import User | ||
if len(User.objects.filter(is_superuser=True)) == 0: | ||
name = 'ansible' | ||
pwd = ''.join(random_choice(ascii_letters + digits + '!.-+') for _ in range(14)) | ||
|
||
if ENV_KEY_INIT_ADMIN_NAME in environ: | ||
name = environ[ENV_KEY_INIT_ADMIN_NAME] | ||
|
||
if ENV_KEY_INIT_ADMIN_PWD in environ: | ||
pwd = environ[ENV_KEY_INIT_ADMIN_PWD] | ||
|
||
User.objects.create_superuser( | ||
username=name, | ||
email=f"{name}@localhost", | ||
password=pwd | ||
) | ||
|
||
log_warn('No admin was found in the database!') | ||
if ENV_KEY_INIT_ADMIN_PWD in environ: | ||
log(msg='The user was created as provided!', level=4) | ||
|
||
else: | ||
log(msg=f"Generated user: '{name}'", level=3) | ||
log(msg=f"Generated pwd: '{pwd}'", level=3) | ||
log_warn('Make sure to change the password!') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.