Fix "Rootkit" class
This commit is contained in:
parent
bf70db567a
commit
52f0e9b8a3
2 changed files with 61 additions and 69 deletions
|
@ -28,7 +28,7 @@ class Executable:
|
||||||
def exe_file(self) -> Optional[Path]:
|
def exe_file(self) -> Optional[Path]:
|
||||||
return self.__find_exe_file(self.exe_name)
|
return self.__find_exe_file(self.exe_name)
|
||||||
|
|
||||||
def __build_cmd(self, args, kwargs) -> List[Path, Any, ...]:
|
def __build_cmd(self, args, kwargs) -> List:
|
||||||
cmd = [self.exe_file, *args]
|
cmd = [self.exe_file, *args]
|
||||||
|
|
||||||
_logger.debug(f"Executable cmd{cmd}, kwargs{kwargs}")
|
_logger.debug(f"Executable cmd{cmd}, kwargs{kwargs}")
|
||||||
|
@ -40,7 +40,7 @@ class Executable:
|
||||||
**kwargs
|
**kwargs
|
||||||
)
|
)
|
||||||
|
|
||||||
def Popen(self, process_args, **kwargs) -> subprocess.Popen[str]:
|
def Popen(self, process_args, **kwargs) -> subprocess.Popen:
|
||||||
return subprocess.Popen(
|
return subprocess.Popen(
|
||||||
self.__build_cmd(process_args, kwargs),
|
self.__build_cmd(process_args, kwargs),
|
||||||
**kwargs
|
**kwargs
|
||||||
|
|
|
@ -1,65 +1,69 @@
|
||||||
# system
|
import functools
|
||||||
import logging
|
import logging
|
||||||
import os
|
|
||||||
import subprocess
|
import subprocess
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Optional, TypeVar, Union, List
|
||||||
|
|
||||||
|
import attr
|
||||||
|
|
||||||
# local
|
|
||||||
from ._constants import IMAGES_DIRECTORY_NAME, LOCAL_IMAGES_NAME, DEFAULT_IMAGE_NAME
|
from ._constants import IMAGES_DIRECTORY_NAME, LOCAL_IMAGES_NAME, DEFAULT_IMAGE_NAME
|
||||||
from .executable import Executable
|
from .executable import Executable
|
||||||
|
|
||||||
|
_logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
PSL = TypeVar("PSL", Union[Path, str], List[Union[Path, str]])
|
||||||
|
|
||||||
|
|
||||||
|
def prefix_path(path: PSL, prefix: Path = Path("/mnt")) -> PSL:
|
||||||
|
if isinstance(path, Path):
|
||||||
|
return prefix.joinpath(path.absolute())
|
||||||
|
|
||||||
def _prefix_path(prefix, path):
|
|
||||||
if isinstance(path, str):
|
if isinstance(path, str):
|
||||||
abs_path = os.path.abspath(path)
|
return prefix_path(Path(path), prefix)
|
||||||
return os.path.realpath(f"{prefix}/{abs_path}")
|
|
||||||
|
|
||||||
elif isinstance(path, list):
|
elif isinstance(path, list):
|
||||||
return [_prefix_path(prefix, p) for p in path]
|
return [prefix_path(prefix, p) for p in path]
|
||||||
|
|
||||||
|
|
||||||
def prefix_path_mnt(path):
|
@attr.s
|
||||||
return _prefix_path('/mnt/', path)
|
class Rootkit:
|
||||||
|
image_tag: str = attr.ib()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
def _image_name(image_tag):
|
@functools.lru_cache(maxsize=None)
|
||||||
|
def __image_name(image_tag: Optional[str]) -> str:
|
||||||
if image_tag is not None:
|
if image_tag is not None:
|
||||||
return f"{LOCAL_IMAGES_NAME}:{image_tag}"
|
return f"{LOCAL_IMAGES_NAME}:{image_tag}"
|
||||||
else:
|
else:
|
||||||
return DEFAULT_IMAGE_NAME
|
return DEFAULT_IMAGE_NAME
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
class Rootkit:
|
@functools.lru_cache(maxsize=None)
|
||||||
class __Rootkit:
|
def __exists(image_tag: str) -> bool:
|
||||||
__image_tag = None
|
|
||||||
|
|
||||||
def __init__(self, image_tag=None):
|
|
||||||
self.__image_tag = image_tag
|
|
||||||
|
|
||||||
def __exists(self):
|
|
||||||
ps = Executable('docker').run([
|
ps = Executable('docker').run([
|
||||||
'images',
|
'images',
|
||||||
'--filter', f"reference={_image_name(self.__image_tag)}",
|
'--filter', f"reference={Rootkit.__image_name(image_tag)}",
|
||||||
'--format', '{{.Repository}}:{{.Tag}}'
|
'--format', '{{.Repository}}:{{.Tag}}'
|
||||||
], stdout=subprocess.PIPE)
|
], stdout=subprocess.PIPE)
|
||||||
|
|
||||||
return str(ps.stdout, 'utf-8').strip() == _image_name(self.__image_tag)
|
return str(ps.stdout, 'utf-8').strip() == Rootkit.__image_name(image_tag)
|
||||||
|
|
||||||
def __build_image(self):
|
def __build_image(self) -> None:
|
||||||
if self.__exists():
|
if Rootkit.__exists(self.image_tag):
|
||||||
logging.info(f"Using image {_image_name(self.__image_tag)}")
|
_logger.info(f"Using image {Rootkit.__image_name(self.image_tag)}")
|
||||||
else:
|
else:
|
||||||
if self.__image_tag is None:
|
if self.image_tag is None:
|
||||||
logging.info(f"Pulling image {_image_name(self.__image_tag)}")
|
_logger.info(f"Pulling image {Rootkit.__image_name(self.image_tag)}")
|
||||||
Executable('docker').run([
|
Executable('docker').run([
|
||||||
'pull', _image_name(self.__image_tag)
|
'pull', Rootkit.__image_name(self.image_tag)
|
||||||
], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
logging.info(f"Building image {_image_name(self.__image_tag)}")
|
_logger.info(f"Building image {Rootkit.__image_name(self.image_tag)}")
|
||||||
Executable('docker').run([
|
Executable('docker').run([
|
||||||
'build',
|
'build',
|
||||||
'-t', _image_name(self.__image_tag),
|
'-t', Rootkit.__image_name(self.image_tag),
|
||||||
'-f', f"{IMAGES_DIRECTORY_NAME}/{self.__image_tag}.Dockerfile",
|
'-f', f"{IMAGES_DIRECTORY_NAME}/{self.image_tag}.Dockerfile",
|
||||||
f"{IMAGES_DIRECTORY_NAME}"
|
f"{IMAGES_DIRECTORY_NAME}"
|
||||||
], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||||
|
|
||||||
|
@ -69,18 +73,6 @@ class Rootkit:
|
||||||
'run', '--rm',
|
'run', '--rm',
|
||||||
'-v', '/:/mnt',
|
'-v', '/:/mnt',
|
||||||
'-u', 'root',
|
'-u', 'root',
|
||||||
_image_name(self.__image_tag),
|
Rootkit.__image_name(self.image_tag),
|
||||||
*process_args
|
*process_args
|
||||||
], **kwargs)
|
], **kwargs)
|
||||||
|
|
||||||
__image_tag = None
|
|
||||||
__instances = {}
|
|
||||||
|
|
||||||
def __init__(self, image_tag=None):
|
|
||||||
self.__image_tag = image_tag
|
|
||||||
|
|
||||||
if _image_name(self.__image_tag) not in Rootkit.__instances:
|
|
||||||
Rootkit.__instances[_image_name(self.__image_tag)] = Rootkit.__Rootkit(image_tag)
|
|
||||||
|
|
||||||
def __getattr__(self, item):
|
|
||||||
return getattr(self.__instances[_image_name(self.__image_tag)], item)
|
|
||||||
|
|
Loading…
Reference in a new issue