Skip to content

Instantly share code, notes, and snippets.

@trlinkin
Created January 12, 2016 22:34
Show Gist options
  • Select an option

  • Save trlinkin/eba5fcdc13d759fe13a6 to your computer and use it in GitHub Desktop.

Select an option

Save trlinkin/eba5fcdc13d759fe13a6 to your computer and use it in GitHub Desktop.
crappy classifier stuff
require 'optparse'
require 'puppetclassify'
require 'yaml'
namespace :classify do
ARGV.delete "--"
##
## Example execution: rake classify:create_env_group -- -p'Parent Group' --name='App Test Group' --e='my_env'
##
desc "Task for creating a new environment group"
task :create_env_group do
options = {
:config => 'classifier.yaml',
}
OptionParser.new do |opts|
opts.banner = "Usage: rake classify::create_env_group [options]"
opts.on("-c", "--config [CONFIG]",
"Location of configuration file containing details to reach classifier API") { |config| options[:config] = config }
opts.on("-p", "--parent=PARENT_GROUP", String,
"Human readable name of group that will be the new group's parent") { |parent| options[:parent] = parent }
opts.on("-n", "--name=GROUP_NAME", String,
"Name of the group being created") { |name| options[:name] = name }
opts.on("-e", "--environment=ENVIRONMENT", String,
"Environment that will be enforced by this group") { |environment| options[:environment] = environment }
opts.on("-v", "--pipelineversion=PIPELINE_VERSION", String,
"Value to match the \"pipeline_version\" fact to.") { |pipeline| options[:pipeline] = pipeline }
end.parse!
fail ArgumentError, "Must pass a parent group name (--parent)" if options[:parent].nil?
fail ArgumentError, "Must pass a group name (--name)" if options[:name].nil?
fail ArgumentError, "Must pass an environment name (--environment)" if options[:environment].nil?
classifier = get_puppetclassify(options[:config])
parent_id = classifier.groups.get_group_id(options[:parent])
fail "Parent Group \"#{options[:parent]}\" must exist" unless parent_id
group = {
"name" => options[:name],
"environment" => options[:environment],
"parent" => parent_id,
"environment_trumps" => true,
"rule" => ["=", ["fact", "pipeline_build"], options[:pipeline]],
"classes" => {}
}
new_group_id = classifier.groups.create_group(group)
fail "Group Creation Failed" unless new_group_id
puts "New group \"#{options[:name]}\" created!"
end
##
## Example Execution: rake classify:pin_node -- --agent="testagent" --name='Group Name'
##
desc "Pin a node to a classification group"
task :pin_node do
options = {
:config => 'classifier.yaml',
}
OptionParser.new do |opts|
opts.banner = "Usage: rake add [options]"
opts.on("-c", "--config ARG", String,
"Location of configuration file containing details to reach classifier API") { |config| options[:config] = config }
opts.on("-a", "--agent CERTNAME", String,
"Certificate name of the agent that will be pinned") { |agent| options[:agent] = agent }
opts.on("-n", "--name GROUP_NAME", String,
"Human readable name of group that agent will be pinned to") { |name| options[:name] = name }
end.parse!
fail ArgumentError, "Must pass a group name (--name)" if options[:name].nil?
fail ArgumentError, "Must pass an agent name (--agent)" if options[:agent].nil?
classifier = get_puppetclassify(options[:config])
group_id = classifier.groups.get_group_id(options[:name])
fail "Group \"#{options[:name]}\" must exist to pin agent" unless group_id
group_details = classifier.groups.get_group(group_id)
if group_details["rule"]
rule = group_details["rule"]
if pinned_rule_exists?(options[:agent], rule)
puts "Pinned rule for agent \"#{options[:agent]}\" already exisits, nothing to do..."
exit 0
end
if rule.first != "or"
rule = [rule]
rule.unshift "or"
end
rule.push get_pinned_rule(options[:agent])
else
rule = ["or", get_pinned_rule(options[:agent])]
end
delta = {
"id" => group_id,
"rule" => rule
}
puts "Pinning agent \"#{options[:agent]}\" to group \"#{options[:name]}\""
classifier.groups.update_group(delta).class
end
##
## Example Execution: rake classify:delete_group -- --name='Delete Me'
##
desc "Delete a classification group"
task :delete_group do
options = {
:config => 'classifier.yaml',
}
OptionParser.new do |opts|
opts.banner = "Usage: rake add [options]"
opts.on("-c", "--config ARG", String,
"Location of configuration file containing details to reach classifier API") { |config| options[:config] = config }
opts.on("-n", "--name GROUP_NAME", String,
"Human readable name of group that agent will be pinned to") { |name| options[:name] = name }
end.parse!
fail ArgumentError, "Must pass a group name (--name)" if options[:name].nil?
classifier = get_puppetclassify(options[:config])
group_id = classifier.groups.get_group_id(options[:name])
fail "Group \"#{options[:name]}\" does not exist, nothing to do..." unless group_id
puts "Deleting group \"#{options[:name]}\""
classifier.groups.delete_group(group_id)
end
end
def get_puppetclassify(config_path)
if File.file?(config_path)
begin
config = YAML.load(File.read(config_path)) || {}
rescue SyntaxError => e
raise "Cannot Parse configuration, #{e}"
end
else
raise "Config File \"#{config_path}\" is not a valid file or path"
end
%w{ca_certificate_path certificate_path private_key_path console_url}.each do |directive|
raise "Configuration is missing a value for #{directive}" unless config[directive]
end
puppetclassify = PuppetClassify.new(config["console_url"], config)
## Test connection with a query that works and everybody knows it
puppetclassify.groups.get_group_id("All Nodes")
puppetclassify
end
def get_pinned_rule(node)
["=", "name", node]
end
def pinned_rule_exists?(agent,rule)
pinned = false
if rule.first == "or"
rule.each do |r|
if r.class == Array
if r[0] == "=" and r[1] == "name"
if r[2] == agent
pinned = true
break
end
end
end
end
end
pinned
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment