Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_os_release():
'''Given the base layer object, determine if an os-release file exists and
return the PRETTY_NAME string from it. If no release file exists,
return an empty string. Assume that the layer filesystem is mounted'''
# os-release may exist under /etc/ or /usr/lib. We should first check
# for the preferred /etc/os-release and fall back on /usr/lib/os-release
# if it does not exist under /etc
etc_path = os.path.join(rootfs.get_working_dir(), constants.mergedir,
constants.etc_release_path)
lib_path = os.path.join(rootfs.get_working_dir(), constants.mergedir,
constants.lib_release_path)
if not os.path.exists(etc_path):
if not os.path.exists(lib_path):
return ''
etc_path = lib_path
# file exists at this point, try to read it
with open(etc_path, 'r') as f:
lines = f.readlines()
pretty_name = ''
# iterate through os-release file to find OS
for l in lines:
key, val = l.rstrip().split('=', 1)
if key == "PRETTY_NAME":
pretty_name = val
def get_mount_path():
"""Get the path where the filesystem is mounted"""
return os.path.join(rootfs.get_working_dir(), constants.mergedir)
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 common.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)
def cleanup():
"""Clean up the working directory"""
rootfs.clean_up()
rootfs.root_command(rootfs.remove, rootfs.get_working_dir())
def get_image_config(self, manifest):
'''Assuming there now exists a working directory where the image
metadata exists, return the image config'''
config_file = self.get_image_config_file(manifest)
# assuming that the config file path is in the same root path as the
# manifest file
temp_path = rootfs.get_working_dir()
with pushd(temp_path):
with open(config_file) as f:
json_obj = json.loads(f.read())
return json_obj
def get_base_bin():
'''Given the base layer object, find the binary used to identify the
base OS layer. Assume that the layer filesystem is mounted'''
binary = ''
# the path to where the filesystem is mounted
# look at utils/rootfs.py mount_base_layer module
cwd = os.path.join(rootfs.get_working_dir(), constants.mergedir)
for key, value in command_lib.command_lib['base'].items():
for path in value['path']:
if os.path.exists(os.path.join(cwd, path)):
binary = key
break
return binary