Class: IClassify::Agent

Inherits:
Object
  • Object
show all
Defined in:
lib/iclassify-interface/agent.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uuidfile = "/etc/icagent/icagent.uuid", server_url = "http://localhost:3000") ⇒ Agent

Create a new Agent. Takes a path to a file to either read or drop a UUID, and a server URL.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/iclassify-interface/agent.rb', line 14

def initialize(uuidfile="/etc/icagent/icagent.uuid", server_url="http://localhost:3000")
  @uuid = nil
  @password = nil
  if File.exists?(uuidfile)
    IO.foreach(uuidfile) do |line|
      @uuid, @password = line.chomp.split("!")
    end
    unless @password
      @password = random_password(30)
      write_uuidfile(uuidfile)
    end
  else
    @uuid = UUIDTools::UUID.random_create
    @password = random_password(30)
    write_uuidfile(uuidfile)
  end
  @client = IClassify::Client.new(server_url, @uuid, @password)
end

Instance Attribute Details

#nodeObject

Returns the value of attribute node.



6
7
8
# File 'lib/iclassify-interface/agent.rb', line 6

def node
  @node
end

#passwordObject

Returns the value of attribute password.



8
9
10
# File 'lib/iclassify-interface/agent.rb', line 8

def password
  @password
end

#uuidObject

Returns the value of attribute uuid.



7
8
9
# File 'lib/iclassify-interface/agent.rb', line 7

def uuid
  @uuid
end

Instance Method Details

#add_attrib(name, values) ⇒ Object

Add an attribute to this node. Requires a name and either a string or array of values.

Will be cumulative!



110
111
112
113
# File 'lib/iclassify-interface/agent.rb', line 110

def add_attrib(name, values)
  load unless @node
  @node.attribs << { :name => name, :values => values.kind_of?(Array) ? values : [ values ] }
end

#add_tag(tag) ⇒ Object

Add a tag to this node.



101
102
103
104
# File 'lib/iclassify-interface/agent.rb', line 101

def add_tag(tag)
  load unless @node
  @node.tags << tag
end

#attrib?(attrib) ⇒ Boolean

Returns the values for this attribute, if it exists for this node. If there is only one, it will return it, if it’s an array, you get the array. You have to check!

Returns:

  • (Boolean)


81
82
83
# File 'lib/iclassify-interface/agent.rb', line 81

def attrib?(attrib)
  @node.attrib?(attrib)
end

#attrib_has_value?(attrib, value) ⇒ Boolean

Returns the value if the given attribute has a given attribute.

Returns:

  • (Boolean)


91
92
93
94
95
96
97
98
# File 'lib/iclassify-interface/agent.rb', line 91

def attrib_has_value?(attrib, value)
  na = @node.attribs.detect { |a| a[:name] == attrib }
  if na 
    return na.values.detect { |v| v == value}
  else
    return nil
  end
end

#deleteObject

Deletes this node from the iClassify service.



67
68
69
# File 'lib/iclassify-interface/agent.rb', line 67

def delete
  @client.delete_node(@node)
end

#description(value) ⇒ Object

Set the description for the node



127
128
129
# File 'lib/iclassify-interface/agent.rb', line 127

def description(value)
  @node.description = value
end

#description?Boolean

return the value of @node.description

Returns:

  • (Boolean)


132
133
134
# File 'lib/iclassify-interface/agent.rb', line 132

def description?()
  @node.description
end

#loadObject

Loads data about this node from the iClassify service



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/iclassify-interface/agent.rb', line 36

def load
  begin 
    @node = @client.get_node(@uuid)
  rescue Net::HTTPServerException => e
    if e.to_s =~ /^404/
      @node = IClassify::Node.new()
      @node.description = "New Node"
      @node.tags << "unclassified"
      @node.password = @password
      @node.uuid = @uuid
    else
      throw(e)
    end
  end
end

#replace_attrib(name, values) ⇒ Object

Replace the attribute with the given name’s values in place. Will add a new attribute if it needs to.



117
118
119
120
121
122
123
124
# File 'lib/iclassify-interface/agent.rb', line 117

def replace_attrib(name, values)
  exists = @node.attribs.detect { |a| a[:name] == name }
  if exists
    exists[:values] = values.kind_of?(Array) ? values : [ values ]
  else
    add_attrib(name, values)
  end
end

#run_script(scriptfile) ⇒ Object

Run an iclassify script.



137
138
139
# File 'lib/iclassify-interface/agent.rb', line 137

def run_script(scriptfile)
  eval(IO.read(scriptfile))
end

#tag?(tag) ⇒ Boolean

Returns the tag name if this node has that tag.

Returns:

  • (Boolean)


74
75
76
# File 'lib/iclassify-interface/agent.rb', line 74

def tag?(tag)
  @node.tag?(tag)
end

#to_sObject

Returns the current node as a string.



86
87
88
# File 'lib/iclassify-interface/agent.rb', line 86

def to_s
  @node.to_s
end

#updateObject

Updates this node in the iClassify service.



55
56
57
58
59
60
61
62
# File 'lib/iclassify-interface/agent.rb', line 55

def update
  if @node.description == "New Node"
    hostname = attrib?("hostname")
    hostname ||= "New Node"
    @node.description = hostname
  end
  @client.update_node(@node)
end