|
| 1 | +#!/bin/bash |
| 2 | +set -e |
| 3 | + |
| 4 | +SCRIPTS_DIR="/opt/scripts" |
| 5 | + |
| 6 | +# Set up dummy probe scripts |
| 7 | +# liveness always returns success |
| 8 | +# readiness always returns failure |
| 9 | +setup_dummy_probes() { |
| 10 | + echo "Setting up dummy probe scripts..." |
| 11 | + cp /usr/local/bin/dummy-probe.sh "$SCRIPTS_DIR/probe.sh" |
| 12 | + cp /usr/local/bin/dummy-readinessprobe "$SCRIPTS_DIR/readinessprobe" |
| 13 | + echo "Dummy probe scripts ready" |
| 14 | +} |
| 15 | + |
| 16 | +# wait max 5m for init container to be ready |
| 17 | +find_init_container() { |
| 18 | + for i in {1..150}; do |
| 19 | + local pid |
| 20 | + pid=$(pgrep -f "agent-utilities-holder_marker" | head -n1) |
| 21 | + if [[ -n "$pid" && -d "/proc/$pid/root/scripts" ]]; then |
| 22 | + echo "$pid" |
| 23 | + return 0 |
| 24 | + fi |
| 25 | + echo "Waiting for init container... (attempt $i)" >&2 |
| 26 | + sleep 2 |
| 27 | + done |
| 28 | + return 1 |
| 29 | +} |
| 30 | + |
| 31 | +link_agent_scripts() { |
| 32 | + local init_scripts_dir="$1" |
| 33 | + |
| 34 | + echo "Linking agent launcher scripts..." |
| 35 | + for script in agent-launcher.sh agent-launcher-lib.sh; do |
| 36 | + ln -sf "$init_scripts_dir/$script" "$SCRIPTS_DIR/$script" |
| 37 | + echo "Linked $script" |
| 38 | + done |
| 39 | +} |
| 40 | + |
| 41 | +# Link probe scripts from init container, replacing dummy ones |
| 42 | +link_probe_scripts() { |
| 43 | + local init_probes_dir="$1" |
| 44 | + |
| 45 | + echo "Linking probe scripts..." |
| 46 | + for probe in probe.sh readinessprobe; do |
| 47 | + if [[ -f "$init_probes_dir/$probe" ]]; then |
| 48 | + ln -sf "$init_probes_dir/$probe" "$SCRIPTS_DIR/$probe" |
| 49 | + echo "Replaced dummy $probe with real one" |
| 50 | + else |
| 51 | + echo "WARNING: $probe not found in init container" |
| 52 | + exit 1 |
| 53 | + fi |
| 54 | + done |
| 55 | +} |
| 56 | + |
| 57 | +# Main function to set up all files |
| 58 | +main() { |
| 59 | + setup_dummy_probes |
| 60 | + |
| 61 | + if init_pid=$(find_init_container); then |
| 62 | + echo "Found init container with PID: $init_pid" |
| 63 | + |
| 64 | + init_root="/proc/$init_pid/root" |
| 65 | + init_scripts="$init_root/scripts" |
| 66 | + init_probes="$init_root/probes" |
| 67 | + |
| 68 | + # Verify scripts directory exists |
| 69 | + if [[ ! -d "$init_scripts" ]]; then |
| 70 | + echo "ERROR: Scripts directory $init_scripts not found" |
| 71 | + exit 1 |
| 72 | + fi |
| 73 | + |
| 74 | + # Verify probes directory exists |
| 75 | + if [[ ! -d "$init_probes" ]]; then |
| 76 | + echo "ERROR: Probes directory $init_probes not found" |
| 77 | + exit 1 |
| 78 | + fi |
| 79 | + |
| 80 | + # Link scripts from init container |
| 81 | + link_agent_scripts "$init_scripts" |
| 82 | + link_probe_scripts "$init_probes" |
| 83 | + |
| 84 | + echo "File setup completed successfully" |
| 85 | + exit 0 |
| 86 | + else |
| 87 | + echo "No init container found" |
| 88 | + exit 1 |
| 89 | + fi |
| 90 | +} |
| 91 | + |
| 92 | +# Only run main if script is executed directly |
| 93 | +if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then |
| 94 | + main "$@" |
| 95 | +fi |
0 commit comments