Skip to content

Instantly share code, notes, and snippets.

@tily
Created November 9, 2012 11:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tily/4045229 to your computer and use it in GitHub Desktop.
Save tily/4045229 to your computer and use it in GitHub Desktop.
patty.rb、最小限 Chef 実行
require 'tmpdir'
require 'rubygems'
require 'chef/shef/ext'
require 'chef/providers'
require 'chef/resources'
require 'sinatra/base'
class Patty < Shef::StandAloneSession
def reset!
loading do
rebuild_node
@node = @client.node
rebuild_context
@recipe = Chef::Recipe.new(nil, nil, run_context)
end
end
end
Chef::Log.level = :debug
BANNER = 'Usage: ruby patty.rb recipe.rb'
def main(args)
path = args.first or abort BANNER
File.exists?(path) or abort "File not found: #{path}"
patty = Patty.instance
patty.reset!
patty.recipe.instance_eval File.read(path)
Dir.mktmpdir do |dir|
sections = read_sections(path)
sections.each do |name, content|
File.open("#{dir}/#{name}", "w") do |f|
f.write(content.first)
end
end
Dir.chdir dir
Chef::Runner.new(patty.run_context).converge
end
end
def read_sections(path)
Sinatra::Base.inline_templates = path
Sinatra::Base.templates
end
main(ARGV.dup)
file '/tmp/hello.txt' do
content "hello!\n"
end
template "/tmp/hello2.txt" do
local true
source "hello"
variables :name => 'world'
end
template "/tmp/foobar.txt" do
local true
source "foobar"
variables :name => 'tily'
end
__END__
@@ hello
hello, <%= @name %>
@@ foobar
this is another template: name is <%= @name %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment