def filter_container_image( image_file, out_file, remove_entries, ): util.existing_file(image_file) if not remove_entries: raise ValueError('remove_entries must not be empty') # allow absolute paths remove_entries = [e.lstrip('/') for e in remove_entries] with tarfile.open(image_file) as tf: manifest = json.load(tf.extractfile('manifest.json')) if not len(manifest) == 1: raise NotImplementedError() manifest = manifest[0] cfg_name = manifest['Config'] with tarfile.open(image_file, 'r') as in_tf, tarfile.open(out_file, 'w') as out_tf: _filter_files( manifest=manifest, cfg_name=cfg_name, in_tarfile=in_tf, out_tarfile=out_tf, remove_entries=set(remove_entries), )
def _push_image(image_reference: str, image_file: str, threads=8): import util util.not_none(image_reference) util.existing_file(image_file) transport = _mk_transport() image_reference = normalise_image_reference(image_reference) image_reference = _parse_image_reference(image_reference) creds = _mk_credentials( image_reference=image_reference, privileges=Privileges.READ_WRITE, ) with v2_2_image.FromTarball(image_file) as v2_2_img: try: with docker_session.Push( image_reference, creds, transport, threads=threads, ) as session: session.upload(v2_2_img) digest = v2_2_img.digest() logger.info( f'{image_reference} was uploaded - digest: {digest}') except Exception as e: import traceback traceback.print_exc() raise e
def store_bulk(file: str, cfg_name: str): elastic_cfg = util.ctx().cfg_factory().elasticsearch(cfg_name) elastic_client = ccc.elasticsearch.from_cfg(elasticsearch_cfg=elastic_cfg) util.existing_file(file) with open(file) as file_handle: result = elastic_client.store_bulk( body=file_handle.read() ) print(result)
def validate(self): semver.parse(self.release_version) # if a tag with the given release version exists, we cannot create another release # pointing to it if self.github_helper.tag_exists(tag_name=self.release_version): raise RuntimeError( f"Cannot create tag '{self.release_version}' for release: Tag already exists" ) if self.component_descriptor_file_path: existing_file(self.component_descriptor_file_path) with open(self.component_descriptor_file_path) as f: # TODO: Proper validation not_empty(f.read().strip())
def store_files(index: str, files: [str], cfg_name: str): elastic_cfg = util.ctx().cfg_factory().elasticsearch(cfg_name) elastic_client = ccc.elasticsearch.from_cfg(elasticsearch_cfg=elastic_cfg) for file in files: util.existing_file(file) for file in files: with open(file) as file_handle: json_body = json.load(file_handle) result = elastic_client.store_document( index=index, body=json_body, ) print(result)
def test_existing_file(self): import sys existing_file = sys.executable result = examinee.existing_file(existing_file) self.assertEqual(existing_file, result) with capture_out() as (stdout, stderr): with self.assertRaises(Failure): examinee.existing_file('no such file, I hope') self.assertIn('not an existing file', stdout.getvalue().strip()) self.assertTrue(len(stderr.getvalue()) == 0) # should also work with pathlib.Path existing_file = pathlib.Path(existing_file) self.assertEqual(examinee.existing_file(existing_file), existing_file)
def get_kubecfg(self): if self.kubeconfig: return kubernetes.client.ApiClient(configuration=self.kubeconfig) kubeconfig = os.environ.get('KUBECONFIG', None) args = global_ctx().args if args and hasattr(args, 'kubeconfig') and args.kubeconfig: kubeconfig = args.kubeconfig if self.kubeconfig: kubeconfig = self.kubeconfig if not kubeconfig: fail('KUBECONFIG env var must be set') return config.load_kube_config(existing_file(kubeconfig))
def _push_image(image_reference: str, image_file: str, threads=8): import util util.not_none(image_reference) util.existing_file(image_file) transport = _mk_transport() image_reference = normalise_image_reference(image_reference) name = _parse_image_reference(image_reference) try: # first try container_registry cfgs from available cfg creds = _credentials(image_reference=image_reference, privileges=Privileges.READ_WRITE) if not creds: print('could not find rw-creds') # fall-back to default docker lookup creds = docker_creds.DefaultKeychain.Resolve(name) except Exception as e: util.fail('Error resolving credentials for {name}: {e}'.format( name=name, e=e)) with v2_2_image.FromTarball(image_file) as v2_2_img: try: with docker_session.Push( name, creds, transport, threads=threads, ) as session: session.upload(v2_2_img) digest = v2_2_img.digest() print(f'{name} was uploaded - digest: {digest}') except Exception as e: import traceback traceback.print_exc() raise e
def validate(self): existing_dir(self.repo_dir) semver.parse(self.release_version) if (self.release_commit_callback): existing_file(self.release_commit_callback) if self.next_version_callback: existing_file(self.next_version_callback) existing_file(self.repository_version_file_path) # perform version ops once to validate args self._calculate_next_cycle_dev_version( release_version=self.release_version, version_operation=self.version_operation, prerelease_suffix=self.prerelease_suffix, )
def step_template(name): step_file = util.existing_file(os.path.join(steps_dir, name + '.mako')) return mako.template.Template(filename=step_file)
def enumerate_single_file(self, file_path: str): file_path = existing_file(file_path) with open(file_path) as f: yield from self._filter_codeowners_entries(f.readlines())
#!/usr/bin/env python3 import os import pathlib import fileinput from util import ( check_env, existing_file, ) repo_dir = check_env('REPO_DIR') effective_version = check_env('EFFECTIVE_VERSION') template_file = existing_file( pathlib.Path(repo_dir, 'concourse', 'resources', 'defaults.mako')) lines_replaced = 0 string_to_match = 'tag = ' for line in fileinput.FileInput(str(template_file), inplace=True): if string_to_match in line: if lines_replaced is not 0: raise RuntimeError( f'More than one image tag found in template file') leading_spaces = line.index(string_to_match) print(f'{leading_spaces * " "}{string_to_match}"{effective_version}"') lines_replaced = 1 else: print(line, end='')