Created
November 9, 2012 11:26
-
-
Save tily/4045229 to your computer and use it in GitHub Desktop.
patty.rb、最小限 Chef 実行
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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