Created
June 14, 2013 15:28
-
-
Save bakins/5782742 to your computer and use it in GitHub Desktop.
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 "ridley" | |
require "multi_json" | |
require 'pp' | |
require 'chef_zero/server' | |
plan = MultiJson.load(IO.read(ARGV[0])) | |
#pp plan | |
environment = { | |
:name => "troupe_#{plan['id']}_#{plan['version']}", | |
:cookbook_versions => plan['cookbooks'], | |
:override_attributes => { | |
:troupe => plan['id'], | |
:scope => plan['scope'] | |
} | |
} | |
nodes = [] | |
roles = [] | |
plan['nodes'].each do |k,v| | |
role = { | |
:name => "troupe_#{plan['id']}_#{k}_#{plan['version']}", | |
:override_attributes => ((plan['attributes'] || {}).merge(v['attributes'] || {})).merge('troupe_role' => k), | |
:run_list => [] | |
} | |
node = { | |
:run_list => [ "role[#{role[:name]}]" ], | |
:chef_environment => environment[:name] | |
} | |
((plan['recipes'] || []) + (v['recipes'] || [])).each do |r| | |
cookbook, recipe = r.split('::') | |
raise "you must declare cookbook #{cookbook}" unless environment[:cookbook_versions][cookbook] | |
role[:run_list] << "recipe[#{r}]" | |
end | |
nodes << node | |
roles << role | |
end | |
#puts MultiJson.dump(roles, :pretty => true) | |
#puts MultiJson.dump(nodes, :pretty => true) | |
#puts MultiJson.dump(environment, :pretty => true) | |
server = ChefZero::Server.new(port: 4000) | |
server.start_background | |
ridley = Ridley.new( | |
server_url: "http://localhost:4000", | |
client_name: "bakins", | |
client_key: "/Users/bakins/.chef/bakins.pem" | |
) | |
ridley.environment.create(environment) | |
roles.each do |role| | |
ridley.role.create(role) | |
end | |
#this is just for testing. we'd have to create real names | |
nodes.each do |node| | |
name = Array.new(8){[*'0'..'9', *'a'..'z', *'A'..'Z'].sample}.join | |
n = ridley.node.create(node.merge(:name => name)) | |
pp n | |
pp ridley.node.find(name) | |
end | |
pp ridley.node.all | |
#ridley.node.all.each do |node| | |
# pp node | |
#end | |
#ridley.search(:node, "troupe:#{plan['id']}").each do |node| | |
# pp node | |
#end | |
#compare environment to whats on chef and raise an error if already exists and is different. create an environment if needed | |
#compare roles to whats on chef and raise an error if already exists and is different. create roles | |
#search for all the nodes of each troupe_role an make sure we have enough, create them if we don't, modify them so they have correct role and environment | |
#we could traverse the dependencies of the cookbooks in the environment and include their dependencies as well | |
#could/should this be a knife plugin?? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment