def load(): '''Load the cache''' global cache # Do not try to populate the cache if there is no cache available if not os.path.exists(os.path.join(get_top_dir(), cache_file)): return with open(os.path.join(get_top_dir(), cache_file)) as f: cache = yaml.safe_load(f)
def set_mount_dir(bind=None, working_dir=None): '''Set mount directory according to --bind-mount CLI option (or lack thereof). The mount_dir value is used to set the working directory properly in get_working_dir().''' global mount_dir if bind: mount_dir = bind else: mount_dir = general.get_top_dir(working_dir)
def setup(working_dir=None): """Environment setup working_dir: a directory path other than the default directory""" # create the top directory and cache file logger.debug("Setting up...") top_dir = general.get_top_dir(working_dir) if not os.path.isdir(top_dir): os.makedirs(top_dir) # set the working directory according to user input rootfs.set_working_dir(working_dir) # load the cache cache.load()
def setup(working_dir=None): """Environment setup working_dir: a directory path other than the default directory""" # create the top directory and cache file logger.debug("Setting up...") top_dir = general.get_top_dir(working_dir) if not os.path.isdir(top_dir): os.makedirs(top_dir) # set the working directory according to user input rootfs.set_working_dir(working_dir) # load the cache cache.load() # required to run in a container natively on Windows fs_hash_path = pkg_resources.resource_filename("tern", "tools/fs_hash.sh") rootfs.root_command(["chmod", "+x", fs_hash_path])
def extract_image_metadata(image_tag_string): '''Run docker save and extract the files in a temporary directory''' temp_path = rootfs.get_working_dir() placeholder = os.path.join(general.get_top_dir(), temp_tarfile) try: if general.check_tar(image_tag_string) is True: # image_tag_string is the path to the tar file for raw images rootfs.extract_tarfile(image_tag_string, temp_path) else: image = client.images.get(image_tag_string) result = image.save(chunk_size=2097152, named=True) # write all of the tar byte stream into temporary tar file with open(placeholder, 'wb') as f: for chunk in result: f.write(chunk) # extract tarfile into folder rootfs.extract_tarfile(placeholder, temp_path) # remove temporary tar file os.remove(placeholder) if not os.listdir(temp_path): raise IOError('Unable to untar Docker image') except docker.errors.APIError: # pylint: disable=try-except-raise raise
def extract_image(image_obj): """Run docker save and extract the resulting tarball into the working directory.""" temp_path = rootfs.get_working_dir() placeholder = os.path.join(general.get_top_dir(), constants.temp_tarfile) # try to save the image try: result = image_obj.save(chunk_size=2097152, named=True) # write all of the tar byte stream into temporary tar file with open(placeholder, 'wb') as f: for chunk in result: f.write(chunk) # extract temporary tar file into the working directory rootfs.extract_tarfile(placeholder, temp_path) # remove the tar file os.remove(placeholder) # If these operations didn't work, return False if not os.listdir(temp_path): logger.critical('Unable to extract Docker image') return False return True except docker.errors.APIError as e: logger.critical('Something happened with the Docker client: %s', e) return False
def create_top_dir(working_dir=None): '''Create the top level working directory''' top_dir = general.get_top_dir(working_dir) if not os.path.isdir(top_dir): os.makedirs(top_dir)
def clear(): '''Empty the cache - don't use unless you really have to''' global cache cache = {} with open(os.path.join(get_top_dir(), cache_file), 'w') as f: yaml.dump(cache, f, default_flow_style=False)
def save(): '''Save the cache to the cache file''' with open(os.path.join(get_top_dir(), cache_file), 'w') as f: yaml.dump(cache, f, default_flow_style=False)
def create_top_dir(): '''Create the top level working directory''' top_dir = general.get_top_dir() if not os.path.isdir(top_dir): os.mkdir(top_dir)
def set_working_dir(wd=None): '''Set the working/mount directory according to the --working-dir CLI option (or lack thereof). This value is used to set the working directory properly in get_working_dir().''' global working_dir working_dir = general.get_top_dir(wd)
def get_working_dir(): '''General purpose utility to return the absolute path of the working directory''' return os.path.join(general.get_top_dir(), constants.temp_folder)
def remove_working_dir(): """Remove the working directory for tests""" # don't remove the cache, just the temp folder working_dir = os.path.join(general.get_top_dir(), constants.temp_folder) if os.path.exists(working_dir): shutil.rmtree(working_dir)
def create_working_dir(): """Create the working directory for tests""" working_dir = os.path.join(general.get_top_dir(), constants.temp_folder) if not os.path.isdir(working_dir): os.makedirs(working_dir)