Skip to content

Instantly share code, notes, and snippets.

@sjmcgrath
sjmcgrath / authorization.py
Last active March 9, 2016 20:44 — forked from airtonix/authorization.py
A django-tastypie Authorization Class that uses django-guardian for row level permission checking.
import logging
from tastypie.authorization import DjangoAuthorization
from tastypie.exceptions import Unauthorized
from guardian.shortcuts import get_objects_for_user
from guardian.core import ObjectPermissionChecker
logger = logging.getLogger(__name__)
@sjmcgrath
sjmcgrath / batch.py
Last active August 24, 2016 18:22
Generator that breaks a sequence up into a given size for batch processing items in the sequence
def batches(sequence, batch_size):
"""Iterate over a sequence in batches
Example:
for batch in batches([1, 2, 3, 4, 5, 6, 7], 3):
process(batch)
"""
try:
sequence_length
except NameError:
@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
@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 / 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 / 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 / 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 / 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 / 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 / gist:8d670d4ab1303327652d28c2dd65a610
Created October 5, 2018 18:05
Bash Lessons: Subshells vs Lists
$ foo=1
$
$ # subshell
$ (
> foo=2
> )
$ echo $foo
1
$ # not a subshell
$ {