changeset 208:f8f898b39058

Add unprivileged user creation to install procedure.
author Daniele Nicolodi <daniele@grinta.net>
date Thu, 17 Nov 2011 11:46:12 +0100
parents dd5e5fc17a62
children 0c2936ea34bf
files src/ltpdarepo/admin.py src/ltpdarepo/install.py
diffstat 2 files changed, 29 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/ltpdarepo/admin.py	Thu Nov 17 11:45:11 2011 +0100
+++ b/src/ltpdarepo/admin.py	Thu Nov 17 11:46:12 2011 +0100
@@ -331,6 +331,7 @@
     # install
     install = install
     cmd = commands.add(install)
+    cmd.add_argument('--create-user', '-c', action='store_true', dest='createuser')
     cmd.add_argument('--user', '-u', dest='_username', metavar='USER')
     cmd.add_argument('--passwd', '-p', dest='_password', metavar='PASSWORD', default='')
 
@@ -409,7 +410,7 @@
     def setup(self):
         """setup test environment"""
 
-        self.install()
+        self.install(createuser=True)
         self.useradd('u1', admin=True)
         self.passwd('u1', 'u1')
         self.createdb('db1', description=u'Test database One')
--- a/src/ltpdarepo/install.py	Thu Nov 17 11:45:11 2011 +0100
+++ b/src/ltpdarepo/install.py	Thu Nov 17 11:46:12 2011 +0100
@@ -3,9 +3,35 @@
 # This software may be used and distributed according to the terms of
 # the GNU Affero General Public License version 3 or any later version.
 
-def install(instance):
+
+def createuser(instance):
+
+    conn = instance.connect()
+    curs = conn.cursor()
+
+    # create user
+    curs.execute("""CREATE USER %s@'localhost' IDENTIFIED BY %s""",
+                 (instance.USERNAME, instance.PASSWORD))
+
+    # permissions on all databases
+    curs.execute("""GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, CREATE VIEW, CREATE USER
+                    ON *.* TO %s@'localhost' WITH GRANT OPTION""", instance.USERNAME)
+
+    # permissions on the administrative database
+    curs.execute("""GRANT EXECUTE, CREATE TEMPORARY TABLES
+                    ON `%s`.* TO %%s@'localhost'""" % instance.DATABASE, instance.USERNAME)
+
+    conn.commit()
+    conn.close()
+
+
+def install(instance, **kwargs):
     """initialize database environment"""
 
+    # create user
+    if kwargs.get('createuser', False) and instance.USERNAME != 'root':
+        createuser(instance)
+
     conn = instance.connect()
     curs = conn.cursor()