-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathctl
More file actions
executable file
·228 lines (202 loc) · 5.71 KB
/
ctl
File metadata and controls
executable file
·228 lines (202 loc) · 5.71 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env bash
# CHANGE: control the API-first docker-git controller container from the host
# WHY: host should only need Docker while all orchestration runs inside the API controller
# QUOTE(TZ): "Поднимается сервер и ты через него можешь общаться с контейнером"
# REF: user-request-2026-03-15-api-controller
# SOURCE: n/a
# FORMAT THEOREM: forall cmd: valid(cmd) -> controller_action(cmd) terminates
# PURITY: SHELL
# EFFECT: Effect<IO, Error, Env>
# INVARIANT: every API request is executed from inside the controller container; host does not need curl/node/pnpm
# COMPLEXITY: O(1) + network/docker
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
COMPOSE_FILE="$ROOT/docker-compose.yml"
CONTAINER_NAME="docker-git-api"
API_PORT="${DOCKER_GIT_API_PORT:-3334}"
API_HOST="${DOCKER_GIT_API_BIND_HOST:-127.0.0.1}"
API_BASE_URL="http://127.0.0.1:${API_PORT}"
DOCKER_CMD=()
usage() {
cat <<'USAGE'
Usage: ./ctl <command>
Controller:
up Build and start the API controller
down Stop and remove the API controller
ps Show controller status
logs Tail controller logs
restart Restart the controller
shell Open a shell inside the controller
url Print the published API URL
health GET /health through curl running inside the controller
API:
projects GET /projects
request request <METHOD> <PATH> [JSON_BODY]
examples:
./ctl request GET /projects
./ctl request POST /projects '{"repoUrl":"https://github.com/org/repo.git"}'
./ctl request POST /projects/<projectId>/up
USAGE
}
compose() {
"${DOCKER_CMD[@]}" compose -f "$COMPOSE_FILE" "$@"
}
require_running() {
if ! "${DOCKER_CMD[@]}" ps --format '{{.Names}}' | grep -Fxq "$CONTAINER_NAME"; then
echo "Controller is not running. Start it with: ./ctl up" >&2
exit 1
fi
}
api_exec() {
"${DOCKER_CMD[@]}" exec "$CONTAINER_NAME" "$@"
}
normalize_api_path() {
local raw_path="$1"
if [[ "$raw_path" != /projects/* ]]; then
printf '%s' "$raw_path"
return
fi
local normalized
normalized="$("${DOCKER_CMD[@]}" exec -i "$CONTAINER_NAME" node - "$raw_path" <<'NODE'
const raw = process.argv[2] ?? ""
const [pathname, query = ""] = raw.split(/\?(.*)/s, 2)
const prefix = "/projects/"
const joinWithQuery = (path) => query.length > 0 ? `${path}?${query}` : path
const encodeProjectPath = (projectId, suffix = "") =>
joinWithQuery(`${prefix}${encodeURIComponent(projectId)}${suffix}`)
if (!pathname.startsWith(prefix)) {
process.stdout.write(raw)
process.exit(0)
}
const remainder = pathname.slice(prefix.length)
if (!remainder.startsWith("/")) {
process.stdout.write(raw)
process.exit(0)
}
const patterns = [
{
regex: /^(.*)\/agents\/([^/]+)\/(attach|stop|logs)$/u,
render: ([, projectId, agentId, action]) =>
encodeProjectPath(projectId, `/agents/${encodeURIComponent(agentId)}/${action}`)
},
{
regex: /^(.*)\/agents\/([^/]+)$/u,
render: ([, projectId, agentId]) =>
encodeProjectPath(projectId, `/agents/${encodeURIComponent(agentId)}`)
},
{
regex: /^(.*)\/agents$/u,
render: ([, projectId]) => encodeProjectPath(projectId, "/agents")
},
{
regex: /^(.*)\/(up|down|recreate|ps|logs|events)$/u,
render: ([, projectId, action]) => encodeProjectPath(projectId, `/${action}`)
},
{
regex: /^(.*)$/u,
render: ([, projectId]) => encodeProjectPath(projectId)
}
]
for (const { regex, render } of patterns) {
const match = remainder.match(regex)
if (match !== null) {
process.stdout.write(render(match))
process.exit(0)
}
}
process.stdout.write(raw)
NODE
)"
printf '%s' "$normalized"
}
api_request() {
local method="$1"
local path="$2"
local body="${3:-}"
require_running
local normalized_path
normalized_path="$(normalize_api_path "$path")"
if [[ -n "$body" ]]; then
printf '%s' "$body" | "${DOCKER_CMD[@]}" exec -i "$CONTAINER_NAME" sh -lc \
"curl -fsS -X '$method' '$API_BASE_URL$normalized_path' -H 'content-type: application/json' --data-binary @-"
printf '\n'
return
fi
"${DOCKER_CMD[@]}" exec "$CONTAINER_NAME" sh -lc "curl -fsS -X '$method' '$API_BASE_URL$normalized_path'"
printf '\n'
}
wait_for_health() {
require_running
local attempts=30
local delay_seconds=2
local attempt=1
while (( attempt <= attempts )); do
if "${DOCKER_CMD[@]}" exec "$CONTAINER_NAME" sh -lc "curl -fsS '$API_BASE_URL/health' >/dev/null"; then
return 0
fi
sleep "$delay_seconds"
attempt=$((attempt + 1))
done
echo "Controller did not become healthy in time." >&2
return 1
}
resolve_docker_cmd() {
if docker info >/dev/null 2>&1; then
DOCKER_CMD=(docker)
return
fi
if sudo -n docker info >/dev/null 2>&1; then
DOCKER_CMD=(sudo docker)
return
fi
DOCKER_CMD=(docker)
}
resolve_docker_cmd
case "${1:-}" in
up)
compose up -d --build
wait_for_health
echo "Controller API: http://${API_HOST}:${API_PORT}"
;;
down)
compose down
;;
ps)
compose ps
;;
logs)
compose logs -f --tail=200
;;
restart)
compose restart
wait_for_health
;;
shell)
require_running
"${DOCKER_CMD[@]}" exec -it "$CONTAINER_NAME" bash
;;
url)
echo "http://${API_HOST}:${API_PORT}"
;;
health)
api_request GET /health
;;
projects)
api_request GET /projects
;;
request)
if [[ $# -lt 3 ]]; then
echo "Usage: ./ctl request <METHOD> <PATH> [JSON_BODY]" >&2
exit 1
fi
api_request "$2" "$3" "${4:-}"
;;
help|--help|-h|"")
usage
;;
*)
echo "Unknown command: $1" >&2
usage >&2
exit 1
;;
esac