class Unsub::Service::Chef

Attributes

api[R]
knife[R]

Public Class Methods

new(knife, log) click to toggle source
# File lib/unsub/service/chef.rb, line 11
def initialize knife, log
  @log   = log
  @knife = Config.new knife
  @api   = ChefAPI::Connection.new \
    endpoint: @knife.chef_server_url,
    client: @knife.node_name,
    key: @knife.client_key
end

Public Instance Methods

add_tag(host, tag) click to toggle source
# File lib/unsub/service/chef.rb, line 32
def add_tag host, tag
  success = if name = host[:chef_name]
    node = api.nodes.fetch name
    node.normal['tags'] ||= []
    node.normal['tags'] << tag
    node.normal['tags'].uniq!
    !!node.save
  end

  log.info service: 'chef', event: 'add_tag', host: host, tag: tag, success: success
  success
end
extend_host(host) click to toggle source
# File lib/unsub/service/chef.rb, line 21
def extend_host host
  name = if ip = host[:ip]
    api.search.query(:node, 'ipaddress:"%s"' % ip).rows.shift['name'] rescue nil
  end

  old_host = host.dup ; host.merge! chef_name: name if name
  log.info service: 'chef', event: 'extend_host', old_host: old_host, host: host
  host
end
remove_tag(host, tag) click to toggle source
# File lib/unsub/service/chef.rb, line 46
def remove_tag host, tag
  success = if name = host[:chef_name]
    node = api.nodes.fetch name
    node.normal['tags'] ||= []
    node.normal['tags'].delete tag
    !!node.save
  end

  log.info service: 'chef', event: 'remove_tag', host: host, tag: tag, success: success
  success
end