Skip to content

Instantly share code, notes, and snippets.

Avatar

David Alger davidalger

View GitHub Profile
@davidalger
davidalger / postgres-cheatsheet.md
Created May 14, 2021 — forked from Kartones/postgres-cheatsheet.md
PostgreSQL command line cheatsheet
View postgres-cheatsheet.md

PSQL

Magic words:

psql -U postgres

Some interesting flags (to see all, use -h or --help depending on your psql version):

  • -E: will describe the underlaying queries of the \ commands (cool for learning!)
  • -l: psql will list all databases and then exit (useful if the user you connect with doesn't has a default database, like at AWS RDS)
@davidalger
davidalger / kubectl-list_images
Last active Apr 8, 2021
List all non-Google/GKE/k8s/Stackdriver images in a cluster using fully qualified names and without tags.
View kubectl-list_images
#!/usr/bin/env bash
kubectl get all --all-namespaces -o yaml \
| grep \ image: \
| egrep -iv 'image:\ sha256|(gke|k8s)\.gcr\.io|gcr\.io/(gke|google|stackdriver|kubernetes)-.*' \
| perl -pe 's#^[\W-]+image:\W+([^\@:]+)([:@].+)?$#$1#i' \
| perl -pe 's#^([a-z0-9-_]+)$#docker.io/library/$1#i' \
| perl -pe 's#^([a-z0-9-_/]+)$#docker.io/$1#i' \
| sort -n | uniq
View gcloud-asset-search.sh
PS1='$ ' && clear
GCP_PROJECT=my-first-project \
&& gcloud services enable cloudasset.googleapis.com --project "${GCP_PROJECT}" \
&& echo Waiting for API enablement to propagate \
&& utimer -c 2m \
&& gcloud asset search-all-resources --scope "projects/${GCP_PROJECT}" --sort-by 'assetType' \
--format='table(assetType.basename(), name.basename(), name.scope(projects).segment(0):label=PROJECT_ID, labels)'
View kubectl-cheats.md

List evicted pods

kubectl get pods --all-namespaces -o json \
  | jq -r '.items[] | select(.status.reason == "Evicted") | "pod/\(.metadata.name) -n \(.metadata.namespace)"'

Delete evicted pods

@davidalger
davidalger / delete-default-vpc-networks.sh
Created Nov 17, 2020
Removes internet gateways and subnets from and then deletes all default VPCs in an AWS account.
View delete-default-vpc-networks.sh
#!/usr/bin/env bash
set -euo pipefail
function error {
>&2 printf "\033[31mERROR\033[0m: $@\n"
}
INIT_ERROR=
for DEP_NAME in aws-vault jq aws; do
if ! which "${DEP_NAME}" 2>/dev/null >/dev/null; then
@davidalger
davidalger / redis-cli-flushall-with-auth.sh
Last active Aug 4, 2020
Pulls password for Redis authentication from Magento 2 configuration file to run `redis-cli flushall`
View redis-cli-flushall-with-auth.sh
REDISCLI_AUTH=$(php -r '
$env = require_once "app/etc/env.php";
echo @$env["cache"]["frontend"]["default"]["backend_options"]["password"];
') redis-cli flushall
@davidalger
davidalger / warmer-cron.sh
Last active Apr 29, 2021
Script to crawl and warm the cache two levels deep on Magento demo Pods via CronJob spec
View warmer-cron.sh
#!/bin/bash
set -euo pipefail
FRONT_URL="${FRONT_URL:-https://app.exampleproject.test/}"
echo "==> [$(date +%H:%M:%S)] waiting on readiness"
ELAPSED_SECONDS=0
while : ; do
ELAPSED_SECONDS=$(echo ${ELAPSED_SECONDS}+2 | bc)
RESPONSE_CODE=$(curl -sI "${FRONT_URL}" 2>/dev/null | head -n1 | awk '{print $2}')
@davidalger
davidalger / user-data.yaml
Last active Jul 21, 2020
Cloud config user data for creating VM with davidalger administrative user rather than 'centos' or 'ubuntu' user
View user-data.yaml
#cloud-config
users:
- groups: adm, systemd-journal
name: davidalger
shell: /bin/bash
ssh_authorized_keys:
- |
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA4UTV2t+5Ze6JDiU7Ws9SLKUrh8oq+3lkAIXsx54w8bW8huk4TViqALb4PgYTWwH9eRFUjBzx/9dxTZMrkUPCeHRWxoKmUONTYaH8xz5vGcCbSw/qC/BVXZ6TofRB7vkFWOMDgq8Y57pN/kq7k8cvMUNWqZ+My/k1HPpZLBTHiSDaGb3AU1lXEyPzu0rCHbPeuJcRhIbixhbxTL9RgiKYDcz1UXoklqCnFVC2wP0Hd9LjxASZGJQYdjqHm2bnnEL9ztHDxnY2/9/kRDzXw3wEALDhJQq8/M5xiB9Ul3FVpm2qCqu7NKxZxzM8iH0fyKUz0CZ0pmqkfXSqaYhyH65FdQ== davidmalger@gmail.com
sudo: ALL=(ALL) NOPASSWD:ALL
@davidalger
davidalger / db-dump.sh
Last active Sep 29, 2020
Uses n98-magerun over SSH to dump a database and pull to local environment for import
View db-dump.sh
#!/usr/bin/env bash
set -euo pipefail
trap 'error "$(printf "Command \`%s\` on line $LINENO failed with exit code $?" "$BASH_COMMAND")"' ERR
## setup functions for use throughout the script
function error {
>&2 printf "\033[31mERROR\033[0m: $@\n"
}
function :: {
@davidalger
davidalger / run-ansible
Last active Jul 10, 2020
Shell script to wrap ansible runtime in a docker context using these images: https://github.com/davidalger/docker-images-ansible
View run-ansible
#!/usr/bin/env bash
set -euo pipefail
trap 'error "$(printf "Command \`%s\` on line $LINENO failed with exit code $?" "$BASH_COMMAND")"' ERR
function error {
>&2 printf "\033[31mERROR\033[0m: $@\n"
}
function fatal {
error "$@"