コード例 #1
0
def build_library(repository=None,
                  branch=None,
                  namespace=None,
                  push=False,
                  debug=False,
                  prefill=True,
                  registry=None,
                  targetlist=None,
                  repos_folder=None,
                  logger=None):
    ''' Entrypoint method build_library.
        repository:     Repository containing a library/ folder. Can be a
                        local path or git repository
        branch:         If repository is a git repository, checkout this branch
                        (default: DEFAULT_BRANCH)
        namespace:      Created repositories will use the following namespace.
                        (default: no namespace)
        push:           If set to true, push images to the repository
        debug:          Enables debug logging if set to True
        prefill:        Retrieve images from public repository before building.
                        Serves to prefill the builder cache.
        registry:       URL to the private registry where results should be
                        pushed. (only if push=True)
        targetlist:     String indicating which library files are targeted by
                        this build. Entries should be comma-separated. Default
                        is all files.
        repos_folder:   Fixed location where cloned repositories should be
                        stored. Default is None, meaning folders are temporary
                        and cleaned up after the build finishes.
        logger:         Logger instance to use. Default is None, in which case
                        build_library will create its own logger.
    '''
    dst_folder = None
    summary = Summary()
    if logger is None:
        logger = logging.getLogger(__name__)
        logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',
                            level='INFO')

    if repository is None:
        repository = DEFAULT_REPOSITORY
    if branch is None:
        branch = DEFAULT_BRANCH
    if debug:
        logger.setLevel('DEBUG')
    if targetlist is not None:
        targetlist = targetlist.split(',')

    if not repository.startswith(('https://', 'git://')):
        logger.info('Repository provided assumed to be a local path')
        dst_folder = repository

    try:
        client.version()
    except Exception as e:
        logger.error('Could not reach the docker daemon. Please make sure it '
                     'is running.')
        logger.warning('Also make sure you have access to the docker UNIX '
                       'socket (use sudo)')
        return

    if not dst_folder:
        logger.info('Cloning docker repo from {0}, branch: {1}'.format(
            repository, branch))
        try:
            rep, dst_folder = git.clone_branch(repository, branch)
        except Exception as e:
            logger.exception(e)
            logger.error('Source repository could not be fetched. Check '
                         'that the address is correct and the branch exists.')
            return
    try:
        dirlist = os.listdir(os.path.join(dst_folder, 'library'))
    except OSError as e:
        logger.error('The path provided ({0}) could not be found or didn\'t'
                     'contain a library/ folder.'.format(dst_folder))
        return
    for buildfile in dirlist:
        if buildfile == 'MAINTAINERS':
            continue
        if (targetlist and buildfile not in targetlist):
            continue
        f = open(os.path.join(dst_folder, 'library', buildfile))
        linecnt = 0
        for line in f:
            linecnt += 1
            if not line or line.strip() == '':
                continue
            elif line.lstrip().startswith('#'):  # # It's a comment!
                continue
            logger.debug('{0} ---> {1}'.format(buildfile, line))
            try:
                tag, url, ref, dfile = parse_line(line, logger)
                if prefill:
                    logger.debug('Pulling {0} from official repository (cache '
                                 'fill)'.format(buildfile))
                    try:
                        client.pull(buildfile)
                    except:
                        # Image is not on official repository, ignore prefill
                        pass

                img, commit = build_repo(url, ref, buildfile, dfile, tag,
                                         namespace, push, registry,
                                         repos_folder, logger)
                summary.add_success(buildfile, (linecnt, line), img, commit)
            except Exception as e:
                logger.exception(e)
                summary.add_exception(buildfile, (linecnt, line), e)

        f.close()
    cleanup(dst_folder, dst_folder != repository, repos_folder is None)
    summary.print_summary(logger)
    return summary
コード例 #2
0
ファイル: brew.py プロジェクト: Acidburn0zzz/stackbrew
def build_library(repository=None, branch=None, namespace=None, push=False,
                  debug=False, prefill=True, registry=None, targetlist=None,
                  repos_folder=None, logger=None):
    ''' Entrypoint method build_library.
        repository:     Repository containing a library/ folder. Can be a
                        local path or git repository
        branch:         If repository is a git repository, checkout this branch
                        (default: DEFAULT_BRANCH)
        namespace:      Created repositories will use the following namespace.
                        (default: no namespace)
        push:           If set to true, push images to the repository
        debug:          Enables debug logging if set to True
        prefill:        Retrieve images from public repository before building.
                        Serves to prefill the builder cache.
        registry:       URL to the private registry where results should be
                        pushed. (only if push=True)
        targetlist:     String indicating which library files are targeted by
                        this build. Entries should be comma-separated. Default
                        is all files.
        repos_folder:   Fixed location where cloned repositories should be
                        stored. Default is None, meaning folders are temporary
                        and cleaned up after the build finishes.
        logger:         Logger instance to use. Default is None, in which case
                        build_library will create its own logger.
    '''
    dst_folder = None
    summary = Summary()
    if logger is None:
        logger = logging.getLogger(__name__)
        logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',
                            level='INFO')

    if repository is None:
        repository = DEFAULT_REPOSITORY
    if branch is None:
        branch = DEFAULT_BRANCH
    if debug:
        logger.setLevel('DEBUG')
    if targetlist is not None:
        targetlist = targetlist.split(',')

    if not repository.startswith(('https://', 'git://')):
        logger.info('Repository provided assumed to be a local path')
        dst_folder = repository

    try:
        client.version()
    except Exception as e:
        logger.error('Could not reach the docker daemon. Please make sure it '
                     'is running.')
        logger.warning('Also make sure you have access to the docker UNIX '
                       'socket (use sudo)')
        return

    if not dst_folder:
        logger.info('Cloning docker repo from {0}, branch: {1}'.format(
            repository, branch))
        try:
            rep, dst_folder = git.clone_branch(repository, branch)
        except Exception as e:
            logger.exception(e)
            logger.error('Source repository could not be fetched. Check '
                         'that the address is correct and the branch exists.')
            return
    try:
        dirlist = os.listdir(os.path.join(dst_folder, 'library'))
    except OSError as e:
        logger.error('The path provided ({0}) could not be found or didn\'t'
                     'contain a library/ folder.'.format(dst_folder))
        return
    for buildfile in dirlist:
        if buildfile == 'MAINTAINERS':
            continue
        if (targetlist and buildfile not in targetlist):
            continue
        f = open(os.path.join(dst_folder, 'library', buildfile))
        linecnt = 0
        for line in f:
            linecnt += 1
            if not line or line.strip() == '':
                continue
            elif line.lstrip().startswith('#'):  # # It's a comment!
                continue
            logger.debug('{0} ---> {1}'.format(buildfile, line))
            try:
                tag, url, ref, dfile = parse_line(line, logger)
                if prefill:
                    logger.debug('Pulling {0} from official repository (cache '
                                 'fill)'.format(buildfile))
                    try:
                        client.pull(buildfile)
                    except:
                        # Image is not on official repository, ignore prefill
                        pass

                img, commit = build_repo(url, ref, buildfile, dfile, tag,
                                         namespace, push, registry,
                                         repos_folder, logger)
                summary.add_success(buildfile, (linecnt, line), img, commit)
            except Exception as e:
                logger.exception(e)
                summary.add_exception(buildfile, (linecnt, line), e)

        f.close()
    cleanup(dst_folder, dst_folder != repository, repos_folder is None)
    summary.print_summary(logger)
    return summary