-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource_cache
More file actions
91 lines (78 loc) · 3.46 KB
/
Copy pathsource_cache
File metadata and controls
91 lines (78 loc) · 3.46 KB
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
# Cache system for CI logs.
# Logs are written to Redis (synchronous) and S3 (background).
# Legacy disk transfer functions (SSH to bastion) are kept but no longer used in normal flow.
# NOTE: This file depends on source_redis being sourced first.
# Transfer log to S3 (accepts gzipped data on stdin).
# Usage: cat file.gz | cache_s3_transfer <key>
# S3 path: <CI_LOGS_S3_LOCATION>/logs/<prefix>/<key>.log.gz
function cache_s3_transfer {
local key="$1"
local prefix="${key:0:4}"
local location="${CI_LOGS_S3_LOCATION:-s3://aztec-ci-artifacts/logs}"
local no_scheme="${location#s3://}"
local bucket="${no_scheme%%/*}"
local base_prefix="${no_scheme#*/}"
aws s3 ${S3_BUILD_CACHE_AWS_PARAMS:-} cp - "s3://${bucket}/${base_prefix}/${prefix}/${key}.log.gz" &>/dev/null
}
# Transfer to S3 with explicit subfolder (accepts gzipped data on stdin).
# Usage: cat file.gz | cache_s3_transfer_to <subfolder> <key>
function cache_s3_transfer_to {
local subfolder="$1"
local key="$2"
local location="${CI_LOGS_S3_LOCATION:-s3://aztec-ci-artifacts/logs}"
local no_scheme="${location#s3://}"
local bucket="${no_scheme%%/*}"
local base_prefix="${no_scheme#*/}"
aws s3 ${S3_BUILD_CACHE_AWS_PARAMS:-} cp - "s3://${bucket}/${base_prefix}/${subfolder}/${key}.log.gz" &>/dev/null
}
# Transfer log to bastion via SSH (accepts gzipped data on stdin)
# Usage: cat file.gz | cache_disk_transfer <key>
function cache_disk_transfer {
local key="$1"
local prefix="${key:0:4}"
# Transfer via SSH pipe using persistent multiplexed connection
# Use first 4 chars as subdirectory to avoid too many files in one dir
ssh -F "${ci3}/aws/build_instance_ssh_config" \
-o ConnectTimeout=5 \
ci-bastion.aztecprotocol.com \
"mkdir -p /logs-disk/${prefix} && cat > /logs-disk/${prefix}/${key}.log.gz" &>/dev/null
}
# Transfer to bastion with explicit subfolder (accepts gzipped data on stdin)
# Usage: cat file.gz | cache_disk_transfer_to <subfolder> <key>
# Legacy: transfer to bastion disk via SSH. Kept for fallback but no longer called in normal flow.
function cache_disk_transfer_to {
local subfolder="$1"
local key="$2"
ssh -F "${ci3}/aws/build_instance_ssh_config" \
-o ConnectTimeout=5 \
ci-bastion.aztecprotocol.com \
"mkdir -p /logs-disk/${subfolder} && cat > /logs-disk/${subfolder}/${key}.log.gz" &>/dev/null
}
# Persistent cache function that writes to both Redis and S3
function cache_persistent {
local key="$1"
local expire="$2"
local tmpfile="/tmp/cache_log_$$_${key}.gz"
cat | gzip > "$tmpfile"
# Write to Redis (synchronous). Skipped when redis is unavailable: the noop
# redis_cli doesn't read stdin, so piping into it would SIGPIPE cat (141
# under pipefail). The S3 write below still runs, keeping the http log
# fallback working on keyless runners.
if [ "$CI_REDIS_AVAILABLE" -eq 1 ]; then
cat "$tmpfile" | redis_cli -x SETEX "$key" "$expire" &>/dev/null
fi
# Write to S3 in background, or in the foreground for a log that must outlive the caller:
# the build instance's top-level log is persisted as its last act, and a background upload
# is killed with the container before it lands.
if [ "${CACHE_PERSIST_SYNC:-0}" -eq 1 ]; then
cat "$tmpfile" | cache_s3_transfer "$key"
rm -f "$tmpfile"
else
{
cat "$tmpfile" | cache_s3_transfer "$key"
rm -f "$tmpfile"
} >&- 2>&- &
fi
}
# Export functions
export -f cache_s3_transfer cache_s3_transfer_to cache_disk_transfer cache_disk_transfer_to cache_persistent