2 commits - pykolab/setup pykolab/utils.py

Jeroen van Meeuwen vanmeeuwen at kolabsys.com
Thu Nov 22 10:32:02 CET 2012


 pykolab/setup/components.py  |    3 +
 pykolab/setup/setup_mysql.py |   80 +++++++++++++++++++++++++++----------------
 pykolab/utils.py             |   26 +++++++++++++
 3 files changed, 80 insertions(+), 29 deletions(-)

New commits:
commit d7042619ed10491e79282a6bad38327891bd2081
Author: Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen at kolabsys.com>
Date:   Thu Nov 22 09:29:54 2012 +0000

    Provide a mechanism to select a process in which an existing MySQL server is used (#1177)

diff --git a/pykolab/setup/setup_mysql.py b/pykolab/setup/setup_mysql.py
index 0536304..8d419ad 100644
--- a/pykolab/setup/setup_mysql.py
+++ b/pykolab/setup/setup_mysql.py
@@ -58,33 +58,54 @@ def execute(*args, **kw):
         log.error(_("Could not configure to start on boot, the " + \
                 "MySQL database service."))
 
-    print >> sys.stderr, utils.multiline_message(
-            _("""
-                    Please supply a root password for MySQL. This password will
-                    be the administrative user for this MySQL server, and it
-                    should be kept a secret. After this setup process has
-                    completed, Kolab is going to discard and forget about this
-                    password, but you will need it for administrative tasks in
-                    MySQL.
-                """)
-        )
-
-    mysql_root_password = utils.ask_question(
-            _("MySQL root password"),
-            default=utils.generate_password(),
-            password=True,
-            confirm=True
-        )
-
-    p1 = subprocess.Popen(['echo', 'UPDATE mysql.user SET Password=PASSWORD(\'%s\') WHERE User=\'root\';' % (mysql_root_password)], stdout=subprocess.PIPE)
-    p2 = subprocess.Popen(['mysql'], stdin=p1.stdout)
-    p1.stdout.close()
-    p2.communicate()
-
-    p1 = subprocess.Popen(['echo', 'FLUSH PRIVILEGES;'], stdout=subprocess.PIPE)
-    p2 = subprocess.Popen(['mysql'], stdin=p1.stdout)
-    p1.stdout.close()
-    p2.communicate()
+    options = {
+            1: "Existing MySQL server (with root password already set).",
+            2: "New MySQL server (needs to be initialized)."
+        }
+
+    answer = utils.ask_menu(_("What MySQL server are we setting up?"), options)
+
+    if answer == "1" or answer == 1:
+        print >> sys.stderr, utils.multiline_message(
+                _("""
+                        Please supply the root password for MySQL, so we can set
+                        up user accounts for other components that use MySQL.
+                    """)
+            )
+
+        mysql_root_password = utils.ask_question(
+                _("MySQL root password"),
+                password=True
+            )
+
+    else:
+        print >> sys.stderr, utils.multiline_message(
+                _("""
+                        Please supply a root password for MySQL. This password
+                        will be the administrative user for this MySQL server,
+                        and it should be kept a secret. After this setup process
+                        has completed, Kolab is going to discard and forget
+                        about this password, but you will need it for
+                        administrative tasks in MySQL.
+                    """)
+            )
+
+        mysql_root_password = utils.ask_question(
+                _("MySQL root password"),
+                default=utils.generate_password(),
+                password=True,
+                confirm=True
+            )
+
+        p1 = subprocess.Popen(['echo', 'UPDATE mysql.user SET Password=PASSWORD(\'%s\') WHERE User=\'root\';' % (mysql_root_password)], stdout=subprocess.PIPE)
+        p2 = subprocess.Popen(['mysql'], stdin=p1.stdout)
+        p1.stdout.close()
+        p2.communicate()
+
+        p1 = subprocess.Popen(['echo', 'FLUSH PRIVILEGES;'], stdout=subprocess.PIPE)
+        p2 = subprocess.Popen(['mysql'], stdin=p1.stdout)
+        p1.stdout.close()
+        p2.communicate()
 
     data = """
 [mysql]
diff --git a/pykolab/utils.py b/pykolab/utils.py
index a4d8455..4516297 100644
--- a/pykolab/utils.py
+++ b/pykolab/utils.py
@@ -123,6 +123,32 @@ def ask_confirmation(question, default="y", all_inclusive_no=True):
             else:
                 return True
 
+def ask_menu(question, options={}):
+    print question
+    answer_correct = False
+    max_key_length = 0
+
+    keys = options.keys()
+    keys.sort()
+
+    while not answer_correct:
+        for key in keys:
+            key_length = len("%s" % key)
+            if key_length > max_key_length:
+                max_key_length = key_length
+
+        str_format = "%%%ds" % max_key_length
+
+        for key in keys:
+            print " - " + eval("str_format % key") + ": " + options[key]
+
+        answer = raw_input(_("Choice") + ": ")
+
+        if answer in [str(x) for x in options.keys()]:
+            answer_correct = True
+
+    return answer
+
 def ensure_directory(_dir, _user='root', _group='root'):
     if not os.path.isdir(_dir):
         os.makedirs(_dir)


commit 08c0dacd3186052739cb7fa2f735a2b77d8c4365
Author: Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen at kolabsys.com>
Date:   Thu Nov 22 08:59:13 2012 +0000

    Remove /tmp/kolab-setup-my.cnf after setup, and chmod it 0600 before writing out anything relevant.

diff --git a/pykolab/setup/components.py b/pykolab/setup/components.py
index f12fe84..d280372 100644
--- a/pykolab/setup/components.py
+++ b/pykolab/setup/components.py
@@ -201,6 +201,9 @@ def execute(component_name, *args, **kw):
 
     components[component_name]['function'](conf.cli_args, kw)
 
+    if os.path.exists('/tmp/kolab-setup-my.cnf'):
+        os.unlink('/tmp/kolab-setup-my.cnf')
+
 def register_group(dirname, module):
     components_base_path = os.path.join(os.path.dirname(__file__), module)
 
diff --git a/pykolab/setup/setup_mysql.py b/pykolab/setup/setup_mysql.py
index c956eeb..0536304 100644
--- a/pykolab/setup/setup_mysql.py
+++ b/pykolab/setup/setup_mysql.py
@@ -56,8 +56,8 @@ def execute(*args, **kw):
         subprocess.call(['/usr/sbin/update-rc.d', 'mysql', 'defaults'])
     else:
         log.error(_("Could not configure to start on boot, the " + \
-                "MySQL database service."))                
-                
+                "MySQL database service."))
+
     print >> sys.stderr, utils.multiline_message(
             _("""
                     Please supply a root password for MySQL. This password will
@@ -93,6 +93,7 @@ password='%s'
 """ % (mysql_root_password)
 
     fp = open('/tmp/kolab-setup-my.cnf', 'w')
+    os.chmod('/tmp/kolab-setup-my.cnf', 0600)
     fp.write(data)
     fp.close()
 





More information about the commits mailing list