Skip to content

Instantly share code, notes, and snippets.

@erchn
erchn / srv-pillar-top.sls.yaml
Last active December 21, 2015 11:18
salt user management
base:
'*':
# account details are available everywhere
- users.accounts
# ops group data
- users.ops
# individual users on a given machine
- users.nodeusers
'web*':
trigger script when updated:
file.managed:
- name: /usr/local/bin/script.sh
- source: salt://script.sh
- mode: 700
cmd.wait:
- name: /usr/local/bin/script.sh
- cwd: /tmp
- watch:
@erchn
erchn / top.sls.yaml
Created November 24, 2013 16:29
auto-assign states to a minion based on cluster name if they exist on the master. The concept shamelessly copied from another community member.
{# ######################################################################################### #}
{# ######################################################################################### #}
{# this block of code auto applies salt states to a given minion if they exist on the master #}
{% set id = grains['id'] %}
{# web01 host id = 'web' cluster id, allow a custom grain or pillar named 'cluster' to override #}
{% set cluster = salt['config.get']('cluster', id.split('.')[0].rstrip('0123456789')) %}
{# all available states on the master #}
{% set states = salt['cp.list_master']() %}
@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
@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 / 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 / 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 / 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 / _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 / 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,