docker-assemble 0.2.2__tar.gz → 0.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {docker_assemble-0.2.2 → docker_assemble-0.3.0}/.gitignore +1 -0
- {docker_assemble-0.2.2/docker_assemble.egg-info → docker_assemble-0.3.0}/PKG-INFO +1 -1
- docker_assemble-0.3.0/docker_assemble/image_exporter.py +169 -0
- docker_assemble-0.3.0/docker_assemble/main.py +40 -0
- {docker_assemble-0.2.2 → docker_assemble-0.3.0/docker_assemble.egg-info}/PKG-INFO +1 -1
- docker_assemble-0.2.2/docker_assemble/image_exporter.py +0 -68
- docker_assemble-0.2.2/docker_assemble/main.py +0 -18
- {docker_assemble-0.2.2 → docker_assemble-0.3.0}/.github/workflows/pypi-publish.yml +0 -0
- {docker_assemble-0.2.2 → docker_assemble-0.3.0}/README.md +0 -0
- {docker_assemble-0.2.2 → docker_assemble-0.3.0}/docker-assemble +0 -0
- {docker_assemble-0.2.2 → docker_assemble-0.3.0}/docker_assemble/__init__.py +0 -0
- {docker_assemble-0.2.2 → docker_assemble-0.3.0}/docker_assemble/docker_utils.py +0 -0
- {docker_assemble-0.2.2 → docker_assemble-0.3.0}/docker_assemble.egg-info/SOURCES.txt +0 -0
- {docker_assemble-0.2.2 → docker_assemble-0.3.0}/docker_assemble.egg-info/dependency_links.txt +0 -0
- {docker_assemble-0.2.2 → docker_assemble-0.3.0}/docker_assemble.egg-info/entry_points.txt +0 -0
- {docker_assemble-0.2.2 → docker_assemble-0.3.0}/docker_assemble.egg-info/requires.txt +0 -0
- {docker_assemble-0.2.2 → docker_assemble-0.3.0}/docker_assemble.egg-info/top_level.txt +0 -0
- {docker_assemble-0.2.2 → docker_assemble-0.3.0}/pyproject.toml +0 -0
- {docker_assemble-0.2.2 → docker_assemble-0.3.0}/requirements.txt +0 -0
- {docker_assemble-0.2.2 → docker_assemble-0.3.0}/setup.cfg +0 -0
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import docker
|
|
2
|
+
import tarfile
|
|
3
|
+
import tempfile
|
|
4
|
+
import os
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
import logging
|
|
7
|
+
import io
|
|
8
|
+
|
|
9
|
+
def extract_image(image_name: str, output_dir: str):
|
|
10
|
+
client = docker.from_env()
|
|
11
|
+
|
|
12
|
+
try:
|
|
13
|
+
image = client.images.get(image_name)
|
|
14
|
+
logging.info(f"Image '{image_name}' found locally.")
|
|
15
|
+
except docker.errors.ImageNotFound:
|
|
16
|
+
logging.info(f"Image '{image_name}' not found locally. Pulling...")
|
|
17
|
+
image = client.images.pull(image_name)
|
|
18
|
+
|
|
19
|
+
container = client.containers.run(image=image_name, command="sleep infinity", detach=True)
|
|
20
|
+
logging.debug(f"Created temporary container: {container.id[:12]}")
|
|
21
|
+
|
|
22
|
+
try:
|
|
23
|
+
stream, _ = container.get_archive("/")
|
|
24
|
+
tmp_tar_path = tempfile.mktemp(suffix=".tar")
|
|
25
|
+
with open(tmp_tar_path, "wb") as f:
|
|
26
|
+
for chunk in stream:
|
|
27
|
+
f.write(chunk)
|
|
28
|
+
|
|
29
|
+
logging.debug(f"Filesystem archive saved to: {tmp_tar_path}")
|
|
30
|
+
|
|
31
|
+
output_path = Path(output_dir).resolve()
|
|
32
|
+
output_path.mkdir(parents=True, exist_ok=True)
|
|
33
|
+
|
|
34
|
+
extract_tar_safely(tmp_tar_path, output_path)
|
|
35
|
+
|
|
36
|
+
logging.info(f"Image filesystem extracted to: {output_path}")
|
|
37
|
+
|
|
38
|
+
finally:
|
|
39
|
+
container.remove(force=True)
|
|
40
|
+
if os.path.exists(tmp_tar_path):
|
|
41
|
+
os.remove(tmp_tar_path)
|
|
42
|
+
logging.debug("Cleaned up temporary container and tar file.")
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def extract_tar_safely(tar_path: str, output_path: Path):
|
|
46
|
+
# def is_safe_path(base: Path, target: Path) -> bool:
|
|
47
|
+
# try:
|
|
48
|
+
# return target.resolve().is_relative_to(base.resolve())
|
|
49
|
+
# except AttributeError:
|
|
50
|
+
# # For Python < 3.9 fallback
|
|
51
|
+
# return str(target.resolve()).startswith(str(base.resolve()))
|
|
52
|
+
|
|
53
|
+
with tarfile.open(tar_path, "r") as tar:
|
|
54
|
+
for member in tar.getmembers():
|
|
55
|
+
member.name = member.name.lstrip("/")
|
|
56
|
+
member_path = output_path / member.name
|
|
57
|
+
|
|
58
|
+
# if not is_safe_path(output_path, member_path):
|
|
59
|
+
# logging.warning(f"Blocked unsafe path: {member.name}, output_path: {output_path}, member_path: {member_path}")
|
|
60
|
+
# continue
|
|
61
|
+
|
|
62
|
+
tar.extract(member, path=output_path)
|
|
63
|
+
logging.debug(f"Extracted: {member.name}")
|
|
64
|
+
|
|
65
|
+
logging.info(f"Extraction completed to: {output_path}")
|
|
66
|
+
|
|
67
|
+
def check_large_files(output_dir, max_size_bytes):
|
|
68
|
+
logging.info(f"Checking for files larger than {max_size_bytes} bytes.")
|
|
69
|
+
large_files = []
|
|
70
|
+
for root, _, files in os.walk(output_dir):
|
|
71
|
+
for file in files:
|
|
72
|
+
file_path = Path(root) / file
|
|
73
|
+
try:
|
|
74
|
+
file_size = os.path.getsize(file_path)
|
|
75
|
+
if file_size > max_size_bytes:
|
|
76
|
+
large_files.append((file_path, file_size))
|
|
77
|
+
except FileNotFoundError:
|
|
78
|
+
logging.error(f"File not found: {file_path}")
|
|
79
|
+
except OSError as e:
|
|
80
|
+
logging.error(f"OS error while getting size of {file_path}: {e}")
|
|
81
|
+
|
|
82
|
+
if large_files:
|
|
83
|
+
logging.warning("The following files exceed the maximum file size:")
|
|
84
|
+
for path, size in large_files:
|
|
85
|
+
logging.warning(f"- {path}: {size} bytes")
|
|
86
|
+
else:
|
|
87
|
+
logging.info("No files exceed the maximum file size.")
|
|
88
|
+
|
|
89
|
+
return large_files
|
|
90
|
+
|
|
91
|
+
def remove_files(large_files):
|
|
92
|
+
while True:
|
|
93
|
+
indices_str = input("Enter the indices of files to remove (comma-separated, or 'no' to skip): ")
|
|
94
|
+
if indices_str.lower() == 'no':
|
|
95
|
+
logging.info("No files will be removed.")
|
|
96
|
+
break
|
|
97
|
+
|
|
98
|
+
try:
|
|
99
|
+
indices = [int(i) for i in indices_str.split(',')]
|
|
100
|
+
files_to_remove = [large_files[i][0] for i in indices]
|
|
101
|
+
|
|
102
|
+
print("Files to be removed:")
|
|
103
|
+
for file in files_to_remove:
|
|
104
|
+
print(file)
|
|
105
|
+
|
|
106
|
+
confirmation = input("Are you sure you want to delete these files? (yes/no): ")
|
|
107
|
+
if confirmation.lower() == 'yes':
|
|
108
|
+
for file in files_to_remove:
|
|
109
|
+
os.remove(file)
|
|
110
|
+
logging.info(f"Removed file: {file}")
|
|
111
|
+
break
|
|
112
|
+
else:
|
|
113
|
+
print("Removal cancelled.")
|
|
114
|
+
except (ValueError, IndexError) as e:
|
|
115
|
+
print(f"Invalid input: {e}")
|
|
116
|
+
|
|
117
|
+
def create_new_image(output_dir, new_image_name):
|
|
118
|
+
client = docker.from_env()
|
|
119
|
+
logging.info(f"Creating new image {new_image_name} from directory: {output_dir}")
|
|
120
|
+
|
|
121
|
+
# Create a temporary Dockerfile
|
|
122
|
+
dockerfile_content = f"""
|
|
123
|
+
FROM scratch
|
|
124
|
+
COPY . /
|
|
125
|
+
"""
|
|
126
|
+
with tempfile.TemporaryDirectory() as build_context:
|
|
127
|
+
dockerfile_path = Path(build_context) / 'Dockerfile'
|
|
128
|
+
with open(dockerfile_path, 'w') as f:
|
|
129
|
+
f.write(dockerfile_content)
|
|
130
|
+
|
|
131
|
+
# Create a tar archive of the output directory
|
|
132
|
+
def generate_tar(directory):
|
|
133
|
+
tar_buffer = io.BytesIO()
|
|
134
|
+
with tarfile.open(fileobj=tar_buffer, mode='w:gz') as tar:
|
|
135
|
+
tar.add(dockerfile_path, arcname='Dockerfile')
|
|
136
|
+
|
|
137
|
+
for root, _, files in os.walk(directory):
|
|
138
|
+
for file in files:
|
|
139
|
+
file_path = os.path.join(root, file)
|
|
140
|
+
rel_path = os.path.relpath(file_path, directory)
|
|
141
|
+
try:
|
|
142
|
+
tar.add(file_path, arcname=rel_path)
|
|
143
|
+
except FileNotFoundError:
|
|
144
|
+
logging.error(f"File not found while creating tar: {file_path}")
|
|
145
|
+
continue
|
|
146
|
+
except Exception as e:
|
|
147
|
+
logging.error(f"Error adding {file_path} to tar: {e}")
|
|
148
|
+
continue
|
|
149
|
+
|
|
150
|
+
tar_buffer.seek(0)
|
|
151
|
+
return tar_buffer.getvalue()
|
|
152
|
+
|
|
153
|
+
tar_stream = generate_tar(output_dir)
|
|
154
|
+
|
|
155
|
+
try:
|
|
156
|
+
with open(dockerfile_path, 'rb') as df:
|
|
157
|
+
response = client.images.build(
|
|
158
|
+
fileobj=io.BytesIO(tar_stream),
|
|
159
|
+
tag=new_image_name,
|
|
160
|
+
custom_context=True,
|
|
161
|
+
rm=True
|
|
162
|
+
)
|
|
163
|
+
for line in response:
|
|
164
|
+
logging.info(line)
|
|
165
|
+
logging.info(f"New image created: {new_image_name}")
|
|
166
|
+
|
|
167
|
+
except docker.errors.BuildError as e:
|
|
168
|
+
logging.error(f"Failed to build image: {e}")
|
|
169
|
+
raise
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
import logging
|
|
3
|
+
import docker_assemble.image_exporter as image_exporter
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def parse_size(size_str):
|
|
7
|
+
suffixes = {'K': 1024, 'M': 1024**2, 'G': 1024**3}
|
|
8
|
+
size_str = size_str.upper()
|
|
9
|
+
if size_str[-1] in suffixes:
|
|
10
|
+
num = size_str[:-1]
|
|
11
|
+
suffix = size_str[-1]
|
|
12
|
+
return int(float(num) * suffixes[suffix])
|
|
13
|
+
else:
|
|
14
|
+
return int(size_str)
|
|
15
|
+
|
|
16
|
+
def run():
|
|
17
|
+
parser = argparse.ArgumentParser(description="Docker Assemble CLI")
|
|
18
|
+
parser.add_argument("-d", action="store_true", help="Disassemble an image")
|
|
19
|
+
parser.add_argument("--debug", action="store_true", help="Enable debug mode")
|
|
20
|
+
parser.add_argument("--maximum-file-size", type=str, help="Maximum file size (e.g., 1G, 100M, 10K). Files larger than this size will be listed.")
|
|
21
|
+
parser.add_argument("--new-image-name", type=str, help="Name for the new Docker image after removing files.")
|
|
22
|
+
parser.add_argument("image", help="Docker image name")
|
|
23
|
+
parser.add_argument("output_dir", nargs="?", default=".", help="Optional output directory")
|
|
24
|
+
|
|
25
|
+
args = parser.parse_args()
|
|
26
|
+
|
|
27
|
+
logging.basicConfig(level=logging.DEBUG if args.debug else logging.INFO)
|
|
28
|
+
|
|
29
|
+
logging.debug(f"Extracting image: {args.image} to directory: {args.output_dir}")
|
|
30
|
+
image_exporter.extract_image(image_name=args.image, output_dir=args.output_dir)
|
|
31
|
+
|
|
32
|
+
if args.maximum_file_size:
|
|
33
|
+
max_size_bytes = parse_size(args.maximum_file_size)
|
|
34
|
+
large_files = image_exporter.check_large_files(args.output_dir, max_size_bytes)
|
|
35
|
+
|
|
36
|
+
if large_files:
|
|
37
|
+
image_exporter.remove_files(large_files)
|
|
38
|
+
|
|
39
|
+
if args.new_image_name:
|
|
40
|
+
image_exporter.create_new_image(args.output_dir, args.new_image_name)
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import docker
|
|
2
|
-
import tarfile
|
|
3
|
-
import tempfile
|
|
4
|
-
import os
|
|
5
|
-
import shutil
|
|
6
|
-
from pathlib import Path
|
|
7
|
-
import logging
|
|
8
|
-
|
|
9
|
-
def extract_image(image_name: str, output_dir: str):
|
|
10
|
-
client = docker.from_env()
|
|
11
|
-
|
|
12
|
-
try:
|
|
13
|
-
image = client.images.get(image_name)
|
|
14
|
-
logging.info(f"Image '{image_name}' found locally.")
|
|
15
|
-
except docker.errors.ImageNotFound:
|
|
16
|
-
logging.info(f"Image '{image_name}' not found locally. Pulling...")
|
|
17
|
-
image = client.images.pull(image_name)
|
|
18
|
-
|
|
19
|
-
container = client.containers.run(image=image_name, command="sleep infinity", detach=True)
|
|
20
|
-
logging.debug(f"Created temporary container: {container.id[:12]}")
|
|
21
|
-
|
|
22
|
-
try:
|
|
23
|
-
stream, _ = container.get_archive("/")
|
|
24
|
-
tmp_tar_path = tempfile.mktemp(suffix=".tar")
|
|
25
|
-
with open(tmp_tar_path, "wb") as f:
|
|
26
|
-
for chunk in stream:
|
|
27
|
-
f.write(chunk)
|
|
28
|
-
|
|
29
|
-
logging.debug(f"Filesystem archive saved to: {tmp_tar_path}")
|
|
30
|
-
|
|
31
|
-
output_path = Path(output_dir).resolve()
|
|
32
|
-
output_path.mkdir(parents=True, exist_ok=True)
|
|
33
|
-
|
|
34
|
-
extract_tar_safely(tmp_tar_path, output_path)
|
|
35
|
-
|
|
36
|
-
logging.info(f"Image filesystem extracted to: {output_path}")
|
|
37
|
-
|
|
38
|
-
finally:
|
|
39
|
-
container.remove(force=True)
|
|
40
|
-
if os.path.exists(tmp_tar_path):
|
|
41
|
-
os.remove(tmp_tar_path)
|
|
42
|
-
logging.debug("Cleaned up temporary container and tar file.")
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
def extract_tar_safely(tar_path: str, output_path: Path):
|
|
46
|
-
# def is_safe_path(base: Path, target: Path) -> bool:
|
|
47
|
-
# try:
|
|
48
|
-
# return target.resolve().is_relative_to(base.resolve())
|
|
49
|
-
# except AttributeError:
|
|
50
|
-
# # For Python < 3.9 fallback
|
|
51
|
-
# return str(target.resolve()).startswith(str(base.resolve()))
|
|
52
|
-
|
|
53
|
-
with tarfile.open(tar_path, "r") as tar:
|
|
54
|
-
for member in tar.getmembers():
|
|
55
|
-
member.name = member.name.lstrip("/")
|
|
56
|
-
member_path = output_path / member.name
|
|
57
|
-
|
|
58
|
-
# if not is_safe_path(output_path, member_path):
|
|
59
|
-
# logging.warning(f"Blocked unsafe path: {member.name}, output_path: {output_path}, member_path: {member_path}")
|
|
60
|
-
# continue
|
|
61
|
-
|
|
62
|
-
tar.extract(member, path=output_path)
|
|
63
|
-
logging.debug(f"Extracted: {member.name}")
|
|
64
|
-
|
|
65
|
-
logging.info(f"Extraction completed to: {output_path}")
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import argparse
|
|
2
|
-
import logging
|
|
3
|
-
import os
|
|
4
|
-
from docker_assemble.image_exporter import extract_image
|
|
5
|
-
|
|
6
|
-
def run():
|
|
7
|
-
parser = argparse.ArgumentParser(description="Docker Assemble CLI")
|
|
8
|
-
parser.add_argument("-d", action="store_true", help="Disassemble an image")
|
|
9
|
-
parser.add_argument("--debug", action="store_true", help="Enable debug mode")
|
|
10
|
-
parser.add_argument("image", help="Docker image name")
|
|
11
|
-
parser.add_argument("output_dir", nargs="?", default=".", help="Optional output directory")
|
|
12
|
-
|
|
13
|
-
args = parser.parse_args()
|
|
14
|
-
|
|
15
|
-
logging.basicConfig(level=logging.DEBUG if args.debug else logging.INFO)
|
|
16
|
-
|
|
17
|
-
logging.debug(f"Extracting image: {args.image} to directory: {args.output_dir}")
|
|
18
|
-
extract_image(image_name=args.image, output_dir=args.output_dir)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{docker_assemble-0.2.2 → docker_assemble-0.3.0}/docker_assemble.egg-info/dependency_links.txt
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|