Ejemplo n.º 1
0
def setup_app(command, conf, variables):
    """Place any commands to setup baruwa here"""
    # Don't reload the app if it was loaded under the testing environment
    if not pylons.test.pylonsapp:
        load_environment(conf.global_conf, conf.local_conf)

    # Create the tables if they don't already exist
    print '-' * 100
    log.info("Creating tables")
    Base.metadata.create_all(bind=Session.bind)
    basepath = os.path.dirname(os.path.dirname(__file__))
    # Create the custom functions
    print '-' * 100
    log.info("Creating custom functions")
    sqlfile = os.path.join(basepath, 'baruwa', 'config', 'sql',
                           'functions.sql')
    if os.path.exists(sqlfile):
        with open(sqlfile, 'r') as handle:
            sql = handle.read()
            try:
                conn = Session.connection()
                conn.execute(text(sql))
                Session.commit()
            except ProgrammingError:
                Session.rollback()
    defaultserver = Session.query(Server)\
                    .filter(Server.hostname == 'default')\
                    .all()
    # Create the Mailscanner SQL config views
    print '-' * 100
    log.info("Populating initial sql")
    sqlfile = os.path.join(basepath, 'baruwa', 'config', 'sql',
                           'integration.sql')
    if os.path.exists(sqlfile):
        with open(sqlfile, 'r') as handle:
            sql = handle.read()
        for sqlcmd in sql.split(';'):
            if sqlcmd:
                try:
                    sqlcmd = "%s;" % sqlcmd
                    Session.execute(text(sqlcmd))
                    Session.commit()
                except ProgrammingError:
                    Session.rollback()
    if not defaultserver:
        log.info("Creating the default settings node")
        dfls = Server('default', True)
        Session.add(dfls)
        confserial = ConfigSettings('confserialnumber', 'ConfSerialNumber', 0)
        confserial.value = 1
        confserial.server_id = 1
        Session.add(confserial)
        Session.commit()
        log.info("Default settings node created !")
Ejemplo n.º 2
0
def update_ms_serial(logger):
    """Update MS configuration serial"""
    try:
        msconf = Session.query(ConfigSettings)\
                .filter(ConfigSettings.internal == u'confserialnumber').one()
        msconf.value = int(msconf.value) + 1
    except NoResultFound:
        msconf = ConfigSettings('confserialnumber', 'ConfSerialNumber', 0)
        msconf.value = 1
        msconf.server_id = 1
    Session.add(msconf)
    Session.commit()
    # Session.close()
    logger.info('Scanner serial number updated: %s' % str(msconf.value))
Ejemplo n.º 3
0
def update_ms_serial(logger):
    """Update MS configuration serial"""
    try:
        msconf = Session.query(ConfigSettings)\
                .filter(ConfigSettings.internal == 'confserialnumber').one()
        msconf.value = int(msconf.value) + 1
    except NoResultFound:
        msconf = ConfigSettings(
                'confserialnumber',
                'ConfSerialNumber',
                0)
        msconf.value = 1
        msconf.server_id = 1
    Session.add(msconf)
    Session.commit()
    # Session.close()
    logger.info('Scanner serial number updated: %s' % str(msconf.value))
Ejemplo n.º 4
0
def setup_app(command, conf, variables):
    """Place any commands to setup baruwa here"""
    # Don't reload the app if it was loaded under the testing environment
    if not pylons.test.pylonsapp:
        load_environment(conf.global_conf, conf.local_conf)

    # Create the tables if they don't already exist
    print '-' * 100
    log.info("Creating tables")
    Base.metadata.create_all(bind=Session.bind)
    basepath = os.path.dirname(os.path.dirname(__file__))
    # Create the custom functions
    print '-' * 100
    log.info("Creating custom functions")
    sqlfile = os.path.join(basepath,
                        'baruwa',
                        'config',
                        'sql',
                        'functions.sql')
    if os.path.exists(sqlfile):
        with open(sqlfile, 'r') as handle:
            sql = handle.read()
            try:
                conn = Session.connection()
                conn.execute(text(sql))
                Session.commit()
            except ProgrammingError:
                Session.rollback()
    defaultserver = Session.query(Server)\
                    .filter(Server.hostname == 'default')\
                    .all()
    # Create the Mailscanner SQL config views
    print '-' * 100
    log.info("Populating initial sql")
    sqlfile = os.path.join(basepath,
                        'baruwa',
                        'config',
                        'sql',
                        'integration.sql')
    if os.path.exists(sqlfile):
        with open(sqlfile, 'r') as handle:
            sql = handle.read()
        for sqlcmd in sql.split(';'):
            if sqlcmd:
                try:
                    sqlcmd = "%s;" % sqlcmd
                    Session.execute(text(sqlcmd))
                    Session.commit()
                except ProgrammingError:
                    Session.rollback()
    if not defaultserver:
        log.info("Creating the default settings node")
        dfls = Server('default', True)
        Session.add(dfls)
        confserial = ConfigSettings('confserialnumber',
                                    'ConfSerialNumber',
                                    0)
        confserial.value = 1
        confserial.server_id = 1
        Session.add(confserial)
        Session.commit()
        log.info("Default settings node created !")
    admin = Session.query(User).filter(User.account_type==1).all()
    if not admin:
        def timeout_handler(signum, frame):
            raise TimeoutException()

        old_handler = signal.signal(signal.SIGALRM, timeout_handler) 
        signal.alarm(30)
        try:
            create_user = raw_input('Do you want to configure '
                                'an admin account? (Y/N): ')
        except (TimeoutException, EOFError):
            sys.exit(0)
        finally:
            signal.signal(signal.SIGALRM, old_handler)
        signal.alarm(0)
        if str(create_user).lower() == 'y':
            print '-' * 100
            log.info("Creating initial admin account")
            value_map = {'username': True, 'password1': True,
                        'password2': True, 'firstname': False,
                        'lastname': False, 'email': True}
            values = {}

            def get_input(field, required):
                "Get user input"
                prompt = "Please enter the %s:" % field
                while 1:
                    if field in ['password1', 'password2']:
                        value = getpass.getpass(prompt=prompt)
                    else:
                        value = raw_input(prompt)
                    if not required:
                        break
                    if required and value.strip() != "":
                        if not field in ['email', 'password1', 'password2']:
                            break
                        if field == 'email':
                            if not ADDRESS_RE.match(value):
                                print "Please provide a valid email address."
                            else:
                                break
                        if field == 'password1':
                            try:
                                cracklib.VeryFascistCheck(value)
                            except ValueError, message:
                                print str(message)
                            else:
                                break
                        if field == 'password2':
                            if values['password1'] == value:
                                break
                            else:
                                print 'password2 does not match password1'
                return value

            for attr in value_map:
                value = get_input(attr, value_map[attr])
                values[attr] = value
            user = User(values['username'], values['email'])
            for name in ['firstname', 'lastname']:
                if values[name]:
                    setattr(user, name, values[name])
            user.internal = True
            user.active = True
            user.local = True
            user.account_type = 1
            user.set_password(values['password1'])
            Session.add(user)
            Session.commit()
Ejemplo n.º 5
0
def setup_app(command, conf, variables):
    """Place any commands to setup baruwa here"""
    # Don't reload the app if it was loaded under the testing environment
    if not pylons.test.pylonsapp:
        load_environment(conf.global_conf, conf.local_conf)

    # Create the tables if they don't already exist
    print '-' * 100
    log.info("Creating tables")
    Base.metadata.create_all(bind=Session.bind)
    basepath = os.path.dirname(os.path.dirname(__file__))
    # Create the custom functions
    print '-' * 100
    log.info("Creating custom functions")
    sqlfile = os.path.join(basepath,
                        'baruwa',
                        'config',
                        'sql',
                        'functions.sql')
    if os.path.exists(sqlfile):
        with open(sqlfile, 'r') as handle:
            sql = handle.read()
            try:
                conn = Session.connection()
                conn.execute(text(sql))
                Session.commit()
            except ProgrammingError:
                Session.rollback()
    defaultserver = Session.query(Server)\
                    .filter(Server.hostname == 'default')\
                    .all()
    # Create the Mailscanner SQL config views
    print '-' * 100
    log.info("Populating initial sql")
    sqlfile = os.path.join(basepath,
                        'baruwa',
                        'config',
                        'sql',
                        'integration.sql')
    if os.path.exists(sqlfile):
        with open(sqlfile, 'r') as handle:
            sql = handle.read()
        for sqlcmd in sql.split(';'):
            if sqlcmd:
                try:
                    sqlcmd = "%s;" % sqlcmd
                    Session.execute(text(sqlcmd))
                    Session.commit()
                except ProgrammingError:
                    Session.rollback()
    if not defaultserver:
        log.info("Creating the default settings node")
        dfls = Server('default', True)
        Session.add(dfls)
        confserial = ConfigSettings('confserialnumber',
                                    'ConfSerialNumber',
                                    0)
        confserial.value = 1
        confserial.server_id = 1
        Session.add(confserial)
        Session.commit()
        log.info("Default settings node created !")
Ejemplo n.º 6
0
def setup_app(command, conf, variables):
    """Place any commands to setup baruwa here"""
    # Don't reload the app if it was loaded under the testing environment
    if not pylons.test.pylonsapp:
        load_environment(conf.global_conf, conf.local_conf)

    # Create the tables if they don't already exist
    print '-' * 100
    log.info("Creating tables")
    Base.metadata.create_all(bind=Session.bind)
    basepath = os.path.dirname(os.path.dirname(__file__))
    # Create the custom functions
    print '-' * 100
    log.info("Creating custom functions")
    sqlfile = os.path.join(basepath, 'baruwa', 'config', 'sql',
                           'functions.sql')
    if os.path.exists(sqlfile):
        with open(sqlfile, 'r') as handle:
            sql = handle.read()
            try:
                conn = Session.connection()
                conn.execute(text(sql))
                Session.commit()
            except ProgrammingError:
                Session.rollback()
    defaultserver = Session.query(Server)\
                    .filter(Server.hostname == 'default')\
                    .all()
    # Create the Mailscanner SQL config views
    print '-' * 100
    log.info("Populating initial sql")
    sqlfile = os.path.join(basepath, 'baruwa', 'config', 'sql',
                           'integration.sql')
    if os.path.exists(sqlfile):
        with open(sqlfile, 'r') as handle:
            sql = handle.read()
        for sqlcmd in sql.split(';'):
            if sqlcmd:
                try:
                    sqlcmd = "%s;" % sqlcmd
                    Session.execute(text(sqlcmd))
                    Session.commit()
                except ProgrammingError:
                    Session.rollback()
    if not defaultserver:
        log.info("Creating the default settings node")
        dfls = Server('default', True)
        Session.add(dfls)
        confserial = ConfigSettings('confserialnumber', 'ConfSerialNumber', 0)
        confserial.value = 1
        confserial.server_id = 1
        Session.add(confserial)
        Session.commit()
        log.info("Default settings node created !")
    admin = Session.query(User).filter(User.account_type == 1).all()
    if not admin:

        def timeout_handler(signum, frame):
            raise TimeoutException()

        old_handler = signal.signal(signal.SIGALRM, timeout_handler)
        signal.alarm(30)
        try:
            create_user = raw_input('Do you want to configure '
                                    'an admin account? (Y/N): ')
        except (TimeoutException, EOFError):
            sys.exit(0)
        finally:
            signal.signal(signal.SIGALRM, old_handler)
        signal.alarm(0)
        if str(create_user).lower() == 'y':
            print '-' * 100
            log.info("Creating initial admin account")
            value_map = {
                'username': True,
                'password1': True,
                'password2': True,
                'firstname': False,
                'lastname': False,
                'email': True
            }
            values = {}

            def get_input(field, required):
                "Get user input"
                prompt = "Please enter the %s:" % field
                while 1:
                    if field in ['password1', 'password2']:
                        value = getpass.getpass(prompt=prompt)
                    else:
                        value = raw_input(prompt)
                    if not required:
                        break
                    if required and value.strip() != "":
                        if not field in ['email', 'password1', 'password2']:
                            break
                        if field == 'email':
                            if not ADDRESS_RE.match(value):
                                print "Please provide a valid email address."
                            else:
                                break
                        if field == 'password1':
                            try:
                                cracklib.VeryFascistCheck(value)
                            except ValueError, message:
                                print str(message)
                            else:
                                break
                        if field == 'password2':
                            if values['password1'] == value:
                                break
                            else:
                                print 'password2 does not match password1'
                return value

            for attr in value_map:
                value = get_input(attr, value_map[attr])
                values[attr] = value
            user = User(values['username'], values['email'])
            for name in ['firstname', 'lastname']:
                if values[name]:
                    setattr(user, name, values[name])
            user.internal = True
            user.active = True
            user.local = True
            user.account_type = 1
            user.set_password(values['password1'])
            Session.add(user)
            Session.commit()