Skip to content

Commit c5ebf21

Browse files
committed
Clean local-dir when loading it from a storage with no slots
This allows to blindly remove all slots in the storage in emergency and expect that the subsequent CI runs will start from scratch and not with the leftover of the runner's work directory from the previous run (more idempotent behavior).
1 parent 2422066 commit c5ebf21

7 files changed

+85
-21
lines changed

ci-storage

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ STORAGE_MAX_AGE_SEC_DEFAULT = 3600
1717
STORAGE_MAX_AGE_SEC_BAK = 60
1818
STORAGE_DIR_DEFAULT = "~/ci-storage"
1919
META_FILE = ".ci-storage.meta"
20-
LOCAL_META_FILE_DIR = "/tmp" if os.access("/tmp", os.W_OK) else tempfile.gettempdir()
20+
EMPTY_DIR = ".ci-storage.empty-dir"
21+
TEMP_DIR = "/tmp" if os.access("/tmp", os.W_OK) else tempfile.gettempdir()
2122
MAX_FULL_SNAPSHOT_HISTORY = 10
2223

2324

@@ -214,7 +215,19 @@ def action_load(
214215
prefix = f'Checking slot-id="{id}"...'
215216
if id == "*":
216217
if not slot_infos:
217-
print(f"{prefix} {in_storage} has no slots, so exiting with a no-op")
218+
if layer:
219+
print(
220+
f"{prefix} {in_storage} has no slots, so exiting with a no-op"
221+
)
222+
else:
223+
print(
224+
f"{prefix} {in_storage} has no slots, so cleaning f{local_dir}"
225+
)
226+
action_clean(
227+
local_dir=local_dir,
228+
exclude=exclude,
229+
verbose=verbose,
230+
)
218231
return
219232
elif not layer:
220233
slot_id = list(slot_infos.keys())[0]
@@ -283,6 +296,42 @@ def action_load(
283296
slot_info.meta.write_to(local_dir=local_dir)
284297

285298

299+
#
300+
# Removes everything in local_dir. We use rsync and not rm to keep the excludes
301+
# intact and compatible with the "load" action.
302+
#
303+
def action_clean(
304+
*,
305+
local_dir: str,
306+
exclude: list[str],
307+
verbose: bool,
308+
):
309+
empty_dir = f"{TEMP_DIR}/{EMPTY_DIR}.{normalize_slot_id(local_dir)}"
310+
os.makedirs(empty_dir, exist_ok=True)
311+
try:
312+
check_call(
313+
cmd=[
314+
"rsync",
315+
*build_rsync_args(
316+
host=None,
317+
port=None,
318+
action="load",
319+
exclude=exclude,
320+
layer=[],
321+
verbose=verbose,
322+
),
323+
f"{empty_dir}/",
324+
f"{local_dir}/",
325+
],
326+
print_elapsed=True,
327+
)
328+
finally:
329+
try:
330+
os.rmdir(empty_dir)
331+
except Exception:
332+
pass
333+
334+
286335
#
287336
# Stores the content of the local directory in the storage with the provided
288337
# slot id on a remote host.
@@ -684,7 +733,7 @@ class SlotMeta:
684733

685734
@staticmethod
686735
def _path(local_dir: str) -> str:
687-
return f"{LOCAL_META_FILE_DIR}/{META_FILE}.{normalize_slot_id(local_dir)}"
736+
return f"{TEMP_DIR}/{META_FILE}.{normalize_slot_id(local_dir)}"
688737

689738

690739
#
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
source ./common.sh
3+
4+
test -e "$LOCAL_DIR/file-1"
5+
test -e "$LOCAL_DIR/dir-a"
6+
7+
ci-storage \
8+
--slot-id="*" \
9+
load
10+
11+
grep -qF 'Checking slot-id="*"... storage has no slots, so cleaning' "$OUT"
12+
13+
test ! -e "$LOCAL_DIR/file-1"
14+
test ! -e "$LOCAL_DIR/dir-a"

tests/0030-load-star-is-okay-when-no-slots-in-storage.test.sh

Lines changed: 0 additions & 9 deletions
This file was deleted.

tests/0040-load-star-with-slots-in-storage.test.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ source ./common.sh
44
ci-storage \
55
--slot-id=myslot \
66
store
7-
7+
88
ci-storage \
99
--slot-id="*" \
10-
load || error=$?
10+
load
1111

12-
test "$error" == 0
1312
grep -qF 'Checking slot-id="*"... loading the most recent full (non-layer) slot-id="myslot"' "$OUT"

tests/0050-load-absent-slot-ids-and-star-is-okay-when-no-slots-in-storage.test.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ ci-storage \
55
--slot-id="absent1" \
66
--slot-id="absent2" \
77
--slot-id="*" \
8-
load || error=$?
8+
load
99

10-
test "$error" == 0
11-
grep -qF 'Checking slot-id="*"... storage has no slots, so exiting with a no-op' "$OUT"
10+
grep -qF 'Checking slot-id="*"... storage has no slots, so cleaning' "$OUT"

tests/0060-load-absent-slot-ids-and-star-with-slots-in-storage.test.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ source ./common.sh
44
ci-storage \
55
--slot-id=myslot \
66
store
7-
7+
88
ci-storage \
99
--slot-id=absent1 \
1010
--slot-id=absent2 \
1111
--slot-id="*" \
12-
load || error=$?
12+
load
1313

14-
test "$error" == 0
1514
grep -qF 'Checking slot-id="absent1"... not found' "$OUT"
1615
grep -qF 'Checking slot-id="absent2"... not found' "$OUT"
1716
grep -qF 'Checking slot-id="*"... loading the most recent full (non-layer) slot-id="myslot"' "$OUT"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
source ./common.sh
3+
4+
test -e "$LOCAL_DIR/file-1"
5+
test -e "$LOCAL_DIR/dir-a"
6+
7+
ci-storage \
8+
--slot-id="*" \
9+
--layer="*" \
10+
load
11+
12+
test -e "$LOCAL_DIR/file-1"
13+
test -e "$LOCAL_DIR/dir-a"

0 commit comments

Comments
 (0)