-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathosrd-compose
executable file
·127 lines (106 loc) · 3.38 KB
/
osrd-compose
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
#!/usr/bin/env bash
BASE_PATH="$(realpath "$(dirname "$0")")"
STATE_FILE="${BASE_PATH}/.osrd-compose-state"
BASE_COMPOSE="${BASE_PATH}/docker-compose.yml"
OVERRIDE_DIR="${BASE_PATH}/docker"
# Help
show_help() {
cat << EOF
Usage: $0 [FLAGS] [DOCKER_COMPOSE_COMMANDS]
This script simplifies docker compose operations by managing override files.
Flags (optional, can be combined):
dev-front Add front-end development container (docker/docker-compose.front.yml)
sw Enable single worker mode (docker/docker-compose.single-worker.yml)
host Use host networking mode (docker/docker-compose.host.yml)
default Reset to base configuration only and clear saved state
Examples:
$0 dev-front sw up # Start with front-end and single worker
$0 ps # Use previous configuration
$0 default up # Reset and start base configuration
State System:
The script maintains a state system to remember your last configuration.
When you run the script with flags (dev-front, sw, host), these choices are saved
in a hidden file (.osrd-compose-state). Next time you run the script without
flags, it will automatically use this saved configuration. This lets you run
quick commands like 'ps' or 'logs' without re-specifying your setup each time.
If you want to start fresh, use the 'default' flag to clear the saved state.
State Management:
- Previous flags are saved to .osrd-compose-state
- Without flags, last known state is loaded
- Without flags and no state file, only base compose is used
- 'default' flag clears saved state
EOF
}
# Display help if requested or no args given
if [ "$1" = "--help" ] || [ "$1" = "help" ] || [ $# -eq 0 ]; then
show_help
exit 0
fi
# Initialize arrays for compose files and flags
compose_files=("$BASE_COMPOSE")
flags=()
# Parse command line arguments for flags first
while [[ $# -gt 0 ]]; do
case $1 in
default)
DEFAULT_FLAG=true
shift
;;
dev-front|sw|host)
flags+=("$1")
shift
;;
*)
break
;;
esac
done
# If the default flag was provided while other flags are present, raise an error
if [ -n "$DEFAULT_FLAG" ] && [ ${#flags[@]} -gt 0 ]; then
echo "Error: 'default' flag cannot be combined with other flags"
exit 1
fi
# If the default flag was provided, clear the state file
if [ -n "$DEFAULT_FLAG" ]; then
rm -f "$STATE_FILE"
fi
# If flags were provided, save them
if [ ${#flags[@]} -gt 0 ]; then
printf "%s\n" "${flags[@]}" > "$STATE_FILE"
# If no flags but state exists, load it
elif [ -f "$STATE_FILE" ]; then
mapfile -t flags < "$STATE_FILE"
fi
has_flag() {
local flag="$1"
local f
for f in "${flags[@]}"; do
if [ "$f" = "$flag" ]; then
return 0
fi
done
return 1
}
if has_flag "host"; then
compose_files+=("$OVERRIDE_DIR/docker-compose.host.yml")
fi
if has_flag "dev-front"; then
if has_flag "host"; then
compose_files+=("$OVERRIDE_DIR/docker-compose.host-front.yml")
else
compose_files+=("$OVERRIDE_DIR/docker-compose.front.yml")
fi
fi
if has_flag "sw"; then
compose_files+=("$OVERRIDE_DIR/docker-compose.single-worker.yml")
fi
# Build compose command.
cmd="docker compose"
for file in "${compose_files[@]}"; do
cmd+=" -f $file"
done
# Add remaining arguments
cmd+=" $*"
# Execute the command
echo "$cmd"
exec $cmd