{{ post.title }}
+{{ post.text|linebreaksbr }}
+ {% if post.image %} + + {% endif %} +diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e26903f --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.*.swp + +*.py[cod] + +db.sqlite3 diff --git a/.s2i/action_hooks/build b/.s2i/action_hooks/build new file mode 100755 index 0000000..ceaac45 --- /dev/null +++ b/.s2i/action_hooks/build @@ -0,0 +1,37 @@ +#!/bin/bash + +# This is a 'build' action hook script. This script must be executable +# and will be run by the S2I process after the original S2I 'assemble' +# script has been run. This hook is to allow a user to run additional +# build steps which may need the source code or build artefacts in +# place, or to setup any data required for the application. + +set -eo pipefail + +# Dump out the set of environment variables which were used by the build. + +echo " -----> Initial environment variables set by builder image." + +env + +# Dump out the name of the current working directory. + +echo " -----> Current working directory." + +pwd + +# Run the application specific build steps for this project. + +echo " -----> Running collection of Django static resources." + +python manage.py collectstatic --noinput + +echo " -----> Creating directory for uploaded image files." + +mkdir -p media/images && chmod g+w media/images + +# Dump out the contents of the current working directory. + +echo " -----> Contents of the current working directory after build run." + +ls -R . diff --git a/.s2i/action_hooks/build_env b/.s2i/action_hooks/build_env new file mode 100644 index 0000000..140d6ea --- /dev/null +++ b/.s2i/action_hooks/build_env @@ -0,0 +1,17 @@ +# This is a 'build_env' action hook. This script will be executed inline +# to the custom 'assemble' script and therefore doesn't need to start +# with a '#!' line nor be marked as an executable file. This script will +# be interpreted as if it is a 'bash' script. This script can be used to +# dynamically set additional environment variables required by the build +# process. These might for example be environment variables which +# specify where third party libraries or packages installed from the +# 'pre_build' hook are located and which may be needed when building +# application artefacts. When we source the 'build_env' script, any +# environment variables set by it will be automatically exported. +# Although principally intended for setting environment variables, it +# can include other script logic, but this should be avoided exception +# to the extent it is required to work out what to set any environment +# variables to. + +BUILD_OVERRIDE_VALUE=value +BUILD_DEFAULT_VALUE=${BUILD_DEFAULT_VALUE:-default} diff --git a/.s2i/action_hooks/deploy b/.s2i/action_hooks/deploy new file mode 100755 index 0000000..77028d2 --- /dev/null +++ b/.s2i/action_hooks/deploy @@ -0,0 +1,39 @@ +#!/bin/bash + +# This is a 'deploy' action hook script. This script must be executable +# and will be run by the S2I process just before the original S2I 'run' +# script is run. This script is to allow a user to run any final steps +# just before the application is to be started. This can include running +# background tasks. + +set -eo pipefail + +# Dump out the set of environment variables which were used by the build. + +echo " -----> Environment variables set for the deployed application." + +env + +# Dump out the name of the current working directory. + +echo " -----> Current working directory." + +pwd + +# Run the application specific deploy steps for this project. + +if [ x"$DATABASE_URL" != x"" ]; then + echo " -----> Running Django database table migrations." + + python manage.py migrate --noinput +else + DJANGO_ADMIN_USERNAME=developer \ + DJANGO_ADMIN_EMAIL=developer@example.com \ + DJANGO_ADMIN_PASSWORD=developer `dirname $0`/setup +fi + +# Dump out the contents of the current working directory. + +echo " -----> Contents of the current working directory after build run." + +ls -R . diff --git a/.s2i/action_hooks/deploy_env b/.s2i/action_hooks/deploy_env new file mode 100644 index 0000000..cf068fa --- /dev/null +++ b/.s2i/action_hooks/deploy_env @@ -0,0 +1,16 @@ +# This is a 'deploy_env' action hook. This script will be executed +# inline to the custom 'run' script as well as for any interactive shell +# if so enabled. As it is executed inline, it doesn't need to start with +# a '#!' line nor be marked as an executable file. This script will be +# interpreted as if it is a 'bash' script. This script allows a user to +# dynamically set additional environment variables required by the +# deploy process. These might for example be environment variables which +# tell an application where files it requires are located. When we +# source the 'deploy_env' script, any environment variables set by it +# will be automatically exported. Although principally intended for +# setting environment variables, it can include other script logic, but +# this should be avoided exception to the extent it is required to work +# out what to set any environment variables to. + +DEPLOY_OVERRIDE_VALUE=value +DEPLOY_DEFAULT_VALUE=${DEPLOY_DEFAULT_VALUE:-default} diff --git a/.s2i/action_hooks/pre_build b/.s2i/action_hooks/pre_build new file mode 100755 index 0000000..c7e8d2c --- /dev/null +++ b/.s2i/action_hooks/pre_build @@ -0,0 +1,36 @@ +#!/bin/bash + +# This is a 'pre_build' action hook script. This script must be +# executable and will be run by the S2I process as the very first step. +# This script can be used to install additional third party libraries +# or packages that may be required by the build process. If the +# 'pre_build' hook needs any files from the application source code, it +# must grab them from the '/tmp/src' directory as they will only be +# copied into place by the original S2I 'assemble' script later on in +# the build process. + +set -eo pipefail + +# Dump out the initial set of environment variables. + +echo " -----> Initial environment variables set by builder image." + +env + +# Dump out the contents of the '/tmp/src' directory. + +echo " -----> Initial contents of the /tmp/src directory." + +ls -R /tmp/src + +# Dump out the name of the current working directory. + +echo " -----> Current working directory." + +pwd + +# Dump out the contents of the current working directory. + +echo " -----> Initial contents of the current working directory." + +ls -R . diff --git a/.s2i/action_hooks/setup b/.s2i/action_hooks/setup new file mode 100755 index 0000000..dc11fd2 --- /dev/null +++ b/.s2i/action_hooks/setup @@ -0,0 +1,24 @@ +#!/bin/bash + +echo " -----> Running Django database table migrations." + +python manage.py migrate --noinput + +if [ x"$DJANGO_ADMIN_USERNAME" != x"" ]; then + echo " -----> Creating predefined Django super user" + (cat - | python manage.py shell) << ! +from django.contrib.auth.models import User; +User.objects.create_superuser('$DJANGO_ADMIN_USERNAME', + '$DJANGO_ADMIN_EMAIL', + '$DJANGO_ADMIN_PASSWORD') +! +else + if (tty > /dev/null 2>&1); then + echo " -----> Running Django super user creation" + python manage.py createsuperuser + fi +fi + +echo " -----> Pre-loading Django database with blog posts." + +python manage.py loaddata `dirname $0`/../../posts.json diff --git a/.s2i/bin/assemble b/.s2i/bin/assemble new file mode 100755 index 0000000..ac3a7a5 --- /dev/null +++ b/.s2i/bin/assemble @@ -0,0 +1,108 @@ +#!/bin/bash + +echo " -----> Running application assemble script." + +# Ensure we fail fast if there is a problem. + +set -eo pipefail + +# It is presumed the current working directory is where the application +# code will reside, or at least that this directory is writable and we +# can add our own files for hook scripts there. This would normally be +# the HOME directory for the builder account. + +S2I_SOURCE_PATH=`pwd` +export S2I_SOURCE_PATH + +# At this point the application source code is still located under the +# directory '/tmp/src'. The source code or application artefacts +# compiled from it will not be moved into place until the original +# 'assemble' script is run. What we will do at this point is move the +# contents of the '.s2i' directory across so that any hook scripts are +# where we need them to be. This shouldn't upset anything as the 's2i' +# program has already extracted out the '.s2i/bin' directory and other +# files it wants separately. + +test -d /tmp/src/.s2i && mv /tmp/src/.s2i . + +# Now run the 'pre_build' hook from the '.s2i/action_hooks' directory if +# it exists. This hook is to allow a user to install additional third +# party libraries or packages that may be required by the build process. +# If the 'pre_build' hook needs any files from the application source +# code, it must grab them from the '/tmp/src' directory. + +if [ -f $S2I_SOURCE_PATH/.s2i/action_hooks/pre_build ]; then + if [ ! -x $S2I_SOURCE_PATH/.s2i/action_hooks/pre_build ]; then + echo "WARNING: Script $S2I_SOURCE_PATH/.s2i/action_hooks/pre_build not executable." + else + echo " -----> Running $S2I_SOURCE_PATH/.s2i/action_hooks/pre_build" + $S2I_SOURCE_PATH/.s2i/action_hooks/pre_build + fi +fi + +# Now source the 'build_env' script from the '.s2i/action_hooks' +# directory if it exists. This script allows a user to dynamically set +# additional environment variables required by the build process. These +# might for example be environment variables which specify where third +# party libraries or packages installed form the 'pre_build' hook are +# located and which may be needed when building application artefacts. +# When we source the 'build_env' script, any environment variables set +# by it will be automatically exported. + +if [ -f $S2I_SOURCE_PATH/.s2i/action_hooks/build_env ]; then + echo " -----> Running $S2I_SOURCE_PATH/.s2i/action_hooks/build_env" + S2I_SHELL_PWD=$PWD + set -a; . $S2I_SOURCE_PATH/.s2i/action_hooks/build_env; set +a + cd $S2I_SHELL_PWD +fi + +# Now run the original 'assemble' script. This will move source code into +# the correct location or otherwise build application artefacts from the +# source code and move that into place. + +echo " -----> Running builder assemble script ($S2I_SCRIPTS_PATH/assemble)" + +$S2I_SCRIPTS_PATH/assemble + +# Now run the 'build' hook from '.s2i/action_hooks' directory if it +# exists. This hook is to allow a user to run additional build steps +# which may need the source code or build artefacts in place, or to +# setup any data required for the application. + +if [ -f $S2I_SOURCE_PATH/.s2i/action_hooks/build ]; then + if [ ! -x $S2I_SOURCE_PATH/.s2i/action_hooks/build ]; then + echo "WARNING: Script $S2I_SOURCE_PATH/.s2i/action_hooks/build not executable." + else + echo " -----> Running $S2I_SOURCE_PATH/.s2i/action_hooks/build" + $S2I_SOURCE_PATH/.s2i/action_hooks/build + fi +fi + +# Now fix up the shell login environment so it will trigger 'deploy_env'. + +if [ x"$S2I_BASH_ENV" != x"" ]; then + if [ -f $S2I_BASH_ENV ]; then + cat >> $S2I_BASH_ENV << EOF +# Now source the 'deploy_env' script from the '.s2i/action_hooks' +# directory if it exists. This script allows a user to dynamically set +# additional environment variables required by the deploy process. These +# might for example be environment variables which tell an application +# where files it requires are located. When we source the 'deploy_env' +# script, any environment variables set by it will be automatically +# exported. Note that we only source the 'deploy_env' script if it +# hasn't already been run. + +if [ x"S2I_MARKERS_ENVIRON" != x"" ]; then + S2I_MARKERS_ENVIRON=OK + export S2I_MARKERS_ENVIRON + + if [ -f $S2I_SOURCE_PATH/.s2i/action_hooks/deploy_env ]; then + S2I_SHELL_PWD=$PWD + cd $S2I_SOURCE_PATH + set -a; . $S2I_SOURCE_PATH/.s2i/action_hooks/deploy_env; set +a + cd $S2I_SHELL_PWD + fi +fi +EOF + fi +fi diff --git a/.s2i/bin/run b/.s2i/bin/run new file mode 100755 index 0000000..054d463 --- /dev/null +++ b/.s2i/bin/run @@ -0,0 +1,60 @@ +#!/bin/bash + +echo " -----> Running application run script." + +# Ensure we fail fast if there is a problem. + +set -eo pipefail + +# It is presumed the current working directory is where the application +# code will reside, or at least that this directory is writable and we +# can add our own files for hook scripts there. This would normally be +# the HOME directory for the builder account. + +S2I_SOURCE_PATH=`pwd` +export S2I_SOURCE_PATH + +# Now source the 'deploy_env' script from the '.s2i/action_hooks' +# directory if it exists. This script allows a user to dynamically set +# additional environment variables required by the deploy process. These +# might for example be environment variables which tell an application +# where files it requires are located. When we source the 'deploy_env' +# script, any environment variables set by it will be automatically +# exported. Note that we only source the 'deploy_env' script if it hasn't +# already been run. It could have already been run from the shell login +# environment. + +if [ x"S2I_MARKER_ENVIRON" != x"" ]; then + S2I_MARKER_ENVIRON=`date` + export S2I_MARKER_ENVIRON + + if [ -f $S2I_SOURCE_PATH/.s2i/action_hooks/deploy_env ]; then + echo " -----> Running $S2I_SOURCE_PATH/.s2i/action_hooks/deploy_env" + S2I_SHELL_PWD=$PWD + set -a; . $S2I_SOURCE_PATH/.s2i/action_hooks/deploy_env; set +a + cd $S2I_SHELL_PWD + fi +fi + +# Now run the 'deploy' hook from the '.s2i/action_hooks' directory if it +# exists. This hook is to allow a user to run any final steps just before +# the application is to be started. This can include running background +# tasks. + +if [ -f $S2I_SOURCE_PATH/.s2i/action_hooks/deploy ]; then + if [ ! -x $S2I_SOURCE_PATH/.s2i/action_hooks/deploy ]; then + echo "WARNING: Script $S2I_SOURCE_PATH/.s2i/action_hooks/deploy not executable." + else + echo " -----> Running $S2I_SOURCE_PATH/.s2i/action_hooks/deploy" + $S2I_SOURCE_PATH/.s2i/action_hooks/deploy + fi +fi + +# Now run the original 'run' script to start up the application. This +# must be run using 'exec' so that the original 'run' script will take +# over process ID 1. This is necessary so that the application will +# receive signals properly. + +echo " -----> Running builder run script ($S2I_SCRIPTS_PATH/run)" + +exec $S2I_SCRIPTS_PATH/run diff --git a/.s2i/environment b/.s2i/environment new file mode 100644 index 0000000..76be426 --- /dev/null +++ b/.s2i/environment @@ -0,0 +1,4 @@ +S2I_SCRIPTS_PATH=/usr/libexec/s2i +S2I_BASH_ENV=/opt/app-root/etc/scl_enable +DISABLE_COLLECTSTATIC=1 +DISABLE_MIGRATE=1 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..417440f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,23 @@ +FROM centos/python-35-centos7:latest + +USER root + +COPY . /tmp/src + +RUN mv /tmp/src/.s2i/bin /tmp/scripts + +RUN rm -rf /tmp/src/.git* && \ + chown -R 1001 /tmp/src && \ + chgrp -R 0 /tmp/src && \ + chmod -R g+w /tmp/src + +USER 1001 + +ENV S2I_SCRIPTS_PATH=/usr/libexec/s2i \ + S2I_BASH_ENV=/opt/app-root/etc/scl_enable \ + DISABLE_COLLECTSTATIC=1 \ + DISABLE_MIGRATE=1 + +RUN /tmp/scripts/assemble + +CMD [ "/tmp/scripts/run" ] diff --git a/app.sh b/app.sh new file mode 100755 index 0000000..48a241f --- /dev/null +++ b/app.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +exec python manage.py runmodwsgi --log-to-terminal --port 8080 \ + --url-alias /media media diff --git a/blog/__init__.py b/blog/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/blog/admin.py b/blog/admin.py new file mode 100644 index 0000000..47f03fd --- /dev/null +++ b/blog/admin.py @@ -0,0 +1,4 @@ +from django.contrib import admin +from .models import Post + +admin.site.register(Post) diff --git a/blog/apps.py b/blog/apps.py new file mode 100644 index 0000000..7930587 --- /dev/null +++ b/blog/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class BlogConfig(AppConfig): + name = 'blog' diff --git a/blog/forms.py b/blog/forms.py new file mode 100644 index 0000000..5482b27 --- /dev/null +++ b/blog/forms.py @@ -0,0 +1,9 @@ +from django import forms + +from .models import Post + +class PostForm(forms.ModelForm): + + class Meta: + model = Post + fields = ('title', 'text', 'image') diff --git a/blog/migrations/0001_initial.py b/blog/migrations/0001_initial.py new file mode 100644 index 0000000..3686ba4 --- /dev/null +++ b/blog/migrations/0001_initial.py @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.6 on 2017-03-15 03:24 +from __future__ import unicode_literals + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion +import django.utils.timezone + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name='Post', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('title', models.CharField(max_length=200)), + ('text', models.TextField()), + ('image', models.ImageField(upload_to='images/', verbose_name='Image')), + ('created_date', models.DateTimeField(default=django.utils.timezone.now)), + ('published_date', models.DateTimeField(blank=True, null=True)), + ('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + ), + ] diff --git a/blog/migrations/__init__.py b/blog/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/blog/models.py b/blog/models.py new file mode 100644 index 0000000..e825ecb --- /dev/null +++ b/blog/models.py @@ -0,0 +1,20 @@ +from django.db import models +from django.utils import timezone + + +class Post(models.Model): + author = models.ForeignKey('auth.User') + title = models.CharField(max_length=200) + text = models.TextField() + image = models.ImageField("Image", blank=True, null=True, upload_to="images/") + created_date = models.DateTimeField( + default=timezone.now) + published_date = models.DateTimeField( + blank=True, null=True) + + def publish(self): + self.published_date = timezone.now() + self.save() + + def __str__(self): + return self.title diff --git a/blog/static/css/blog.css b/blog/static/css/blog.css new file mode 100644 index 0000000..e32a7d3 --- /dev/null +++ b/blog/static/css/blog.css @@ -0,0 +1,46 @@ +.page-header { + background-color: #ff9400; + margin-top: 0; + padding: 20px 20px 20px 40px; +} + +.page-header h1, .page-header h1 a, .page-header h1 a:visited, .page-header h1 a:active { + color: #ffffff; + font-size: 36pt; + text-decoration: none; +} + +.content { + margin-left: 40px; +} + +h1, h2, h3, h4 { + font-family: 'Lobster', cursive; +} + +.date { + color: #828282; +} + +.save { + float: right; +} + +.post-form textarea, .post-form input { + width: 100%; +} + +.top-menu, .top-menu:hover, .top-menu:visited { + color: #ffffff; + float: right; + font-size: 26pt; + margin-right: 20px; +} + +.post { + margin-bottom: 70px; +} + +.post h1 a, .post h1 a:visited { + color: #000000; +} diff --git a/blog/templates/blog/base.html b/blog/templates/blog/base.html new file mode 100644 index 0000000..b2c574f --- /dev/null +++ b/blog/templates/blog/base.html @@ -0,0 +1,28 @@ +{% load staticfiles %} + +
+{{ post.text|linebreaksbr }}
+ {% if post.image %} + + {% endif %} +