Skip to content

Commit

Permalink
feat: Handles the creation of NGINX files
Browse files Browse the repository at this point in the history
  • Loading branch information
justinmerrell committed Dec 8, 2021
1 parent 86f9c05 commit ef42f21
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 11 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 |
Expand Down
9 changes: 5 additions & 4 deletions django_devops/management/commands/prep_gunicorn.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
'''

import os
from textwrap import dedent
from os.path import exists

from django.core.management.base import BaseCommand, CommandError
Expand Down Expand Up @@ -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
Expand All @@ -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()
63 changes: 63 additions & 0 deletions django_devops/management/commands/prep_nginx.py
Original file line number Diff line number Diff line change
@@ -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()
18 changes: 12 additions & 6 deletions django_devops/management/commands/update_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit ef42f21

Please sign in to comment.