Index: trunk/tools/subversion/user-management/homedirectorymanager.py |
— | — | @@ -45,7 +45,6 @@ |
46 | 46 | self.logfile = None |
47 | 47 | |
48 | 48 | self.loglevel = INFO |
49 | | - self.logfile = None |
50 | 49 | |
51 | 50 | self.dryRun = False |
52 | 51 | |
Index: trunk/tools/subversion/user-management/manage-exports |
— | — | @@ -3,81 +3,107 @@ |
4 | 4 | from optparse import OptionParser |
5 | 5 | from subprocess import call |
6 | 6 | |
7 | | -try: |
8 | | - import ldap |
| 7 | +try: |
| 8 | + import ldap |
9 | 9 | import ldap.modlist |
10 | 10 | except ImportError: |
11 | | - sys.stderr.write("Unable to import LDAP library.\n") |
12 | | - sys.exit(1) |
| 11 | + sys.stderr.write("Unable to import LDAP library.\n") |
13 | 12 | |
14 | | -def main(): |
15 | | - parser = OptionParser(conflict_handler="resolve") |
16 | | - parser.set_usage('manage-exports [options]') |
| 13 | +NONE = 0 |
| 14 | +INFO = 10 |
| 15 | +DEBUG = 20 |
17 | 16 | |
18 | | - ldapSupportLib = ldapsupportlib.LDAPSupportLib() |
19 | | - ldapSupportLib.addParserOptions(parser) |
| 17 | +class ExportManager: |
| 18 | + def __init__(self): |
| 19 | + self.basedir = "/export/home/" |
| 20 | + self.loglevel = INFO |
| 21 | + self.logfile = None |
20 | 22 | |
21 | | - parser.add_option("--logfile", dest="logfile", help="Write output to the specified log file. (default: stdin)") |
22 | | - parser.add_option("--loglevel", dest="loglevel", help="Change level of logging; NONE, INFO, DEBUG (default: INFO)") |
23 | | - (options, args) = parser.parse_args() |
24 | | - ldapSupportLib.setBindInfoByOptions(options, parser) |
| 23 | + def run(self): |
| 24 | + parser = OptionParser(conflict_handler="resolve") |
| 25 | + parser.set_usage('manage-exports [options]') |
| 26 | + |
| 27 | + ldapSupportLib = ldapsupportlib.LDAPSupportLib() |
| 28 | + ldapSupportLib.addParserOptions(parser) |
| 29 | + |
| 30 | + parser.add_option("--logfile", dest="logfile", help="Write output to the specified log file. (default: stdin)") |
| 31 | + parser.add_option("--loglevel", dest="loglevel", help="Change level of logging; NONE, INFO, DEBUG (default: INFO)") |
| 32 | + (options, args) = parser.parse_args() |
| 33 | + ldapSupportLib.setBindInfoByOptions(options, parser) |
25 | 34 | |
26 | | - base = ldapSupportLib.getBase() |
27 | | - ds = ldapSupportLib.connect() |
| 35 | + if options.logfile: |
| 36 | + self.logfile = options.logfile |
| 37 | + if options.loglevel: |
| 38 | + self.loglevel = options.loglevel |
28 | 39 | |
29 | | - # w00t We're in! |
30 | | - try: |
31 | | - projectdata = ds.search_s("ou=groups," + base,ldap.SCOPE_SUBTREE,"(&(cn=*)(owner=*))") |
32 | | - projects = [] |
33 | | - basedir = "/export/home/" |
34 | | - if not projectdata: |
35 | | - raise ldap.NO_SUCH_OBJECT() |
36 | | - hdm = homedirectorymanager.HomeDirectoryManager() |
37 | | - for project in projectdata: |
38 | | - project_name = project[1]["cn"][0] |
39 | | - if not os.path.exists(basedir + project_name): |
40 | | - os.mkdir(basedir + project_name, 0755) |
41 | | - if options.logfile: |
42 | | - hdm.logfile = options.logfile |
43 | | - if options.loglevel: |
44 | | - hdm.setDebugLevel(options.loglevel) |
45 | | - hdm.basedir = basedir + project_name + "/" |
46 | | - hdm.group = project_name |
47 | | - hdm.run() |
48 | | - hostdata = ds.search_s("ou=hosts," + base,ldap.SCOPE_SUBTREE,"(puppetvar=instanceproject=" + project_name + ")") |
49 | | - hosts = [] |
50 | | - for host in hostdata: |
51 | | - |
52 | | - host_ip = host[1]["aRecord"][0] |
53 | | - hosts.append(host_ip + "(rw,no_subtree_check)") |
54 | | - projects.append(basedir + project_name + " " + " ".join(hosts) + "\n") |
55 | | - |
56 | | - exports = open('/etc/exports', 'w') |
57 | | - exports.writelines(projects) |
58 | | - exports.close() |
59 | | - retcode = call("/usr/sbin/exportfs" + " -r", shell=True) |
60 | | - |
61 | | - except ldap.NO_SUCH_OBJECT: |
62 | | - sys.stderr.write("The project search returned no entries.\n") |
63 | | - ds.unbind() |
64 | | - sys.exit(1) |
65 | | - except ldap.PROTOCOL_ERROR: |
66 | | - sys.stderr.write("There was an LDAP protocol error; see traceback.\n") |
67 | | - traceback.print_exc(file=sys.stderr) |
68 | | - ds.unbind() |
69 | | - sys.exit(1) |
70 | | - except Exception: |
| 40 | + base = ldapSupportLib.getBase() |
| 41 | + ds = ldapSupportLib.connect() |
| 42 | + |
| 43 | + # w00t We're in! |
71 | 44 | try: |
72 | | - sys.stderr.write("There was a general error, this is unexpected; see traceback.\n") |
| 45 | + projectdata = ds.search_s("ou=groups," + base,ldap.SCOPE_SUBTREE,"(&(cn=*)(owner=*))") |
| 46 | + projects = [] |
| 47 | + if not projectdata: |
| 48 | + raise ldap.NO_SUCH_OBJECT() |
| 49 | + hdm = homedirectorymanager.HomeDirectoryManager() |
| 50 | + for project in projectdata: |
| 51 | + project_name = project[1]["cn"][0] |
| 52 | + if not os.path.exists(self.basedir + project_name): |
| 53 | + self.log( "Creating a project directory for %s" % (project_name) ) |
| 54 | + os.mkdir(self.basedir + project_name, 0755) |
| 55 | + if options.logfile: |
| 56 | + hdm.logfile = options.logfile |
| 57 | + if options.loglevel: |
| 58 | + hdm.setDebugLevel(options.loglevel) |
| 59 | + hdm.basedir = self.basedir + project_name + "/" |
| 60 | + hdm.group = project_name |
| 61 | + hdm.run() |
| 62 | + hostdata = ds.search_s("ou=hosts," + base,ldap.SCOPE_SUBTREE,"(puppetvar=instanceproject=" + project_name + ")") |
| 63 | + hosts = [] |
| 64 | + for host in hostdata: |
| 65 | + host_ip = host[1]["aRecord"][0] |
| 66 | + hosts.append(host_ip + "(rw,no_subtree_check)") |
| 67 | + projects.append(self.basedir + project_name + " " + " ".join(hosts) + "\n") |
| 68 | + exports = open('/etc/exports', 'w') |
| 69 | + exports.writelines(projects) |
| 70 | + exports.close() |
| 71 | + retcode = call("/usr/sbin/exportfs" + " -r", shell=True) |
| 72 | + # TODO: Check directories that exist against projects; move deleted projects |
| 73 | + except ldap.NO_SUCH_OBJECT: |
| 74 | + sys.stderr.write("The project search returned no entries.\n") |
| 75 | + ds.unbind() |
| 76 | + return 1 |
| 77 | + except ldap.PROTOCOL_ERROR: |
| 78 | + sys.stderr.write("There was an LDAP protocol error; see traceback.\n") |
73 | 79 | traceback.print_exc(file=sys.stderr) |
74 | 80 | ds.unbind() |
| 81 | + return 1 |
75 | 82 | except Exception: |
76 | | - sys.stderr.write("Also failed to unbind.\n") |
77 | | - traceback.print_exc(file=sys.stderr) |
78 | | - sys.exit(1) |
| 83 | + try: |
| 84 | + sys.stderr.write("There was a general error, this is unexpected; see traceback.\n") |
| 85 | + traceback.print_exc(file=sys.stderr) |
| 86 | + ds.unbind() |
| 87 | + except Exception: |
| 88 | + sys.stderr.write("Also failed to unbind.\n") |
| 89 | + traceback.print_exc(file=sys.stderr) |
| 90 | + return 1 |
| 91 | + |
| 92 | + ds.unbind() |
| 93 | + return 0 |
| 94 | + |
| 95 | + def log(self, logstring): |
| 96 | + if self.loglevel >= INFO: |
| 97 | + log = datetime.datetime.now().strftime("%m/%d/%Y - %H:%M:%S - ") + logstring + "\n" |
| 98 | + if self.logfile: |
| 99 | + lf = open(self.logfile, 'a') |
| 100 | + lf.write(log) |
| 101 | + lf.close() |
| 102 | + else: |
| 103 | + print log |
79 | 104 | |
80 | | - ds.unbind() |
81 | | - sys.exit(0) |
| 105 | +def main(): |
| 106 | + export_manager = ExportManager() |
| 107 | + export_manager.run() |
82 | 108 | |
83 | 109 | if __name__ == "__main__": |
84 | 110 | main() |