Module: Bixby::Agent::Handshake

Includes:
Crypto
Included in:
Bixby::Agent
Defined in:
lib/bixby-agent/agent/handshake.rb

Instance Method Summary collapse

Methods included from Crypto

#create_keypair, #crypto_enabled?, #decrypt_from_server, #encrypt_for_server, #have_server_key?, #keypair, #private_key, #private_key_file, #public_key, #server_key, #server_key_file

Instance Method Details

#create_uuidObject



72
73
74
# File 'lib/bixby-agent/agent/handshake.rb', line 72

def create_uuid
  UUIDTools::UUID.random_create.hexdigest
end

#get_hostnameObject



52
53
54
# File 'lib/bixby-agent/agent/handshake.rb', line 52

def get_hostname
  `hostname`.strip
end

#get_mac_addressObject

Get the mac address of the system’s primary interface



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/bixby-agent/agent/handshake.rb', line 57

def get_mac_address
  return @mac if not @mac.nil?
  Facter.collection.fact(:ipaddress).value # force value to be loaded now (usually lazy-loaded)
  Facter.collection.fact(:interfaces).value
  Facter.collection.fact(:macaddress).value
  vals = Facter.collection.to_hash
  ip = vals["ipaddress"]
  raise "Unable to find IP address" if ip.nil?
  # use the primary IP of the system to find the associated interface name (e.g., en0 or eth0)
  int = vals.find{ |k,v| v == ip && k != "ipaddress" }.first.to_s.split(/_/)[1]
  raise "Unable to find primary interface" if int.nil? or int.empty?
  # finally, get the mac address
  @mac = vals["macaddress_#{int}"]
end

#mac_changed?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/bixby-agent/agent/handshake.rb', line 48

def mac_changed?
  (not @mac_address.nil? and (@mac_address != get_mac_address()))
end

#register_agent(url, token, tags = nil) ⇒ JsonResponse

Register the agent with the server

Parameters:

  • url (String)

    Bixby manager URL

  • token (String)

    Client registration token

  • tags (String) (defaults to: nil)

    Comma-separated list of tags (e.g., “foo,bar”)

Returns:

  • (JsonResponse)

    response from server



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/bixby-agent/agent/handshake.rb', line 21

def register_agent(url, token, tags=nil)
  Bixby.manager_uri = @manager_uri = url
  ret = Bixby::Inventory.register_agent({
    :uuid       => @uuid,
    :public_key => self.public_key.to_s,
    :hostname   => get_hostname(),
    :token      => token,
    :tags       => tags,
    :version    => Bixby::Agent::VERSION
    })

  if ret.fail? then
    return ret
  end

  @access_key = ret.data["access_key"]
  @secret_key = ret.data["secret_key"]
  Bixby.client = Bixby::Client.new(access_key, secret_key)

  # success, store server's pub key
  File.open(self.server_key_file, 'w') do |f|
    f.puts(ret.data["server_key"])
  end

  return ret
end