Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 0 additions & 41 deletions commands/host/platform-resume

This file was deleted.

40 changes: 0 additions & 40 deletions commands/host/platform-ulitool

This file was deleted.

293 changes: 293 additions & 0 deletions commands/host/upbash
Original file line number Diff line number Diff line change
@@ -0,0 +1,293 @@
#!/usr/bin/env bash
#ddev-generated
#annertech-ddev

# Bash version of uptools - Upsun/Platform.sh tools
# Converted to bash automatically with Claude Code.
# We'll chose which one will be maintained further.

## Description: Upsun/Platform.sh tools (ssh, uli, resume, activities, disk) - Bash version
## Usage: upbash
## Example: "ddev upbash"

# Colours
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
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)"
)

# 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"
local cmd="fzf --reverse --height=50% --header=${header} -1"
if [[ -n "$query" ]]; then
cmd="$cmd --query=${query}"
fi
eval "$cmd"
}

# 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
}

# Run resume
run_resume() {
local project_id="$1"
local project_name="$2"
echo_green "${project_name} Selected"
echo_yellow "Starting resume process..."
ddev exec upsun environment:resume --project="${project_id}"
}

# Run ULI
run_uli() {
local project_id="$1"
local project_name="$2"
local environment_id="$3"
echo_green "${project_name} / ${environment_id} Selected"
echo_yellow "Getting URL..."
ddev exec upsun drush uli --project="${project_id}" -e "${environment_id}"
}

# Run SSH
run_ssh() {
local project_id="$1"
local project_name="$2"
echo_green "${project_name} Selected"
echo_yellow "Connecting via SSH..."
ddev exec upsun ssh --project="${project_id}"
}

# Run activities
run_activities() {
local project_id="$1"
local project_name="$2"
echo_green "${project_name} Selected"
echo_yellow "Fetching activities..."
ddev exec upsun act --project="${project_id}"
}

# Run activity in progress
run_activity_in_progress() {
local project_id="$1"
local project_name="$2"
local environment_id="$3"
echo_green "${project_name} / ${environment_id} Selected"
echo_yellow "Fetching running activity log..."
ddev exec upsun act:log --state=in_progress --project="${project_id}" -e "${environment_id}"
}

# Run disk helper
run_disk_helper() {
local project_id="$1"
local project_name="$2"
echo_green "${project_name} Selected"

echo ""
echo "Currently used and assigned disk space:"
ddev exec upsun disk -1 --columns Service,Used,Limit --project="${project_id}"

# Get disk bytes for services
local web db solr total_assigned total_available
web=$(ddev exec upsun disk -1 --columns Limit -s app --bytes --no-header --format=plain --project="${project_id}" 2>/dev/null | tr -d '[:space:]')
db=$(ddev exec upsun disk -1 --columns Limit -s mysqldb --bytes --no-header --format=plain --project="${project_id}" 2>/dev/null | tr -d '[:space:]')
solr=$(ddev exec upsun disk -1 --columns Limit -s solrsearch --bytes --no-header --format=plain --project="${project_id}" 2>/dev/null | tr -d '[:space:]')

web=${web:-0}
db=${db:-0}
solr=${solr:-0}
total_assigned=$((web + db + solr))

# Get total available storage
total_available=$(ddev exec upsun project:info subscription.storage --project="${project_id}" 2>/dev/null | tr -d '[:space:]')
total_available=${total_available:-0}

echo ""
echo "Total assigned (app+mysqldb+solrsearch):"
numfmt --to iec --format "%3.2f" "$total_assigned"

echo ""
echo "Total storage available in project:"
numfmt --from-unit=1048576 --to iec --format="%3.2f" "$total_available"

echo_yellow ""
echo_yellow "WARNING: command uses hardcoded values for service totals. Verify numbers with table above!"
echo ""
}

# Run log checker
run_log_checker() {
local project_id="$1"
local project_name="$2"
local environment_id="$3"
echo_green "${project_name} / ${environment_id} Selected"
echo_yellow "Analyzing access logs (top 10 requests)..."
ddev exec upsun ssh --project="${project_id}" -e "${environment_id}" -- \
"tail -n 5000 /var/log/access.log | cut -d' ' -f 12- | sort | uniq -c | sort -nr | head -n 10"
}

# Run log checker with goaccess
run_log_checker_goaccess() {
local project_id="$1"
local project_name="$2"
local environment_id="$3"
echo_green "${project_name} / ${environment_id} Selected"
echo_yellow "Fetching logs and loading in goaccess..."
ddev exec upsun ssh --project="${project_id}" -e "${environment_id}" -- "cat /var/log/access.log" | \
goaccess --log-format=COMBINED -
}

# Main function
main() {
# Select action
local action_selection
action_selection=$(select_action)
if [[ -z "$action_selection" ]]; then
echo_red "Nothing selected... Exiting"
exit 0
fi

# Extract action from selection (remove number prefix)
local action_label
action_label=$(echo "$action_selection" | sed 's/^[0-9]*\. //')

# Select project
local project_selection
project_selection=$(select_project)
if [[ -z "$project_selection" ]]; then
echo_red "Nothing selected... Exiting"
exit 0
fi

# Parse project info
local project_id project_name
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"

# Execute selected action
case "$action_label" in
"SSH")
run_ssh "$project_id" "$project_name"
;;
"Drush ULI")
local env
env=$(select_environment "$project_id")
if [[ -z "$env" ]]; then
echo_red "No environment selected... Exiting"
exit 0
fi
run_uli "$project_id" "$project_name" "$env"
;;
"Resume Environment")
run_resume "$project_id" "$project_name"
;;
"Activities")
run_activities "$project_id" "$project_name"
;;
"Running activity")
local env
env=$(select_environment "$project_id")
if [[ -z "$env" ]]; then
echo_red "No environment selected... Exiting"
exit 0
fi
run_activity_in_progress "$project_id" "$project_name" "$env"
;;
"Disk Allocation Helper (beta)")
run_disk_helper "$project_id" "$project_name"
;;
"Log-checker (beta)")
local env
env=$(select_environment "$project_id")
if [[ -z "$env" ]]; then
echo_red "No environment selected... Exiting"
exit 0
fi
run_log_checker "$project_id" "$project_name" "$env"
;;
"Log-checker with goaccess (beta)")
local env
env=$(select_environment "$project_id")
if [[ -z "$env" ]]; then
echo_red "No environment selected... Exiting"
exit 0
fi
run_log_checker_goaccess "$project_id" "$project_name" "$env"
;;
*)
echo_red "Unknown action: $action_label"
exit 1
;;
esac
}

main
Loading