From a961abbe43d3edbb5f941fe8e7c13544f01c521e Mon Sep 17 00:00:00 2001 From: Thomas Date: Thu, 19 Sep 2019 19:31:51 +0200 Subject: [PATCH] docker macht alles besser! --- .dockerignore | 10 ++++ .gitignore | 4 ++ Dockerfile | 14 +++++ cashonly/__init__.py | 0 config/copysecret.py | 18 +++++++ config/init.sh | 9 ++++ config/settings.py | 123 +++++++++++++++++++++++++++++++++++++++++++ config/urls.py | 27 ++++++++++ config/wsgi.py | 16 ++++++ docker-compose.yml | 25 +++++++++ requirements.txt | 1 + 11 files changed, 247 insertions(+) create mode 100644 .dockerignore create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 cashonly/__init__.py create mode 100644 config/copysecret.py create mode 100755 config/init.sh create mode 100644 config/settings.py create mode 100644 config/urls.py create mode 100644 config/wsgi.py create mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..df72f94 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,10 @@ +online/ +venv +Dockerfile +docker-compose.yml +.DS_Store +.git +.idea +.vscode +.gitignore +.dockerignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1857c4f --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +venv/ +online/ +.idea/ +.DS_Store \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..fa25985 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM python:3 + +ENV PYTHONUNBUFFERED 1 + +RUN mkdir /django +COPY . /django +WORKDIR /django +RUN mkdir static_thirdparty \ + && wget https://code.jquery.com/jquery-3.4.1.min.js \ + && wget https://github.com/twbs/bootstrap/releases/download/v3.4.1/bootstrap-3.4.1-dist.zip \ + && unzip bootstrap-3.4.1-dist.zip \ + && rm bootstrap-3.4.1-dist.zip + +RUN pip install -r requirements.txt diff --git a/cashonly/__init__.py b/cashonly/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/config/copysecret.py b/config/copysecret.py new file mode 100644 index 0000000..50d5da3 --- /dev/null +++ b/config/copysecret.py @@ -0,0 +1,18 @@ +fp = open("/django/server/settings.py") +key = "" +for line in fp: + if line.startswith("SECRET_KEY"): + key = line.split(" ")[2] + break +fp.close() + +with open('/django/config/settings.py', 'r') as file: + data = file.readlines() + +for i, line in enumerate(data): + if line.startswith("SECRET_KEY"): + data[i] = "{} = {}".format("SECRET_KEY", key) + break + +with open('/django/config/settings.py', 'w') as file: + file.writelines(data) diff --git a/config/init.sh b/config/init.sh new file mode 100755 index 0000000..5d5dfe2 --- /dev/null +++ b/config/init.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +django-admin startproject server . +python config/copysecret.py +mv config/* server/ +rm -rf config +python manage.py migrate +echo "from django.contrib.auth.models import User; User.objects.create_superuser('${ADMIN_USER}', '${ADMIN_EMAIL}', '${ADMIN_PW}')" | python manage.py shell +python manage.py runserver 0.0.0.0:8000 \ No newline at end of file diff --git a/config/settings.py b/config/settings.py new file mode 100644 index 0000000..e3d2369 --- /dev/null +++ b/config/settings.py @@ -0,0 +1,123 @@ +""" +Django settings for djangoserver project. + +Generated by 'django-admin startproject' using Django 1.11.20. + +For more information on this file, see +https://docs.djangoproject.com/en/1.11/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/1.11/ref/settings/ +""" + +import os + +# Build paths inside the project like this: os.path.join(BASE_DIR, ...) +BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = '&h!kbrmk)la2m%+%-qmdj1&ifg^rj3$o*)sos2dedw$$b4_lp$' + +# 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', + 'cashonly.core', + 'cashonly.web', + 'bootstrapform', +] + +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 = 'server.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 = 'server.wsgi.application' + +# Database +# https://docs.djangoproject.com/en/1.11/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.postgresql', + 'NAME': 'postgres', + 'USER': 'postgres', + 'PASSWORD': os.environ.get("POSTGRES_PASSWORD"), + 'HOST': os.environ.get("POSTGRES_HOST"), + 'PORT': 5432, # default + } +} + +# Password validation +# https://docs.djangoproject.com/en/1.11/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', + }, +] + +LOGIN_URL = '/login/' +LOGIN_REDIRECT_URL = '/' + +# Internationalization +# https://docs.djangoproject.com/en/1.11/topics/i18n/ + +LANGUAGE_CODE = 'de-de' +TIME_ZONE = 'UTC' +USE_I18N = True +USE_L10N = True +USE_TZ = True + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/1.11/howto/static-files/ + +STATIC_URL = '/static/' +STATICFILES_DIRS = ['/django/static_thirdparty'] + +# Cashonly Stuff +# https://git.blinkenbunt.org/cashonly/cashonly +CASHONLY_DAILY_DIGEST_RANGE_HOURS = 24 +CASHONLY_DEFAULT_DEBIT_LIMIT = 35 diff --git a/config/urls.py b/config/urls.py new file mode 100644 index 0000000..5434817 --- /dev/null +++ b/config/urls.py @@ -0,0 +1,27 @@ +"""djangoserver URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/1.11/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. Import the include() function: from django.conf.urls import url, include + 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls')) +""" +from django.conf.urls import url, include +from django.contrib import admin +from django.contrib.auth import views as auth_views +from django.conf.urls.static import static +from django.conf import settings + +urlpatterns = [ + url(r'^admin/', admin.site.urls), + url(r'', include('cashonly.web.urls')), + url(r'^login/$', auth_views.LoginView.as_view(template_name='cashonly/web/login.html'), name='login'), + url(r'^logout/$', auth_views.logout_then_login, name='logout_then_login'), +] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) diff --git a/config/wsgi.py b/config/wsgi.py new file mode 100644 index 0000000..f5f75f0 --- /dev/null +++ b/config/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for server 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.11/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "server.settings") + +application = get_wsgi_application() diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..c747d0a --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,25 @@ +version: '3' + +services: + db: + container_name: cashonly_db + image: postgres + restart: always + environment: + POSTGRES_PASSWORD: 2z4wMKcVcCio66o6 + + web: + container_name: cashonly_web + build: . + restart: always + command: bash -c "/django/config/init.sh" + ports: + - 80:8000 + depends_on: + - db + environment: + ADMIN_USER: admin + ADMIN_PW: changeme + ADMIN_EMAIL: example@admin.me + POSTGRES_HOST: cashonly_db + POSTGRES_PASSWORD: 2z4wMKcVcCio66o6 diff --git a/requirements.txt b/requirements.txt index a87af30..7e1a715 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ Django==1.11.20 django-bootstrap-form==3.4 +psycopg2-binary Pillow