-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_image.py
78 lines (67 loc) · 2.57 KB
/
build_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
70
71
72
73
74
75
76
77
78
import subprocess
import os
import platform
# Configuration Variables
DOCKERFILE_LOCATION = '.' # Directory where the Dockerfile is located
REPOSITORY_NAME = 'italocjs/track_env' # Docker repository name
VERSION = 'latest' # Tag version
# ANSI color codes for terminal output
GREEN = '\033[0;32m'
RED = '\033[0;31m'
YELLOW = '\033[0;33m'
RESET = '\033[0m' # Reset color
def is_docker_installed():
"""
Checks if Docker is installed by attempting to run `docker --version`.
Returns True if Docker is installed, False otherwise.
"""
try:
subprocess.run(['docker', '--version'], check=True)
return True
except (subprocess.CalledProcessError, FileNotFoundError):
return False
def print_system_info():
"""
Prints the current working directory, the username of the user running the script,
the operating system, and detailed system information.
"""
print("Current Location:", os.getcwd())
print("User:", os.getlogin())
print("System:", platform.system())
print("System Information:", platform.platform())
def run_command(command):
"""
Executes a shell command and returns True if the command was executed successfully,
or False if the command failed. Also prints 'Success' in green for success,
or 'Failure' in red for failure.
"""
try:
subprocess.run(command, shell=True, check=True, text=True)
print(GREEN + 'Success' + RESET)
return True
except subprocess.CalledProcessError:
print(RED + 'Failure' + RESET)
return False
def main():
"""
Main function to check Docker installation, print system info, build and push a Docker image
only if the build is successful. The Dockerfile location, repository name, and version
are configurable at the start of the script.
"""
if not is_docker_installed():
print(YELLOW + 'Docker is not installed. Please install Docker to proceed.' + RESET)
return
print_system_info()
# Build the Docker image
build_command = f'docker build -t {REPOSITORY_NAME}:{VERSION} {DOCKERFILE_LOCATION}'
print("Building Docker image...")
build_success = run_command(build_command)
if build_success:
# Push the Docker image only if build was successful
push_command = f'docker push {REPOSITORY_NAME}:{VERSION}'
print("Pushing Docker image...")
run_command(push_command)
else:
print(RED + "Build failed, not pushing the image." + RESET)
if __name__ == '__main__':
main()