Commit a9611645 authored by Vincent Pelletier's avatar Vincent Pelletier

recipe.erp5_bootstrap: Add support for non-HTTP schemes.

urllib chooses the proper httplib class based on url scheme, so use it.
urllib handles in-url credentials for basic auth, removing the need to
implement it ourselves.
Simplifies URL parsing, uncovering errors on recipe execution rather than
on generated script's execution.
Use %r for automated escaping & quoting.
parent 08606022
############################################################################## ##############################################################################
# #
# Copyright (c) 2012 Vifib SARL and Contributors. All Rights Reserved. # Copyright (c) 2012-2014 Vifib SARL and Contributors. All Rights Reserved.
# #
# WARNING: This program as such is intended to be used by professional # WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility of assessing all potential # programmers who take the whole responsibility of assessing all potential
...@@ -26,7 +26,6 @@ ...@@ -26,7 +26,6 @@
############################################################################## ##############################################################################
from slapos.recipe.librecipe import GenericBaseRecipe from slapos.recipe.librecipe import GenericBaseRecipe
import os
import sys import sys
import urlparse import urlparse
...@@ -36,31 +35,25 @@ class Recipe(GenericBaseRecipe): ...@@ -36,31 +35,25 @@ class Recipe(GenericBaseRecipe):
""" """
def install(self): def install(self):
parsed = urlparse.urlparse(self.options['mysql-url']) mysql = urlparse.urlsplit(self.options['mysql-url'])
mysql_connection_string = "%(database)s@%(hostname)s:%(port)s "\ zope = urlparse.urlsplit(self.options['zope-url'])
"%(username)s %(password)s" % dict( # Note: raises when there is more than a single element in path, as it's
database=parsed.path.split('/')[1], # not supported by manage_addERP5Site anyway.
hostname=parsed.hostname, _, zope_path = zope.path.split('/')
port=parsed.port, return [self.createExecutable(
username=parsed.username,
password=parsed.password
)
zope_parsed = urlparse.urlparse(self.options['zope-url'])
config = dict(
python_path=sys.executable,
user=zope_parsed.username,
password=zope_parsed.password,
site_id=zope_parsed.path.split('/')[1],
host="%s:%s" % (zope_parsed.hostname, zope_parsed.port),
sql_connection_string=mysql_connection_string,
)
# Runners
runner_path = self.createExecutable(
self.options['runner-path'], self.options['runner-path'],
self.substituteTemplate(self.getTemplateFilename('erp5_bootstrap.in'), self.substituteTemplate(
config)) self.getTemplateFilename('erp5_bootstrap.in'),
{
return [runner_path] 'python_path': sys.executable,
'base_url': urlparse.urlunsplit((zope.scheme, zope.netloc, '', '', '')),
'site_id': zope_path,
'sql_connection_string': '%(database)s@%(hostname)s:%(port)s %(username)s %(password)s' % {
'database': mysql.path.split('/')[1],
'hostname': mysql.hostname,
'port': mysql.port,
'username': mysql.username,
'password': mysql.password
},
},
))]
#!%(python_path)s #!%(python_path)s
import httplib
import urllib import urllib
import base64
user = "%(user)s"
password = "%(password)s"
host = "%(host)s"
site_id = "%(site_id)s"
erp5_catalog_storage = 'erp5_mysql_innodb_catalog'
mysql_url = "%(sql_connection_string)s"
header_dict = {'Authorization': 'Basic %%s' %% \
base64.encodestring('%%s:%%s' %% (user, password)).strip(),
'Referer':'http://%%s/manage_addProduct/ERP5/addERP5Site' %% host}
zope_connection = httplib.HTTPConnection(host)
# Check if an ERP5 site is already created, as ERP5 does support having
# 2 instances in the same zope, and this script should not destroy user data
zope_connection.request('GET', '/isERP5SitePresent', headers=header_dict)
result = zope_connection.getresponse()
if result.status == 204: # and (result.read() == "False"): def isSuccess(response):
return 200 <= response.code < 300
# Use a new connection
zope_connection = httplib.HTTPConnection(host) base_url = %(base_url)r
response = urllib.urlopen(base_url + '/isERP5SitePresent')
# Create the expected ERP5 instance if isSuccess(response) and response.read() == '':
zope_connection.request( mysql_url = %(sql_connection_string)r
'POST', '/manage_addProduct/ERP5/manage_addERP5Site', response = urllib.urlopen(
urllib.urlencode({ base_url + '/manage_addProduct/ERP5/manage_addERP5Site',
'id': site_id, data=urllib.urlencode({
'erp5_catalog_storage': erp5_catalog_storage, 'id': %(site_id)r,
'erp5_catalog_storage': 'erp5_mysql_innodb_catalog',
'erp5_sql_connection_string': mysql_url, 'erp5_sql_connection_string': mysql_url,
'cmf_activity_sql_connection_string': mysql_url, 'cmf_activity_sql_connection_string': mysql_url,
}), }),
headers=header_dict) )
# Wait for the erp5 response, to prevent multiple requests if not isSuccess(response):
# been done by the same script. raise ValueError('Failed creating site, status=%%i: %%s' %% (response.code, response.read()))
result = zope_connection.getresponse()
# Read result make sure the site really finished to
#created the ERP5 site.
result.read()
print "ERP5 site created." print "ERP5 site created."
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment