Skip to content

Instantly share code, notes, and snippets.

View atheiman's full-sized avatar
😬

Austin Heiman atheiman

😬
View GitHub Profile
@atheiman
atheiman / chef_search_nodes.rb
Last active June 11, 2016 18:46
Chef Node Search API
require 'chef'
def config_chef
chef_config_env_var = 'CHEF_CONFIG'
if ENV[chef_config_env_var]
chef_config = ENV[chef_config_env_var]
else
default_chef_configs = ['~/.chef/knife.rb', '/etc/chef/client.rb']
default_chef_configs.each do |c|
if File.exists?(File.expand_path(c))

Messing around with knife exec

SEARCH='chef_environment:*dev*' knife exec partial_search.rb
# - name: node-a
#   hostname: node-a
#   fqdn: node-a.domain.net
#   ipaddress: 10.190.116.124
#   run_list:
# - role[base_os]
@loren
loren / install.sh
Last active June 26, 2016 02:29
Chef install script with retry logic on dpkg to get around race with unattended upgrades
#!/bin/sh
# WARNING: REQUIRES /bin/sh
#
# - must run on /bin/sh on solaris 9
# - must run on /bin/sh on AIX 6.x
#
# Copyright:: Copyright (c) 2010-2015 Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
@atheiman
atheiman / README.md
Last active July 17, 2016 18:00
Deep merge hashes. If both values are numeric they are summed. (Ruby)

Deep merge hashes. If both values are numeric they are summed.

$ ruby deep_sum_merge.rb
source:
layer_1: 10
deeper:
  layer_2: 20
  deeper:
    layer_3: 30
 deeper:
@atheiman
atheiman / java_properties.rb
Last active January 29, 2017 01:00
Ruby hash to Java properties file
class Hash
def to_java_properties_hash(prefix='')
properties = {}
self.each do |property, value|
new_prefix = prefix.empty? ? property.to_s : prefix + '.' + property.to_s
if value.respond_to? :to_java_properties_hash
properties.merge!(value.to_java_properties_hash(new_prefix))
else
properties[new_prefix] = value.to_s
@hartmantis
hartmantis / spec_helper.rb
Last active November 15, 2017 15:50
ChefSpec stubs for testing a recipe in isolation
require 'chefspec'
module SpecHelper
def global_stubs
# Don't worry about external cookbook dependencies
Chef::Cookbook::Metadata.any_instance.stub(:depends)
# Test each recipe in isolation, regardless of includes
@included_recipes = []
Chef::RunContext.any_instance.stub(:loaded_recipe?).and_return(false)
@atheiman
atheiman / include_recipe_spec.rb
Last active November 21, 2017 15:42
Stubbing `include_recipe` while ensuring correct recipes are included. complete example: https://github.com/atheiman/test-cookbook/pull/4
# test_cookbook/recipes/default.rb
include_recipe 'test_cookbook::included_recipe'
include_recipe 'apt'
# test_cookbook/spec/recipes/default_spec.rb
describe 'test_cookbook::default' do
before(:all) { @included_recipes = [] }
before do
@stubbed_recipes = %w[test_cookbook::included_recipe apt]
@atheiman
atheiman / echo_and_run.sh
Created November 3, 2017 20:12
echo_and_run simple shell function to print timestamp and command then execute the command (useful in build tools that dont log well)
$ echo_and_run() { echo "$(date +%T) $*"; $*; }
$ echo_and_run ls
# 15:09:35 ls
# Gemfile Gemfile.lock README.md spec
@atheiman
atheiman / 2x2Vpc.yaml
Last active November 3, 2020 06:19
VPC with generated CIDR block determined by AWS account ID + Region. 4 subnets (Public/Private, 2 AZs).
Description: >
Builds a basic /24 2x2 VPC (Public/Private, 2 AZs). The VPC CIDR block is determined by a combination
of the account ID and Region, giving a _very strong_ probability of a unique range within an
Organization.
Parameters:
VpcNameTag:
Type: String
Default: 2x2
VpcCidrSuffix:
@ambakshi
ambakshi / iam-assume-role.sh
Last active October 25, 2021 15:50
Assume an IAM role. An interesting way of doing IAM roles is to give the instance permissions to assume another role, but no actual permissions by default. I got this idea while setting up security monkey: http://securitymonkey.readthedocs.org/en/latest/quickstart1.html#setup-iam-roles.
#!/bin/bash
#
# Assume the given role, and print out a set of environment variables
# for use with aws cli.
#
# To use:
#
# $ eval $(./iam-assume-role.sh)
#