Skip to content

Instantly share code, notes, and snippets.

View rjchicago's full-sized avatar
💭
🐳 🚢 🏗️

Ryan Jones rjchicago

💭
🐳 🚢 🏗️
View GitHub Profile
/** Fibonacci 0.0.1 */
/**
* Fibonacci
*
* @author Ryan Jones <rjchicago@gmail.com>
* @license The MIT license.
* @copyright Copyright (c) 2010 RJChicago <rjchicago@gmail.com>
*/
// Fibonacci sequence = 0, 1, 1, 2, 3, 5, 8, 13, 21...
@rjchicago
rjchicago / index.html
Created July 17, 2017 19:02 — forked from anonymous/index.html
Merge Lists
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/1.2.1/lodash.min.js" type="text/javascript" />
var str = '%\\|_';
var test = str.replace(/[%_\\|]/g, '\\$&');
console.log(test);
@rjchicago
rjchicago / kzdt.sh
Last active May 24, 2021 18:44
Kill Zombie Docker Tasks (based on https://hub.docker.com/r/dperny/tasknuke)
#!/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 18:44
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.
# 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 / htop.sh
Created May 24, 2021 18:48
Run HTOP on any host running Docker.
docker run --rm -it --pid host frapsoft/htop
# sort by CPU: Shift + P
# sort by Memory: Shift + M
# collapse threads: Shift + H
@rjchicago
rjchicago / git-reset-master.sh
Last active May 24, 2021 19:03
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.
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 / docker-network-services.sh
Created May 27, 2021 16:46
Get Docker services by network
NETWORK=$1
docker network inspect $NETWORK -v | jq -r '.[0] .Services | to_entries[] | .key'
@rjchicago
rjchicago / arr2chunks
Created February 28, 2022 14:59
chunk large object arrays to string
# 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 / import_table.sql
Created May 9, 2022 15:05
Postgres Import Table Function
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;