Example #1
0
def init_db(connect_str='sqlite:///:memory:', do_bootstrap=False):
    """
    Policy-Engine specific db initialization and setup for testing.

    :param connect_str: connection string, defaults to sqllite in-memory if none provided
    :return:
    """
    setup_engine_config(connect_str)
    from anchore_engine.db import initialize
    initialize(specific_tables=get_policy_tables(), bootstrap_db=do_bootstrap)
    init_distro_mappings()
    def init_db(connect_str='sqlite:///:memory:', do_bootstrap=True):
        """
        Policy-Engine specific db initialization and setup for testing.

        :param connect_str: connection string, defaults to sqllite in-memory if none provided
        :return:
        """
        conf = TestRDBMSArchiveDriver.setup_engine_config(connect_str)
        from anchore_engine.db import initialize, ArchiveDocument, Anchore, ObjectStorageRecord
        from anchore_engine.db.entities.common import do_create
        from anchore_engine.version import version, db_version
        initialize(versions={'service_version': version, 'db_version': db_version}, localconfig=conf)
        do_create(specific_tables=[ArchiveDocument.__table__,  Anchore.__table__, ObjectStorageRecord.__table__])
    def init_db(cls, connect_str='sqlite:///:memory:', do_bootstrap=True):
        """
        Policy-Engine specific db initialization and setup for testing.

        :param connect_str: connection string, defaults to sqllite in-memory if none provided
        :return:

        """
        conf = cls.setup_engine_config(connect_str)
        from anchore_engine.db import initialize, Account, AccountUser, AccessCredential, Anchore
        from anchore_engine.db.entities.common import do_create
        from anchore_engine.version import version, db_version
        initialize(versions={'service_version': version, 'db_version': db_version}, localconfig=conf)
        do_create(specific_tables=[Account.__table__, AccountUser.__table__, AccessCredential.__table__, Anchore.__table__])
Example #4
0
def init_db(connect_str='sqlite:///:memory:', do_bootstrap=False):
    """
    Policy-Engine specific db initialization and setup for testing.

    :param connect_str: connection string, defaults to sqllite in-memory if none provided
    :return:
    """
    conf = setup_engine_config(connect_str)
    from anchore_engine.db import initialize
    from anchore_engine.db.entities.common import do_create
    pol_tables = get_policy_tables()
    initialize(localconfig=conf)
    if do_bootstrap:
        do_create(pol_tables)

    init_distro_mappings()
def init():
    config = {'credentials': {'database': {'db_connect': conn_str}}}
    initialize(localconfig=config)
Example #6
0
def makeService(snames, options, bootstrap_db=False, bootstrap_users=False):

    try:
        # config and init
        configfile = configdir = None
        if options['config']:
            configdir = options['config']
            configfile = os.path.join(options['config'], 'config.yaml')

        anchore_engine.configuration.localconfig.load_config(
            configdir=configdir, configfile=configfile)
        localconfig = anchore_engine.configuration.localconfig.get_config()
        localconfig['myservices'] = []
        logger.spew("localconfig=" +
                    json.dumps(localconfig, indent=4, sort_keys=True))
    except Exception as err:
        log.err("cannot load configuration: exception - " + str(err))
        raise err

    # get versions of things
    try:
        versions = anchore_engine.configuration.localconfig.get_versions()
    except Exception as err:
        log.err("cannot detect versions of service: exception - " + str(err))
        raise err

    logger.info("initializing database")

    # connect to DB
    try:
        db.initialize(versions=versions,
                      bootstrap_db=bootstrap_db,
                      bootstrap_users=bootstrap_users)
    except Exception as err:
        log.err("cannot connect to configured DB: exception - " + str(err))
        raise err

    #credential bootstrap
    with session_scope() as dbsession:
        system_user = db_users.get('anchore-system', session=dbsession)
        localconfig['system_user_auth'] = (system_user['userId'],
                                           system_user['password'])

    # application object
    application = service.Application("multi-service-" + '-'.join(snames))

    #from twisted.python.log import ILogObserver, FileLogObserver
    #from twisted.python.logfile import DailyLogFile
    #logfile = DailyLogFile("ghgh.log", "/tmp/")

    #multi-service
    retservice = service.MultiService()
    retservice.setServiceParent(application)

    success = False
    try:
        scount = 0
        for sname in snames:
            if sname in localconfig['services'] and localconfig['services'][
                    sname]['enabled']:

                smodule = importlib.import_module("anchore_engine.services." +
                                                  sname)

                s = smodule.createService(sname, localconfig)
                s.setServiceParent(retservice)

                rc = smodule.initializeService(sname, localconfig)
                if not rc:
                    raise Exception("failed to initialize service")

                rc = smodule.registerService(sname, localconfig)
                if not rc:
                    raise Exception("failed to register service")

                logger.debug("starting service: " + sname)
                success = True
                scount += 1
                localconfig['myservices'].append(sname)
            else:
                log.err(
                    "service not enabled in config, not starting service: " +
                    sname)

        if scount == 0:
            log.err(
                "no services/subservices were enabled/started on this host")
            success = False
    except Exception as err:
        log.err("cannot create/init/register service: " + sname +
                " - exception: " + str(err))
        success = False

    if not success:
        log.err("cannot start service (see above for information)")
        traceback.print_exc('Service init failure')
        raise Exception("cannot start service (see above for information)")

    return (retservice)
import sys
from anchore_engine.configuration import localconfig
from anchore_engine import db
from anchore_engine.db.entities import upgrade
from anchore_engine.subsys import logger

logger.enable_bootstrap_logging()

if __name__ == '__main__':
    conf = sys.argv[1]
    localconfig.load_config(conf)
    db.initialize(localconfig.get_config())
    logger.info("Running upgrade test...")
    logger.info("Found version: {}".format(upgrade.get_versions()))
    upgrade.run_upgrade()
    logger.info("Found version: {}".format(upgrade.get_versions()))
    logger.info("Upgrade complete")
Example #8
0
def makeService(snames,
                options,
                db_connect=True,
                require_system_user_auth=True,
                module_name="anchore_engine.services",
                validate_params={}):

    try:
        logger.enable_bootstrap_logging(service_name=','.join(snames))

        try:
            # config and init
            configfile = configdir = None
            if options['config']:
                configdir = options['config']
                configfile = os.path.join(options['config'], 'config.yaml')

            anchore_engine.configuration.localconfig.load_config(
                configdir=configdir,
                configfile=configfile,
                validate_params=validate_params)
            localconfig = anchore_engine.configuration.localconfig.get_config()
            localconfig['myservices'] = []
            logger.spew("localconfig=" +
                        json.dumps(localconfig, indent=4, sort_keys=True))
        except Exception as err:
            logger.error("cannot load configuration: exception - " + str(err))
            raise err

        # get versions of things
        try:
            versions = anchore_engine.configuration.localconfig.get_versions()
        except Exception as err:
            logger.error("cannot detect versions of service: exception - " +
                         str(err))
            raise err

        if db_connect:
            logger.info("initializing database")

            # connect to DB
            try:
                db.initialize(localconfig=localconfig, versions=versions)
            except Exception as err:
                logger.error("cannot connect to configured DB: exception - " +
                             str(err))
                raise err

            #credential bootstrap
            localconfig['system_user_auth'] = (None, None)
            if require_system_user_auth:
                gotauth = False
                max_retries = 60
                for count in range(1, max_retries):
                    if gotauth:
                        continue
                    try:
                        with session_scope() as dbsession:
                            localconfig[
                                'system_user_auth'] = get_system_user_auth(
                                    session=dbsession)
                        if localconfig['system_user_auth'] != (None, None):
                            gotauth = True
                        else:
                            logger.error(
                                "cannot get system user auth credentials yet, retrying ("
                                + str(count) + " / " + str(max_retries) + ")")
                            time.sleep(5)
                    except Exception as err:
                        logger.error(
                            "cannot get system-user auth credentials - service may not have system level access"
                        )
                        localconfig['system_user_auth'] = (None, None)

                if not gotauth:
                    raise Exception(
                        "service requires system user auth to start")

        # application object
        application = service.Application("multi-service-" + '-'.join(snames))

        #multi-service
        retservice = service.MultiService()
        retservice.setServiceParent(application)

        success = False
        try:
            scount = 0
            for sname in snames:
                if sname in localconfig['services'] and localconfig[
                        'services'][sname]['enabled']:

                    smodule = importlib.import_module(module_name + "." +
                                                      sname)

                    s = smodule.createService(sname, localconfig)
                    s.setServiceParent(retservice)

                    rc = smodule.initializeService(sname, localconfig)
                    if not rc:
                        raise Exception("failed to initialize service")

                    rc = smodule.registerService(sname, localconfig)
                    if not rc:
                        raise Exception("failed to register service")

                    logger.debug("starting service: " + sname)
                    success = True
                    scount += 1
                    localconfig['myservices'].append(sname)
                else:
                    logger.error(
                        "service not enabled in config, not starting service: "
                        + sname)

            if scount == 0:
                logger.error(
                    "no services/subservices were enabled/started on this host"
                )
                success = False
        except Exception as err:
            logger.error("cannot create/init/register service: " + sname +
                         " - exception: " + str(err))
            success = False

        if not success:
            logger.error("cannot start service (see above for information)")
            traceback.print_exc('Service init failure')
            raise Exception("cannot start service (see above for information)")

        return (retservice)
    finally:
        logger.disable_bootstrap_logging()