Class: Unsub::Service::Chef

Inherits:
Base
  • Object
show all
Defined in:
lib/unsub/service/chef.rb,
lib/unsub/service/chef/config.rb

Defined Under Namespace

Classes: Config

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#log

Constructor Details

#initialize(knife, log) ⇒ Chef

Returns a new instance of Chef.



11
12
13
14
15
16
17
18
# 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

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



9
10
11
# File 'lib/unsub/service/chef.rb', line 9

def api
  @api
end

#knifeObject (readonly)

Returns the value of attribute knife.



9
10
11
# File 'lib/unsub/service/chef.rb', line 9

def knife
  @knife
end

Instance Method Details

#add_tag(host, tag) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# 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) ⇒ Object



21
22
23
24
25
26
27
28
29
# 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) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# 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