class Abiquo::Chef::BootstrapConfigParser

Attributes

chef_server_url[R]
node_config[R]
node_name[R]
validation_cert[R]
validation_client_name[R]

Public Class Methods

new(xml) click to toggle source

Parses an XML chunk

xml: the xml content

# File lib/abiquo-chef-agent.rb, line 62
def initialize(xml)
  @raw_xml = xml
  # HACK, FIXME
  @hash = XmlSimple.xml_in xml.gsub("
","\n")
  parse
end

Public Instance Methods

parse() click to toggle source
# File lib/abiquo-chef-agent.rb, line 69
def parse
  validate
  @node_name = @hash['node'].first
  raise Exception.new("Invalid bootstrap XML. Missing <node> info.") unless @node_name.is_a? String and not @node_name.strip.chomp.empty?

  @node_info = @hash['chef'].first

  @validation_client_name = @node_info['validation-client-name'].first
  raise Exception.new("Invalid bootstrap XML. Missing <validation-client-name> info.") unless @validation_client_name.is_a? String and not @validation_client_name.strip.chomp.empty?

  @validation_cert = @node_info['validation-cert'].first
  raise Exception.new("Invalid bootstrap XML. Missing <validation-cert> info.") unless @validation_cert.is_a? String and not @validation_cert.strip.chomp.empty?

  @chef_server_url = @node_info['chef-server-url'].first
  raise Exception.new("Invalid bootstrap XML. Missing <chef-server-url> info.") unless @chef_server_url.is_a? String and not @chef_server_url.strip.chomp.empty?

  runlist = @node_info['runlist'].first['element']
  attributes = @node_info['attributes'].first unless @node_info['attributes'].nil?
  @node_config = JSON.parse(attributes || '{}')
  @node_config.merge!(JSON.parse("{\"run_list\" : [#{runlist.map{ |r| "\"#{r}\""}.join(',')}]}")) if runlist
end

Private Instance Methods

validate() click to toggle source
# File lib/abiquo-chef-agent.rb, line 93
def validate
  raise Exception.new "Invalid bootstrap XML. Missing <node> element." unless @hash.has_key? 'node'
  raise Exception.new "Invalid bootstrap XML. Missing <chef> element." unless @hash.has_key? 'chef'
  raise Exception.new("Invalid bootstrap XML. Missing <chef> info.") if @hash['chef'].empty? or @hash['chef'][0].empty?
  raise Exception.new "Invalid bootstrap XML. Missing <validation-cert> element." unless @hash['chef'].first.has_key? 'validation-cert'
  raise Exception.new "Invalid bootstrap XML. Missing <validation-client-name> element." unless @hash['chef'].first.has_key? 'validation-client-name'
  raise Exception.new "Invalid bootstrap XML. Missing <chef-server-url> element." unless @hash['chef'].first.has_key? 'chef-server-url'
end