captaincore/app/quicksave/usage-update
2022-09-13 19:27:07 -04:00

105 lines
2.7 KiB
Bash

#!/usr/bin/env bash
#
# Updates Quicksave usage stats
#
# `captaincore quicksave-usage-update`
#
# [<site>...]
# One or more sites.
#
# [@<target>]
# Target groups of sites like @all @production or @staging.
#
# Load configuration
root_path="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"; root_path=${root_path%app*}
source ${root_path}lib/arguments
run_command() {
INDEX=0
for website in "$@"; do
let INDEX=${INDEX}+1
# Extract environment
if [[ "$website" == *"-staging"* ]]; then
environment=staging
else
environment=production
fi
# Load site configs
while read site_configs; do declare "$site_configs"; done <<< "$(captaincore site get $website --bash --captain_id=$captain_id)"
# Return error if domain not found
if [[ "$domain" == "" ]] || [[ "$site" == "" ]]; then
echo "Can't locate website for $site"
continue
fi
site_path=${site}_${site_id}/${environment}
if [[ "$environment" == "staging" ]]; then
site=${site}-staging
fi
if [ ! -d "$path/$site_path/quicksave/" ]; then
echo "Skipping $site. Folder not found: $path/$site_path/quicksave/."
continue
fi
cd $path/$site_path/quicksave/
# Generate quicksave usage stats
quicksave_count=$( git rev-list --all --count )
# Folder size in bytes without apparent-size flag
if [[ "$OSTYPE" == "linux-gnu" ]]; then
quicksave_storage=$( du -s --block-size=1 . )
quicksave_storage=$( echo $quicksave_storage | cut -d' ' -f 1 )
fi
# Calculate folder size in bytes http://superuser.com/questions/22460/how-do-i-get-the-size-of-a-linux-or-mac-os-x-directory-from-the-command-line
if [[ "$OSTYPE" == "darwin"* ]]; then
quicksave_storage=$( find . -type f -print0 | xargs -0 stat -f%z | awk '{b+=$1} END {print b}' )
fi
quicksaves_usage="{\"count\":\"${quicksave_count}\",\"storage\":\"${quicksave_storage}\"}"
# Store updated info in WordPress datastore
cd ${root_path}data
if [[ $environment == "production" ]]; then
wp post meta update $id quicksaves_usage "$quicksaves_usage"
fi
if [[ $environment == "staging" ]]; then
wp post meta update $id quicksaves_usage_staging $quicksaves_usage
fi
# Clear out variables
site=''
domain=''
home_directory=''
done
}
# See if any sites are specifed
if [ ${#arguments[*]} -gt 0 ]; then
# Runs on specifed sites
run_command ${arguments[*]}
fi
# Runs on targeted sites
if [ ${#targets[*]} -gt 0 ]; then
run_command $(captaincore site list ${targets[*]/targets=/@} --captain_id=$captain_id)
fi
# Error if no sites specifed
if [ ${#targets[*]} -eq 0 ] && [ ${#arguments[*]} -eq 0 ]; then
echo -e "${COLOR_RED}Error:${COLOR_NORMAL} Please specify one or more sites, or use a target @all, @production or @staging."
fi