From 1eef581123b86fbb4f92fc06eb04e812b9fec35c Mon Sep 17 00:00:00 2001
From: Victor Seva <vseva@sipwise.com>
Date: Wed, 29 Apr 2015 13:23:51 +0200
Subject: [PATCH] MT#12487 debian packaging

Change-Id: Ifedb26186ae7c167172398f1c3d1536c48a37350
---
 Makefile                  | 19 ++-----------------
 debian/changelog          |  5 +++++
 debian/compat             |  1 +
 debian/control            | 19 +++++++++++++++++++
 debian/copyright          | 28 ++++++++++++++++++++++++++++
 debian/dirs               |  3 +++
 debian/install            |  5 +++++
 debian/links              |  1 +
 debian/rules              | 25 +++++++++++++++++++++++++
 debian/source/format      |  1 +
 repoapi/repoapi_uwsgi.ini | 21 ++++++++++++---------
 repoapi/settings/dev.py   |  2 +-
 repoapi/settings/prod.py  |  9 ++++++---
 repoapi/wsgi.py           |  2 +-
 requirements/common.txt   |  2 ++
 requirements/prod.txt     |  1 -
 requirements/test.txt     |  1 -
 17 files changed, 112 insertions(+), 33 deletions(-)
 create mode 100644 debian/changelog
 create mode 100644 debian/compat
 create mode 100644 debian/control
 create mode 100644 debian/copyright
 create mode 100644 debian/dirs
 create mode 100644 debian/install
 create mode 100644 debian/links
 create mode 100755 debian/rules
 create mode 100644 debian/source/format

diff --git a/Makefile b/Makefile
index e1c1a79..5d7fe90 100644
--- a/Makefile
+++ b/Makefile
@@ -29,18 +29,9 @@ test: venv_test
 		./manage.py jenkins --settings="repoapi.settings.dev"
 
 deploy: venv_prod
-	mkdir -p ./venv_prod/etc/uwsgi/vassals/
-	[ -L ./venv_prod/etc/uwsgi/vassals/repoapi_uwsgi.ini ] || \
-		ln -s $(shell pwd)/repoapi/repoapi_uwsgi.ini \
-			./venv_prod/etc/uwsgi/vassals/
-	touch ./venv_prod/etc/uwsgi/vassals/repoapi_uwsgi.ini
-	chown jenkins:www-data -R ./venv_prod/etc/uwsgi
 	source ./venv_prod/bin/activate && \
 		./manage.py collectstatic --noinput --settings="repoapi.settings.prod"
-	chown jenkins:www-data -R ./static_media/ && chmod 660 -R ./static_media/
-	touch /var/log/uwsgi-repoapi.log && \
-		chown jenkins:www-data /var/log/uwsgi-repoapi.log && \
-		chmod 664 /var/log/uwsgi-repoapi.log
+	chown www-data:www-data -R ./static_media/
 
 ###################################
 
@@ -48,17 +39,11 @@ run_dev: venv_dev
 	source ./venv_dev/bin/activate && \
 		./manage.py runserver_plus --settings="repoapi.settings.dev"
 
-run: deploy
-	mkdir -p ./venv_prod/run
-	chown jenkins:www-data ./venv_prod/run && chmod 770 ./venv_prod/run
-	source ./venv_prod/bin/activate && \
-		uwsgi --emperor ./venv_prod/etc/uwsgi/vassals/ \
-			--uid www-data --gid www-data
-
 ###################################
 
 # get rid of test files
 clean:
+	find . -type f -name '*.pyc' -exec rm {} \;
 	rm -rf reports install.log
 
 # also get rid of virtual environments
diff --git a/debian/changelog b/debian/changelog
new file mode 100644
index 0000000..63dad8f
--- /dev/null
+++ b/debian/changelog
@@ -0,0 +1,5 @@
+repoapi (0.1.0) unstable; urgency=low
+
+  * Initial Release.
+
+ -- Victor Seva <vseva@sipwise.com>  Wed, 29 Apr 2015 12:57:17 +0200
diff --git a/debian/compat b/debian/compat
new file mode 100644
index 0000000..ec63514
--- /dev/null
+++ b/debian/compat
@@ -0,0 +1 @@
+9
diff --git a/debian/control b/debian/control
new file mode 100644
index 0000000..b19ae11
--- /dev/null
+++ b/debian/control
@@ -0,0 +1,19 @@
+Source: repoapi
+Priority: extra
+Maintainer: Sipwise Development Team <support@sipwise.com>
+Build-Depends: debhelper (>= 8~), python-virtualenv, python-dev
+Standards-Version: 3.9.6
+Section: python
+Homepage: http://sipwise.com/
+
+Package: repoapi
+Section: python
+Architecture: all
+Depends: make,
+         uwsgi-plugin-python,
+         python,
+         python-virtualenv,
+         sqlite3,
+         ${misc:Depends}
+Description: REST API webapp
+ This package provides repoapi webapp.
diff --git a/debian/copyright b/debian/copyright
new file mode 100644
index 0000000..ad41f66
--- /dev/null
+++ b/debian/copyright
@@ -0,0 +1,28 @@
+Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
+Upstream-Name: repoapi
+Source: git://git.mgm.sipwise.com/repoapi
+
+Files: *
+Copyright: 2015 Sipwise GmbH, Austria
+License: GPL-3.0+
+
+Files: debian/*
+Copyright: 2015 SipWise Team <development@sipwise.com>
+License: GPL-3.0+
+
+License: GPL-3.0+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+ .
+ This package is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ GNU General Public License for more details.
+ .
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+ .
+ On Debian systems, the complete text of the GNU General
+ Public License version 3 can be found in "/usr/share/common-licenses/GPL-3".
diff --git a/debian/dirs b/debian/dirs
new file mode 100644
index 0000000..deb40f9
--- /dev/null
+++ b/debian/dirs
@@ -0,0 +1,3 @@
+/usr/share/repoapi
+/etc/uwsgi/apps-available
+/var/lib/repoapi
diff --git a/debian/install b/debian/install
new file mode 100644
index 0000000..c68f192
--- /dev/null
+++ b/debian/install
@@ -0,0 +1,5 @@
+Makefile usr/share/repoapi
+manage.py usr/share/repoapi
+repoapi usr/share/repoapi
+requirements usr/share/repoapi
+static_media usr/share/repoapi
diff --git a/debian/links b/debian/links
new file mode 100644
index 0000000..ee1fa24
--- /dev/null
+++ b/debian/links
@@ -0,0 +1 @@
+/usr/share/repoapi/repoapi/repoapi_uwsgi.ini /etc/uwsgi/apps-available/repoapi.ini
diff --git a/debian/rules b/debian/rules
new file mode 100755
index 0000000..758789b
--- /dev/null
+++ b/debian/rules
@@ -0,0 +1,25 @@
+#!/usr/bin/make -f
+# -*- makefile -*-
+# Sample debian/rules that uses debhelper.
+# This file was originally written by Joey Hess and Craig Small.
+# As a special exception, when this file is copied by dh-make into a
+# dh-make output file, you may use that output file without restriction.
+# This special exception was added by Craig Small in version 0.37 of dh-make.
+
+# Uncomment this to turn on verbose mode.
+#export DH_VERBOSE=1
+
+%:
+	dh $@
+
+override_dh_auto_test:
+
+override_dh_auto_install:
+	echo "fakesecretkey" > .secret_key
+	make deploy
+	make clean
+
+override_dh_fixperms:
+	dh_fixperms
+	chmod 4750 debian/repoapi/var/lib/repoapi
+	chown www-data:www-data debian/repoapi/var/lib/repoapi
diff --git a/debian/source/format b/debian/source/format
new file mode 100644
index 0000000..89ae9db
--- /dev/null
+++ b/debian/source/format
@@ -0,0 +1 @@
+3.0 (native)
diff --git a/repoapi/repoapi_uwsgi.ini b/repoapi/repoapi_uwsgi.ini
index e426019..98eebb5 100644
--- a/repoapi/repoapi_uwsgi.ini
+++ b/repoapi/repoapi_uwsgi.ini
@@ -2,22 +2,25 @@
 
 # Django-related settings
 # the base directory (full path)
-chdir           = /srv/repoapi
+chdir           = /usr/share/repoapi
 # Django's wsgi file
-module          = repoapi.wsgi
+module          = repoapi.wsgi:application
 # the virtualenv (full path)
-home            = /srv/repoapi/venv_prod
+home            = /usr/share/repoapi/venv_prod
+# django setting
+env = DJANGO_SETTINGS_MODULE=repoapi.settings.prod
 
+# This is defined by default on uwsgi
 # process-related settings
 # master
-master          = true
+#master          = true
 # maximum number of worker processes
-processes       = 5
+#processes       = 5
 # the socket (use the full path to be safe
-socket          = /srv/repoapi/venv_prod/run/repoapi.sock
+#socket          = /usr/share/repoapi/venv_prod/run/repoapi.sock
 # ... with appropriate permissions - may be needed
 # chmod-socket    = 664
 # clear environment on exit
-vacuum          = true
-logto = /var/log/uwsgi-repoapi.log
-env = DJANGO_SETTINGS_MODULE=repoapi.settings.prod
+#vacuum          = true
+#logto = /var/log/uwsgi-repoapi.log
+
diff --git a/repoapi/settings/dev.py b/repoapi/settings/dev.py
index b94a194..22f9194 100644
--- a/repoapi/settings/dev.py
+++ b/repoapi/settings/dev.py
@@ -16,7 +16,7 @@
 # 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__)))
+BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
 
 
 # Quick-start development settings - unsuitable for production
diff --git a/repoapi/settings/prod.py b/repoapi/settings/prod.py
index dba711a..2b5dc1d 100644
--- a/repoapi/settings/prod.py
+++ b/repoapi/settings/prod.py
@@ -16,15 +16,18 @@
 # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
 import os
 
-BASE_DIR = '/srv/repoapi'
+BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
 
+VAR_DIR = '/var/lib/repoapi'
+if not os.path.exists(VAR_DIR):
+    VAR_DIR = BASE_DIR
 
 # 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!
 # read it from external file
-SECRET_KEY = open(os.path.join(BASE_DIR, '.secret_key')).read().strip()
+SECRET_KEY = open(os.path.join(VAR_DIR, '.secret_key')).read().strip()
 
 # SECURITY WARNING: don't run with debug turned on in production!
 DEBUG = False
@@ -89,7 +92,7 @@ WSGI_APPLICATION = 'repoapi.wsgi.application'
 DATABASES = {
     'default': {
         'ENGINE': 'django.db.backends.sqlite3',
-        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
+        'NAME': os.path.join(VAR_DIR, 'db.sqlite3'),
     }
 }
 
diff --git a/repoapi/wsgi.py b/repoapi/wsgi.py
index 4e6dcca..5fa0b6f 100644
--- a/repoapi/wsgi.py
+++ b/repoapi/wsgi.py
@@ -25,6 +25,6 @@ import os
 
 from django.core.wsgi import get_wsgi_application
 
-os.environ.setdefault("DJANGO_SETTINGS_MODULE", "repoapi.settings")
+os.environ.setdefault("DJANGO_SETTINGS_MODULE", "repoapi.settings.prod")
 
 application = get_wsgi_application()
diff --git a/requirements/common.txt b/requirements/common.txt
index 97fa1ab..48f7d58 100644
--- a/requirements/common.txt
+++ b/requirements/common.txt
@@ -6,3 +6,5 @@ djangorestframework
 django-rest-swagger
 markdown
 django-filter
+six
+webassets
diff --git a/requirements/prod.txt b/requirements/prod.txt
index c1dd14b..6624a30 100644
--- a/requirements/prod.txt
+++ b/requirements/prod.txt
@@ -1,2 +1 @@
 -r common.txt
-uwsgi
diff --git a/requirements/test.txt b/requirements/test.txt
index 13b4bbe..f9995eb 100644
--- a/requirements/test.txt
+++ b/requirements/test.txt
@@ -2,6 +2,5 @@
 django-nose
 git+https://github.com/linuxmaniac/django-jenkins.git@vseva/nose#egg=django-jenkins
 flake8
-six
 pylint
 coverage