Skip to content

Instantly share code, notes, and snippets.

@gswallow
Created January 30, 2014 00:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gswallow/8700460 to your computer and use it in GitHub Desktop.
Save gswallow/8700460 to your computer and use it in GitHub Desktop.
ruby_block "Comb through assays and create templates" do
block do
def find_assays(customer)
assays = Array.new
assays_dir = Dir.entries("/var/scripts/assays/#{customer}/current")
assays_dir.each { |e| assays << e unless e =~ /^\./ }
assays
end
def create_template(general_config, options={})
t = Chef::Resource::Template.new("#{Chef::Config[:file_cache_path]}/#{options[:customer]}-#{options[:filename]}", node.run_context)
t.cookbook("ascent-compute")
t.source("#{options[:filename]}.erb")
t.owner(general_config["web"]["deployment_user"])
t.group(general_config["web"]["deployment_group"])
t.mode("0600")
yield(t)
t.run_action("create")
end
def create_rest_json(general_config, options={})
passwords = Chef::EncryptedDataBagItem.load("site_passwords", options[:customer])
create_template(general_config, options) do |t|
t.variables(
:username => "indigo_custom_reports",
:password => passwords['custom_reports'],
:site => "#{options[:customer]}.#{general_config['web']['domain_name']}"
)
end
end
def create_copy_command(customer, dir)
"cp -p #{Chef::Config[:file_cache_path]}/#{customer}-rest.json /var/scripts/assays/#{customer}/current/#{dir}/rest.json"
end
def create_store_assay_command(customer, assay, mongo_ips)
"/usr/bin/referee --assayDir=/var/scripts/assays/#{customer}/current/#{assay} --hosts '#{mongo_ips}' --dbName ascent_production_#{customer} --store-rule-specs"
end
def execute_all(*commands)
command = commands.join(" && ")
e = Chef::Resource::Execute.new(command, node.run_context)
e.command(command)
e.run_action("run")
end
# Get mongo servers
q = Chef::Search::Query.new
mongo_servers = q.search(:node, "role:mongo_server AND chef_environment:#{node['ascent']['compute_executor']['mongo_search_environment']}").first
mongo_ips = mongo_servers.collect { |n| n.ipaddress }.join(" ")
# Load the general config
general_config = Chef::DataBagItem.load("ascent","ascent-software")[node['ascent']['compute_executor']['general_search_environment']]
# Loop through all customer directories and find those that contain "current" symlinks
customer_dirs = Array.new
base_dir = Dir.entries("/var/scripts/assays")
base_dir.each do |dir|
if node["customer"].nil?
if dir !~ /^\./ and File.exists?("/var/scripts/assays/#{dir}/current")
customer_dirs << dir
end
else
if dir == node["customer"] and File.exists?("/var/scripts/assays/#{dir}/current")
Chef::Log.info "Found matching #{dir} for #{node['customer']}"
customer_dirs << dir
end
end
end
# Find assays in each customer directory
customer_dirs.each do |customer|
assay_dirs = find_assays(customer)
create_rest_json(general_config, { customer: customer, filename: "rest.json" })
execute_all(assay_dirs.collect { |dir| create_copy_command(customer, dir) })
execute_all(assay_dirs.collect { |dir| create_store_assay_command(customer, dir, mongo_ips) }) unless node.role?("migrator")
end
end
action :create
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment