Skip to content

Instantly share code, notes, and snippets.

View mpasternacki's full-sized avatar

Maciej Pasternacki mpasternacki

View GitHub Profile
@mpasternacki
mpasternacki / git-diffstat.sh
Created November 23, 2010 13:53
Full-width diffstat "porcelain" for git
#!/bin/bash
COLUMNS=`stty size | cut -d' ' -f2`
exec git diff --stat=$COLUMNS,$(($COLUMNS - 20)) "${@}"
@mpasternacki
mpasternacki / reap_instances.rake
Created January 16, 2011 20:03
Reap Node and Client instances for dead EC2 instances on a chef server.
# -*- mode: ruby; coding: utf-8 -*-
# Requires fog gem.
# Configure AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY in config/rake.rb
desc <<EOF
Delete nodes and clients for nonexistent EC2 instances from chef server
YES=1 variable makes task skip confirmation questions.
EOF
@mpasternacki
mpasternacki / update_dns.rake
Created February 1, 2011 13:36
Rake task to update Amazon Route53 DNS from by Chef node search
# -*- ruby -*-
# Needs following parameters configured in rake.rb:
# DNS_DOMAIN: domain for which to set entries, including trailing dot
# (e.g. "example.com.")
# DNS_ATTRIBUTE: attribute containing hostname to CNAME to, defaults
# to 'fqdn'; for EC2, use "ec2.public_hostname"
# DNS_ENTRIES: hash mapping hostname to node search query,
# e.g. {'buildbot' => 'recipes:buildbot', 'monitoring' =>
# 'roles:monitoring'}
@mpasternacki
mpasternacki / shef.rb
Created February 3, 2011 12:50
Make shef try to load current knife config by default
# ~/.chef/shef.rb
begin
# Load configuration from knife.rb
require 'chef/knife'
Chef::Knife.new.configure_chef
rescue
puts <<-EOF
Can't load knife config: #{$!}
This is probably nothing serious.
EOF
@mpasternacki
mpasternacki / backports_apt_package.rb
Created February 9, 2011 17:53
Debian backports package resource for Opscode Chef
# backports_apt_package.rb
#
# Resource and provider to correctly install packages from Debian's
# backports, which are a non-default apt source. Creates
# backports_apt_package resource which does the Right Thing.
#
# Drop this file into cookbook's libraries/ dir.
require 'chef/provider/package/apt'
require 'chef/resource/package'
@mpasternacki
mpasternacki / chef_fileedit_g.rb
Created April 15, 2011 17:55
Add g method (named after (s)ed's g//) to Chef::Util::FileEdit for more flexible text manipulation
class Chef
class Util
class FileEdit
public
# Run block for matching lines, passing it matching line.
# Replaces matching lines with value returned by the block.
def g(regex)
exp = Regexp.new(regex)
new_contents = []
contents.each do |line|
@mpasternacki
mpasternacki / data_bag_from_yaml.rb
Created August 19, 2011 17:39
Knife plugin to create data bags from YAML files
#
# Author:: Maciej Pasternacki (<maciej@pasternacki.net>)
# Copyright:: Copyright (c) 2010 Maciej Pasternacki
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
@mpasternacki
mpasternacki / metadata.rb
Created September 1, 2011 11:28
Opscode Chef cookbook's metadata.rb fragment to pull cookbook dependencies out of data bag files.
# Data bag items in 'projects' can declare 'include_recipes' property
# (list or string); these recipes will be included in the
# projects::default recipe, but - if they're not already loaded - need
# to be declared in 'depends'. This code loads the data bag JSON from
# files, and declares all needed dependencies.
require 'json'
soft_deps = []
Dir[File.join( File.dirname(__FILE__),
'../../data_bags/projects/*.json' )].each do |p|
soft_deps |= File::open(p) { |f| JSON::load(f) }["include_recipes"].to_a.
@mpasternacki
mpasternacki / knife.rb
Created October 21, 2011 11:59
Make Chef's `knife ssh` use your actual login name
# knife.rb config snippet to make knife ssh command use your own login
# instead of hard-coded `root', `ubuntu' or other name. Tries to get
# login for your domain from ~/.ssh/config and if it's not found
# there, uses your local login.
require 'net/ssh'
require 'etc'
knife[:ssh_user] =
Net::SSH::Config.for('some.host.inside.your.domain')[:user] ||
@mpasternacki
mpasternacki / check_with_hysteresis.pl
Created December 23, 2011 23:49
Hysteresis support for nagios service checks' limit
#!/usr/bin/perl -w
#
# check_with_hysteresis.pl - hysteresis for nagios service checks' limits
#
# Usage: check_with_hysteresis.pl $LASTSERVICESTATE$ default check --- STATE1 check for state1 --- STATE2 check for state2 ...
#
# If $LASTSERVICESTATE$ is STATE1, "check for state1" will be
# executed, if it's STATE2, "check for state2" will be executed, and
# so on; if state is undefined, "default check" wil be executed.
#