r86023 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r86022‎ | r86023 | r86024 >
Date:05:27, 14 April 2011
Author:tstarling
Status:deferred
Tags:
Comment:
* Fixed the actual bug: missing splitlines() on curl fetch in add-ldap-user
* More useful dry run mode in homedirectorymanager.py
* Added dry run mode to add-ldap-user
* Committing for test on server.
Modified paths:
  • /trunk/tools/subversion/user-management/add-ldap-user (modified) (history)
  • /trunk/tools/subversion/user-management/homedirectorymanager.py (modified) (history)

Diff [purge]

Index: trunk/tools/subversion/user-management/homedirectorymanager.py
@@ -38,8 +38,9 @@
3939 self.dryRun = False
4040 self.debugStatus = False
4141
42 - os.system('nscd -i passwd')
43 - os.system('nscd -i group')
 42+ if (os.path.exists('/usr/sbin/nscd')):
 43+ os.system('nscd -i passwd')
 44+ os.system('nscd -i group')
4445
4546 def run(self):
4647 parser = OptionParser(conflict_handler="resolve")
@@ -114,31 +115,35 @@
115116 alreadyCreated = []
116117
117118 for user in users.keys():
118 - if user not in self.excludedFromCreation:
119 - if os.path.exists(self.savedir + user):
120 - # User's home directory already exists
121 - alreadyCreated.append(user)
122 - continue
123 - if not os.path.exists(self.basedir + user):
124 - self.log( "Creating a home directory for %s at %s%s" % (user, self.basedir, user) )
125 - if not self.dryRun:
126 - os.mkdir(self.basedir + user, 0700)
127 - os.mkdir(self.basedir + user + '/.ssh', 0700)
128 - self.writeKeys(user, users[user]['sshPublicKey'])
129 - os.chmod(self.basedir + user + '/.ssh/authorized_keys', 0600)
130 - for skeldir,skels in self.skelFiles.iteritems():
131 - for skel in skels:
132 - shutil.copy(skeldir + skel, self.basedir + user + "/")
133 - os.chmod(self.basedir + user + "/" + skel, 0600)
134 - newGid = users[user]['gidNumber']
135 - newUid = users[user]['uidNumber']
136 - os.chown(self.basedir + user, newUid, newGid)
137 - for root, dirs, files in os.walk(self.basedir + user):
138 - for name in files:
139 - os.chown(os.path.join(root, name), newUid, newGid)
140 - for name in dirs:
141 - os.chown(os.path.join(root, name), newUid, newGid)
 119+ if user in self.excludedFromCreation:
 120+ continue
142121
 122+ if os.path.exists(self.savedir + user):
 123+ # User's home directory already exists
 124+ alreadyCreated.append(user)
 125+ continue
 126+
 127+ if os.path.exists(self.basedir + user):
 128+ continue
 129+
 130+ self.log( "Creating a home directory for %s at %s%s" % (user, self.basedir, user) )
 131+ self.mkdir(self.basedir + user, 0700)
 132+ self.mkdir(self.basedir + user + '/.ssh', 0700)
 133+ self.writeKeys(user, users[user]['sshPublicKey'])
 134+ self.chmod(self.basedir + user + '/.ssh/authorized_keys', 0600)
 135+ for skeldir,skels in self.skelFiles.iteritems():
 136+ for skel in skels:
 137+ shutil.copy(skeldir + skel, self.basedir + user + "/")
 138+ self.chmod(self.basedir + user + "/" + skel, 0600)
 139+ newGid = users[user]['gidNumber']
 140+ newUid = users[user]['uidNumber']
 141+ self.chown(self.basedir + user, newUid, newGid)
 142+ for root, dirs, files in self.walk(self.basedir + user):
 143+ for name in files:
 144+ self.chown(os.path.join(root, name), newUid, newGid)
 145+ for name in dirs:
 146+ self.chown(os.path.join(root, name), newUid, newGid)
 147+
143148 if alreadyCreated != []:
144149 self.log( "The following users already have a home directory in the SAVE directory: " + ", ".join(alreadyCreated) )
145150
@@ -169,9 +174,7 @@
170175
171176 # Write a list of keys to the user's authorized_keys file
172177 def writeKeys(self, user, keys):
173 - f = open(self.basedir + user + '/.ssh/authorized_keys', 'w')
174 - f.writelines(keys)
175 - f.close()
 178+ self.writeFile(self.basedir + user + '/.ssh/authorized_keys', ''.join(keys))
176179
177180 # Moved deleted users to SAVE
178181 def moveUsers(self, users):
@@ -196,58 +199,61 @@
197200 self.deleteUser(userdir)
198201
199202 def renameUser(self, olduserdir, newuserdir):
200 - self.log( "Moving " + self.basedir + olduserdir + " to " + self.basedir + newuserdir )
201 - if not self.dryRun:
202 - os.rename(self.basedir + olduserdir, self.basedir + newuserdir)
 203+ self.rename(self.basedir + olduserdir, self.basedir + newuserdir)
203204
204205 def deleteUser(self, userdir):
205206 # User has been deleted, move user's home directory to SAVE
206207 if os.path.isdir(self.savedir + userdir):
207208 self.log( userdir + " exists at both " + self.basedir + userdir + " and " + self.savedir + userdir )
208209 else:
209 - self.log( "Moving " + self.basedir + userdir + " to " + self.savedir + userdir )
210 - if not self.dryRun:
211 - os.rename(self.basedir + userdir, self.savedir + userdir)
 210+ self.rename(self.basedir + userdir, self.savedir + userdir)
212211
213212 # Changes the group ownership of a directory when a user's gid changes
214213 def changeGid(self, users):
215214 for userdir in os.listdir(self.basedir):
216 - if os.path.isdir(self.basedir + userdir) and userdir not in self.excludedFromModification:
217 - stat = os.stat(self.basedir + userdir)
218 - gid = stat.st_gid
219 - if userdir in users.keys() and users[userdir]["gidNumber"] != gid:
220 - newGid = users[userdir]["gidNumber"]
221 - self.log( "Changing group ownership of %s%s to %s; was set to %s" % (self.basedir, userdir, newGid, gid) )
222 - if not self.dryRun:
223 - # Python doesn't have a recursive chown, so we have to walk the directory
224 - # and change everything manually
225 - self.logDebug("Doing chgrp for: " + self.basedir + userdir + " with gid: " + str(gid))
226 - os.chown(self.basedir + userdir, -1, newGid)
227 - for root, dirs, files in os.walk(self.basedir + userdir):
228 - for name in files:
229 - os.chown(os.path.join(root, name), -1, newGid)
230 - for name in dirs:
231 - os.chown(os.path.join(root, name), -1, newGid)
 215+ if not os.path.isdir(self.basedir + userdir) or userdir in self.excludedFromModification:
 216+ continue
232217
 218+ stat = os.stat(self.basedir + userdir)
 219+ gid = stat.st_gid
 220+ if userdir not in users.keys() or users[userdir]["gidNumber"] == gid:
 221+ continue
 222+
 223+ newGid = users[userdir]["gidNumber"]
 224+ self.log( "Changing group ownership of %s%s to %s; was set to %s" % (self.basedir, userdir, newGid, gid) )
 225+
 226+ # Python doesn't have a recursive chown, so we have to walk the directory
 227+ # and change everything manually
 228+ self.logDebug("Doing chgrp for: " + self.basedir + userdir + " with gid: " + str(gid))
 229+ self.chown(self.basedir + userdir, -1, newGid)
 230+ for root, dirs, files in os.walk(self.basedir + userdir):
 231+ for name in files:
 232+ self.chown(os.path.join(root, name), -1, newGid)
 233+ for name in dirs:
 234+ self.chown(os.path.join(root, name), -1, newGid)
 235+
233236 # Changes the ownership of a directory when a user's uid changes
234237 def changeUid(self, users):
235238 for userdir in os.listdir(self.basedir):
236 - if os.path.isdir(self.basedir + userdir) and userdir not in self.excludedFromModification:
237 - stat = os.stat(self.basedir + userdir)
238 - uid = stat.st_uid
239 - if userdir in users.keys() and users[userdir]["uidNumber"] != uid:
240 - newUid = users[userdir]["uidNumber"]
241 - self.log( "Changing ownership of %s%s to %s; was set to %s" % (self.basedir, userdir, newUid, uid) )
242 - if not self.dryRun:
243 - # Python doesn't have a recursive chown, so we have to walk the directory
244 - # and change everything manually
245 - os.chown(self.basedir + userdir, newUid, -1)
246 - for root, dirs, files in os.walk(self.basedir + userdir):
247 - for name in files:
248 - os.chown(os.path.join(root, name), newUid, -1)
249 - for name in dirs:
250 - os.chown(os.path.join(root, name), newUid, -1)
 239+ if not os.path.isdir(self.basedir + userdir) or userdir in self.excludedFromModification:
 240+ continue
251241
 242+ stat = os.stat(self.basedir + userdir)
 243+ uid = stat.st_uid
 244+ if userdir not in users.keys() or users[userdir]["uidNumber"] == uid:
 245+ continue
 246+
 247+ newUid = users[userdir]["uidNumber"]
 248+ self.log( "Changing ownership of %s%s to %s; was set to %s" % (self.basedir, userdir, newUid, uid) )
 249+ # Python doesn't have a recursive chown, so we have to walk the directory
 250+ # and change everything manually
 251+ self.chown(self.basedir + userdir, newUid, -1)
 252+ for root, dirs, files in os.walk(self.basedir + userdir):
 253+ for name in files:
 254+ self.chown(os.path.join(root, name), newUid, -1)
 255+ for name in dirs:
 256+ self.chown(os.path.join(root, name), newUid, -1)
 257+
252258 def log(self, logstring):
253259 print datetime.datetime.now().strftime("%m/%d/%Y - %H:%M:%S - ") + logstring
254260
@@ -255,6 +261,38 @@
256262 if self.debugStatus == True:
257263 sys.stderr.write("Debug: " + logstring + "\n")
258264
 265+ def chown(self, path, user, group):
 266+ if not self.dryRun:
 267+ os.chown(self.basedir + userdir, -1, newGid)
 268+ if self.dryRun or self.debugStatus:
 269+ self.log('chown %s %d %d' % (path, user, group))
 270+
 271+ def mkdir(self, path):
 272+ if not self.dryRun:
 273+ os.mkdir(path)
 274+ if self.dryRun or self.debugStatus:
 275+ self.log('mkdir %s' % (path))
 276+
 277+ def chmod(self, path, mode):
 278+ if not self.dryRun:
 279+ os.chmod(path)
 280+ if self.dryRun or self.debugStatus:
 281+ self.log('chmod %s %o' % (path, mode))
 282+
 283+ def writeFile(self, path, contents):
 284+ if not self.dryRun:
 285+ f = open(path, 'w')
 286+ f.write(contents)
 287+ f.close()
 288+ if self.dryRun or self.debugStatus:
 289+ self.log("\nwrite file %s:\n%s" % (path, contents))
 290+
 291+ def rename(self, oldPath, newPath):
 292+ if not self.dryRun:
 293+ os.rename(oldPath, newPath)
 294+ if self.dryRun or self.debugStatus:
 295+ self.log('rename %s %s' % (oldPath, newPath))
 296+
259297 def main():
260298 homeDirectoryManager = HomeDirectoryManager()
261299 homeDirectoryManager.run()
Index: trunk/tools/subversion/user-management/add-ldap-user
@@ -25,10 +25,13 @@
2626 parser.add_option("--cn", action="store", dest="cn", help="The user's CN (default: firstname + lastname, or username, if first/last not set)")
2727 parser.add_option("--firstname", action="store", dest="givenName", help="The user's first name (default: username)")
2828 parser.add_option("--lastname", action="store", dest="sn", help="The user's last name (default: username)")
 29+ parser.add_option("--dry-run", action="store_true", dest="dryRun", help="Dry run mode (no changes)")
2930 (options, args) = parser.parse_args()
3031
3132 if len(args) != 2:
3233 parser.error("add-ldap-user expects exactly two arguments.")
 34+
 35+ dryRun = parser.options.dryRun
3336
3437 ldapSupportLib.setBindInfoByOptions(options, parser)
3538
@@ -52,7 +55,7 @@
5356 c.setopt(c.WRITEFUNCTION, buffer.write)
5457 c.perform()
5558 c.close()
56 - raw_keys = buffer.getvalue()
 59+ raw_keys = buffer.getvalue().splitlines(True)
5760 else:
5861 file = open(keyLocation, 'r')
5962 raw_keys = file.readlines()
@@ -118,11 +121,16 @@
119122 userEntry['sn'] = sn
120123 userEntry['cn'] = cn
121124
122 - modlist = ldap.modlist.addModlist(userEntry)
123 - ds.add_s(dn, modlist)
 125+ if not dryRun:
 126+ modlist = ldap.modlist.addModlist(userEntry)
 127+ ds.add_s(dn, modlist)
 128+ else:
 129+ for key in userEntry.keys():
 130+ print "%s -> %s" % (key, userEntry[key])
124131
125132 userdict = {uid: {"uidNumber": int(uidNumber), "gidNumber": int(gidNumber), "sshPublicKey": keys}}
126133 hdm = homedirectorymanager.HomeDirectoryManager()
 134+ hdm.dryRun = self.dryRun
127135 hdm.createHomeDir(userdict)
128136 except ldap.UNWILLING_TO_PERFORM, msg:
129137 sys.stderr.write("LDAP was unwilling to create the user. Error was: %s\n" % msg[0]["info"])

Status & tagging log