Advertisement
Guest User

Untitled

a guest
Aug 7th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. from __future__ import unicode_literals
  2.  
  3. import code
  4. import platform
  5. import sys
  6. import os
  7. import getpass
  8.  
  9. from cmd import Cmd
  10.  
  11. from django import get_version
  12. from django.apps import apps
  13. from django.conf import settings
  14. from django.core.management.base import BaseCommand
  15. from django.db.models import Model
  16.  
  17. APPS = ['circuits', 'dcim', 'extras', 'ipam', 'secrets', 'tenancy', 'users']
  18.  
  19. class AbstractQuickstartCmd(Cmd):
  20. def emptyline(self):
  21. pass
  22.  
  23. def help_exit(self):
  24. self.usage("exit")
  25. self.description("Exit early and abort quickstart.")
  26.  
  27. def do_exit(self, s):
  28. sys.exit(0)
  29.  
  30. def help_shell(self):
  31. self.usage("shell <commands>...")
  32. self.description("Execute a command in a shell")
  33.  
  34. def do_shell(self, s):
  35. os.system(s)
  36.  
  37. def printPad(self, value):
  38. pad = " "
  39. print(pad + value)
  40.  
  41. def usage(self, command):
  42. print("\nUsage: " + command + "\n")
  43.  
  44. def description(self, desc):
  45. print(desc + "\n")
  46.  
  47. def arg(self, argument, desc):
  48. self.printPad(argument + " " + desc)
  49.  
  50. def setChildPrompt(self, parent):
  51. self.prompt = parent.prompt[:-2] + ' ' + self.prompt + ')> '
  52.  
  53. def generatePrompt(self, title):
  54. return self.prompt[:-2] + ' ' + title + ')> '
  55.  
  56. def error(self, message):
  57. print("*** " + message + "\n")
  58.  
  59.  
  60. class MainCmd(AbstractQuickstartCmd):
  61. prompt = "NetBox Quickstart)> "
  62. 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)"
  63.  
  64. def help_restore(self):
  65. self.usage("restore [backup file]")
  66. self.description("Perform a guided restoration using a database dump (.sql file) generated from pg_dump.")
  67.  
  68. def do_restore(self, s):
  69. innerInterpreter = RestorationCmd()
  70. innerInterpreter.setChildPrompt(self)
  71. innerInterpreter.cmdloop()
  72.  
  73. def help_install(self):
  74. self.usage("install")
  75. self.description("Perform a guided installation.")
  76.  
  77. def do_install(self, s):
  78. innerInterpreter = InstallCmd()
  79. innerInterpreter.setChildPrompt(self)
  80. innerInterpreter.cmdloop()
  81.  
  82. class RestorationCmd(AbstractQuickstartCmd):
  83. 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.)"
  84. prompt = "Restore"
  85.  
  86. backupFile = ""
  87.  
  88. def help_backupfile(self, s):
  89. self.usage("backupfile [backup file]")
  90. self.description("View or set the backupfile.")
  91. self.arg("[backup file]", "The relative path to the SQL file. This file must be under the docker-compose directory.")
  92.  
  93. def do_backupfile(self, s):
  94. if len(s) != 0:
  95. self.backupFile = s
  96.  
  97. if len(self.backupFile == 0):
  98. print("Backup file is not set.")
  99. else:
  100. print("Backup file is set to " + self.backupFile)
  101.  
  102. def help_restore(self, s):
  103. self.usage("restore [backup file]")
  104. self.description("Perform a guided restoration using a database dump (.sql file) generated from pg_dump.")
  105. self.arg("[backup file]", "The relative path to the SQL file. This file must be under the docker-compose directory.")
  106.  
  107. def do_restore(self, s):
  108. self.backupFile = s
  109.  
  110. if len(self.backupFile) == 0:
  111. self.error("Set the backup file with `backupfile` first.")
  112. return
  113. else:
  114. command = "PGPASSWORD=$POSTGRES_PASSWORD pg_restore -d $POSTGRES_DB -h $POSTGRES_HOST -U $POSTGRES_USER " + self.backupFile
  115. os.system(command)
  116.  
  117. class InstallCmd(AbstractQuickstartCmd):
  118. 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.)"
  119. prompt = "Install"
  120.  
  121. def help_useradd(self, s):
  122. self.usage("useradd [username] [email] [password]")
  123. 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.")
  124. self.arg("[username]", "The username for the user")
  125. self.arg("[email]", "The email address for the user")
  126. self.arg("[password]", "The plaintext password. Password is hashed inside the database.")
  127.  
  128. def do_useradd(self, s):
  129. arguments = s.split()
  130.  
  131. # TODO: optimize this
  132.  
  133. if len(arguments) == 0:
  134. username = input("Username: ")
  135. else:
  136. username = arguments[0]
  137.  
  138. if len(arguments) <= 1:
  139. email = input("Email: ")
  140. else:
  141. email = arguments[1]
  142.  
  143. if len(arguments) <= 2:
  144. password = getpass.getpass("Password: ")
  145. else:
  146. password = arguments[2]
  147.  
  148. print("Creating", username, '...')
  149. from django.apps import apps
  150. from django.contrib.auth.models import User
  151. User.objects.create_superuser(username, email, password)
  152. print("Complete.")
  153.  
  154. class Command(BaseCommand):
  155. help = "Quick commands to get started."
  156.  
  157. def handle(self, **options):
  158. interpreter = MainCmd()
  159. interpreter.cmdloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement