-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpull_docker_image.py
executable file
·69 lines (51 loc) · 1.88 KB
/
pull_docker_image.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python3
# This file should not depend on any repo python files outside of the top-level directory.
from setup_common import LATEST_DOCKER_HUB_IMAGE, update_env_json
import argparse
from pathlib import Path
import os
import pty
import subprocess
import sys
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument("-i", '--docker-hub-image', default=LATEST_DOCKER_HUB_IMAGE,
help='Docker Hub image name (default: %(default)s)')
return parser.parse_args()
def docker_pull(image):
print(f'Pulling {image}...')
cmd = ["docker", "pull", image]
# Open a pseudo-terminal pair
master_fd, slave_fd = pty.openpty()
# Start the subprocess with the slave end as its stdout and stderr.
process = subprocess.Popen(cmd, stdout=slave_fd, stderr=slave_fd, universal_newlines=True)
os.close(slave_fd) # close slave fd in the parent process
# Read from the master end and write to sys.stdout.
try:
while True:
output = os.read(master_fd, 1024)
if not output:
break
sys.stdout.write(output.decode())
sys.stdout.flush()
except OSError:
pass
process.wait()
update_env_json({'DOCKER_IMAGE': image})
if process.returncode == 0:
print('✅ Docker pull successful.')
else:
# Handle unexpected output or errors
print('❗ Unexpected output from docker pull.')
# Optionally, you can raise an exception or handle it as needed
raise RuntimeError("Unexpected output from docker pull.")
def blow_away_target_dir():
repo_root = Path(__file__).parent.resolve()
target_dir = repo_root / 'target'
print(f'Blowing away {target_dir}...')
os.system(f'rm -rf {target_dir}')
def main():
args = get_args()
docker_pull(args.docker_hub_image)
if __name__ == '__main__':
main()