Skip to content

Commit 68417b2

Browse files
committed
OCPBUGS-72387: Create binary archives asynchronously
1 parent 39b242a commit 68417b2

File tree

1 file changed

+73
-16
lines changed

1 file changed

+73
-16
lines changed

bindata/assets/deployments/downloads-deployment.yaml

Lines changed: 73 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,11 @@ spec:
7474
args:
7575
- '-c'
7676
- |
77-
cat <<EOF >>/tmp/serve.py
77+
cat <<EOF >/tmp/serve.py
7878
import errno, http.server, os, re, signal, socket, sys, tarfile, tempfile, threading, time, zipfile
7979
8080
def shutdown_handler(signum, frame):
81+
print("Received signal {}, shutting down...".format(signum), flush=True)
8182
os._exit(0)
8283
signal.signal(signal.SIGTERM, shutdown_handler)
8384
@@ -118,14 +119,39 @@ spec:
118119
119120
httpd.serve_forever()
120121
122+
print('Starting downloads server...', flush=True)
121123
temp_dir = tempfile.mkdtemp()
122-
print('serving from {}'.format(temp_dir))
124+
print('Serving from: {}'.format(temp_dir), flush=True)
123125
os.chdir(temp_dir)
126+
127+
print('Creating arch directories...', flush=True)
124128
for arch in ['amd64', 'arm64', 'ppc64le', 's390x']:
125129
os.mkdir(arch)
130+
126131
content = ['<a href="oc-license">license</a>']
132+
print('Creating license symlink...', flush=True)
127133
os.symlink('/usr/share/openshift/LICENSE', 'oc-license')
128134
135+
# Function to create archives in background
136+
def create_archives_async(arch, operating_system, path, basename, archive_path_root):
137+
try:
138+
print(' [Background] Creating archives for {} {}...'.format(arch, operating_system), flush=True)
139+
140+
print(' [Background] Creating tar archive...', flush=True)
141+
with tarfile.open('{}.tar'.format(archive_path_root), 'w') as tar:
142+
tar.add(path, basename)
143+
144+
print(' [Background] Creating zip archive...', flush=True)
145+
with zipfile.ZipFile('{}.zip'.format(archive_path_root), 'w') as zip:
146+
zip.write(path, basename)
147+
148+
print(' [Background] Done with archives for {} {}'.format(arch, operating_system), flush=True)
149+
except Exception as e:
150+
print(' [Background] ERROR creating archives for {} {}: {}'.format(arch, operating_system, str(e)), flush=True)
151+
152+
print('Creating oc binary symlinks (archives will be created asynchronously)...', flush=True)
153+
archive_threads = []
154+
129155
for arch, operating_system, path in [
130156
('amd64', 'linux', '/usr/share/openshift/linux_amd64/oc'),
131157
('amd64', 'mac', '/usr/share/openshift/mac/oc'),
@@ -135,21 +161,48 @@ spec:
135161
('ppc64le', 'linux', '/usr/share/openshift/linux_ppc64le/oc'),
136162
('s390x', 'linux', '/usr/share/openshift/linux_s390x/oc'),
137163
]:
138-
basename = os.path.basename(path)
139-
target_path = os.path.join(arch, operating_system, basename)
140-
os.mkdir(os.path.join(arch, operating_system))
141-
os.symlink(path, target_path)
142-
base_root, _ = os.path.splitext(basename)
143-
archive_path_root = os.path.join(arch, operating_system, base_root)
144-
with tarfile.open('{}.tar'.format(archive_path_root), 'w') as tar:
145-
tar.add(path, basename)
146-
with zipfile.ZipFile('{}.zip'.format(archive_path_root), 'w') as zip:
147-
zip.write(path, basename)
148-
content.append(
149-
'<a href="{0}">oc ({1} {2})</a> (<a href="{3}.tar">tar</a> <a href="{3}.zip">zip</a>)'.format(
150-
target_path, arch, operating_system, archive_path_root
164+
try:
165+
print(' Processing {} {} ({})...'.format(arch, operating_system, path), flush=True)
166+
167+
# Check if source file exists
168+
if not os.path.exists(path):
169+
print(' WARNING: {} does not exist, skipping'.format(path), flush=True)
170+
continue
171+
172+
file_size = os.path.getsize(path)
173+
print(' Source file size: {} MB'.format(file_size // (1024*1024)), flush=True)
174+
175+
basename = os.path.basename(path)
176+
target_path = os.path.join(arch, operating_system, basename)
177+
178+
print(' Creating directory...', flush=True)
179+
os.mkdir(os.path.join(arch, operating_system))
180+
181+
print(' Creating symlink...', flush=True)
182+
os.symlink(path, target_path)
183+
184+
base_root, _ = os.path.splitext(basename)
185+
archive_path_root = os.path.join(arch, operating_system, base_root)
186+
187+
# Start background thread to create archives
188+
archive_thread = threading.Thread(
189+
target=create_archives_async,
190+
args=(arch, operating_system, path, basename, archive_path_root),
191+
daemon=True
192+
)
193+
archive_thread.start()
194+
archive_threads.append(archive_thread)
195+
196+
content.append(
197+
'<a href="{0}">oc ({1} {2})</a> (<a href="{3}.tar">tar</a> <a href="{3}.zip">zip</a>)'.format(
198+
target_path, arch, operating_system, archive_path_root
199+
)
151200
)
152-
)
201+
print(' Done with {} {} (archives creating in background)'.format(arch, operating_system), flush=True)
202+
except Exception as e:
203+
print(' ERROR processing {} {}: {}'.format(arch, operating_system, str(e)), flush=True)
204+
205+
print('All symlinks created. {} background threads creating archives...'.format(len(archive_threads)), flush=True)
153206
154207
for root, directories, filenames in os.walk(temp_dir):
155208
root_link = os.path.relpath(temp_dir, os.path.join(root, 'child')).replace(os.path.sep, '/')
@@ -162,6 +215,7 @@ spec:
162215
write_index(
163216
path=os.path.join(temp_dir, 'index.html'),
164217
message='\n'.join(
218+
['<p><em>Note: Archive files (.tar, .zip) are generated on server startup and may take a few moments to become available.</em></p>'] +
165219
['<ul>'] +
166220
[' <li>{}</li>'.format(entry) for entry in content] +
167221
['</ul>']
@@ -184,10 +238,13 @@ spec:
184238
else:
185239
raise
186240
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
241+
print('Binding to {}...'.format(addr), flush=True)
187242
sock.bind(addr)
188243
sock.listen(5)
189244
245+
print('Starting 100 worker threads...', flush=True)
190246
[Thread(i, socket=sock) for i in range(100)]
247+
print('Server ready on port 8080!', flush=True)
191248
time.sleep(9e9)
192249
EOF
193250
exec python3 /tmp/serve.py

0 commit comments

Comments
 (0)