def main(): # message to use for sending mail logs MESSAGE = "" MESSAGE += "MAKING ACCOUNTS ON %s FOR %s\n\n" % (HOST, DOMAIN) # get authentication modules modules = auth_modules.load_modules() # fancy python list comprehension to: # select users with the pending_create status that don't have # the DOMAIN_created flag for this domain pending_users = [u for u in User.query.filter_by(status='pending_create').all() if u.in_domain(DOMAIN) and "%s_created" % DOMAIN not in u.get_flags() ] usernames = [u.username for u in pending_users] # if no pending accounts, do nothing if pending_users == []: sys.exit() MESSAGE += "New Accounts\n------------\n" # create login entries for each login method MESSAGE += "Creating logins for:\n%s\n\n" % ", ".join(usernames) for name, module in modules.iteritems(): MESSAGE += module.create_logins(pending_users) MESSAGE += "\n\n" # call ZFS create scripts, and mail scripts on louie if it's research MESSAGE += "Creating %s ZFS filesystems for:\n%s\n\n" % (DOMAIN, ", ".join(usernames)) for user in pending_users: MESSAGE += "DEBUG: zfs_create(user)\n" #zfs_create(user) if user.in_domain('research'): MESSAGE += "DEBUG: research_mail(user)\n" #research_mail(user) # Creating users is complete, add the flag for creation in the DB for user in pending_users: user.add_flag(DOMAIN+"_created") MESSAGE += "\n\nDONE MAKING ACCOUNTS ON %s FOR %s\n" % (HOST, DOMAIN) print MESSAGE mail.send("*****@*****.**", "Super-User", MAILTO, \ "New Accounts - %s" % DOMAIN, MESSAGE, MAILHOST)
def main(): # Security checks # if this is run as root for some reason, drop to "account" if os.getuid() == 0: os.setgid(UID) os.setuid(UID) stderr.write("ERROR: remind.py must be run as root!") # very fancy list comprehensions to get dict of users # by sponsor (maps sponsors to the users they sponsor) # collisions are not possible, a user can not be in multiple sponsor lists users = User.query.filter_by(status='pending_sponsor').all() sponsors = map(lambda s: User.query.get(s), {user.sponsor for user in users}) users_by_sponsor = {sponsor : [user for user in users if user.sponsor == sponsor.username] for sponsor in sponsors} for sponsor, users in users_by_sponsor.iteritems(): # for key, value in dict.iteritems() subject, message = generate_email(users) mail.send("*****@*****.**", "ECE/CIS Labstaff", \ "*****@*****.**" % sponsor.username, subject, message, MAILHOST)
# call ZFS create scripts, and mail scripts on louie if it's research MESSAGE += "Creating %s ZFS filesystems for:\n%s\n\n" % (DOMAIN, ", ".join(usernames)) for user in pending_users: MESSAGE += "DEBUG: zfs_create(user)\n" #zfs_create(user) if user.in_domain('research'): MESSAGE += "DEBUG: research_mail(user)\n" #research_mail(user) # Creating users is complete, add the flag for creation in the DB for user in pending_users: user.add_flag(DOMAIN+"_created") MESSAGE += "\n\nDONE MAKING ACCOUNTS ON %s FOR %s\n" % (HOST, DOMAIN) print MESSAGE mail.send("*****@*****.**", "Super-User", MAILTO, \ "New Accounts - %s" % DOMAIN, MESSAGE, MAILHOST) if __name__ == "__main__": if not os.path.isfile(LOCKFILE): os.mknod(LOCKFILE) main() os.remove(LOCKFILE) else: error = "ERROR: %s still exists, create.py is still running on %s\n\n" % (LOCKFILE, HOST) mail.send("*****@*****.**", "Super-User", MAILTO, \ "New Accounts ERROR - %s" % DOMAIN, error, MAILHOST)
# Security checks # if this is run as root for some reason, drop to "account" if os.getuid() == 0: os.setgid(UID) os.setuid(UID) stderr.write("ERROR: remind.py must be run as root!") # very fancy list comprehensions to get dict of users # by sponsor (maps sponsors to the users they sponsor) # collisions are not possible, a user can not be in multiple sponsor lists users = User.query.filter_by(status='pending_sponsor').all() sponsors = map(lambda s: User.query.get(s), {user.sponsor for user in users}) users_by_sponsor = {sponsor : [user for user in users if user.sponsor == sponsor.username] for sponsor in sponsors} for sponsor, users in users_by_sponsor.iteritems(): # for key, value in dict.iteritems() subject, message = generate_email(users) mail.send("*****@*****.**", "ECE/CIS Labstaff", \ "*****@*****.**" % sponsor.username, subject, message, MAILHOST) if __name__ == "__main__": if not os.path.isfile(LOCKFILE): os.mknod(LOCKFILE) main() os.remove(LOCKFILE) else: error = "ERROR: %s still exists, remind.py is still running on %s\n\n" % (LOCKFILE, HOST) mail.send("*****@*****.**", "Account-user", MAILTO, \ "Sponsor Reminder ERROR - %s" % DOMAIN, error, MAILHOST)