-
Notifications
You must be signed in to change notification settings - Fork 11
/
build_docker_windows.py
160 lines (141 loc) · 5.71 KB
/
build_docker_windows.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# Copyright (C) 2023 - 2024 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""Script to build docker image for the project."""
import os
from pathlib import Path
import shutil
import subprocess
import tempfile
import urllib.request
# First, get all environment variables that start with AWP_ROOT
awp_root = {}
for env_key, env_val in os.environ.items():
if env_key.startswith("AWP_ROOT"):
# There is an Ansys installation... Check that the version is at
# least 2023R2. Environment variables are in the form
# AWP_ROOT232=/path/to/2023R2
#
# Get the version number
version = env_key.split("AWP_ROOT")[1]
if version < "232":
# This version is too old, so we will ignore it
continue
else:
# This version is new enough, so we will add it to the list
awp_root[version] = env_val
if len(awp_root) == 0:
# There are no Ansys installations
print("XXXXXXX No Ansys compatible installations found.. exiting process. XXXXXXX")
print("XXXXXXX Please install Ansys 2023R2 or newer. XXXXXXX")
exit(0)
# Request the user to select the version of Ansys to use
print(">>> Select the version of Ansys to use:")
for i, (env_key, _) in enumerate(awp_root.items()):
print(f"{i+1}: {env_key}")
selection = input("Selection [default - last option]: ")
# If no selection is made, use the first version
if selection == "":
print("... No selection made, using default")
selection = len(awp_root)
else:
selection = int(selection)
ANSYS_VER = list(awp_root.keys())[selection - 1]
print(f">>> Using {ANSYS_VER}")
# Get the path to the Ansys installation
ANSYS_PATH = Path(awp_root[ANSYS_VER])
# Verify that the Geometry Service is installed
if not Path.exists(ANSYS_PATH / "GeometryService"):
print("XXXXXXX Geometry Service not installed.. exiting process. XXXXXXX")
exit(0)
# Create a temporary directory to copy the Geometry Service files to
print(">>> Creating temporary directory for building docker image")
TMP_DIR = Path(tempfile.mkdtemp(prefix="docker_geometry_service_"))
# Copy the Geometry Service files to the temporary directory
print(f">>> Copying Geometry Service files to temporary directory to {TMP_DIR}")
BIN_DIR = TMP_DIR / "bins" / "DockerWindows" / "bin" / "x64" / "Release_Headless" / "net472"
# Create the directory structure
shutil.copytree(
ANSYS_PATH / "GeometryService",
BIN_DIR,
)
# ZIP the temporary directory and delete it
print(">>> Zipping temporary directory. This might take some time...")
zip_file = shutil.make_archive(
"windows-binaries",
"zip",
root_dir=TMP_DIR / "bins",
)
# Move the ZIP file to the docker directory
print(">>> Moving ZIP file to temporary directory")
shutil.move(zip_file, TMP_DIR)
# Remove the temporary directory
print(">>> Removing Geometry Service files")
shutil.rmtree(TMP_DIR / "bins")
# Download the Dockerfile from the repository
print(">>> Downloading Dockerfile")
urllib.request.urlretrieve(
"https://raw.githubusercontent.com/ansys/pyansys-geometry/main/docker/windows/Dockerfile",
TMP_DIR / "Dockerfile",
)
# Search for the AWP_ROOT* env variables and replace them with the correct
# value
print(">>> Updating Dockerfile")
with Path.open(TMP_DIR / "Dockerfile", "r") as f:
dockerfile = f.read()
# Find environment variables that start with AWP_ROOT
# inside the Dockerfile and replace them with the correct value
LENGTH_NO_VER = 12
LENGTH_VER = LENGTH_NO_VER + 3
line = dockerfile.find("ENV AWP_ROOT")
if line != -1:
# Get the environment variable name
env_var = dockerfile[line : LENGTH_NO_VER + line] + ANSYS_VER
# Replace the environment variable with the correct value
dockerfile = dockerfile.replace(
dockerfile[line : LENGTH_VER + line],
env_var,
)
else:
print("XXXXXXX No AWP_ROOT environment variable found in Dockerfile.. exiting process. XXXXXXX")
exit(0)
# Check if Docker is installed on the system
print(">>> Checking if Docker is installed")
if shutil.which("docker") is None:
print("XXXXXXX Docker is not installed.. exiting process. XXXXXXX")
exit(0)
# Build the docker image
print(">>> Building docker image. This might take some time...")
out = subprocess.run(
["docker", "build", "-t", "ghcr.io/ansys/geometry:windows-latest", "."],
cwd=TMP_DIR,
capture_output=True,
)
if out.returncode != 0:
print(out.stdout.decode())
print(out.stderr.decode())
print("XXXXXXX Docker build failed.. exiting process. XXXXXXX")
exit(0)
else:
print(">>> Docker image built successfully")
print(">>> Cleaning up temporary directory")
shutil.rmtree(TMP_DIR)
print(">>> Docker image is ready to use")