From c0d9e7f3c73ae3aee1721cd11e3c76e0382a64e1 Mon Sep 17 00:00:00 2001 From: Michael Spang Date: Mon, 2 Nov 2009 20:18:55 +0000 Subject: [PATCH] Add CLI version of mysql thing --- bin/ceo | 17 ++++++++++------- ceo/console/main.py | 3 ++- ceo/console/mysql.py | 38 ++++++++++++++++++++++++++++++++++++++ src/op-mysql | 2 ++ 4 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 ceo/console/mysql.py diff --git a/bin/ceo b/bin/ceo index 6fae10eee..7322493df 100755 --- a/bin/ceo +++ b/bin/ceo @@ -8,16 +8,19 @@ from ceo import ldapi, members, library def start(): try: - print "Reading config file...", - members.configure() - library.configure() - - print "Connecting to LDAP..." - members.connect(AuthCallback()) - if len(sys.argv) == 1: + print "Reading config file...", + members.configure() + library.configure() + + print "Connecting to LDAP..." + members.connect(AuthCallback()) + ceo.urwid.main.start() else: + members.configure() + library.configure() + members.connect(AuthCallback()) ceo.console.main.start() except ldap.LOCAL_ERROR, e: print ldapi.format_ldaperror(e) diff --git a/ceo/console/main.py b/ceo/console/main.py index f43150d4b..463fb2124 100644 --- a/ceo/console/main.py +++ b/ceo/console/main.py @@ -5,15 +5,16 @@ from ceo.console.memberlist import MemberList from ceo.console.updateprograms import UpdatePrograms from ceo.console.expiredaccounts import ExpiredAccounts from ceo.console.inactive import Inactive +from ceo.console.mysql import MySQL commands = { 'memberlist' : MemberList(), 'updateprograms' : UpdatePrograms(), 'expiredaccounts' : ExpiredAccounts(), 'inactive': Inactive(), + 'mysql': MySQL(), } help_opts = [ '--help', '-h' ] - def start(): args = sys.argv[1:] if args[0] in help_opts: diff --git a/ceo/console/mysql.py b/ceo/console/mysql.py new file mode 100644 index 000000000..f617751b7 --- /dev/null +++ b/ceo/console/mysql.py @@ -0,0 +1,38 @@ +from ceo import members, terms, mysql + +class MySQL: + help = ''' +mysql create + +Creates a mysql database for a user. +''' + def main(self, args): + if len(args) != 2 or args[0] != 'create': + print self.help + return + username = args[1] + problem = None + try: + password = mysql.create_mysql(username) + + try: + mysql.write_mysql_info(username, password) + helpfiletext = "Settings written to ~%s/ceo-mysql-info." % username + except (KeyError, IOError, OSError), e: + helpfiletext = "An error occured writing the settings file: %s" % e + + print "MySQL database created" + print ("Connection Information: \n" + "\n" + "Database: %s\n" + "Username: %s\n" + "Hostname: localhost\n" + "Password: %s\n" + "\n" + "%s\n" + % (username, username, password, helpfiletext)) + except mysql.MySQLException, e: + print "Failed to create MySQL database" + print + print "We failed to create the database. The error was:\n\n%s" % e + diff --git a/src/op-mysql b/src/op-mysql index ba5435876..c47a220b6 100755 --- a/src/op-mysql +++ b/src/op-mysql @@ -29,6 +29,8 @@ def check_auth(remote_user, mysql_user, response): if remote_user == mysql_user: return response_message(response, 0, 'user %s creating database for self' % remote_user) club = members.get(mysql_user) + if not club: + return response_message(response, errno.EPERM, 'user %s does not exist' % mysql_user) if 'club' in club.get('objectClass', []): if check_group(remote_user, mysql_user): return response_message(response, 0, 'user %s is in club group %s' % (remote_user, mysql_user))