Exemplo n.º 1
0
def upgradedb():
    """Upgrades the existing database to the latest schema and adds the
    default configuration items if they are missing"""
    alembic_version_table_exists = db.engine.dialect.has_table(db.session.connection(), 'alembic_version')

    if not alembic_version_table_exists:
        virtual_domains_table_exists = db.engine.dialect.has_table(db.session.connection(), 'virtual_domains')
        virtual_users_table_exists = db.engine.dialect.has_table(db.session.connection(), 'virtual_users')
        virtual_aliases_table_exists = db.engine.dialect.has_table(db.session.connection(), 'virtual_aliases')

        # If the alembic_version table doesn't exist and the virtual_* tables exist, that means the database is
        # in the default state after following the mail server guide on Linode or DigitalOcean.
        if virtual_domains_table_exists and virtual_users_table_exists and virtual_aliases_table_exists:
            # This marks the first revision as complete, which is the revision that creates the virtual_* tables
            flask_migrate.stamp(revision='bcc85aaa7896')

    flask_migrate.upgrade()
    add_default_configuration_settings()
Exemplo n.º 2
0
def initialize():
    try:
        db.session.remove()
        db.drop_all()
        db.create_all()
        add_default_configuration_settings()
        admin2 = models.Admins().from_json(
            {'username': 'admin2', 'password': 'PostMaster2', 'name': 'Some Admin'})
        enable_ldap_auth = models.Configs.query.filter_by(setting='Enable LDAP Authentication').first()
        enable_ldap_auth.value = 'True'
        ldap_server = models.Configs.query.filter_by(setting='AD Server LDAP String').first()
        ldap_server.value = 'LDAPS://postmaster.local:636'
        domain = models.Configs.query.filter_by(setting='AD Domain').first()
        domain.value = 'postmaster.local'
        ldap_admin_group = models.Configs.query.filter_by(setting='AD PostMaster Group').first()
        ldap_admin_group.value = 'PostMaster Admins'

        try:
            db.session.add(admin2)
            db.session.add(enable_ldap_auth)
            db.session.add(ldap_server)
            db.session.add(domain)
            db.session.add(ldap_admin_group)
            db.session.commit()
        except:
            return False

        domain = models.VirtualDomains().from_json({'name': 'postmaster.com'})
        domain2 = models.VirtualDomains().from_json({'name': 'postmaster.org'})

        try:
            db.session.add(domain)
            db.session.add(domain2)
            db.session.commit()
        except:
            return False

        emailUser = models.VirtualUsers().from_json({'email': '[email protected]', 'password': 'password'})
        emailUser2 = models.VirtualUsers().from_json({'email': '[email protected]', 'password': 'password'})
        emailUser3 = models.VirtualUsers().from_json({'email': '[email protected]', 'password': 'password'})

        try:
            db.session.add(emailUser)
            db.session.add(emailUser2)
            db.session.add(emailUser3)
            db.session.commit()
        except:
            return False

        alias = models.VirtualAliases().from_json({'domain_id': 1, 'source': '[email protected]', 'destination': '[email protected]'})
        alias2 = models.VirtualAliases().from_json({'domain_id': 1, 'source': '[email protected]', 'destination': '[email protected]'})
        alias3 = models.VirtualAliases().from_json({'domain_id': 1, 'source': '[email protected]', 'destination': '[email protected]'})

        try:
            db.session.add(alias)
            db.session.add(alias2)
            db.session.add(alias3)
            db.session.commit()
        except:
            return False

        return True

    except Exception as e:
        print("Unexpected error: {0}".format(e.message))
        return False

    return False