Commit 48da9c40 authored by Ioannis Tsafaras's avatar Ioannis Tsafaras

Merge pull request #35 from gouzouni625/LAM-53-Django

LAM-53 Django-PostgreSQL backend
parents a2a2d3b9 5b230f65
# ~okeanos Lambda on Demand backend service
## Description
In this directory you can find the code for the ~okeanos-LoD backend service. It
is implemented as a Django project and currently contains `backend` application.
The backend code can be deployed using the Ansible code inside `ansible/` directory.
# ~okeanos LoD backend service Ansible
## Description
In this directory you can find the Ansible code that deployes the backend service vm
used by ~okeanos Lambda on Demand project.
## Prerequisites
To use this code you need:
1. A vm running Debian OS.
2. The vm should have a public ip.
3. Python should be installed on the vm.
## Usage
To use this code, run
`ansible-playbook -v playbooks/setup.yml -i <inventory>`
The vms should be under `service-vms` group in the inventory file.
## Results
This Ansible code will install and configure Apache server, Django and PostgreSQL. It will also clone ~okeanos-LoD
repository from Github and set Apache server to serve ~okeanos-LoD webapp.
To check that everything is installed correctly after running the Ansible code, open a web browser and
enter your vm's public ip on the address bar. You should see the following text:
```
It worked!
Congratulations on your first Django-powered page.
Of course, you haven't actually done any work yet. Next, start your first app by running python manage.py startapp [app_label].
You're seeing this message because you have DEBUG = True in your Django settings file and you haven't configured any URLs. Get to work!
```
[service-vms]
lod-service
../roles/
\ No newline at end of file
---
- hosts: service-vms
user: root
roles:
- roles/service-vm
---
- name: django_dbs_migrate
django_manage: command=migrate app_path={{ repository_download_path }}/okeanos-LoD/webapp/
changed_when: True
notify:
- makemigrations_backend_app
- name: makemigrations_backend_app
django_manage: command="makemigrations --noinput backend" app_path={{ repository_download_path }}/okeanos-LoD/webapp/
changed_when: True
notify:
- migrate_backend_app
- name: migrate_backend_app
django_manage: command=migrate app_path={{ repository_download_path }}/okeanos-LoD/webapp/ apps=backend
- name: restart_apache2
service: name=apache2 state=restarted
---
- name: Install apache2, apache2-dev and libapache2-mod-wsgi packages.
apt: name={{ item }} state=latest
with_items:
- apache2
- apache2-dev
- libapache2-mod-wsgi
- name: Get the hostname of the vm.
command: hostname
register: hostname
- name: Copy Lambda sites-available configuration.
template: src=lambda-service-backend.conf.j2 dest=/etc/apache2/sites-available/lambda-service-backend.conf
- name: Add lambda-service-backend to sites-enabled.
file: src=/etc/apache2/sites-available/lambda-service-backend.conf dest=/etc/apache2/sites-enabled/lambda-service-backend.conf state=link
- name: Remove default sites-enabled.
file: name=/etc/apache2/sites-enabled/000-default.conf state=absent
- name: Configure Apache.
lineinfile: dest=/etc/apache2/apache2.conf line="WSGIPythonPath {{ repository_download_path }}/okeanos-LoD/webapp" state=present insertafter=EOF
notify:
- restart_apache2
---
- name: Fix locale problem.
command: update-locale LANGUAGE="en_US.UTF-8" LC_ALL="en_US.UTF-8"
- name: Upgrade packages.
apt: upgrade=dist update_cache=yes
- name: Install python-pip, python-dev, sudo and git packages.
apt: name={{ item }} state=latest
with_items:
- python-pip
- python-dev
- sudo
- git
- name: Include apache2 tasks.
include: apache-install.yml
- name: Include postgresql-install tasks.
include: postgresql-install.yml
- name: Install Django using Pip.
pip: name=Django
- name: Download Django code from Github.
git: repo=https://github.com/gouzouni625/okeanos-LoD.git dest={{ repository_download_path }}/okeanos-LoD version=LAM-53-Django # version can be used to select a branch. See ansible git module documentation.
# The repository should be changed when the pull request gets merged.
- name: Change repository permissions.
file: path={{ repository_download_path }} owner=root group=root recurse=yes
- name: Setup Django database configuration.
template: src=settings.py.j2 dest={{ repository_download_path}}/okeanos-LoD/webapp/webapp/settings.py
notify:
- django_dbs_migrate
---
- name: Install PosgreSQL and libpq-dev packages.
apt: name={{ item }} state=latest
with_items:
- postgresql
- libpq-dev
- name: Install Python connector for PostgreSQL.
pip: name=psycopg2
- name: Create PostgreSQL database for service user.
sudo_user: postgres
postgresql_db: name={{ database_name }}
- name: Create PostgreSQL role for lambdaservice user.
sudo_user: postgres
postgresql_user: db={{ database_name }} name={{ database_username }} password={{ database_user_password }}
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerName {{ hostname.stdout_lines | replace("[", "") | replace("u", "") | replace("'", "") | replace("]", "") }}.vm.okeanos.grnet.gr
#ServerAdmin webmaster@localhost
DocumentRoot {{ repository_download_path }}/okeanos-LoD/webapp/
WSGIScriptAlias / {{ repository_download_path }}/okeanos-LoD/webapp/webapp/wsgi.py
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
"""
Django settings for webapp project.
Generated by 'django-admin startproject' using Django 1.8.3.
For more information on this file, see
https://docs.djangoproject.com/en/1.8/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '-ay^f7=9e!@jr94v7!v&tl@4zm5=0g8&8d9(a*ffzqywg#@6iy'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'backend',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF = 'webapp.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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 = 'webapp.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': '{{ database_name }}',
'USER': '{{ database_username }}',
'PASSWORD': '{{ database_user_password }}',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
STATIC_URL = '/static/'
---
database_username: lambda
database_name: lambda_db
database_user_password: change_me
repository_download_path: /var/www
from django.contrib import admin
\ No newline at end of file
from django.db import models
class User(models.Model):
id = models.CharField("UUID", primary_key=True, null=False, blank=False, unique=True, default="", max_length=255, help_text="Unique user id asign by Astakos")
token = models.CharField("Okeanos Token", null=True, blank=True, unique=True, default="", max_length=64, help_text="Token provided by ~okeanos.")
class Project(models.Model):
id = models.AutoField("Project ID", primary_key=True, null=False, blank=False, unique=True, default="", help_text="Project id provided by kamaki.")
description = models.TextField("Project Description", null=True, blank=True, unique=False, default="", help_text="The description of a project.")
class Cluster(models.Model):
id = models.AutoField("Cluster ID", primary_key=True, null=False, help_text="Auto-increment cluster id.")
# OneToOneField is a ForeignKey with unique=True. Django recommends using OneToOneField instead of a ForeignKey
# with unique=True.
master_server = models.OneToOneField('Server', null=True, blank=True, on_delete=models.CASCADE)
class Server(models.Model):
id = models.AutoField("Server ID", primary_key=True, null=False, blank=False, unique=True, default="", help_text="Server id provided by kamaki.")
hostname = models.CharField("Hostname", null=False, blank=False, unique=True, max_length=100)
public_ip = models.GenericIPAddressField("Public IP", null=False, blank=False, unique=True)
private_ip = models.GenericIPAddressField("Private IP", null=False, blank=False, unique=False)
class PrivateNetwork(models.Model):
id = models.AutoField("Network ID", primary_key=True, null=False, blank=False, unique=True, default="", help_text="Private network id provided by kamaki.")
subnet = models.CharField(max_length=100)
gateway = models.GenericIPAddressField("Gateway", null=False, blank=False, unique=False)
class UserProjectConnection(models.Model):
user_id = models.ForeignKey('User', null=False, blank=False, unique=False, on_delete=models.CASCADE)
project_id = models.ForeignKey('Project', null=False, blank=False, unique=False, on_delete=models.CASCADE)
class UserClusterConnection(models.Model):
user_id = models.ForeignKey('User', null=False, blank=False, unique=False, on_delete=models.CASCADE)
cluster_id = models.ForeignKey('Cluster', null=False, blank=False, unique=False, on_delete=models.CASCADE)
class ClusterServerConnection(models.Model):
server_id = models.ForeignKey('Server', null=False, blank=False, unique=False, on_delete=models.CASCADE)
cluster_id = models.ForeignKey('Cluster', null=False, blank=False, unique=False, on_delete=models.CASCADE)
class ClusterNetworkConnection(models.Model):
network_id = models.ForeignKey('PrivateNetwork', null=False, blank=False, unique=False, on_delete=models.CASCADE)
cluster_id = models.ForeignKey('Cluster', null=False, blank=False, unique=False, on_delete=models.CASCADE)
class ClusterProjectConnection(models.Model):
network_id = models.ForeignKey('Project', null=False, blank=False, unique=False, on_delete=models.CASCADE)
cluster_id = models.ForeignKey('Cluster', null=False, blank=False, unique=False, on_delete=models.CASCADE)
from django.test import TestCase
# Create your tests here.
from django.shortcuts import render
# Create your views here.
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "webapp.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
"""webapp URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.8/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Add an import: from blog import urls as blog_urls
2. Add a URL to urlpatterns: url(r'^blog/', include(blog_urls))
"""
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
]
"""
WSGI config for webapp 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/1.8/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "webapp.settings")
application = get_wsgi_application()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment