Skip to content

Instantly share code, notes, and snippets.

View atheiman's full-sized avatar
😬

Austin Heiman atheiman

😬
View GitHub Profile
@thomseddon
thomseddon / gist:3511330
Last active March 8, 2023 03:39
AngularJS byte format filter
app.filter('bytes', function() {
return function(bytes, precision) {
if (isNaN(parseFloat(bytes)) || !isFinite(bytes)) return '-';
if (typeof precision === 'undefined') precision = 1;
var units = ['bytes', 'kB', 'MB', 'GB', 'TB', 'PB'],
number = Math.floor(Math.log(bytes) / Math.log(1024));
return (bytes / Math.pow(1024, Math.floor(number))).toFixed(precision) + ' ' + units[number];
}
});
@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)
@mrchief
mrchief / LICENSE.md
Last active March 23, 2024 12:28
Add "Open with Sublime Text 2" to Windows Explorer Context Menu (including folders)

MIT License

Copyright (c) [year] [fullname]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

@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)
#
@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 / logging.sh
Created June 6, 2016 21:12
simple shell script logging utility - logging.sh
#!/bin/bash
# Prints all arguments passed to the function with a timestamp prepended. `LOG_DATE_STRING` can be
# overridden by exporting the var. Prepend to the log using `LOG_LEVEL`.
log() {
[[ -z "$LOG_LEVEL" ]] && log_str='' || log_str="$LOG_LEVEL "
log_str="${log_str}$(date "+${LOG_DATE_STRING-%Y-%m-%d %H:%M:%S}") $@"
echo "$log_str"
}
info() { LOG_LEVEL='INFO' log "$@"; }
@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]
@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