81 lines
2.9 KiB
Python
81 lines
2.9 KiB
Python
|
# w.c.s. - web application for online forms
|
||
|
# Copyright (C) 2005-2014 Entr'ouvert
|
||
|
#
|
||
|
# 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 2 of the License, or
|
||
|
# (at your option) any later version.
|
||
|
#
|
||
|
# This program 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/>.
|
||
|
|
||
|
import json
|
||
|
import os
|
||
|
import sys
|
||
|
import urllib2
|
||
|
|
||
|
from qommon.ctl import Command, make_option
|
||
|
|
||
|
|
||
|
def is_for_us(service):
|
||
|
# TODO: this function would check if the requested service is to be hosted
|
||
|
# on this server, it would match against a pattern file such as:
|
||
|
# | *.au-quotidien.com (globs are accepted)
|
||
|
# | -*.dev.au-quotidien.com (possibility to prefix with - to exclude)
|
||
|
# and return True only if appropriate.
|
||
|
return True
|
||
|
|
||
|
|
||
|
class CmdCheckHobos(Command):
|
||
|
name = 'check-hobos'
|
||
|
|
||
|
def execute(self, base_options, sub_options, args):
|
||
|
import publisher
|
||
|
|
||
|
publisher.WcsPublisher.configure(self.config, sub_options.extra)
|
||
|
pub = publisher.WcsPublisher.create_publisher()
|
||
|
|
||
|
global_app_dir = pub.app_dir
|
||
|
|
||
|
hobos = []
|
||
|
for arg in args:
|
||
|
try:
|
||
|
hobos.extend(json.load(urllib2.urlopen(arg + '/hobos.json')))
|
||
|
except (urllib2.URLError, urllib2.HTTPError), e:
|
||
|
print >> sys.stderr, 'failed to get URL', arg, e
|
||
|
continue
|
||
|
|
||
|
services = []
|
||
|
for hobo in hobos:
|
||
|
try:
|
||
|
all_services = json.load(
|
||
|
urllib2.urlopen(hobo + 'environment/installed_services.json'))
|
||
|
except (urllib2.URLError, urllib2.HTTPError), e:
|
||
|
print >> sys.stderr, 'failed to get URL', hobo, e
|
||
|
continue
|
||
|
services.extend([x for x in all_services if x.get('service-id') == 'wcs'])
|
||
|
|
||
|
for service in services:
|
||
|
if not is_for_us(service):
|
||
|
continue
|
||
|
parsed_url = urllib2.urlparse.urlsplit(service.get('base_url'))
|
||
|
instance_path = parsed_url.netloc
|
||
|
if parsed_url.path:
|
||
|
instance_path = '%s+' % parsed_url.path.replace('/', '+')
|
||
|
pub.app_dir = os.path.join(global_app_dir, instance_path)
|
||
|
if not os.path.exists(pub.app_dir):
|
||
|
print 'initializing instance in', pub.app_dir
|
||
|
os.mkdir(pub.app_dir)
|
||
|
pub.initialize_app_dir()
|
||
|
if not pub.cfg.get('misc'):
|
||
|
pub.cfg['misc'] = {}
|
||
|
pub.cfg['misc']['sitename'] = service.get('title').encode('utf-8')
|
||
|
pub.write_cfg()
|
||
|
|
||
|
CmdCheckHobos.register()
|