Class: SQSPoller::Handler::Puppetsdb

Inherits:
Object
  • Object
show all
Defined in:
lib/sqspoller/handlers/puppetsdb.rb

Instance Method Summary collapse

Instance Method Details

#add_node_from_ref(ref_node, dest_node, as_name) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/sqspoller/handlers/puppetsdb.rb', line 68

def add_node_from_ref(ref_node, dest_node, as_name)
  yaml_data = @puppet_enc.get_node_yaml(ref_node)
  if yaml_data.empty?
    SQSPoller.warn("#{@queue_name}: Reference node configuration '#{ref_node}' not found for autoscaling group '#{as_name}'")
  else
    SQSPoller.info("#{@queue_name}: Writing node configuration for #{dest_node} due to event from autoscaling group #{as_name}")
    @puppet_enc.write_node_data(dest_node, yaml_data)
  end
end

#del_node(node, as_name) ⇒ Object



78
79
80
81
# File 'lib/sqspoller/handlers/puppetsdb.rb', line 78

def del_node(node, as_name)
    SQSPoller.info("#{@queue_name}: Deleting node configuration for #{node} due to event from autoscaling group #{as_name}")
    @puppet_enc.delete_item(node)
end

#process(queue_name, msg, conf) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/sqspoller/handlers/puppetsdb.rb', line 37

def process(queue_name, msg, conf)
  @queue_name = queue_name
  domain = conf['domain']
  template_map = conf['template_map']
  begin
    sns_msg = msg.as_sns_message
    sns_msg_data = sns_msg.to_h
    sns_msg_body = JSON.load(sns_msg_data[:body])
  rescue
    SQSPoller.warn("#{@queue_name}: Message is not a properly formatted SNS message")
    return
  end
  msg_id = sns_msg.message_id
  instance_id = sns_msg_body['EC2InstanceId']
  event_type = sns_msg_body['Event']
  autoscale_name = sns_msg_body['AutoScalingGroupName']
  reference_node = template_map[autoscale_name]

  SQSPoller.debug("#{@queue_name}: Found message '#{msg_id}'")
  SQSPoller.debug("#{@queue_name}: Processing event '#{event_type}' for autoscaling group '#{autoscale_name}' and instance-id #{instance_id}")
  (SQSPoller.warn("#{@queue_name}: Unregistered autoscaling group: #{autoscale_name}"); return) if reference_node.nil?

  @puppet_enc = PuppetSDB::PuppetSDB.new(:domain => domain)
  case event_type
  when "autoscaling:EC2_INSTANCE_LAUNCH"
    add_node_from_ref(reference_node, instance_id, autoscale_name)
  when "autoscaling:EC2_INSTANCE_TERMINATE"
    del_node(instance_id, autoscale_name)
  end
end