From d8c7b4ec377947d27e9d43e18cd8486c56e4eac3 Mon Sep 17 00:00:00 2001 From: Tim Sutton Date: Fri, 30 Aug 2013 16:57:19 +0200 Subject: [PATCH] #5 We need to support motion webcam daemon --- fabgis/motion.py | 106 ++++++++++++++++++ .../apache/fabgis.motion.conf.templ | 9 ++ 2 files changed, 115 insertions(+) create mode 100644 fabgis/motion.py create mode 100644 fabgis_resources/server_config/apache/fabgis.motion.conf.templ diff --git a/fabgis/motion.py b/fabgis/motion.py new file mode 100644 index 0000000..5615b59 --- /dev/null +++ b/fabgis/motion.py @@ -0,0 +1,106 @@ +# coding=utf-8 +"""Motion Web Cam +================== + + +This module provides tools for setting up the motion web cam daemon. + +http://www.lavrsen.dk/foswiki/bin/view/Motion/WebHome + +""" +import os +from fabgis.fabgis import sed +from fabric.api import cd, task, sudo, fastprint, run +from fabric.colors import green +from fabric.contrib.files import upload_template +from fabtools import require +from .common import setup_env + + +@task +def setup_motion(email): + """Set up the motion web cam daemon. + + :param email: Email address to send motion alerts to. + :type email: str + + """ + require.deb.package('motion') + require.deb.package('mpack') + sed( + '/etc/default/motion', + 'start_motion_daemon=no', + 'start_motion_daemon=yes', + use_sudo=True) + sed( + '/etc/default/motion', + '; on_movie_end value', + ('on_movie_end mpack -s \'[Motion Alert] %%Y-%%m-%%d %%H:%%M:%%S\' ' + '/tmp/motion/%f %s' % email), + use_sudo=True) + require.service.restarted('motion') + + +@task +def setup_apache( + site_name, + web_root, + template_dir=None, + **kwargs): + """Set up the apache server for this site. + + :param site_name: Name of the site e.g. cam.linfiniti.com. Should be a + single word with only alpha characters in it. + :type site_name: str + + :param web_root: Directory where the content lives. + :type web_root: str + + :param template_dir: Directory where the template files live. If none + will default to ``resources/server_config/apache``. Must be a + relative path to the fabfile you are running. + :type template_dir: str + + :param kwargs: Any extra keyword arguments that should be appended to the + token list that will be used when rendering the apache config template. + Use this to pass in sensitive data such as passwords. + :type kwargs: dict + + :returns: Path to the apache conf file. + :rtype: str + """ + setup_env() + # Ensure we have a mailserver setup for our domain + # Note that you may have problems if you intend to run more than one + # site from the same server + require.postfix.server(site_name) + + # Clone and replace tokens in apache conf + if template_dir is None: + template_dir = 'resources/server_config/apache/' + filename = '%s.apache.conf.templ' % site_name + template_path = os.path.join(template_dir, filename) + fastprint(green('Using %s for template' % template_path)) + + context = { + 'escaped_server_name': site_name.replace('.', '\.'), + 'server_name': site_name, + 'web_root': web_root, + 'site_name': site_name} + context.update(kwargs) # merge in any params passed in to this function + destination = '/etc/apache2/sites-available/%s.apache.conf' % site_name + fastprint(context) + + upload_template( + template_path, + destination, + context=context, + use_sudo=True) + + require.deb.package('apache2') + sudo('a2dissite default') + sudo('a2ensite %s.apache.conf' % site_name) + # Check if apache configs are ok - script will abort if not ok + sudo('/usr/sbin/apache2ctl configtest') + require.service.restarted('apache2') + return destination diff --git a/fabgis_resources/server_config/apache/fabgis.motion.conf.templ b/fabgis_resources/server_config/apache/fabgis.motion.conf.templ new file mode 100644 index 0000000..b879094 --- /dev/null +++ b/fabgis_resources/server_config/apache/fabgis.motion.conf.templ @@ -0,0 +1,9 @@ + + ServerName %(site_name)s + ServerAdmin %(server_admin)s + LogLevel warn + CustomLog /var/log/apache2/%(site_name)s.access.log combined + ErrorLog /var/log/apache2/%(site_name)s.error.log + DocumentRoot '%s(web_root)s' + ServerSignature Off +