Skip to content

Instantly share code, notes, and snippets.

@erchn
erchn / alphabet.py
Last active January 23, 2023 18:29
Over engineered helper for Slack trolling
#!/usr/bin/env python3
import subprocess
from string import ascii_letters
from optparse import OptionParser
parser = OptionParser()
parser.add_option(
"-y",
{
"title": "Launch new Chrome or iTerm2 windows via keybindings",
"rules": [
{
"description": "caps lock - launches a new iTerm2 window with the default profile",
"manipulators": [
{
"from": {
"key_code": "caps_lock",
"modifiers": {
@erchn
erchn / ipv4 regex.txt
Last active February 5, 2019 21:36
Match Cloudflare IPs with regex
# https://www.cloudflare.com/ips/
# this is not meant to catch malformed IPs
/(103\.21\.24[4567]\.[0-9]+ # 103.21.244.0/22
|103\.22\.20[0123]\.[0-9]+ # 103.22.200.0/22
|103\.31\.[4567]\.[0-9]+ # 103.31.4.0/22
|104\.(1[6789]|2[0-9]|3[01])\.[0-9]+ # 104.16.0.0/12
|108\.162\.(19[2-9]|2[0-9]+)\.[0-9]+ # 108.162.192.0/18
|131\.0\.7[2345]\.[0-9]+ # 131.0.72.0/22
|141\.101\.(6[4-9]|[7-9][0-9]|1[01][0-9]|12[0-7])\.[0-9]+ # 141.101.64.0/18
@erchn
erchn / fizzbuzz.sql
Last active October 15, 2018 21:50
Athena FizzBuzz
SELECT val,
CASE
WHEN by5+by3 = 0 THEN 'FizzBuzz'
WHEN by5 = 0 THEN 'Fizz'
WHEN by3 = 0 THEN 'Buzz'
ELSE cast(val AS varchar)
END AS fizzbuzz_test
FROM
(SELECT val,
mod(val, 5) AS by5,
@erchn
erchn / _etc_profile.d_rbenv.sh
Created February 1, 2017 19:34
Running rbenv in production using RPMs (tested on centos 6)
pathadd() {
if [ -d "$1" ] && [[ ":$PATH:" != *":$1:"* ]]; then
PATH="$1:${PATH:+"$PATH:"}"
fi
}
# rbenv setup
export RBENV_ROOT=/opt/rbenv
pathadd /opt/rbenv/bin
@erchn
erchn / retry.sh
Created September 26, 2016 19:05
retry running things with a few arguments, goes well with the Cronitor wrapper.
#!/bin/bash
num=5
retrycode=99
sleep=300
usage() {
cat <<EOF
$0 [-r retry_exit_code] [-s sleep_secs] [-n times_to_loop]
@erchn
erchn / 01_rails.rb
Last active August 29, 2015 14:27
Redefinition of Rails.env to support a hierarchy of environments, tested on 3.2.17
# Override Rails.env so we can have a env method that support this environment plus or minus other environments as true
# This initializer file is named 01_rails so it executes before any others
module Rails
# Mini-helper class which subclasses String, duplicating the base of ActiveSupport::StringInquirer
# Adds support for comparing hierarchy of environments, the method_name vs. current Rails.env
#
# Rails.env # => "development"
# Rails.env.development? # => true
@erchn
erchn / cronitor.sh
Last active October 10, 2019 13:38
Cronitor wrapper script for start/stop notifications, shamelessly stolen from the now defunct Proby
#!/bin/bash
#
# This script surrounds the command passed in with start and finish notifications
# to the cronitor monitoring application.
#
# === SETUP
#
# * Make sure the cronitor script is executable.
#
# chmod +x cronitor
@erchn
erchn / consumer.sls
Created May 6, 2014 19:55
Sharing state information
# Get logs from other states
{% set mylogs = [] %}
{% for myfile in salt['cp.list_master']() %}
{% if myfile.split('/')[-1] == "log.incl" %}
{% include myfile %}
{% endif %}
{% endfor %}
# {{ mylogs }}
@erchn
erchn / myenv.py
Created January 23, 2014 16:21
Custom grain based on hostname
# _grains/myenv.py
import sys
import socket
import re
import platform
# Extend the default list of supported distros. This will be used for the
# /etc/DISTRO-release checking that is part of platform.linux_distribution()
from platform import _supported_dists