示例#1
0
def run_command(command, **kwargs):
    # add libexec to PATH if needed
    if constants.LIBEXEC_DIR not in os.environ['PATH'].split(':'):
        os.environ['PATH'] = '%s:%s' % (
            constants.LIBEXEC_DIR,
            os.environ['PATH']
        )
    return utils.run_command(command, **kwargs)
def initialize(templates_dir, clone_url):
    if os.path.exists(templates_dir):
        raise RuntimeError('Failed to initialize, path exists')
    os.makedirs(templates_dir)

    # Clone remote repo:
    ret, _, _ = utils.run_command(['git', 'clone', clone_url, 'git-repo'],
                                  cwd=templates_dir)
    if ret != 0:
        raise RuntimeError('Failed to clone remote repository')
def initialize(templates_dir, clone_url):
    if os.path.exists(templates_dir):
        raise RuntimeError('Failed to initialize, path exists')
    os.makedirs(templates_dir)

    # Clone remote repo:
    ret, _, _ = utils.run_command(['git', 'clone', clone_url, 'git-repo'],
                                  cwd=templates_dir)
    if ret != 0:
        raise RuntimeError('Failed to clone remote repository')
def update(templates_dir):
    logging.info('Updating template directory at %s', templates_dir)

    git_repo = os.path.join(templates_dir, 'git-repo')

    ret, _, _ = utils.run_command(['git', 'fetch', 'origin'], cwd=git_repo)
    if ret != 0:
        logging.warning('Failed to access templates git repo')
        return

    ret, local_head, _ = utils.run_command(['git', 'rev-parse', 'master'],
                                           cwd=git_repo)
    if ret != 0:
        raise RuntimeError('Failed to retrieve current revision')

    logging.debug('Fetching from remote repository')
    ret, remote_head, _ = utils.run_command(
        ['git', 'rev-parse', 'origin/master'], cwd=git_repo)
    if ret != 0:
        raise RuntimeError('Failed to retrieve remote revision')

    if remote_head != local_head:
        logging.debug('Local repository is not up to date, rebasing')
        ret, _, _ = utils.run_command(['git', 'rebase', 'origin/master'],
                                      cwd=git_repo)
        if ret != 0:
            raise RuntimeError('Failed to rebase on remote master')

    for root, dirs, files in os.walk(git_repo):
        dirs[:] = [d for d in dirs if d != '.git']

        for filename in files:
            logging.debug('Checking if %s needs update.', filename)
            path_in_git = os.path.join(root, filename)
            rel_path = path_in_git[len(git_repo):].lstrip('/')
            path_outside_git = os.path.join(templates_dir, rel_path)

            try:
                with open('%s.hash' % path_outside_git) as f:
                    current_rev = f.read()
            except IOError:
                current_rev = ''

            ret, updated_rev, _ = utils.run_command([
                'git', 'log', '-n', '1', '--pretty=format:%H', '--', rel_path
            ],
                                                    cwd=git_repo)
            if ret != 0:
                raise RuntimeError('Failed to retrieve image revision')

            if current_rev != updated_rev:
                logging.debug('Updating %s', filename)
                if os.path.exists(path_outside_git):
                    os.unlink(path_outside_git)
                elif not os.path.exists(os.path.dirname(path_outside_git)):
                    os.makedirs(os.path.dirname(path_outside_git))
                ret, _, _ = qemu_img_convert(path_in_git, 'qcow2',
                                             path_outside_git, 'raw')

                if ret != 0:
                    raise RuntimeError('Failed to convert image')

                with open('%s.hash' % path_outside_git, 'w') as f:
                    f.write(updated_rev)
def qemu_img_convert(frm, frm_format, to, to_format):
    return utils.run_command(
        ['qemu-img', 'convert', '-f', frm_format, frm, '-O', to_format, to])
def update(templates_dir):
    logging.info('Updating template directory at %s', templates_dir)

    git_repo = os.path.join(templates_dir, 'git-repo')

    ret, _, _ = utils.run_command(['git', 'fetch', 'origin'], cwd=git_repo)
    if ret != 0:
        logging.warning('Failed to access templates git repo')
        return

    ret, local_head, _ = utils.run_command(
        ['git', 'rev-parse', 'master'], cwd=git_repo)
    if ret != 0:
        raise RuntimeError('Failed to retrieve current revision')

    logging.debug('Fetching from remote repository')
    ret, remote_head, _ = utils.run_command(
        ['git', 'rev-parse', 'origin/master'], cwd=git_repo)
    if ret != 0:
        raise RuntimeError('Failed to retrieve remote revision')

    if remote_head != local_head:
        logging.debug('Local repository is not up to date, rebasing')
        ret, _, _ = utils.run_command(
            ['git', 'rebase', 'origin/master'], cwd=git_repo)
        if ret != 0:
            raise RuntimeError('Failed to rebase on remote master')

    for root, dirs, files in os.walk(git_repo):
        dirs[:] = [d for d in dirs if d != '.git']

        for filename in files:
            logging.debug('Checking if %s needs update.', filename)
            path_in_git = os.path.join(root, filename)
            rel_path = path_in_git[len(git_repo):].lstrip('/')
            path_outside_git = os.path.join(templates_dir, rel_path)

            try:
                with open('%s.hash' % path_outside_git) as f:
                    current_rev = f.read()
            except IOError:
                current_rev = ''

            ret, updated_rev, _ = utils.run_command(
                ['git', 'log', '-n', '1',
                 '--pretty=format:%H', '--', rel_path],
                cwd=git_repo)
            if ret != 0:
                raise RuntimeError('Failed to retrieve image revision')

            if current_rev != updated_rev:
                logging.debug('Updating %s', filename)
                if os.path.exists(path_outside_git):
                    os.unlink(path_outside_git)
                elif not os.path.exists(os.path.dirname(path_outside_git)):
                    os.makedirs(os.path.dirname(path_outside_git))
                ret, _, _ = qemu_img_convert(path_in_git, 'qcow2',
                                             path_outside_git, 'raw')

                if ret != 0:
                    raise RuntimeError('Failed to convert image')

                with open('%s.hash' % path_outside_git, 'w') as f:
                    f.write(updated_rev)
def qemu_img_convert(frm, frm_format, to, to_format):
    return utils.run_command(['qemu-img', 'convert',
                              '-f', frm_format, frm,
                              '-O', to_format, to])
        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    )

    working_dir = sys.argv[1]
    script_path = sys.argv[2]
    images = sys.argv[3:]

    with utils.RollbackContext() as rollback:
        # We will use each image we update as a snapshot, and if the update
        # is successfull we will merge
        for img in images:
            ret, _, _ = utils.run_command(
                [
                    'qemu-img',
                    'create',
                    '-f', 'qcow2',
                    '-b', img,
                    updating(img)
                ]
            )
            if ret:
                raise RuntimeError('Failed to create copy of image')
            rollback.prependDefer(os.unlink, updating(img))
            # To avoid losing access once livirt changes ownership
            os.chmod(updating(img), 0666)

        config = {
            'nets': {
                NETWORK_NAME: {
                    'dhcp': {
                        'start': 100,