Skip to content

Instantly share code, notes, and snippets.

View andrewgross's full-sized avatar

Andrew Gross andrewgross

View GitHub Profile
@andrewgross
andrewgross / provider_magic.rb
Last active December 16, 2015 00:39
A simple provider in HWRP style
class Chef
class Provider
class CloudMagic < Chef::Provider
# We MUST override this method in our custom provider
def load_current_resource
# Here we keep the existing version of the resource
# if none exists we create a new one from the resource we defined earlier
@current_resource ||= Chef::Resource::CloudMagic.new(new_resource.name)
@andrewgross
andrewgross / magic.rb
Created April 9, 2013 21:25
A LWRP to mimic our HWRP
action :enable, :remove
default_action :enable
attribute :magic, kind_of => [TrueClass, FalseClass], :default => true
attribute :cloud, kind_of => String, :name_attribute => true
@andrewgross
andrewgross / resource_cloud_magic.rb
Last active December 16, 2015 00:39
A sample resource
require 'chef/resource'
class Chef
class Resource
class CloudMagic < Chef::Resource
# Some Magic Happens
end
end
@andrewgross
andrewgross / recipe.rb
Last active December 16, 2015 00:39
A sample usage of our cloud magic resource.
cloud_magic "My Cloud Magic" do
action :create
cloud "My Cloud Magic"
magic true
end
@andrewgross
andrewgross / gist:5348962
Created April 9, 2013 20:10
A sample directory listing of a hybrid resource provider
attributes/
default.rb
libraries/
helpers.rb
provider_default.rb
providers/
resources/
default.rb
recipes/
default.rb
@andrewgross
andrewgross / default.rb
Created April 9, 2013 20:04
Sample of a LWRP with HWRP elements
actions :configure, :enable, :start
# Installation attributes
attribute :server_name, :kind_of => String, :name_attribute => true
# Big hack below to work around this line not working
# attribute :version, :kind_of => String, :default => node['redisio']['version']
def version(arg=nil)
default_set_in_attributes = run_context.node['redisio']['version']
@andrewgross
andrewgross / instance.rb
Created April 9, 2013 19:58
Adding a load_current_resource method to a LWRP in Chef-Redis
def load_current_resource
# Because these attributes are loaded lazily
# we have to call each one explicitly
new_resource.pidfile new_resource.pidfile || "/var/run/redis/#{new_resource.name}.pid"
new_resource.logfile new_resource.logfile || "/var/log/redis/#{new_resource.name}.log"
new_resource.dbfilename new_resource.dbfilename || "#{new_resource.name}.rdb"
new_resource.user new_resource.user || node.redis.user
new_resource.group new_resource.group || node.redis.group
new_resource.slaveof_ip new_resource.slaveof_ip
@andrewgross
andrewgross / parse_config.rb
Created March 12, 2013 19:53
Parse Config files, skip commented lines with #
# Tried
File.readlines(config_file).each{ |line| puts line unless /^#+.*/.match(line) }
File.readlines(config_file).each{ |line| puts line unless line.match(/^#+.*/) }
# Variations on regex that failed
# /^.*\#+.*/
# /\#/
# /#/
# /\#+/
# and many more
gem install bundler
echo "source :rubygems" >> Gemfile
echo "gem 'berkshelf'" >> Gemfile
echo "gem 'vagrant'" >> Gemfile
bundle install
bundle exec berks install
if node["platform"] == "windows"
existence_check = :exists?
# Where will also return files that have extensions matching PATHEXT (e.g.
# *.bat). We don't want the batch file wrapper, but the actual script.
which = 'set PATHEXT=.exe & where'
Chef::Log.debug "Using exists? and 'where', since we're on Windows"
else
existence_check = :executable?
which = 'which'
Chef::Log.debug "Using executable? and 'which' since we're on Linux"