packaging stuff up

This commit is contained in:
Noah Masur
2025-01-30 14:48:17 -05:00
parent 0ebd0bac2c
commit b123ae3e69
19 changed files with 135 additions and 89 deletions

View File

@ -0,0 +1,22 @@
#!/usr/bin/env bash
# Retrieve list of AWS instances
# Use enter to jump into their sessions with SSM
# Specify AWS_PROFILE and AWS_REGION before running this script
aws ec2 describe-instances \
--filters "Name=instance-state-name,Values=running" |
jq -r \
'.Reservations[]
| .Instances[]
| .InstanceId + " - " +
(.PrivateIpAddress // "n/a") + " - " +
(.PublicIpAddress // "n/a") + " - " +
(.Tags // [] | from_entries | .Name // "n/a")' |
fzf \
--height 100% \
--layout reverse \
--header $'Press Enter to start SSM session\nInstance ID - Private IP - Public IP - Name' \
--preview "aws ec2 describe-instances --instance-ids \"\$(echo {} | cut -d' ' -f1)\" | jq -r '.Reservations[].Instances[0]'" \
--bind "enter:become(aws ssm start-session --target \$(echo {} | cut -d' ' -f1))"

View File

@ -0,0 +1,12 @@
{ pkgs, ... }:
pkgs.writeShellApplication {
name = "aws-ec2";
runtimeInputs = [
pkgs.awscli2
pkgs.jq
pkgs.fzf
pkgs.ssm-session-manager-plugin
];
text = builtins.readFile ./aws-ec2.sh;
}

View File

@ -0,0 +1,26 @@
#!/bin/sh
# Stop all containers
if [ "$(docker ps -a -q)" ]; then
echo "Stopping docker containers..."
docker stop "$(docker ps -a -q)"
else
echo "No running docker containers."
fi
# Remove all stopped containers
if [ "$(docker ps -a -q)" ]; then
echo "Removing docker containers..."
docker rm "$(docker ps -a -q)"
else
echo "No stopped docker containers."
fi
# Remove all untagged images
if docker images | grep -q "^<none>"; then
docker rmi "$(docker images | grep "^<none>" | awk '{print $3}')"
else
echo "No untagged docker images."
fi
echo "Cleaned up docker."

View File

@ -0,0 +1,11 @@
{ pkgs, ... }:
pkgs.writeShellApplication {
name = "docker-cleanup";
runtimeInputs = [
pkgs.docker-client
pkgs.gawk
pkgs.gnugrep
];
text = builtins.readFile ./docker-cleanup.sh;
}

25
pkgs/tools/misc/jqr/jqr.sh Executable file
View File

@ -0,0 +1,25 @@
#!/usr/bin/env bash
# Adapted from: https://gist.github.com/reegnz/b9e40993d410b75c2d866441add2cb55
if [[ -z $1 ]] || [[ $1 == "-" ]]; then
input=$(mktemp)
trap 'rm -f $input' EXIT
cat /dev/stdin >"$input"
else
input=$1
fi
# TODO: make available on non-macOS
echo '' |
fzf --phony \
--height 100% \
--preview-window='up:80%' \
--query '.' \
--print-query \
--header $'CTRL-O: jq output\nCTRL-Y: copy output\nALT-Y: copy query' \
--preview "jq --color-output -r {q} $input" \
--bind "ctrl-o:execute(jq -r {q} $input)+clear-query+accept" \
--bind "alt-y:execute(echo {q} | pbcopy)" \
--bind "ctrl-y:execute(jq -r {q} $input | pbcopy)"

View File

@ -0,0 +1,10 @@
{ pkgs, ... }:
pkgs.writeShellApplication {
name = "jqr";
runtimeInputs = [
pkgs.jq
pkgs.fzf
];
text = builtins.readFile ./jqr.sh;
}

View File

@ -0,0 +1,9 @@
{ pkgs, ... }:
pkgs.writeShellScriptBin "loadkey" ''
printf "\nEnter the seed phrase for your SSH key...\n"
printf "\nThen press ^D when complete.\n\n"
mkdir -p ~/.ssh/
${pkgs.melt}/bin/melt restore ~/.ssh/id_ed25519
printf "\n\nContinuing activation.\n\n"
''

59
pkgs/tools/misc/ocr/ocr.sh Executable file
View File

@ -0,0 +1,59 @@
#!/usr/bin/env bash
# Yoinked from https://github.com/JJGO/dotfiles
# Adapted from https://github.com/sdushantha/bin
set -x
TEXT_FILE="/tmp/ocr.txt"
IMAGE_FILE="/tmp/ocr.png"
function notify-send() {
/usr/bin/osascript -e "display notification \"$2\" with title \"OCR\""
}
PATH="/usr/local/bin/:$PATH"
# Take screenshot by selecting the area
/usr/sbin/screencapture -i "$IMAGE_FILE"
# Get the exit code of the previous command.
# So in this case, it is the screenshot command. If it did not exit with an
# exit code 0, then it means the user canceled the process of taking a
# screenshot by doing something like pressing the escape key
STATUS=$?
# If the user pressed the escape key or did something to terminate the proccess
# taking a screenshot, then just exit
[ $STATUS -ne 0 ] && exit 1
# Do the magic (∩^o^)⊃━☆゚.*・。゚
# Notice how I have removing the extension .txt from the file path. This is
# because tesseract adds .txt to the given file path anyways. So if we were to
# specify /tmp/ocr.txt as the file path, tesseract would out the text to
# /tmp/ocr.txt.txt
cd /tmp || {
echo "Failed to jump to directory."
exit 1
}
tesseract "$IMAGE_FILE" "${TEXT_FILE//\.txt/}"
# Check if the text was detected by checking number
# of lines in the file
LINES=$(wc -l <$TEXT_FILE)
if [ "$LINES" -eq 0 ]; then
notify-send "ocr" "no text was detected"
exit 1
fi
# Copy text to clipboard
# xclip -selection clip < "$TEXT_FILE"
/usr/bin/pbcopy <"$TEXT_FILE"
# Send a notification with the text that was grabbed using OCR
notify-send "ocr" "$(cat $TEXT_FILE)"
# Clean up
# "Always leave the area better than you found it"
# - My first grade teacher
rm "$TEXT_FILE"
rm "$IMAGE_FILE"

View File

@ -0,0 +1,7 @@
{ pkgs, ... }:
pkgs.writeShellApplication {
name = "ocr";
runtimeInputs = [ pkgs.tesseract ];
text = builtins.readFile ./ocr.sh;
}

View File

@ -0,0 +1,11 @@
{ pkgs, ... }:
pkgs.writeShellApplication {
name = "terraform-init";
runtimeInputs = [
pkgs.gawk
pkgs.git
pkgs.terraform
];
text = builtins.readFile ./terraform-init.sh;
}

View File

@ -0,0 +1,55 @@
#!/usr/bin/env bash
export AWS_PROFILE="gs"
BUCKET_NAME_PART_1="t2"
BUCKET_NAME_PART_2="global"
BUCKET_NAME_PART_3="terraformstate"
PROJECT_ROOT=$(git rev-parse --show-toplevel)
WORKFLOW_FILE="${PROJECT_ROOT}/.github/workflows/terraform.yml"
if [ ! -f "$WORKFLOW_FILE" ]; then
WORKFLOW_FILE="${PROJECT_ROOT}/.github/workflows/apply.yml"
fi
AWS_ACCOUNT_NUMBER=$(
awk '/aws_account_number: .*/ {print $2}' "$WORKFLOW_FILE" | # Grab account number
echo "$(
read -r s
s=${s//\'/}
echo "$s"
)" # Remove single quote if it exists
)
if [ -z "${AWS_ACCOUNT_NUMBER}" ]; then
AWS_ACCOUNT_NUMBER=$(
awk '/AWS_ACCOUNT_NUMBER: .*/ {print $2}' "$WORKFLOW_FILE" | # Grab account number
echo "$(
read -r s
s=${s//\'/}
echo "$s"
)" # Remove single quote if it exists
)
fi
REPOSITORY=$(
git remote get-url origin |
awk -F'/' -v OFS='/' '{print $(NF-1),$NF }' |
echo "$(
read -r s
s=${s%.git}
echo "$s"
)" # Remove .git suffix if it exists
)
BRANCH=$(git branch --show-current)
terraform init \
-backend-config="region=us-east-1" \
-backend-config="bucket=${BUCKET_NAME_PART_1}${BUCKET_NAME_PART_2}${BUCKET_NAME_PART_3}" \
-backend-config="workspace_key_prefix=accounts/${AWS_ACCOUNT_NUMBER}/${REPOSITORY}" \
-backend-config="key=state.tfstate" \
-backend-config="dynamodb_table=global-tf-state-lock" \
-upgrade
terraform workspace select "$BRANCH"