v3.0.0 "The Great Overhaul"

=====================================================================

Notice the major version change which comes with breaking changes to
2.x! Reconstructs "library" functions for both python and zsh scwrypts,
with changes to virtualenv naming conventions (you'll need to refresh
all virtualenv with the appropriate scwrypt).

--- Changes ------------------------------

- changed a naming convention across zsh scripts, particularly
  removing underscores where there is no need to avoid naming clash
  (e.g. 'zsh/lib/utils/io.zsh' renames '__STATUS' to 'STATUS')

- moved clients reliant on py.lib.http to the py.lib.http module

- python scripts now rely on py.lib.scwrypts.execute

- updated package.json in zx scripts to include `type = module`

- 'scwrypts --list' commandline argument now includes additional
  relevant data for each scwrypt

- environment variables no longer add themselves to be staged in the
  '.env.template'

--- New Features -------------------------

- new 'use' syntax for disjoint import within zsh scripts; took me
  a very long time to convince myself this would be necessary

- introduced scwrypt "groups" to allow portable module creation;
  (i.e. ability add your own scripts from another repo!)

- py.lib.scwrypts.io provides a combined IO stream for quick, hybrid
  use of input/output files and stdin/stdout

- py.lib.fzf provides a wrapper to provide similar functionality to
  zsh/utils/io.zsh including fzf_(head|tail)

- improved efficiency of various scwrypts; notably reducing runtime
  of scwrypts/environment sync

- improved scwrypts CLI by adding new options for exact scwrypt
  matching, better filtering, and prettier/more-detailed interfaces

--- New Scripts --------------------------

- py/twilio )
    basic SMS integration with twilio
     - send-sms

- py/directus )
    interactive directus GET query
     - get-items

- py/discord )
    post message to discord channel or webhook
     - post-message
This commit is contained in:
2023-02-21 18:44:27 -07:00
parent 7617c938b1
commit 76a746a53e
196 changed files with 3472 additions and 2053 deletions

View File

@ -0,0 +1,21 @@
#####################################################################
DEPENDENCIES+=(
aws
)
REQUIRED_ENV+=(
AWS_ACCOUNT
AWS_PROFILE
AWS_REGION
)
#####################################################################
AWS() {
aws \
--profile $AWS_PROFILE \
--region $AWS_REGION \
--output json \
$@
}

View File

@ -0,0 +1,28 @@
#####################################################################
DEPENDENCIES+=(
docker
)
REQUIRED_ENV+=(
AWS_ACCOUNT
AWS_REGION
)
use cloud/aws/cli
#####################################################################
ECR_LOGIN() {
STATUS "performing AWS ECR docker login"
AWS ecr get-login-password \
| docker login \
--username AWS \
--password-stdin \
"$AWS_ACCOUNT.dkr.ecr.$AWS_REGION.amazonaws.com" \
&& SUCCESS "authenticated docker for '$AWS_ACCOUNT' in '$AWS_REGION'" \
|| {
ERROR "unable to authenticate docker for '$AWS_ACCOUNT' in '$AWS_REGION'"
return 1
}
}

View File

@ -0,0 +1,60 @@
#####################################################################
DEPENDENCIES+=(
kubectl
)
REQUIRED_ENV+=(
AWS_ACCOUNT
AWS_REGION
)
use cloud/aws/cli
#####################################################################
EKS_CLUSTER_LOGIN() {
local USAGE="
usage: [...options...]
options
-c, --cluster-name <string> (optional) login a specific cluster
Interactively sets the default kubeconfig to match the selected
cluster in EKS. Also creates the kubeconfig entry if it does not
already exist.
"
local CLUSTER_NAME
while [[ $# -gt 0 ]]
do
case $1 in
-c | --cluster-name ) CLUSTER_NAME="$2"; shift 1 ;;
* ) [ ! $APPLICATION ] && APPLICATION="$1" \
|| ERROR "extra positional argument '$1'"
;;
esac
shift 1
done
[ ! $CLUSTER_NAME ] && CLUSTER_NAME=$(\
AWS eks list-clusters \
| jq -r '.[] | .[]' \
| FZF 'select a cluster'
)
[ ! $CLUSTER_NAME ] && ERROR 'must select a valid cluster or use -c flag'
CHECK_ERRORS
##########################################
STATUS 'creating / updating kubeconfig for EKS cluster'
STATUS "updating kubeconfig for '$CLUSTER_NAME'"
AWS eks update-kubeconfig --name $CLUSTER_NAME \
&& SUCCESS "kubeconfig updated with '$CLUSTER_NAME'" \
|| ERROR "failed to update kubeconfig; do you have permissions to access '$CLUSTER_NAME'?"
}

View File

@ -0,0 +1,140 @@
#####################################################################
DEPENDENCIES+=(
docker
)
REQUIRED_ENV+=(
AWS_ACCOUNT
AWS_REGION
)
use cloud/aws/cli
#####################################################################
RDS__SELECT_DATABASE() {
local DATABASES=$(_RDS__GET_AVAILABLE_DATABASES)
[ ! $DATABASES ] && FAIL 1 'no databases available'
local ID=$(\
echo $DATABASES | jq -r '.instance + " @ " + .cluster' \
| FZF 'select a database (instance@cluster)' \
)
[ ! $ID ] && ABORT
local INSTANCE=$(echo $ID | sed 's/ @ .*$//')
local CLUSTER=$(echo $ID | sed 's/^.* @ //')
echo $DATABASES | jq "select (.instance == \"$INSTANCE\" and .cluster == \"$CLUSTER\")"
}
_RDS__GET_AVAILABLE_DATABASES() {
AWS rds describe-db-instances \
| jq -r '.[] | .[] | {
instance: .DBInstanceIdentifier,
cluster: .DBClusterIdentifier,
type: .Engine,
host: .Endpoint.Address,
port: .Endpoint.Port,
user: .MasterUsername,
database: .DBName
}'
}
RDS__GET_DATABASE_CREDENTIALS() {
local PRINT_PASSWORD=0
local ERRORS=0
while [[ $# -gt 0 ]]
do
case $1 in
--print-password ) PRINT_PASSWORD=1 ;;
* )
WARNING "unrecognized argument $1"
ERRORS+=1
;;
esac
shift 1
done
CHECK_ERRORS
##########################################
local DATABASE=$(RDS__SELECT_DATABASE)
[ ! $DATABASE ] && ABORT
DB_HOST="$(echo $DATABASE | jq -r '.host')"
[ ! $DB_HOST ] && { ERROR 'unable to find host'; return 2; }
DB_PORT="$(echo $DATABASE | jq -r '.port')"
[ ! $DB_PORT ] && DB_PORT=5432
[[ $DB_PORT =~ ^null$ ]] && DB_PORT=5432
##########################################
local AUTH_METHOD=$(\
echo "iam\nsecretsmanager\nuser-input" \
| FZF 'select an authentication method' \
)
[ ! $AUTH_METHOD ] && ABORT
case $AUTH_METHOD in
iam ) _RDS_AUTH__iam ;;
secretsmanager ) _RDS_AUTH__secretsmanager ;;
user-input ) _RDS_AUTH__userinput ;;
esac
STATUS
STATUS "host : $DB_HOST"
STATUS "type : $DB_TYPE"
STATUS "port : $DB_PORT"
STATUS "database : $DB_NAME"
STATUS "username : $DB_USER"
[[ $PRINT_PASSWORD -eq 1 ]] && STATUS "password : $DB_PASS"
STATUS
}
_RDS_AUTH__iam() {
DB_PASS=$(\
AWS rds generate-db-auth-token \
--hostname $DB_HOST \
--port $DB_PORT \
--username $DB_USER \
)
}
_RDS_AUTH__secretsmanager() {
local CREDENTIALS=$(_RDS__GET_SECRETSMANAGER_CREDENTIALS)
echo $CREDENTIALS | jq -e '.pass' >/dev/null 2>&1 \
&& DB_PASS="'$(echo $CREDENTIALS | jq -r '.pass' | sed "s/'/'\"'\"'/g")'"
echo $CREDENTIALS | jq -e '.password' >/dev/null 2>&1 \
&& DB_PASS="'$(echo $CREDENTIALS | jq -r '.password' | sed "s/'/'\"'\"'/g")'"
echo $CREDENTIALS | jq -e '.user' >/dev/null 2>&1 \
&& DB_USER=$(echo $CREDENTIALS | jq -r '.user')
echo $CREDENTIALS | jq -e '.username' >/dev/null 2>&1 \
&& DB_USER=$(echo $CREDENTIALS | jq -r '.username')
echo $CREDENTIALS | jq -e '.name' >/dev/null 2>&1 \
&& DB_NAME=$(echo $CREDENTIALS | jq -r '.name')
echo $CREDENTIALS | jq -e '.dbname' >/dev/null 2>&1 \
&& DB_NAME=$(echo $CREDENTIALS | jq -r '.dbname')
}
_RDS__GET_SECRETSMANAGER_CREDENTIALS() {
local ID=$(\
AWS secretsmanager list-secrets \
| jq -r '.[] | .[] | .Name' \
| FZF 'select a secret' \
)
[ ! $ID ] && return 1
AWS secretsmanager get-secret-value --secret-id "$ID" \
| jq -r '.SecretString' | jq
}

View File

@ -0,0 +1,69 @@
#####################################################################
DEPENDENCIES+=()
REQUIRED_ENV+=(
MEDIA_SYNC__TARGETS
MEDIA_SYNC__S3_BUCKET
)
use cloud/aws/cli
#####################################################################
MEDIA_SYNC__PUSH() {
local FLAGS=($@)
local FAILED_COUNT=0
STATUS 'starting media upload to s3'
local TARGET
for TARGET in ${MEDIA_SYNC__TARGETS[@]}
do
_MEDIA_SYNC push $TARGET $FLAGS || ((FAILED_COUNT+=1))
done
[[ $FAILED_COUNT -eq 0 ]] \
&& SUCCESS 's3 media files now up-to-date' \
|| FAIL $FAILED_COUNT 'unable to upload one or more targets' \
;
}
MEDIA_SYNC__PULL() {
local FLAGS=($@)
local FAILED_COUNT=0
STATUS 'starting media download from s3'
local TARGET
for TARGET in ${MEDIA_SYNC__TARGETS[@]}
do
_MEDIA_SYNC pull $TARGET $FLAGS || ((FAILED_COUNT+=1))
done
[[ $FAILED_COUNT -eq 0 ]] \
&& SUCCESS 'local media files now up-to-date' \
|| FAIL $FAILED_COUNT 'unable to download one or more targets' \
;
}
_MEDIA_SYNC() {
local ACTION="$1"
local REMOTE_TARGET="s3://$MEDIA_SYNC__S3_BUCKET/$2"
local LOCAL_TARGET="$HOME/$2"
local A B
case $ACTION in
push ) A="$LOCAL_TARGET"; B="$REMOTE_TARGET" ;;
pull ) A="$REMOTE_TARGET"; B="$LOCAL_TARGET" ;;
* ) ERROR "unknown action '$1'"; return 1 ;;
esac
local FLAGS=(${@:3})
STATUS "${ACTION}ing $2"
AWS s3 sync $A $B $FLAGS \
&& SUCCESS "$2 up-to-date" \
|| { ERROR "unable to sync $2 (see above)"; return 1; }
}