Skip to content

Instantly share code, notes, and snippets.

@sjmcgrath
sjmcgrath / get-pipeline-role
Created June 17, 2019 16:15
Retrieve the Role ARN for a/all AWS CodePipeline Pipelines
#!/bin/bash
if [[ $# -eq 1 ]]
then
aws codepipeline get-pipeline \
--name "${1}" \
--query pipeline.roleArn \
--output text
else
for pipeline in $( aws codepipeline list-pipelines --query 'pipelines[].name' --output text )
$ # Arrays...
$
$ foo=( word 'this has spaces' )
$
$ # Unless you treat it like an array,
$ # it just behaves like a scalar with
$ # the first element of the array.
$ echo $foo
word
$ echo ${#foo}
@sjmcgrath
sjmcgrath / gist:8d670d4ab1303327652d28c2dd65a610
Created October 5, 2018 18:05
Bash Lessons: Subshells vs Lists
$ foo=1
$
$ # subshell
$ (
> foo=2
> )
$ echo $foo
1
$ # not a subshell
$ {
@sjmcgrath
sjmcgrath / get-list-of-droplets
Created September 18, 2017 21:50
Gets a list of Digital Ocean droplets and their IP addresses
#!/bin/bash
set -o errexit
# DO_KEY_CONFIG_FILE should contain one line similar to the following with a Digital Ocean read key
# header = "Authorization: Bearer 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
DO_KEY_CONFIG_FILE=${DO_KEY_CONFIG_FILE:?Must specify DO_KEY_CONFIG_FILE}
BASE="https://api.digitalocean.com"
@sjmcgrath
sjmcgrath / calculate-resolver-ip.sh
Created June 27, 2017 18:27
Using EC2 metadata determine resolver IP as VPC base + 2.
#!/bin/bash
MAC=$( curl -fs http://169.254.169.254/2016-09-02/meta-data/network/interfaces/macs/ )
VPC_CIDR=$( curl -fs http://169.254.169.254/2016-09-02/meta-data/network/interfaces/macs/${MAC%/}/vpc-ipv4-cidr-block/ )
BASE_IP=${VPC_CIDR%/*}
ABC_IP=${BASE_IP%.*}
D_IP=${BASE_IP##*.}
D_IP=$(( D_IP + 2 ))
RESOLVER_IP=${ABC_IP}.${D_IP}
@sjmcgrath
sjmcgrath / pg_prepare_password
Created June 8, 2017 18:53
Hash a role/password for a CREATE ROLE... in PostgreSQL
#!/bin/bash
[[ $# -eq 0 ]] || {
cat <<EOF >&2
Usage: ${0##*/}
Prompts for Role and Password and outputs the hash in the correct format for a
"CREATE ROLE ... PASSWORD 'xxx';" in PostgreSQL.
EOF
@sjmcgrath
sjmcgrath / delete-cloudfront-origin-access-identity.sh
Created May 26, 2017 21:09
Deletes a CloudFront Origin Access Identity
#!/bin/bash -e
[[ $# -eq 1 ]] || {
cat <<EOF >&2
Usage. ${0##*/} OAI-Id
EOF
exit 1
}
@sjmcgrath
sjmcgrath / create-cloudfront-origin-access-identity.sh
Last active May 26, 2017 21:09
Creates a CloudFront Origin Access Identity
#!/bin/bash -e
[[ $# -le 2 ]] || {
cat <<EOF >&2
Usage: ${0##*/} environment application
Parameters:
environment - dev, staging, production, etc
application - name of application
@sjmcgrath
sjmcgrath / ssh-agent-fix
Created April 27, 2017 20:45
Save your ssh-agent environment to a temp file so you can reload it in screen sessions/etc. that have the environment from a previous ssh session.
#!/bin/bash
SSH_AGENT_FIX_ENV_FILE="${SSH_AGENT_FIX_ENV_FILE:=$HOME/tmp/ssh-agent-env}"
SSH_AGENT_FIX_ENV_DIR=$( dirname ${SSH_AGENT_FIX_ENV_FILE} )
[[ -d ${SSH_AGENT_FIX_ENV_DIR} ]] || {
mkdir -p $SSH_AGENT_FIX_ENV_DIR
}
env \
@sjmcgrath
sjmcgrath / page_ok
Created April 27, 2017 00:03
Checks a URL for a response with retries and wait periods.
#!/bin/bash
NUMBER_OF_RETRIES={NUMBER_OF_RETRIES:-5}
WAIT_IN_SECONDS=${WAIT_IN_SECONDS:-30}
URL="${1:-127.0.0.1/index.php}"
URL_HASH="$( echo -n "${URL}" | cksum | sed 's/\s//g' )"
LOCK_FILE="/tmp/.page_ok.${URL_HASH}"
(
flock --nonblock 9 || exit 0