hobo/hobo/agent/services.py

120 lines
4.0 KiB
Python

import ConfigParser
import fnmatch
import json
import logging
import os
import string
import subprocess
import urllib2
import urlparse
from django.conf import settings
logger = logging.getLogger('hobo.agent')
class BaseService(object):
def __init__(self, base_url, title, secret_key, **kwargs):
self.base_url = base_url
self.title = title
self.secret_key = secret_key
def is_for_us(self):
# This function checks if the requested service is to be hosted
# on this server, and return True if appropriate.
#
# It matches against a set of patterns taken from
# settings.AGENT_HOST_PATTERNS; patterns can be full hostname or use
# globbing characters (ex: "*.au-quotidien.com"); it is also possible
# to # prefix the pattern by an exclamation mark to exclude those ones
# (ex: "! *.dev.au-quotidien.com").
if not settings.AGENT_HOST_PATTERNS:
return True
patterns = settings.AGENT_HOST_PATTERNS.get(self.service_id)
if patterns is None:
return True
parsed_url = urllib2.urlparse.urlsplit(self.base_url)
netloc = parsed_url.netloc
match = False
for pattern in patterns:
if pattern.startswith('!'):
if fnmatch.fnmatch(netloc, pattern[1:].strip()):
match = False
break
else:
if fnmatch.fnmatch(netloc, pattern.strip()):
match = True
return match
def check_timestamp(self, timestamp):
'''Return True if site is uptodate'''
return False
def execute(self, environment):
domain = urlparse.urlparse(self.base_url).netloc.split(':')[0]
cmd_process = subprocess.Popen(self.service_manage_cmd + ' create_tenant ' + domain,
shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
stdout = cmd_process.communicate()
class Passerelle(BaseService):
service_id = 'passerelle'
service_manage_cmd = settings.PASSERELLE_MANAGE_COMMAND
class Wcs(BaseService):
service_id = 'wcs'
def __init__(self, **kwargs):
super(Wcs, self).__init__(**kwargs)
parsed_url = urllib2.urlparse.urlsplit(self.base_url)
instance_path = parsed_url.netloc
if parsed_url.path:
instance_path = '%s+' % parsed_url.path.replace('/', '+')
def execute(self, environment):
cmd = string.Template(settings.AGENT_WCS_COMMAND)
cmd = cmd.substitute(wcs_url=self.base_url)
cmd_process = subprocess.Popen(cmd.split(' '),
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
stdout = cmd_process.communicate(input=json.dumps(environment))
class Authentic(BaseService):
service_id = 'authentic'
def execute(self, environment):
cmd = string.Template(settings.AGENT_AUTHENTIC_COMMAND)
cmd = cmd.substitute(service_url=self.base_url)
cmd_process = subprocess.Popen(cmd.split(' '),
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
stdout = cmd_process.communicate(input=json.dumps(environment))
class Combo(BaseService):
service_id = 'combo'
service_manage_cmd = settings.COMBO_MANAGE_COMMAND
def deploy(environment):
hobo_timestamp = environment.get('timestamp')
service_classes = {}
for klassname, service in globals().items():
if not hasattr(service, 'service_id'):
continue
service_classes[service.service_id] = service
for service in environment.get('services', []):
service_id = service.get('service-id')
if not service_id in service_classes:
continue
service_obj = service_classes.get(service_id)(**service)
if not service_obj.is_for_us():
logger.debug('skipping as not for us: %r', service_obj)
continue
if service_obj.check_timestamp(hobo_timestamp):
logger.debug('skipping uptodate site: %r', service_obj)
continue
service_obj.execute(environment)