# Installation from current git on debian wheezy
# -----------------------------------------------

# 1. Install dependencies
apt-get install python-django-tagging python-django-south python-django \
python-django-extensions python-argparse python-lxml python-rpm python-debian \
python-pygooglechart python-cracklib python-progressbar libapache2-mod-wsgi \
apache2

# 2. Check out current git to e.g. /srv/patchman
cd /srv
git clone https://github.com/furlongm/patchman.git

# 3. Copy server settings example file to /etc/patchman
mkdir /etc/patchman && cp /srv/patchman/etc/settings.py.sample /etc/patchman/settings.py

# 4. Modify settings file to suit your site
vi /etc/patchman/settings.py

# e.g. for a mysql backend:
#
# Make sure mysql-server and the python mysql bindings are installed:
#
# apt-get install mysql-server python-mysqldb
#
# Create database and users:
#
#mysql> CREATE DATABASE patchman CHARACTER SET utf8 COLLATE utf8_general_ci;
#Query OK, 1 row affected (0.00 sec)
#
#mysql> GRANT ALL PRIVILEGES ON patchman.* TO patchman@localhost IDENTIFIED BY 'changeme';
#Query OK, 0 rows affected (0.00 sec)
#
# Then modify /etc/patchman/settings.py to have the following:
#
#ADMINS = (
#    ('Patchman Admin', 'you@example.com'),
#)
#
#DATABASES = {
#    'default': {
#        'ENGINE': 'django.db.backends.mysql',    # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
#        'NAME': 'patchman',                      # Or path to database file if using sqlite3.
#        'USER': 'patchman',                      # Not used with sqlite3.
#        'PASSWORD': 'changeme',                  # Not used with sqlite3.
#        'HOST': '',                               # Set to empty string for localhost. Not used with sqlite3.
#        'PORT': '',                               # Set to empty string for default. Not used with sqlite3.
#        'STORAGE_ENGINE': 'INNODB',
#        'CHARSET' : 'utf8'
#    }
#}

# 5. Enable mod-wsgi and copy the apache conf file
a2enmod wsgi
cp /srv/patchman/etc/patchman-apache.conf /etc/apache2/conf.d/patchman.conf

# 6. Edit the networks allowed to report to apache and reload apache
vi /etc/apache2/conf.d/patchman.conf
service apache2 reload

# 7. Initialise the database, perform migrations, and collect static files.
cd /srv/patchman/patchman
./manage.py syncdb
./manage.py migrate
./manage.py collectstatic

# If the migrate fails with an error such as
# django.db.utils.DatabaseError: (1050, "Table 'operatingsystems_osgroup' already exists")
# you may need to run the migrations for each app manually, ending with the troublesome one. e.g.
#
# ./manage.py migrate hosts
# ./manage.py migrate repos
# ./manage.py migrate arch
# ./manage.py migrate domains
# ./manage.py migrate reports
# ./manage.py migrate packages
# ./manage.py migrate operatingsystems
# ./manage.py migrate --list

# 8. Deploy the client and/or yum/apt plugins via e.g. puppet, and add a daily
# cronjob, e.g.
# 0 7 * * * /bin/sleep $((RANDOM\%600)); PATH=/bin:/sbin:/usr/bin:/usr/sbin /usr/local/sbin/patchman-client

# 9. Optionally install celeryd/rabbitmq for realtime processing of reports:
apt-get install python-django-celery rabbitmq-server
./manage.py syncdb
./manage.py migrate
./manage.py celeryd_detach
# add the last command to an initscript (e.g. /etc/rc.local) to make it
# persistent over reboot

# i0. Alternatively, or in addition to 9, set up a cronjob to run all the daily tasks, e.g.
#
# 30 8 * * * http_proxy=http://myproxy.example.com:3128 PYTHONPATH=/srv/patchman/patchman /srv/patchman/sbin/patchman -a -q
#
# Run PYTHONPATH=/srv/patchman/patchman /srv/patchman/sbin/patchman -h to view all options.

# 11. If you want to run patchman under a subdirectory
#   e.g. http://example.com/mypatchman/
# then you need to add the following to /etc/patchman/settings.py
#  LOGIN_REDIRECT_URL = '/mypatchman/'
#  LOGIN_URL = '/mypatchman/accounts/login/'
# and make the appropriate changes in /etc/apache2/conf.d/patchman.conf

# 12. You can optionally run memcached:
apt-get install memcached python-memcache
# and add the following to /etc/patchman/settings.py
#CACHES = {
#    'default': {
#        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
#        'LOCATION': '127.0.0.1:11211',
#    }
#}
