Skip to content

Commit

Permalink
Merge pull request #77 from johnbedeir/dev
Browse files Browse the repository at this point in the history
Dockerfile | Ansible Playbook for Python project
  • Loading branch information
JohnyDev authored Jul 18, 2022
2 parents 0ef9163 + 7e26ade commit c9553e2
Show file tree
Hide file tree
Showing 28 changed files with 662 additions and 1 deletion.
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,11 @@ gcp-credentials.json

*.rdp

.vagrant
.vagrant


__pycache__

.env

*.sqlite3
5 changes: 5 additions & 0 deletions DevOps-Pipeline-Python/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Dockerfile

playbook.yml

README.md
19 changes: 19 additions & 0 deletions DevOps-Pipeline-Python/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
SECRET_KEY=

DEBUG= True

# DB Connection

DB_ENGINE=django.db.backends.sqlite3
DB_NAME=db.sqlite3
DB_USER=
DB_PASSWORD=
DB_HOST=
DB_PORT=

# Email Settings

EMAIL_HOST=
EMAIL_PORT=

TIME_ZONE=UTC
13 changes: 13 additions & 0 deletions DevOps-Pipeline-Python/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM python:alpine

WORKDIR /usr/src/app

COPY requirements.txt /usr/src/app

RUN pip install --no-cache-dir -r requirements.txt

COPY . /usr/src/app

CMD [ "python3", "manage.py", "runserver", "0.0.0.0:8000" ]

EXPOSE 8000
38 changes: 38 additions & 0 deletions DevOps-Pipeline-Python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Python-Django-Web-App

## How to run?

`NOTE: Make sure you already have Python and Pip installed`

### Step 1: Create .env file

` Create .env file from the env.example template and generate new SECRET_KEY using the following website:`

[DJANGO SECRET KEY GEN](https://djecrety.ir/)

### Step 2: Create the DataBase

`Rename the existing database file "db.sqlite3.example" to be "db.sqlite3"`

### Step 3: Install the requirements.txt

`Run the follwing command to install the requirements.txt`

```
pip install -r requirements.txt
OR
pip3 install -r requirements.txt
```

### Step 4: Run the application

`Run the application using the following command`

`NOTE: Replace <PORT> with the port number you would like to use ex:8000`

```
python3 manage.py runserver 0.0.0.0:<PORT>
```
Binary file added DevOps-Pipeline-Python/db.sqlite3.example
Binary file not shown.
Empty file added DevOps-Pipeline-Python/environ
Empty file.
22 changes: 22 additions & 0 deletions DevOps-Pipeline-Python/manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'polling.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)


if __name__ == '__main__':
main()
21 changes: 21 additions & 0 deletions DevOps-Pipeline-Python/playbook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
- name: "Automate Docker Build using Ansible"
hosts: localhost
tasks:
- name: stop running container
command: docker stop survey-cont
ignore_errors: yes

- name: remove stopped container
command: docker rm survey-cont
ignore_errors: yes

- name: remove used image
command: docker rmi survey-img
ignore_errors: yes

- name: build docker image from dockerfile
command: docker build -t survey-img .

- name: run container from image
command: docker run -d --name survey-cont -p 4000:8000 survey-img
Empty file.
16 changes: 16 additions & 0 deletions DevOps-Pipeline-Python/polling/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for polling project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.1/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'polling.settings')

application = get_asgi_application()
128 changes: 128 additions & 0 deletions DevOps-Pipeline-Python/polling/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
"""
Django settings for tester project.
Generated by 'django-admin startproject' using Django 3.1.1.
For more information on this file, see
https://docs.djangoproject.com/en/3.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""

from pathlib import Path
import os
from decouple import config
from decouple import Csv

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = config('SECRET_KEY')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = config('DEBUG', default=False, cast=bool)

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls',
]

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'polling.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

WSGI_APPLICATION = 'polling.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': config('DB_ENGINE'),
'NAME': config('DB_NAME'),
'USER': config('DB_USER'),
'PASSWORD': config('DB_PASSWORD'),
'HOST': config('DB_HOST'),
'PORT': config('DB_PORT'),
}
}


# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]


# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = config('TIME_ZONE')

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/

STATIC_URL = '/static/'
25 changes: 25 additions & 0 deletions DevOps-Pipeline-Python/polling/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""polling URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import include, path
from django.views.generic import RedirectView
from polls import views

urlpatterns = [
path('', RedirectView.as_view(url='polls/', permanent=True)),
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
16 changes: 16 additions & 0 deletions DevOps-Pipeline-Python/polling/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
WSGI config for polling project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.1/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'polling.settings')

application = get_wsgi_application()
Empty file.
9 changes: 9 additions & 0 deletions DevOps-Pipeline-Python/polls/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.contrib import admin
from .models import Question, Choice

admin.site.site_header = "Polling"
admin.site.site_title = "Polling"
admin.site.index_title = "Welcome to Polling Admin Area"

admin.site.register(Question)
admin.site.register(Choice)
5 changes: 5 additions & 0 deletions DevOps-Pipeline-Python/polls/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class PollsConfig(AppConfig):
name = 'polls'
32 changes: 32 additions & 0 deletions DevOps-Pipeline-Python/polls/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Generated by Django 3.1.1 on 2020-09-02 12:25

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Question',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('question_text', models.CharField(max_length=200)),
('pub_date', models.DateTimeField(verbose_name='date published')),
],
),
migrations.CreateModel(
name='Choice',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('choice_text', models.CharField(max_length=200)),
('votes', models.IntegerField(default=0)),
('question', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='polls.question')),
],
),
]
Empty file.
Loading

0 comments on commit c9553e2

Please sign in to comment.