def build_image_from_git(self, url, image, git_path=None, git_commit=None, copy_dockerfile_to=None, stream=False, use_cache=False): """ build image from provided url and tag it this operation is asynchronous and you should consume returned generator in order to wait for build to finish :param url: str :param image: ImageName, name of the resulting image :param git_path: str, path to dockerfile within gitrepo :param copy_dockerfile_to: str, copy dockerfile to provided path :param stream: bool, True returns generator, False returns str :param use_cache: bool, True if you want to use cache :return: generator """ logger.info("building image '%s' from git repo '%s' specified as URL '%s'", image, git_path, url) logger.info("will copy Dockerfile to '%s'", copy_dockerfile_to) temp_dir = tempfile.mkdtemp() response = None try: clone_git_repo(url, temp_dir, git_commit) df_path, df_dir = figure_out_dockerfile(temp_dir, git_path) if copy_dockerfile_to: # TODO: pre build plugin shutil.copyfile(df_path, copy_dockerfile_to) response = self.build_image_from_path(df_dir, image, stream=stream, use_cache=use_cache) finally: try: shutil.rmtree(temp_dir) except (IOError, OSError) as ex: # no idea why this is happening logger.warning("Failed to remove dir '%s': '%s'", temp_dir, repr(ex)) logger.info("build finished") return response
def build_image_from_git(self, url, image, git_path=None, git_commit=None, copy_dockerfile_to=None, stream=False, use_cache=False): """ build image from provided url and tag it this operation is asynchronous and you should consume returned generator in order to wait for build to finish :param url: str :param image: ImageName, name of the resulting image :param git_path: str, path to dockerfile within gitrepo :param copy_dockerfile_to: str, copy dockerfile to provided path :param stream: bool, True returns generator, False returns str :param use_cache: bool, True if you want to use cache :return: generator """ logger.info("building image '%s' from git repo '%s' specified as URL '%s'", image, git_path, url) logger.info("will copy Dockerfile to '%s'", copy_dockerfile_to) temp_dir = tempfile.mkdtemp() response = None try: clone_git_repo(url, temp_dir, git_commit) df_path, df_dir = figure_out_dockerfile(temp_dir, git_path) if copy_dockerfile_to: # TODO: pre build plugin shutil.copyfile(df_path, copy_dockerfile_to) response = self.build_image_from_path(df_dir, image, stream=stream, use_cache=use_cache) finally: try: shutil.rmtree(temp_dir) except (IOError, OSError) as ex: # no idea why this is happening logger.warning("Failed to remove dir '%s': %r", temp_dir, ex) logger.info("build finished") return response
def test_clone_git_repo_retry(tmpdir, retry_times, raise_exc): tmpdir_path = str(tmpdir.realpath()) (flexmock(time) .should_receive('sleep') .and_return(None)) if raise_exc: (flexmock(subprocess) .should_receive('check_output') .times(retry_times + 1) .and_raise(subprocess.CalledProcessError, 1, "git clone", output="error")) else: (flexmock(subprocess) .should_receive('check_output') .once() .and_return(True)) (flexmock(subprocess) .should_receive('check_call') .once() .and_raise(CustomTestException)) exception = subprocess.CalledProcessError if raise_exc else CustomTestException with pytest.raises(exception): clone_git_repo(DOCKERFILE_GIT, tmpdir_path, retry_times=retry_times)
def test_figure_out_build_file(tmpdir, repository, expected_path): tmpdir_path = str(tmpdir.realpath()) clone_git_repo(repository, tmpdir_path) path, dir = figure_out_build_file(tmpdir_path) assert path == os.path.join(tmpdir_path, expected_path) assert os.path.isfile(path) assert os.path.isdir(dir)
def test_build_image_from_path(tmpdir, temp_image_name): if MOCK: mock_docker() tmpdir_path = str(tmpdir.realpath()) clone_git_repo(DOCKERFILE_GIT, tmpdir_path) df = tmpdir.join("Dockerfile") assert df.check() t = DockerTasker() response = t.build_image_from_path(tmpdir_path, temp_image_name, use_cache=True) list(response) assert response is not None assert t.image_exists(temp_image_name) t.remove_image(temp_image_name)
def test_build_image_from_path(tmpdir, temp_image_name, docker_tasker): if MOCK: mock_docker() buildargs = {'testarg1': 'testvalue1', 'testarg2': 'testvalue2'} tmpdir_path = str(tmpdir.realpath()) clone_git_repo(DOCKERFILE_GIT, tmpdir_path) df = tmpdir.join("Dockerfile") assert df.check() response = docker_tasker.build_image_from_path(tmpdir_path, temp_image_name, use_cache=True, buildargs=buildargs) list(response) assert response is not None assert docker_tasker.image_exists(temp_image_name) docker_tasker.remove_image(temp_image_name)
def test_clone_git_repo_by_sha1(tmpdir): tmpdir_path = str(tmpdir.realpath()) commit_id = clone_git_repo(DOCKERFILE_GIT, tmpdir_path, commit=DOCKERFILE_SHA1) assert commit_id is not None assert commit_id == DOCKERFILE_SHA1 assert len(commit_id) == 40 # current git hashes are this long assert os.path.isdir(os.path.join(tmpdir_path, '.git'))
def test_clone_git_repo_by_sha1(tmpdir): tmpdir_path = str(tmpdir.realpath()) commit_id = clone_git_repo(DOCKERFILE_GIT, tmpdir_path, commit=DOCKERFILE_SHA1) assert commit_id is not None assert six.text_type(commit_id, encoding="ascii") == six.text_type(DOCKERFILE_SHA1) assert len(commit_id) == 40 # current git hashes are this long assert os.path.isdir(os.path.join(tmpdir_path, '.git'))
def test_clone_git_repo_by_sha1(tmpdir): tmpdir_path = str(tmpdir.realpath()) commit_id = clone_git_repo(DOCKERFILE_GIT, tmpdir_path, commit=DOCKERFILE_SHA1) assert commit_id is not None print(six.text_type(commit_id)) print(commit_id) assert six.text_type(commit_id, encoding="ascii") == six.text_type(DOCKERFILE_SHA1) assert len(commit_id) == 40 # current git hashes are this long assert os.path.isdir(os.path.join(tmpdir_path, '.git'))
def test_figure_out_dockerfile(tmpdir): tmpdir_path = str(tmpdir.realpath()) clone_git_repo(DOCKERFILE_GIT, tmpdir_path) path, dir = figure_out_dockerfile(tmpdir_path) assert os.path.isfile(path) assert os.path.isdir(dir)
def test_clone_git_repo(tmpdir): tmpdir_path = str(tmpdir.realpath()) commit_id = clone_git_repo(DOCKERFILE_GIT, tmpdir_path) assert commit_id is not None assert len(commit_id) == 40 # current git hashes are this long assert os.path.isdir(os.path.join(tmpdir_path, '.git'))