-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathupsun-command-center-bash
More file actions
executable file
·358 lines (330 loc) · 11.9 KB
/
upsun-command-center-bash
File metadata and controls
executable file
·358 lines (330 loc) · 11.9 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/usr/bin/env bash
#ddev-generated
#annertech-ddev
## Description: Upsun Command Centre (ssh, uli, resume, activities, disk) - Bash version
## Usage: upsun-command-center-bash [ACTION] [PROJECT] [ENVIRONMENT]
## Example: "ddev upsun-command-center-bash" (interactive) or "ddev uccb run_log_checker_goaccess myproject main"
## Aliases: uptools, uptool, platform-ulitool, ulitool, platform-resume, ucc, ucc
# Colours
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[1;36m'
BOLD='\033[1m'
NC='\033[0m'
echo_red() { echo -e "${RED}$1${NC}" >&2; }
echo_green() { echo -e "${GREEN}$1${NC}" >&2; }
echo_yellow() { echo -e "${YELLOW}$1${NC}" >&2; }
# Menu options
OPTIONS=(
"1. Drush ULI"
"2. Activities"
"3. Running activity"
"4. SSH"
"5. Resume Environment"
"6. Disk Allocation Helper (beta)"
"7. Log-checker (beta)"
"8. Log-checker with goaccess (beta)"
"9. Download access.log (for local advanced forensics)"
"10. Backup Environment"
"11. Fastly API Token"
"12. Open Dashboard"
)
LIB_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/_lib"
. "$LIB_DIR/upsun-resume.sh"
. "$LIB_DIR/upsun-uli.sh"
. "$LIB_DIR/upsun-ssh.sh"
. "$LIB_DIR/upsun-activities.sh"
. "$LIB_DIR/upsun-activity-in-progress.sh"
. "$LIB_DIR/upsun-disk-helper.sh"
. "$LIB_DIR/upsun-log-checker.sh"
. "$LIB_DIR/upsun-log-checker-goaccess.sh"
. "$LIB_DIR/upsun-download-access-log.sh"
. "$LIB_DIR/upsun-backup.sh"
. "$LIB_DIR/upsun-fastly-api-token.sh"
. "$LIB_DIR/upsun-dashboard.sh"
# Get current project ID from .ddev/config.yaml
get_current_project_id() {
if [[ -f ".ddev/config.yaml" ]]; then
grep -oP 'PLATFORM_PROJECT[=:]\s*\K[^\s\n]+' .ddev/config.yaml 2>/dev/null
fi
}
# FZF select helper
fzf_select() {
local header="$1"
local query="$2"
if [[ -n "$query" ]]; then
fzf --reverse --height=50% --header="$header" --query="$query" -1
else
fzf --reverse --height=50% --header="$header" -1
fi
}
# Select action
select_action() {
echo_green "Start typing to select an action"
printf '%s\n' "${OPTIONS[@]}" | fzf --reverse --height=50% --header="Action" -1
}
# Select project
select_project() {
echo_green "Start typing to select a project"
local current_project
current_project=$(get_current_project_id)
local projects
projects=$(ddev exec upsun project:list --no-header --columns=id,title --format=tsv --count=0)
if [[ -z "$projects" ]]; then
echo_red "Failed to get project list"
return 1
fi
# Move current project to top and mark it
if [[ -n "$current_project" ]]; then
local matching
local others
matching=$(echo "$projects" | grep "^${current_project}" | sed 's/$/ (current)/')
others=$(echo "$projects" | grep -v "^${current_project}")
projects=$(printf '%s\n%s' "$matching" "$others" | grep -v '^$')
fi
echo "$projects" | fzf --reverse --height=50% --header="id Project" -1
}
# Select environment
select_environment() {
local project_id="$1"
echo_green "Start typing to select an environment"
local environments
environments=$(ddev exec upsun environment:list --project="${project_id}" --no-header --columns=id --format=tsv)
if [[ -z "$environments" ]]; then
echo_red "Failed to get environment list"
return 1
fi
echo "$environments" | fzf --reverse --height=50% --header="Environment" -1
}
# Find project by name or ID
find_project() {
local search_term="$1"
local projects
projects=$(ddev exec upsun project:list --no-header --columns=id,title --format=tsv --count=0)
# Try exact match on ID first
local match
match=$(echo "$projects" | grep "^${search_term}[[:space:]]" | head -n1)
# If no match, try case-insensitive match on title
if [[ -z "$match" ]]; then
match=$(echo "$projects" | grep -i "[[:space:]]${search_term}" | head -n1)
fi
echo "$match"
}
# Print equivalent non-interactive command
print_command() {
local action="$1"
local project="$2"
local env="$3"
local cmd="ddev ucc ${action} ${project}"
[[ -n "$env" ]] && cmd="${cmd} ${env}"
echo -e "${BOLD}Equivalent command:${NC}"
echo -e "${RED}${BOLD}${cmd}${NC}"
echo ""
}
# Main function
main() {
local action_arg="$1"
local project_arg="$2"
local env_arg="$3"
# Show banner only in interactive mode
if [[ -z "$action_arg" ]]; then
echo_green "UPSUN"
echo_green "Command"
echo_green "Centre"
echo "by Annertech"
echo "and Bill Seremetis"
echo ""
fi
# Select or use provided action
local action_label
if [[ -n "$action_arg" ]]; then
# Map function name to action label
case "$action_arg" in
upsun_ssh) action_label="SSH" ;;
upsun_uli) action_label="Drush ULI" ;;
upsun_resume) action_label="Resume Environment" ;;
upsun_activities) action_label="Activities" ;;
upsun_activity_in_progress) action_label="Running activity" ;;
upsun_disk_helper) action_label="Disk Allocation Helper (beta)" ;;
upsun_log_checker) action_label="Log-checker (beta)" ;;
upsun_log_checker_goaccess) action_label="Log-checker with goaccess (beta)" ;;
upsun_download_access_log) action_label="Download access.log" ;;
upsun_backup) action_label="Backup Environment" ;;
upsun_fastly_api_token) action_label="Fastly API Token" ;;
upsun_dashboard) action_label="Open Dashboard" ;;
*)
echo_red "Unknown action: $action_arg"
echo_yellow "Available actions: upsun_ssh, upsun_uli, upsun_resume, upsun_activities, upsun_activity_in_progress, upsun_disk_helper, upsun_log_checker, upsun_log_checker_goaccess, upsun_download_access_log, upsun_backup, upsun_fastly_api_token"
exit 1
;;
esac
else
local action_selection
action_selection=$(select_action)
if [[ -z "$action_selection" ]]; then
echo_red "Nothing selected... Exiting"
exit 0
fi
action_label=$(echo "$action_selection" | sed 's/^[0-9]*\. //')
fi
# Select or use provided project
local project_id project_name
if [[ -n "$project_arg" ]]; then
local project_selection
project_selection=$(find_project "$project_arg")
if [[ -z "$project_selection" ]]; then
echo_red "Project not found: $project_arg"
exit 1
fi
project_id=$(echo "$project_selection" | cut -f1)
project_name=$(echo "$project_selection" | cut -f2)
[[ -z "$project_name" ]] && project_name="$project_id"
else
local project_selection
project_selection=$(select_project)
if [[ -z "$project_selection" ]]; then
echo_red "Nothing selected... Exiting"
exit 0
fi
project_id=$(echo "$project_selection" | cut -f1 | sed 's/ (current)$//')
project_name=$(echo "$project_selection" | cut -f2 | sed 's/ (current)$//')
[[ -z "$project_name" ]] && project_name="$project_id"
fi
# Execute selected action
case "$action_label" in
"SSH")
print_command "upsun_ssh" "$project_id"
upsun_ssh "$project_id" "$project_name"
;;
"Drush ULI")
local env
if [[ -n "$env_arg" ]]; then
env="$env_arg"
else
env=$(select_environment "$project_id")
if [[ -z "$env" ]]; then
echo_red "No environment selected... Exiting"
exit 0
fi
fi
print_command "upsun_uli" "$project_id" "$env"
upsun_uli "$project_id" "$project_name" "$env"
;;
"Resume Environment")
local env
if [[ -n "$env_arg" ]]; then
env="$env_arg"
else
env=$(select_environment "$project_id")
if [[ -z "$env" ]]; then
echo_red "No environment selected... Exiting"
exit 0
fi
fi
print_command "upsun_resume" "$project_id" "$env"
upsun_resume "$project_id" "$project_name" "$env"
;;
"Activities")
print_command "upsun_activities" "$project_id"
upsun_activities "$project_id" "$project_name"
;;
"Running activity")
local env
if [[ -n "$env_arg" ]]; then
env="$env_arg"
else
env=$(select_environment "$project_id")
if [[ -z "$env" ]]; then
echo_red "No environment selected... Exiting"
exit 0
fi
fi
print_command "upsun_activity_in_progress" "$project_id" "$env"
upsun_activity_in_progress "$project_id" "$project_name" "$env"
;;
"Disk Allocation Helper (beta)")
print_command "upsun_disk_helper" "$project_id"
upsun_disk_helper "$project_id" "$project_name"
;;
"Log-checker (beta)")
local env
if [[ -n "$env_arg" ]]; then
env="$env_arg"
else
env=$(select_environment "$project_id")
if [[ -z "$env" ]]; then
echo_red "No environment selected... Exiting"
exit 0
fi
fi
print_command "upsun_log_checker" "$project_id" "$env"
upsun_log_checker "$project_id" "$project_name" "$env"
;;
"Log-checker with goaccess (beta)")
local env
if [[ -n "$env_arg" ]]; then
env="$env_arg"
else
env=$(select_environment "$project_id")
if [[ -z "$env" ]]; then
echo_red "No environment selected... Exiting"
exit 0
fi
fi
print_command "upsun_log_checker_goaccess" "$project_id" "$env"
upsun_log_checker_goaccess "$project_id" "$project_name" "$env"
;;
"Download access.log (for local advanced forensics)")
local env
if [[ -n "$env_arg" ]]; then
env="$env_arg"
else
env=$(select_environment "$project_id")
if [[ -z "$env" ]]; then
echo_red "No environment selected... Exiting"
exit 0
fi
fi
print_command "upsun_download_access_log" "$project_id" "$env"
upsun_download_access_log "$project_id" "$project_name" "$env"
;;
"Backup Environment")
local env
if [[ -n "$env_arg" ]]; then
env="$env_arg"
else
env=$(select_environment "$project_id")
if [[ -z "$env" ]]; then
echo_red "No environment selected... Exiting"
exit 0
fi
fi
print_command "upsun_backup" "$project_id" "$env"
upsun_backup "$project_id" "$project_name" "$env"
;;
"Fastly API Token")
local env
if [[ -n "$env_arg" ]]; then
env="$env_arg"
else
env=$(select_environment "$project_id")
if [[ -z "$env" ]]; then
echo_red "No environment selected... Exiting"
exit 0
fi
fi
print_command "upsun_fastly_api_token" "$project_id" "$env"
upsun_fastly_api_token "$project_id" "$project_name" "$env"
;;
"Open Dashboard")
print_command "upsun_dashboard" "$project_id"
upsun_dashboard "$project_id"
;;
*)
echo_red "Unknown action: $action_label"
exit 1
;;
esac
}
main "$@"