From 5456ea477dc129b897ac9aee109e5cc9b2f7c773 Mon Sep 17 00:00:00 2001 From: Mike Young Date: Sun, 29 Dec 2019 00:18:11 -0500 Subject: [PATCH] Finished ui design, system dependency installs, nginx config file generation --- config.json | 2 +- install | 82 +++++++++++++++++++++++++++++++++----- pyproject.toml | 2 +- pyshelf_nginx.conf | 38 ------------------ requirements.txt | 1 + src/backend/lib/display.py | 30 +++++++++++++- uwsgi.ini | 1 - 7 files changed, 103 insertions(+), 53 deletions(-) delete mode 100644 pyshelf_nginx.conf diff --git a/config.json b/config.json index c26edc5..18a02eb 100644 --- a/config.json +++ b/config.json @@ -1 +1 @@ -{"TITLE": "pyShelf E-Book Server", "VERSION": "0.3.0", "BOOKPATH": "/home/raelon/Books", "DB_HOST": "localhost", "DB_PORT": "5432", "DATABASE": "pyshelf", "USER": "pyshelf", "PASSWORD": "pyshelf", "BOOKSHELF": "data/shelf.json", "ALLOWED_HOSTS": "*"} +{"TITLE": "pyShelf E-Book Server", "VERSION": "0.3.0", "BOOKPATH": "/home/raelon/Books", "DB_HOST": "localhost", "DB_PORT": "5432", "DATABASE": "pyshelf", "USER": "pyshelf", "PASSWORD": "pyshelf", "BOOKSHELF": "data/shelf.json", "ALLOWED_HOSTS": "*", "hostname": "localhost", "webport": "8000", "wsgiport": "8001"} diff --git a/install b/install index 1fdbf92..7129ca0 100755 --- a/install +++ b/install @@ -1,8 +1,9 @@ -#!/usr/bin/python3.8 +#!/usr/bin/python3 import json import os import pathlib import platform +import pprint import subprocess as sp import sys from shutil import copyfile @@ -10,6 +11,9 @@ from shutil import copyfile import psutil from src.backend.lib.display import TerminalDisplay +log_file = "installer.log" +messages = [] + class Configuration: def __init__(self): @@ -110,6 +114,10 @@ class SystemInstaller: for _installer in installers: _fp = p + "/" + str(_installer["bin"]) if os.path.isfile(_fp): + global messages + messages = messages + [ + "Found system installer binary " + str(_installer["bin"]) + ] return _installer def copy_config(self, _file=None, _dirs=None): @@ -117,15 +125,42 @@ class SystemInstaller: _file = self.nginx_conf if _dirs is None: _dirs = self.site_dirs + + outfile = "/%s" % _file.__str__() + if os.path.isdir(_dirs[0]): + os.system("sudo cp %s %s" % (_file, _dirs[0] + outfile)) + else: + os.system("sudo mkdir %s" % _dirs[0]) + os.system("sudo cp %s %s" % (_file, _dirs[0] + outfile)) + try: + if os.path.isdir(_dirs[1]): + ln_string = str(_dirs[0] + outfile + " " + _dirs[1] + outfile) + os.system("sudo ln -s %s" % ln_string) + except Exception as e: + pass + """ for r in _dirs: - copyfile(_file, r) + if os.path.isdir(r): + os.system("sudo cp %s %s" % (_file, r+"/"+_file.__str__())) + else: + os.system("sudo mkdir %s" % r) + os.system("sudo cp %s %s" % (_file, r+"/"+_file.__str__())) + """ return True def make_nginx_config(self, answers): - breakpoint() + root = os.path.abspath(".") + _fp = "pyshelf_nginx.conf" + for r in answers: + if r["name"] == "hostname": + hostname = r["answer"] + elif r["name"] == "webport": + port = r["answer"] + elif r["name"] == "wsgiport": + wsgiport = r["answer"] nginx_conf_str = """ # pyshelf_nginx.conf - upstream django {server 127.0.0.1:8001;} + upstream django {server 127.0.0.1:%s;} server { listen %s; server_name %s; @@ -137,13 +172,32 @@ class SystemInstaller: location / {uwsgi_pass django; include %s/uwsgi_params;} } """ % ( - answers + wsgiport, + port, + hostname, + root, + root, + root, + root, ) + with open(_fp, "w") as write_file: + write_file.write(nginx_conf_str) + global messages + messages = messages + ["Generated new pyshelf_nginx.conf", nginx_conf_str] + + def log(self): + global log_file + global messages + with open(log_file, "w") as write_file: + write_file.write(TerminalDisplay().banner_render()) + for message in messages: + write_file.write(message + "\n") + messages = messages + ["Log file written to " + log_file.__str__()] config = Configuration().open_file() -installer = None -messages = [] +sysinstall = SystemInstaller() +installer = sysinstall.bin # Get user configuration options install_answers = TerminalDisplay().installer() for key in install_answers: @@ -224,7 +278,6 @@ if RequiredServices().db_server_found(req) is False: + package ) install_status = os.system(cmd) - copy_config = SystemInstaller().copy_config() srvc_start = os.system("sudo systemctl start postgresql") messages = messages + [ "PostgreSQL installed and started", @@ -232,12 +285,19 @@ if RequiredServices().db_server_found(req) is False: " sudo systemctl enable nginx", "\n", ] - if copy_config: - messages = messages + ["pyShelf site config copied to sites_enabled"] + +# Post install configurations +sysinstall.make_nginx_config(install_answers) +copy_config = sysinstall.copy_config() +if copy_config: + messages = messages + ["pyShelf site config copied to sites_enabled"] # Display end screen +sysinstall.log() TerminalDisplay().clear() -TerminalDisplay().h_rule() +TerminalDisplay().banner() for message in messages: print(message) +print() + TerminalDisplay().h_rule() diff --git a/pyproject.toml b/pyproject.toml index ad507ce..eaa5fde 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,4 +7,4 @@ use_parentheses = true # NOTE: the known_third_party setting is managed by # seed-isort-config and should not be modified directly. # Any changes made to this setting will be overwritten. -known_third_party = ["backend", "bs4", "django", "interface", "prompt_toolkit", "psycopg2", "requests"] +known_third_party = ["backend", "bs4", "django", "interface", "prompt_toolkit", "psycopg2", "pyfiglet", "requests"] diff --git a/pyshelf_nginx.conf b/pyshelf_nginx.conf deleted file mode 100644 index f106c86..0000000 --- a/pyshelf_nginx.conf +++ /dev/null @@ -1,38 +0,0 @@ -# pyshelf_nginx.conf - -upstream django { - # server unix:///path/to/your/mysite/mysite.sock; # for a file socket - server 127.0.0.1:8001; # for a web port socket (we'll use this first) -} - -# configuration of the server -server { - listen 8000; - server_name 127.0.0.1; # substitute your machine's IP address or FQDN - charset utf-8; - client_max_body_size 75M; # adjust to taste - - # Django media - location /media { - # your Django project's media files - amend as required - alias /home/raelon/Projects/pyShelf/frontend/interface/media; - } - - location /static { - # your Django project's static files - amend as required - alias /home/raelon/Projects/pyShelf/frontend/interface/static; - } - - location /books { - # Absolute location of your ebook files - internal; - alias /home/raelon/Projects/pyShelf/books; - } - - # Finally, send all non-media requests to the Django server. - location / { - # the uwsgi_params file you installed - uwsgi_pass django; - include /home/raelon/Projects/pyShelf/uwsgi_params; - } -} diff --git a/requirements.txt b/requirements.txt index 87a1a7f..dfb1cd5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -17,3 +17,4 @@ django-debug-toolbar psycopg2-binary prompt_toolkit psutil +pyfiglet diff --git a/src/backend/lib/display.py b/src/backend/lib/display.py index 627f02f..c487d7d 100644 --- a/src/backend/lib/display.py +++ b/src/backend/lib/display.py @@ -2,6 +2,7 @@ from __future__ import unicode_literals import os +import pyfiglet from prompt_toolkit import prompt as prm @@ -11,6 +12,11 @@ class TerminalDisplay: self.w, self.y = os.get_terminal_size()[0], os.get_terminal_size()[1] self.home = os.environ["HOME"] self.user = os.environ["USER"] + self.version = "0.4.0" + self.slogan = "Installer Initiative" + self.green = "\033[1;32m" + self.blue = "\033[94m" + self.clr_term = "\033[m" def screen(self): return self.term @@ -84,7 +90,7 @@ class TerminalDisplay: self.clear() answers = questions for answer in answers: - self.h_rule() + self.banner() answer["answer"] = prm(answer["message"]) if answer["answer"] == "": answer["answer"] = answer["default"] @@ -93,3 +99,25 @@ class TerminalDisplay: def h_rule(self): print("\u2501" * self.w) + + def banner(self): + self.h_rule() + title = pyfiglet.Figlet(font="cyberlarge") + print(self.green + title.renderText("pyShelf") + self.clr_term) + print( + self.blue + " version " + self.version + self.clr_term + " " + self.slogan + ) + self.h_rule() + print() + + def banner_render(self): + title = pyfiglet.Figlet(font="cyberlarge") + _banner = ( + title.renderText("pyShelf") + + "\nversion " + + self.version + + " " + + self.slogan + + "\n" + ) + return _banner diff --git a/uwsgi.ini b/uwsgi.ini index 9d68e9f..18bea03 100644 --- a/uwsgi.ini +++ b/uwsgi.ini @@ -1,5 +1,4 @@ [uwsgi] -# chdir = {Full path to pyShelf/frontend} chdir=/home/raelon/Projects/pyShelf/src module=frontend.wsgi master=True