Created
August 7, 2017 08:23
-
-
Save sapslaj/595d92e490754a198e80b8a03733dc95 to your computer and use it in GitHub Desktop.
How to overcomplicate Django application installers.
This file contains hidden or 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
from __future__ import unicode_literals | |
import code | |
import platform | |
import sys | |
import os | |
import getpass | |
from cmd import Cmd | |
from django import get_version | |
from django.apps import apps | |
from django.conf import settings | |
from django.core.management.base import BaseCommand | |
from django.db.models import Model | |
APPS = ['circuits', 'dcim', 'extras', 'ipam', 'secrets', 'tenancy', 'users'] | |
class AbstractQuickstartCmd(Cmd): | |
def emptyline(self): | |
pass | |
def help_exit(self): | |
self.usage("exit") | |
self.description("Exit early and abort quickstart.") | |
def do_exit(self, s): | |
sys.exit(0) | |
def help_shell(self): | |
self.usage("shell <commands>...") | |
self.description("Execute a command in a shell") | |
def do_shell(self, s): | |
os.system(s) | |
def printPad(self, value): | |
pad = " " | |
print(pad + value) | |
def usage(self, command): | |
print("\nUsage: " + command + "\n") | |
def description(self, desc): | |
print(desc + "\n") | |
def arg(self, argument, desc): | |
self.printPad(argument + " " + desc) | |
def setChildPrompt(self, parent): | |
self.prompt = parent.prompt[:-2] + ' ' + self.prompt + ')> ' | |
def generatePrompt(self, title): | |
return self.prompt[:-2] + ' ' + title + ')> ' | |
def error(self, message): | |
print("*** " + message + "\n") | |
class MainCmd(AbstractQuickstartCmd): | |
prompt = "NetBox Quickstart)> " | |
intro = "Welcome to NetBox Quickstart!\nWould you like to `install` a new NetBox instance or `restore` from a backup?\n(Hint: type `help` for more info)" | |
def help_restore(self): | |
self.usage("restore [backup file]") | |
self.description("Perform a guided restoration using a database dump (.sql file) generated from pg_dump.") | |
def do_restore(self, s): | |
innerInterpreter = RestorationCmd() | |
innerInterpreter.setChildPrompt(self) | |
innerInterpreter.cmdloop() | |
def help_install(self): | |
self.usage("install") | |
self.description("Perform a guided installation.") | |
def do_install(self, s): | |
innerInterpreter = InstallCmd() | |
innerInterpreter.setChildPrompt(self) | |
innerInterpreter.cmdloop() | |
class RestorationCmd(AbstractQuickstartCmd): | |
intro = "To do a restore, first specify the location of the backup file with `backupfile` then run `restore` to perform the restoration.\n(Hint: You can use shell commands with `shell`, e.g. `shell ls` to list files in the current directory.)" | |
prompt = "Restore" | |
backupFile = "" | |
def help_backupfile(self, s): | |
self.usage("backupfile [backup file]") | |
self.description("View or set the backupfile.") | |
self.arg("[backup file]", "The relative path to the SQL file. This file must be under the docker-compose directory.") | |
def do_backupfile(self, s): | |
if len(s) != 0: | |
self.backupFile = s | |
if len(self.backupFile == 0): | |
print("Backup file is not set.") | |
else: | |
print("Backup file is set to " + self.backupFile) | |
def help_restore(self, s): | |
self.usage("restore [backup file]") | |
self.description("Perform a guided restoration using a database dump (.sql file) generated from pg_dump.") | |
self.arg("[backup file]", "The relative path to the SQL file. This file must be under the docker-compose directory.") | |
def do_restore(self, s): | |
self.backupFile = s | |
if len(self.backupFile) == 0: | |
self.error("Set the backup file with `backupfile` first.") | |
return | |
else: | |
command = "PGPASSWORD=$POSTGRES_PASSWORD pg_restore -d $POSTGRES_DB -h $POSTGRES_HOST -U $POSTGRES_USER " + self.backupFile | |
os.system(command) | |
class InstallCmd(AbstractQuickstartCmd): | |
intro = "Run commands that quickly get you set up with NetBox\n(Hint: run `help` to see all commands. You will want to check out `useradd` first.)" | |
prompt = "Install" | |
def help_useradd(self, s): | |
self.usage("useradd [username] [email] [password]") | |
self.description("Create a new superuser in NetBox. You can always add more (hopefully less privileged) users in the Admin panel of the NetBox web interface.") | |
self.arg("[username]", "The username for the user") | |
self.arg("[email]", "The email address for the user") | |
self.arg("[password]", "The plaintext password. Password is hashed inside the database.") | |
def do_useradd(self, s): | |
arguments = s.split() | |
# TODO: optimize this | |
if len(arguments) == 0: | |
username = input("Username: ") | |
else: | |
username = arguments[0] | |
if len(arguments) <= 1: | |
email = input("Email: ") | |
else: | |
email = arguments[1] | |
if len(arguments) <= 2: | |
password = getpass.getpass("Password: ") | |
else: | |
password = arguments[2] | |
print("Creating", username, '...') | |
from django.apps import apps | |
from django.contrib.auth.models import User | |
User.objects.create_superuser(username, email, password) | |
print("Complete.") | |
class Command(BaseCommand): | |
help = "Quick commands to get started." | |
def handle(self, **options): | |
interpreter = MainCmd() | |
interpreter.cmdloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment