Skip to content

Instantly share code, notes, and snippets.

@crazed
Created October 15, 2011 18:41
Show Gist options
  • Save crazed/1289961 to your computer and use it in GitHub Desktop.
Save crazed/1289961 to your computer and use it in GitHub Desktop.
simple chef fist-boot generator
require 'rubygems'
require 'sinatra'
require 'json'
require 'mongoid'
Mongoid.configure do |config|
config.master = Mongo::Connection.new.db('nodes')
config.max_retries_on_connection_failure = 1
end
class ServerType
include Mongoid::Document
include Mongoid::Versioning
field :server_type, :type => String
field :run_list, :type => Array
index :server_type
validates_uniqueness_of :server_type
end
get '/api/chef-first-run/:hostname' do
status 200
first_run = {
'run_list' => [
'role[base]'
]
}
# assumes <environment>-<system_type><host_number>.<datacenter>.<top_level_domain>
expression = /^(\D+)-(\D+)(\d{2,3})\.(\w+)\.(.*)$/
if params[:hostname].match(expression)
environment = $1
server_type = ServerType.where(:server_type => $2).first
host_number = $3.to_i
location = $4
domain = $5
first_run['environment'] = environment
first_run['run_list'] << "role[#{location}]"
unless server_type.nil?
first_run['run_list'] += server_type.run_list
end
end
body(first_run.to_json)
end
after '/api/*', :provides => :json do; end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment