Skip to content

Commit

Permalink
Replace get_untar_dir with ImageLayer method
Browse files Browse the repository at this point in the history
This is work towards tern-tools#948

This commit replaces all instances of the old rootfs get_untar_dir
function with the ImageLayer method get_untar_dir. We also remove
an unused function in passthrough.py that uses layer tar files.

Signed-off-by: Nisha K <[email protected]>
  • Loading branch information
Nisha K committed Dec 15, 2021
1 parent e340f66 commit 1644456
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 23 deletions.
5 changes: 2 additions & 3 deletions tern/analyze/common.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2017-2020 VMware, Inc. All Rights Reserved.
# Copyright (c) 2017-2021 VMware, Inc. All Rights Reserved.
# SPDX-License-Identifier: BSD-2-Clause

'''
Expand All @@ -18,7 +18,6 @@
from tern.utils import cache
from tern.utils import constants
from tern.utils import general
from tern.utils import rootfs
from debian_inspector import debcon
from debian_inspector import copyright as debut_copyright

Expand Down Expand Up @@ -152,7 +151,7 @@ def save_to_cache(image):

def is_empty_layer(layer):
'''Return True if the given image layer is empty'''
cwd = rootfs.get_untar_dir(layer.tar_file)
cwd = layer.get_untar_dir()
if len(os.listdir(cwd)) == 0:
return True
return False
Expand Down
2 changes: 1 addition & 1 deletion tern/analyze/default/container/multi_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def apply_layers(image_obj, top_layer):
"""Apply image diff layers without using a kernel snapshot driver"""
# All merging happens in the merge directory
target = os.path.join(rootfs.get_working_dir(), constants.mergedir)
layer_dir = rootfs.get_untar_dir(image_obj.layers[top_layer].tar_file)
layer_dir = image_obj.layers[top_layer].get_untar_dir()
layer_contents = layer_dir + '/*'
# Account for whiteout files
for fd in image_obj.layers[top_layer].files:
Expand Down
2 changes: 1 addition & 1 deletion tern/analyze/default/container/single_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def find_os_release(host_path):
def get_os_release(base_layer):
"""Assuming that the layer tarball is untarred are ready to be inspected,
get the OS information from the os-release file"""
return find_os_release(rootfs.get_untar_dir(base_layer.tar_file))
return find_os_release(base_layer.get_untar_dir())


def get_os_style(image_layer, binary):
Expand Down
4 changes: 2 additions & 2 deletions tern/analyze/default/default_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def find_shell(fspath):
def get_shell(layer):
'''Find the shell if any on the layer filesystem. Assume that the layer
has already been unpacked. If there is no shell, return an empty string'''
cwd = rootfs.get_untar_dir(layer.tar_file)
cwd = layer.get_untar_dir()
return find_shell(cwd)


Expand All @@ -54,7 +54,7 @@ def get_base_bin(first_layer):
image and looking for known binaries there. Assume that the layer has
already been unpacked with the filesystem'''
binary = ''
cwd = rootfs.get_untar_dir(first_layer.tar_file)
cwd = first_layer.get_untar_dir()
for key, value in command_lib.command_lib['base'].items():
for path in value['path']:
if os.path.exists(os.path.join(cwd, path)):
Expand Down
16 changes: 2 additions & 14 deletions tern/analyze/passthrough.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2019-2020 VMware, Inc. All Rights Reserved.
# Copyright (c) 2019-2021 VMware, Inc. All Rights Reserved.
# SPDX-License-Identifier: BSD-2-Clause

"""
Expand All @@ -9,7 +9,6 @@


import logging
import os
import shutil
from stevedore import driver
from stevedore.exception import NoMatches
Expand Down Expand Up @@ -46,18 +45,7 @@ def get_filesystem_command(layer_obj, command):
# is the last token in the command. So the most straightforward way
# to perform this operation is to append the target directory
cmd_list = get_exec_command(command)
cmd_list.append(rootfs.get_untar_dir(layer_obj.tar_file))
return cmd_list


def get_file_command(layer_tar_file, layer_file, command):
'''Given an ImageLayer object's tar_file property and a FileData object
from that layer, along with the command, return the command in list form
with the target file appended at the end'''
cmd_list = get_exec_command(command)
file_path = os.path.join(
rootfs.get_untar_dir(layer_tar_file), layer_file.path)
cmd_list.append(file_path)
cmd_list.append(layer_obj.get_untar_dir())
return cmd_list


Expand Down
2 changes: 1 addition & 1 deletion tern/prep.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def teardown(keep=False):
def clean_image_tars(image_obj):
"""Given an image object, clean up all the image layer contents"""
for layer in image_obj.layers:
fspath = rootfs.get_untar_dir(layer.tar_file)
fspath = layer.get_untar_dir()
if os.path.exists(fspath):
rootfs.root_command(rootfs.remove, fspath)

Expand Down
2 changes: 1 addition & 1 deletion tern/report/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def write_report(report, args):
def clean_image_tars(image_obj):
'''Clean up untar directories'''
for layer in image_obj.layers:
fspath = rootfs.get_untar_dir(layer.tar_file)
fspath = layer.get_untar_dir()
if os.path.exists(fspath):
rootfs.root_command(rootfs.remove, fspath)

Expand Down

0 comments on commit 1644456

Please sign in to comment.