[Erp5-report] r33086 luke - in /erp5/trunk/buildout: instance-profiles/ local-eggs/erp5.rec...

nobody at svn.erp5.org nobody at svn.erp5.org
Wed Feb 24 11:41:53 CET 2010


Author: luke
Date: Wed Feb 24 11:41:49 2010
New Revision: 33086

URL: http://svn.erp5.org?rev=33086&view=rev
Log:
 - 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.

Modified:
    erp5/trunk/buildout/instance-profiles/zope.cfg
    erp5/trunk/buildout/local-eggs/erp5.recipe.standaloneinstance/src/erp5/recipe/standaloneinstance/__init__.py
    erp5/trunk/buildout/profiles/development.cfg

Modified: erp5/trunk/buildout/instance-profiles/zope.cfg
URL: http://svn.erp5.org/erp5/trunk/buildout/instance-profiles/zope.cfg?rev=33086&r1=33085&r2=33086&view=diff
==============================================================================
--- erp5/trunk/buildout/instance-profiles/zope.cfg [utf8] (original)
+++ erp5/trunk/buildout/instance-profiles/zope.cfg [utf8] Wed Feb 24 11:41:49 2010
@@ -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 @@
 # 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 @@
 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}

Modified: erp5/trunk/buildout/local-eggs/erp5.recipe.standaloneinstance/src/erp5/recipe/standaloneinstance/__init__.py
URL: http://svn.erp5.org/erp5/trunk/buildout/local-eggs/erp5.recipe.standaloneinstance/src/erp5/recipe/standaloneinstance/__init__.py?rev=33086&r1=33085&r2=33086&view=diff
==============================================================================
--- erp5/trunk/buildout/local-eggs/erp5.recipe.standaloneinstance/src/erp5/recipe/standaloneinstance/__init__.py [utf8] (original)
+++ erp5/trunk/buildout/local-eggs/erp5.recipe.standaloneinstance/src/erp5/recipe/standaloneinstance/__init__.py [utf8] Wed Feb 24 11:41:49 2010
@@ -32,13 +32,12 @@
         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(
@@ -60,6 +59,45 @@
 
     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

Modified: erp5/trunk/buildout/profiles/development.cfg
URL: http://svn.erp5.org/erp5/trunk/buildout/profiles/development.cfg?rev=33086&r1=33085&r2=33086&view=diff
==============================================================================
--- erp5/trunk/buildout/profiles/development.cfg [utf8] (original)
+++ erp5/trunk/buildout/profiles/development.cfg [utf8] Wed Feb 24 11:41:49 2010
@@ -24,6 +24,19 @@
 # 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
@@ -41,8 +54,6 @@
 
 eggs =
   Products.ExternalEditor
-
-mysql_database_name = development_site
 
 bt5 =
    erp5_base




More information about the Erp5-report mailing list