-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ddf1b35
commit 7aa199a
Showing
2 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from .grype import Grype | ||
from .core.backends.binary import GrypeBinaryBackend | ||
from .core.backends.docker import GrypeDockerBackend | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import subprocess | ||
from subprocess import CompletedProcess | ||
|
||
from pygrype.core.backends.base import GrypeBackend | ||
|
||
|
||
DOCKER_EXE = 'docker' | ||
IMAGE_NAME = 'anchore/grype' | ||
|
||
|
||
class GrypeDockerBackend(GrypeBackend): | ||
|
||
def __init__(self, tag: str = "latest") -> None: | ||
self.tag: str = tag | ||
super().__init__() | ||
|
||
@property | ||
def docker_image(self) -> str: | ||
return f"{IMAGE_NAME}:{self.tag}" | ||
|
||
def ensure_backend(self): | ||
try: | ||
# Check if Docker is installed | ||
subprocess.run( | ||
[DOCKER_EXE, '--version'], | ||
check=True, | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE | ||
) | ||
except subprocess.CalledProcessError: | ||
raise Exception("Docker is not installed or not available in the PATH.") | ||
|
||
try: | ||
# Ensure the required Docker image is available | ||
subprocess.run( | ||
[DOCKER_EXE, 'pull', self.docker_image], | ||
check=True, | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE | ||
) | ||
except subprocess.CalledProcessError: | ||
raise Exception(f"Failed to pull the required Docker image '{self.docker_image}'.") | ||
|
||
@property | ||
def executable_string(self) -> str: | ||
return DOCKER_EXE | ||
|
||
def execute(self, *args) -> CompletedProcess: | ||
docker_args = ['run', '--rm', self.docker_image] + list(args) | ||
return super().execute(*docker_args) |