Commit 60a7bca8 authored by Łukasz Nowak's avatar Łukasz Nowak

- create database while creting ERP5 site

MySQL database is *required* part of ERP5 site, so it have to be created.
As all parameters to access mysql with proper privileges are known, and MySQLdb
library might be available for recipe there is no reason to not create database.

Previous method by doing tricks with command recipe is to unreliable and much
less buildout like.


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@33086 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent b666876b
......@@ -3,7 +3,6 @@
parts = zope-instance
[zope-instance]
depends = ${create_mysql_database:command}
recipe = erp5.recipe.standaloneinstance
zope2-location = ${software_definition:zope_software}
user = zope:zope
......@@ -12,7 +11,7 @@ user = zope:zope
# Format:
# database[@host[:port]] [user [password [unix_socket]]]
# e.g "erp5db erp5user somepassword" or "erp5db erp5user"
erp5_sql_connection_string = ${:mysql_database_name}@${configuration:mysql_host}:${configuration:mysql_port} root
erp5_sql_connection_string = ${:mysql_database_name}@${:mysql_host}:${:mysql_port} ${:mysql_user} ${:mysql_password}
zope_conf_template =
${buildout:directory}/templates/default-erp5-standalone-zope.conf.in
......@@ -85,13 +84,6 @@ http-force-connection-close = off
zodb-path = ${:instancehome}/var/Data.fs
# zope.conf template part ENDS
[create_mysql_database]
# XXX: This have to be converted into recipe
recipe = plone.recipe.command
command =
echo "CREATE DATABASE IF NOT EXISTS ${zope-instance:mysql_database_name} DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci" | ${buildout:data-bin-directory}/mysql -u root
update-command = ${:command}
[bt5list]
recipe = erp5.recipe.genbt5list
bt5_base = ${bt5-erp5:location}
......
......@@ -32,13 +32,12 @@ class Recipe(plone.recipe.zope2instance.Recipe, erp5.recipe.createsite.Recipe):
os.path.join(standalone_location, 'bin', 'zopectl'))
erp5.recipe.createsite.Recipe.__init__(self, buildout, name, options)
self.egg = zc.recipe.egg.Egg(buildout, options['recipe'], options)
self.buildout, self.options, self.name = buildout, options, name
self.zope2_location = options.get('zope2-location', '')
options['bin-directory'] = os.path.join(standalone_location, 'bin')
options['scripts'] = '' # suppress script generation.
options['file-storage'] = options.get('file-storage',
os.path.join(standalone_location, 'var', 'Data.fs'))
self.buildout, self.options, self.name = buildout, options, name
self.zope2_location = options.get('zope2-location', '')
# Relative path support for the generated scripts
relative_paths = options.get(
......@@ -61,6 +60,45 @@ class Recipe(plone.recipe.zope2instance.Recipe, erp5.recipe.createsite.Recipe):
requirements, ws = self.egg.working_set()
ws_locations = [d.location for d in ws]
if options.get('mysql_create_database', 'false').lower() == 'true':
try:
import MySQLdb
except ImportError:
raise ImportError('To be able to create database MySQLdb is required'
' Install system wide or use software generated python')
mysql_database_name, mysql_user, mysql_password, mysql_port, mysql_host\
= \
options.get('mysql_database_name'), \
options.get('mysql_user'), \
options.get('mysql_password'), \
options.get('mysql_port'), \
options.get('mysql_host')
if not (mysql_database_name and mysql_user):
raise zc.buildout.UserError('mysql_database_name and mysql_user are '
'required to create database and grant privileges')
connection = MySQLdb.connect(
host = self.options.get('mysql_host'),
port = int(self.options.get('mysql_port')),
user = self.options.get('mysql_superuser'),
passwd = self.options.get('mysql_superpassword'),
)
connection.autocommit(0)
cursor = connection.cursor()
cursor.execute(
'CREATE DATABASE IF NOT EXISTS %s DEFAULT CHARACTER SET utf8 COLLATE '
'utf8_unicode_ci' % mysql_database_name)
privileges = ['GRANT ALL PRIVILEGES ON %s.* TO %s' % (
mysql_database_name, mysql_user)]
if mysql_host:
privileges.append('@%s' % mysql_host)
if mysql_password:
privileges.append(' IDENTIFIED BY "%s"' % mysql_password)
cursor.execute(''.join(privileges))
connection.commit()
connection.close()
# What follows is a bit of a hack because the instance-setup mechanism
# is a bit monolithic. We'll run mkzopeinstance and then we'll
# patch the result. A better approach might be to provide independent
......
......@@ -24,6 +24,19 @@ initialization =
# developer by default want to have updatable Data.fs
force-zodb-update = true
# MySQL
mysql_database_name = development_site
mysql_user = development_user
mysql_password = development_password
mysql_host = ${configuration:mysql_host}
mysql_port = ${configuration:mysql_port}
# create database
mysql_create_database = true
# below could be set in configuration
mysql_superuser = root
mysql_superpassword =
# zope.conf template part BEGIN
debug-mode = on
instancehome = ${buildout:var-directory}/zope-instance
......@@ -42,8 +55,6 @@ products =
eggs =
Products.ExternalEditor
mysql_database_name = development_site
bt5 =
erp5_base
erp5_pdm
......
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