Skip to content

Instantly share code, notes, and snippets.

View andrewgross's full-sized avatar

Andrew Gross andrewgross

View GitHub Profile
@andrewgross
andrewgross / functional.sh
Created August 20, 2012 23:11
Semi-Functional Programming in Bash
my_name_function() {
# There aren't many use cases where we would really want to do things this
# way instead of just using a global. Unless we are writing libraries in bash (the horror)
# or have an extremely large script where we are not sure we won't be clobbering
# variable names (equally terrifying)
local __assign_my_results_to_this_variable=$1
local do_some_work=$(echo $ALL_MY_COMMANDS_NAMES | grep -v "bad commands")
@andrewgross
andrewgross / callstack.sh
Created August 20, 2012 23:29
Bash Callstack
# If for some insane reason we really want to inspect the bash callstack
# we can use the BASH_LINENO, BASH_SOURCE and FUNCNAME variables
my_function() {
echo "My Function Name: ${FUNCNAME[0]}, I am at line ${BASH_LINENO[0]} of file ${BASH_SOURCE[0]}"
# Unless we call this from deeper down, we will get `main`, line 0, and the same filename as before
echo "My Parent Function: ${FUNCNAME[1]} is at line ${BASH_LINENO[1]} of file ${BASH_SOURCE[1]}"
@andrewgross
andrewgross / data_volume.rb
Created September 11, 2012 22:39
Data Volume LWRP
#
# Cookbook Name:: yipit_data_volume
# Recipe:: default
#
# Copyright 2012, Yipit.com
#
# All rights reserved - Do Not Redistribute
#
require 'rubygems'
require 'chef/log'
@andrewgross
andrewgross / data_volume.rb
Created September 11, 2012 22:40
Data Volume LWRP Resource
# Company: Yipit.com
# Copyright 2012
# Author: Andrew Gross
actions :create, :check, :repair, :destroy
default_action :create
attribute :chunk, :kind_of => Integer, :default => 256
@andrewgross
andrewgross / default.rb
Created September 11, 2012 22:42
Data Volume Default Recipe
#
# Cookbook Name:: yipit_data_volume
# Recipe:: default
#
# Copyright 2012, Yipit.com
#
# All rights reserved - Do Not Redistribute
#
@andrewgross
andrewgross / mdadm_default.rb
Created September 11, 2012 22:45
mdadm LWRP :create override
action :create do
unless @current_resource.exists
command = "yes | mdadm --create #{@new_resource.raid_device} --chunk=#{@new_resource.chunk} --level #{@new_resource.level} --raid-devices #{@new_resource.devices.length} #{@new_resource.devices.join(" ")}"
# Check the mdadm raid registry
# Existing raids will show as active or inactive
# Take the name of the raid array
find_current_raid_arrays_command = "cat /proc/mdstat | grep active | awk '{print $1}'"
Chef::Log.debug("#{@new_resource} mdadm command: #{command}")
attempts = 0
previous_raid_arrays = shell_out!(find_current_raid_arrays_command).stdout.split()
@andrewgross
andrewgross / deploy.rb
Created September 12, 2012 14:48
Git Deploy
git "update #{program}" do
user node['username']
group node['username']
repository "git@github.com:Yipit/#{program}.git"
reference branch
destination "/var/www/#{program}"
ssh_wrapper "/home/#{node['username']}/.ssh/#{program}_ssh_wrapper.sh"
action :sync
ignore_failure true
end
@andrewgross
andrewgross / monkey.rb
Created September 12, 2012 19:43
Fix Knife Bootstrap Run List issues
class Chef::Knife::Ec2ServerCreate < Chef::Knife
def bootstrap_for_node(server, unknownarg)
bootstrap = Chef::Knife::Bootstrap.new
bootstrap.name_args = [vpc_mode? ? server.private_ip_address : server.dns_name ]
bootstrap.config[:run_list] = config[:run_list]
bootstrap.config[:ssh_user] = config[:ssh_user]
bootstrap.config[:identity_file] = config[:identity_file]
bootstrap.config[:chef_node_name] = config[:chef_node_name] || server.id
bootstrap.config[:prerelease] = config[:prerelease]
bootstrap.config[:bootstrap_version] = locate_config_value(:bootstrap_version)
@andrewgross
andrewgross / upgrade.log
Created September 14, 2012 17:08
Gem Upgrade Not Working
[Fri, 14 Sep 2012 17:05:28 +0000] INFO: Processing gem_package[remote_syslog] action install (papertrail::default line 27)
[Fri, 14 Sep 2012 17:05:28 +0000] DEBUG: gem_package[remote_syslog] using gem from running ruby environment
[Fri, 14 Sep 2012 17:05:28 +0000] DEBUG: gem_package[remote_syslog] found installed gem remote_syslog version 1.6.5 matching remote_syslog (>= 0)
[Fri, 14 Sep 2012 17:05:28 +0000] DEBUG: gem_package[remote_syslog] is already installed - nothing to do
[Fri, 14 Sep 2012 17:05:28 +0000] INFO: Processing gem_package[remote_syslog] action upgrade (papertrail::default line 27)
[Fri, 14 Sep 2012 17:05:28 +0000] DEBUG: gem_package[remote_syslog] using gem from running ruby environment
[Fri, 14 Sep 2012 17:05:28 +0000] DEBUG: gem_package[remote_syslog] found installed gem remote_syslog version 1.6.5 matching remote_syslog (>= 0)
[Fri, 14 Sep 2012 17:05:28 +0000] DEBUG: gem_package[remote_syslog] no candidate version - nothing to do
@andrewgross
andrewgross / add_server.py
Created September 17, 2012 20:25
Add Server
#!/usr/bin/env python
import subprocess
import sys
import time
from boto.ec2.connection import EC2Connection
from chef import ChefAPI, Node
SSH_CONFIG_BLOCK = """