Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
🐳 🚢 🏗️

Ryan Jones rjchicago

💭
🐳 🚢 🏗️
View GitHub Profile
@rjchicago
rjchicago / swarm-host-ip.sh
Created Oct 26, 2022
Docker Swarm Host IPs
View swarm-host-ip.sh
# get hosts (mgr node)
HOSTS=$(docker node ls --format "{{.Hostname}}")
# print host/ip
for HOST in $HOSTS; do host $HOST; done | awk '{print $1, $4}' | sort
View IEEE-754.js
( 0.1 + 0.2 === 0.3 ) ? console.log('LGTM') : console.log('WTF');
@rjchicago
rjchicago / import_table.sql
Created May 9, 2022
Postgres Import Table Function
View import_table.sql
CREATE OR REPLACE FUNCTION "my_schema"."import_table" (fdw_server text, tablename text, where_filter text) RETURNS bigint
VOLATILE
AS $body$
DECLARE
v_fdw_schema TEXT;
v_fdw_tablename TEXT;
v_col_def TEXT;
v_query TEXT;
v_schema_name TEXT;
v_table_name TEXT;
@rjchicago
rjchicago / arr2chunks
Created Feb 28, 2022
chunk large object arrays to string
View arr2chunks
# chunk array to string
private *arr2chunks(arr: any[], n: number) {
for (let i = 0; i < arr.length; i += n) {
const str = JSON.stringify(arr.slice(i, i + n))
const open = i === 0 ? '[':'';
const close = n*(i+1) < arr.length ? ',' : ']';
yield `${open}${str.slice(1, str.length-1)}${close}`;
}
yield null;
}
@rjchicago
rjchicago / docker-network-services.sh
Created May 27, 2021
Get Docker services by network
View docker-network-services.sh
NETWORK=$1
docker network inspect $NETWORK -v | jq -r '.[0] .Services | to_entries[] | .key'
@rjchicago
rjchicago / git-reset-master.sh
Last active May 24, 2021
Sometimes you just want to reset everything and clean up with only the master branch again (locally). This script will reset hard, checkout master, and delete all local non-master branches.
View git-reset-master.sh
git reset --hard && git checkout master && git branch | grep -v "^..master$" | xargs git branch -D && git pull
# add to ~/.bash_profile
# alias git-reset-master='git reset --hard && git checkout master && git branch | grep -v "^..master$" | xargs git branch -D && git pull'
@rjchicago
rjchicago / htop.sh
Created May 24, 2021
Run HTOP on any host running Docker.
View htop.sh
docker run --rm -it --pid host frapsoft/htop
# sort by CPU: Shift + P
# sort by Memory: Shift + M
# collapse threads: Shift + H
@rjchicago
rjchicago / kzdt.sh
Last active May 24, 2021
Kill Zombie Docker Tasks (based on https://hub.docker.com/r/dperny/tasknuke)
View kzdt.sh
#!/bin/sh
SERVICES=$(docker service ls --format '{{print .Name}}')
for SERVICE in $SERVICES; do
IDS=$(docker service ps $SERVICE -f "desired-state=shutdown" | grep Running | awk '{print $1;}')
if [ -n "$IDS" ]; then
echo $SERVICE
fi
for ID in $IDS; do
TASK=$(docker inspect $ID | sed -n 's/.*\"ID\": \"\(.*\)\".*/\1/p' | awsk '{print $1; exit}')
docker run --rm -v /var/run/docker/swarm/control.sock:/var/run/swarmd.sock dperny/tasknuke $TASK | sed 's/^/ - /'
@rjchicago
rjchicago / docker-image-errors.sh
Last active May 24, 2021
We've encountered errors where either we hit the Docker Hub rate limit, or an image tag has been overwritten and therefor SHA's do not match. This script will loop through images currently in use and attempt a Docker pull and print out any errors.
View docker-image-errors.sh
# loop through services and get names
for SERVICE in $(docker service ls --format "{{.Name}}"); do
# get the full image:tag@sha used by the service
IMAGE=$(docker service inspect $SERVICE --format "{{.Spec.TaskTemplate.ContainerSpec.Image}}")
# try image pull and capture error for printing
ERROR="$(docker pull -q $IMAGE 2>&1 > /dev/null)"
if [[ ! -z $ERROR ]]; then
printf "$SERVICE: $IMAGE\n\t$ERROR\n"
fi
done
@rjchicago
rjchicago / export.sh
Last active Nov 15, 2022
Export Variables from .env
View export.sh
set -a; source .env; set +a