double-z
6/22/2015 - 8:53 PM

A ruby script to create a whole bunch of chef nodes.

A ruby script to create a whole bunch of chef nodes.

#!/usr/bin/env ruby

require 'chef-api'
require 'fauxhai'

include ChefAPI::Resource

ChefAPI.configure do |config|
  config.endpoint = 'http://HOSTNAME:4545'
  config.client = 'admin'
  config.key = '~/etc/goiardi/admin.pem'
end

datacenters = [ { :city => "Vagrantheim", :cluster => "VAG", :name => "VAG", :region => "US-West" }, { :city => "San Jose", :cluster => "SJC", :name => "SJC", :region => "US-California" }, { :city => "McMurdo", :cluster => "MCM", :name => "MCM", :region => "Antarctica" } ]

run_lists = [ "recipe[datacenter::vagrant]", "role[foo]", "recipe[scout],recipe[bnerk]", "role[foo],recipe[scout]" ]

pkeys = [ "debian", "ubuntu", "centos" ]
platforms = {
  "debian" => [
  "6.0.5", "7.5", "7.8"
  ],
  "ubuntu" => [
    "12.04", "14.04", "10.04"
  ],
  "centos" => [
    "5.9", "6.6", "7.0"
  ]
}

(100..10000).each do |x|
  p = rand(3)
  v = rand(3)
  auto = Fauxhai.mock(platform: pkeys[p], version: platforms[pkeys[p]][v]) do |node|
    node['hostname'] = "server#{x}.example.com"
  end
  n = Node.new
  n.name = "server#{x}.example.com"
  n.automatic = auto.data
  ds = rand(3)
  rl = rand(4)
  n.default['datacenter'] = datacenters[ds]
  n.run_list = run_lists[rl].split(/,/)
  n.save
  Client.create(:name => n.name)
end