Write mysql file to ~club
authorMichael Spang <mspang@csclub.uwaterloo.ca>
Thu, 10 Sep 2009 19:37:41 +0000 (15:37 -0400)
committerMichael Spang <mspang@csclub.uwaterloo.ca>
Thu, 10 Sep 2009 19:56:52 +0000 (15:56 -0400)
ceo/mysql.py
ceo/urwid/databases.py

index f6b3e69..02461bc 100644 (file)
@@ -1,10 +1,36 @@
-import os, re, subprocess, ldap, socket
+import os, re, subprocess, ldap, socket, pwd
 from ceo import conf, ldapi, terms, remote, ceo_pb2
 from ceo.excep import InvalidArgument
 
 class MySQLException(Exception):
     pass
 
+def write_mysql_info(username, password):
+    homedir = pwd.getpwnam(username).pw_dir
+    password_file = '%s/ceo-mysql-info' % homedir
+    if os.path.exists(password_file):
+        os.rename(password_file, password_file + '.old')
+    fd = os.open(password_file, os.O_CREAT|os.O_EXCL|os.O_WRONLY, 0660)
+    fh = os.fdopen(fd, 'w')
+    fh.write("""MySQL Database Information for %(username)s
+
+Your new MySQL database was created. To connect, use
+the following options:
+
+Database: %(username)s
+Username: %(username)s
+Password: %(password)s
+Hostname: localhost
+
+The command to connect using the MySQL command-line client is
+
+  mysql %(username)s -u %(username)s -p
+
+This database is only accessible from caffeine.
+""" % { 'username': username, 'password': password })
+
+    fh.close()
+
 def create_mysql(username):
     try:
         request = ceo_pb2.AddMySQLUser()
index a61120d..3825a96 100644 (file)
@@ -60,6 +60,12 @@ class EndPage(WizardPanel):
         problem = None
         try:
             password = mysql.create_mysql(self.state['userid'])
+
+            try:
+                mysql.write_mysql_info(self.state['userid'], password)
+                helpfiletext = "Settings written to ~%s/ceo-mysql-info." % self.state['userid']
+            except (KeyError, IOError, OSError), e:
+                helpfiletext = "An error occured writing the settings file: %s" % e
             self.headtext.set_text("MySQL database created")
             self.midtext.set_text("Connection Information: \n"
                                   "\n"
@@ -68,8 +74,8 @@ class EndPage(WizardPanel):
                                   "Hostname: localhost\n"
                                   "Password: %s\n"
                                   "\n"
-                                  "Note: Databases are only accessible from caffeine\n"
-                                  % (self.state['userid'], self.state['userid'], password))
+                                  "%s\n"
+                                  % (self.state['userid'], self.state['userid'], password, helpfiletext))
         except mysql.MySQLException, e:
             self.headtext.set_text("Failed to create MySQL database")
             self.midtext.set_text("We failed to create the database. The error was:\n\n%s" % e)