From ef42f2165b11bac1f08eed4e4ed120e275e35c28 Mon Sep 17 00:00:00 2001 From: Justin Merrell Date: Wed, 8 Dec 2021 17:14:57 +0000 Subject: [PATCH] feat: Handles the creation of NGINX files --- README.md | 4 ++ .../management/commands/prep_gunicorn.py | 9 +-- .../management/commands/prep_nginx.py | 63 +++++++++++++++++++ .../management/commands/update_services.py | 18 ++++-- setup.cfg | 2 +- 5 files changed, 85 insertions(+), 11 deletions(-) create mode 100644 django_devops/management/commands/prep_nginx.py diff --git a/README.md b/README.md index 9d0bb69..0c7434a 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,8 @@ The goal of this repository is to provide a set of programatic tools to help you - Service and Config file management - Auto deploy from GIT +No more returning to the same Stackoverflow pages every time you start a new project just to remind yourself what directory config files should be placed in. All files can now be managed from a project level and quickly deployed/updated. + ## Getting Started To install the package, run the following command: @@ -47,6 +49,8 @@ For additional pip information visit: [https://pypi.org/project/django-DevOps/]( Under your project folder create a ```config_files``` and ```service_files``` folder to place files to be deployed. +A config file with the same name as the project will be treated as the NGINX config file and copied to aite-available. + ## Manage Commands | Command | Description | diff --git a/django_devops/management/commands/prep_gunicorn.py b/django_devops/management/commands/prep_gunicorn.py index 7655c72..04b6b44 100644 --- a/django_devops/management/commands/prep_gunicorn.py +++ b/django_devops/management/commands/prep_gunicorn.py @@ -3,6 +3,7 @@ ''' import os +from textwrap import dedent from os.path import exists from django.core.management.base import BaseCommand, CommandError @@ -46,11 +47,11 @@ def handle(self, *args, **options): Description=gunicorn daemon for {PROJECT_NAME} After=network.target - [service] + [Service] User=root - Group=root + Group=www-data WorkingDirectory=/opt/{PROJECT_NAME}/ - ExecStart=/opt/{PROJECT_NAME}/venv/bin/gunicorn --access-logfile --workers 3 --bind unix:/opt/{PROJECT_NAME}/{PROJECT_NAME}.sock {PROJECT_NAME}.wsgi:application + ExecStart=/opt/{PROJECT_NAME}/venv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/opt/{PROJECT_NAME}/{PROJECT_NAME}.sock {PROJECT_NAME}.wsgi:application [Install] WantedBy=multi-user.target @@ -59,5 +60,5 @@ def handle(self, *args, **options): file_path = f'{settings.BASE_DIR}/{PROJECT_NAME}/service_files' with open(f'{file_path}/gunicorn.service', 'r+', encoding='UTF-8') as file: file.seek(0) - file.write(file_template) + file.write(dedent(file_template)) file.truncate() diff --git a/django_devops/management/commands/prep_nginx.py b/django_devops/management/commands/prep_nginx.py new file mode 100644 index 0000000..e588104 --- /dev/null +++ b/django_devops/management/commands/prep_nginx.py @@ -0,0 +1,63 @@ +''' +A programatic way to prepare the nginx config file. +''' + +import os +from textwrap import dedent +from os.path import exists + +from django.core.management.base import BaseCommand, CommandError + +from django.conf import settings + +from django_devops.utils.user_input import query_yes_no + +PROJECT_NAME = os.path.basename(os.path.normpath(settings.BASE_DIR)) + +class Command(BaseCommand): + ''' + Programaticly create the sites-available file. + ''' + + help = 'Prepare the nginx config file.' + + def handle(self, *args, **options): + ''' + Verifies that the service folder exsis for use with django_devops + ''' + if not exists(f'{settings.BASE_DIR}/{PROJECT_NAME}/config_files'): + raise CommandError(f''' + {settings.BASE_DIR}/{PROJECT_NAME}/config_files does not exist. + First run "python manage.py devops" to configure django_devops. + ''') + + # Check if the file exsists and confirm overwrite. + if exists(f'{settings.BASE_DIR}/{PROJECT_NAME}/config_files/{PROJECT_NAME}'): + if query_yes_no(f'{PROJECT_NAME}/config_files/{PROJECT_NAME} exists. Overwrite?'): + pass + else: + raise CommandError(f''' + {PROJECT_NAME}/config_files/{PROJECT_NAME} will not be overwritten. + ''') + + # Generate nginx config file. + file_template = f''' + server {{ + server_name + + location /static/ {{ + root /var/www/{PROJECT_NAME}; + }} + + location / {{ + include proxy_params; + proxy_pass http://unix:/opt/{PROJECT_NAME}/run/{PROJECT_NAME}.sock; + }} + }} + ''' + + file_path = f'{settings.BASE_DIR}/{PROJECT_NAME}/config_files' + with open(f'{file_path}/{PROJECT_NAME}', 'r+', encoding='UTF-8') as file: + file.seek(0) + file.write(dedent(file_template)) + file.truncate() diff --git a/django_devops/management/commands/update_services.py b/django_devops/management/commands/update_services.py index ec54ace..7729578 100644 --- a/django_devops/management/commands/update_services.py +++ b/django_devops/management/commands/update_services.py @@ -61,27 +61,33 @@ def handle(self, *args, **options): try: file_path = f'{settings.BASE_DIR}/{project_name}/service_files' + for filename in os.listdir(file_path): with open(os.path.join(file_path, filename), 'r', encoding='UTF-8') as file: file_content = file.read() file.close() + # Handle configfile with same name as project as web service config file. + if filename == project_name: + deploy_path = '/etc/nginx/sites-available/' + else: + deploy_path = '/etc/systemd/system/' + # Create service file if it does not exist - with subprocess.Popen(['touch', f'/etc/systemd/system/{filename}']) as script: + with subprocess.Popen(['touch', f'{deploy_path}{filename}']) as script: print(script) # Update service file to match - with open(f'/etc/systemd/system/{filename}', encoding='UTF-8') as file: + with open(f'{deploy_path}{filename}', encoding='UTF-8') as file: file_content_old = file.read() file.close() - if file_content != file_content_old: - file_path="/etc/systemd/system/" - with(open(f'{file_path}{filename}.old', 'w', encoding='UTF-8')) as file: + if file_content != file_content_old: f + with(open(f'{deploy_path}{filename}.old', 'w', encoding='UTF-8')) as file: file.write(file_content_old) file.close() - with open(f'/etc/systemd/system/{filename}', 'w', encoding='UTF-8') as file: + with open(f'{deploy_path}{filename}', 'w', encoding='UTF-8') as file: file.write(file_content) file.close() diff --git a/setup.cfg b/setup.cfg index d4cf9b2..1b4ed97 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = django-DevOps -version = 0.2.2 +version = 0.2.3 description = A Django app to manage DevOps. long_description = file: README.md long_description_content_type = text/markdown