Created
July 29, 2012 03:33
-
-
Save tily/3195930 to your computer and use it in GitHub Desktop.
手軽に chef-solo を実行するためにスケルトンを作ってくれるやつ (ユースケース要検討)
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 'erb' | |
require 'fileutils' | |
require 'rubygems' | |
require 'git' | |
require 'sinatra/base' | |
require 'json' | |
BANNER = 'Usage: ruby chef-solo-sandbox.rb /path/to/dir [cookbook1 [cookbook2 [cookbook3 ...]]]' | |
def main(argv) | |
@file_cache_path, @cookbooks = argv[0], argv[1..-1] | |
abort BANNER if @file_cache_path.nil? | |
abort 'Error: Dir already exists.' if File.exists?(@file_cache_path) | |
@file_cache_path = File.absolute_path(@file_cache_path) | |
@cookbook_path = File.absolute_path File.join(@file_cache_path, 'cookbooks') | |
create_skelton | |
install_cookbooks | |
cmd = "chef-solo -c #{@file_cache_path}/solo.rb -j #{@file_cache_path}/node.json" | |
puts "## #{cmd}" | |
system cmd | |
end | |
def create_skelton | |
FileUtils.mkdir_p(@cookbook_path) | |
FileUtils.touch(File.join(@cookbook_path, '.gitkeep')) | |
git = Git.init(@cookbook_path) | |
read_sections(__FILE__).each do |s| | |
f = File.open(File.join(@file_cache_path, s.first.to_s), 'w') | |
f.write(ERB.new(s[1].first).result(binding)) | |
f.close | |
end | |
git.add('.') | |
git.commit('initial import.') | |
end | |
def install_cookbooks | |
Dir.chdir @cookbook_path | |
@cookbooks.each do |x| | |
cmd = "knife cookbook site install #{x} -c #{@file_cache_path}/solo.rb" | |
puts "## #{cmd}" | |
system cmd | |
end | |
end | |
def read_sections(path) | |
Sinatra::Base.inline_templates = path | |
Sinatra::Base.templates | |
end | |
main(ARGV.dup) | |
__END__ | |
@@ solo.rb | |
file_cache_path "<%= @file_cache_path %>" | |
cookbook_path ["<%= @cookbook_path %>"] | |
@@ node.json | |
{ | |
"run_list": <%= @cookbooks.map{|r| "recipe[#{r}]"}.to_json %> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment