def create(shortname, full_name): session = db.Session() if Site.get(shortname): raise Site.Exists elif shell.exists(shell.join(config.SITES_ROOT, shortname)): raise Site.Exists elif not config.NAME_REGEX.match(shortname) or len(shortname) > config.NAME_LIMIT: raise Site.BadName("Site names must be between 2 and {0} characters and be valid hostnames (only letters, numbers, and dashes)".format(config.NAME_LIMIT)) elif Domain.get('.'.join([shortname, config.DEFAULT_DOMAIN])): existing = Domain.get('.'.join([shortname, config.DEFAULT_DOMAIN])) raise Site.BadName("There is already a domain {0} in piccolo, so adding this site would "\ "create a name conflict. Remove {0} from {1} before "\ "adding this site.".format(existing.domain_name, existing.site.shortname)) else: logger.debug("site doesn't exist yet in db") new_site = Site(shortname, full_name) new_site.db_password = shell.generate_password(length=20) new_site.db_username = re.sub(r'[^\w\d]', '_', new_site.shortname) new_site.db_username_mysql = new_site.db_username[:16] # grrr if not shell.is_pretend(): session.add(new_site) session.commit() try: new_site._shell_create() if not shell.is_pretend(): Domain.create('.'.join([new_site.shortname, config.DEFAULT_DOMAIN]), new_site) except shell.ShellActionFailed as e: logger.exception("Shell action failed") raise else: do("service nginx reload") logger.info('Created site "{0}" [{1}]'.format(full_name, shortname))
def create(username, full_name, email, suppress_welcome=False, fake_create=False): if fake_create: suppress_welcome = True session = db.Session() if not config.NAME_REGEX.match(username) or len(username) > config.NAME_LIMIT: raise User.BadName("{0} is not a valid username (containing only letters, numbers, and dashes and being between 2 and {1} characters)".format(username, config.NAME_LIMIT)) if User.get(username): raise User.Exists("There is already a user named {0} in piccolo".format(username)) try: pwd.getpwnam(username) except KeyError: pass else: if not fake_create: raise User.Exists("There is already a user named {0} in /etc/passwd".format(username)) if shell.exists(shell.join(config.USERS_ROOT, username)) and not fake_create: raise User.BadName("Cannot create user {1} because folder {0} already exists.".format(shell.join(config.USERS_ROOT, username), username)) logger.debug("user doesn't exist yet") new_user = User(username, full_name, email) session.add(new_user) session.commit() new_user._temp_password = shell.generate_password(length=12) try: if not fake_create: new_user._shell_create() except User.ShellActionFailed: session.delete(new_user) session.commit() raise else: logger.info('Created user "{0}" [{1}] with contact email <{2}>'.format(full_name, username, email)) if not suppress_welcome: user_vars = new_user._vars() user_vars.update({"$INITIAL_PASSWORD": new_user._temp_password,}) email_message = shell.format(open(shell.join(config.TEMPLATE_ROOT, 'user_email.txt')).read(), user_vars) email_subject = "New Peninsula Account {0}".format(new_user.username) new_user.send_email(email_subject, email_message) logger.info("Sent welcome email to {0}".format(new_user.email)) elif not fake_create: logger.info("User's initial password: {0}".format(new_user._temp_password))