Class: Bixby::Agent

Inherits:
Object
  • Object
show all
Includes:
Config, Handshake, ShellExec, Log
Defined in:
lib/bixby-agent/agent.rb,
lib/bixby-agent/version.rb,
lib/bixby-agent/agent/config.rb,
lib/bixby-agent/agent/crypto.rb,
lib/bixby-agent/agent/handshake.rb,
lib/bixby-agent/agent/shell_exec.rb

Defined Under Namespace

Modules: Config, Crypto, Handshake, ShellExec

Constant Summary collapse

DEFAULT_ROOT_DIR =
"/opt/bixby"
VERSION =
File.new(File.expand_path("../../../VERSION", __FILE__)).read.strip

Constants included from Config

Config::KEYS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ShellExec

#shell_exec

Methods included from Handshake

#create_uuid, #get_hostname, #get_mac_address, #mac_changed?, #register_agent

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

Methods included from Config

#config_dir, #config_file, included, #init_config_dir, #new=, #new?, #save_config

Constructor Details

#initializeAgent

Returns a new instance of Agent.



45
46
47
48
49
50
51
52
# File 'lib/bixby-agent/agent.rb', line 45

def initialize()
  Bixby::Log.setup_logger()
  @new = true

  @uuid = create_uuid()
  @mac_address = get_mac_address()
  create_keypair()
end

Instance Attribute Details

#access_keyObject

Returns the value of attribute access_key.



25
26
27
# File 'lib/bixby-agent/agent.rb', line 25

def access_key
  @access_key
end

#clientObject

Returns the value of attribute client.



25
26
27
# File 'lib/bixby-agent/agent.rb', line 25

def client
  @client
end

#mac_addressObject

Returns the value of attribute mac_address.



25
26
27
# File 'lib/bixby-agent/agent.rb', line 25

def mac_address
  @mac_address
end

#manager_uriObject

Returns the value of attribute manager_uri.



25
26
27
# File 'lib/bixby-agent/agent.rb', line 25

def manager_uri
  @manager_uri
end

#secret_keyObject

Returns the value of attribute secret_key.



25
26
27
# File 'lib/bixby-agent/agent.rb', line 25

def secret_key
  @secret_key
end

#uuidObject

Returns the value of attribute uuid.



25
26
27
# File 'lib/bixby-agent/agent.rb', line 25

def uuid
  @uuid
end

Class Method Details

.create(root_dir = nil, use_config = true) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bixby-agent/agent.rb', line 28

def self.create(root_dir=nil, use_config = true)

  agent = load_config(root_dir) if use_config
  if agent.nil? then
    # create a new one if unable to load
    agent = new()
  end

  # pass config to some modules
  Bixby.agent = agent
  return agent if agent.new?

  Bixby.manager_uri = agent.manager_uri
  Bixby.client = Bixby::Client.new(agent.access_key, agent.secret_key)
  return agent
end

.setup_envObject

Setup the environment for shelling out. Makes sure the correct Ruby version is on the path and that bixby-agent will be loaded by default



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/bixby-agent/agent.rb', line 57

def self.setup_env
  # make sure the correct ruby version is on the path
  c = begin; ::RbConfig::CONFIG; rescue NameError; ::Config::CONFIG; end
  ruby_dir = File.expand_path(c['bindir'])

  shell = Mixlib::ShellOut.new("which ruby")
  shell.run_command
  if not $?.success? or File.dirname(shell.stdout.strip) != ruby_dir then
    ENV["PATH"] = ruby_dir + File::PATH_SEPARATOR + ENV["PATH"]
  end

  # create RUBYLIB paths
  paths = []
  if ENV.include? "RUBYLIB" and not ENV["RUBYLIB"].empty? then
    paths = ENV["RUBYLIB"].split(/:/)
  end
  $:.each { |p|
    if p =~ %r(/gems/) and not paths.include? p then
      paths << p
    end
  }
  self_lib = File.expand_path(File.join(File.dirname(__FILE__), '../..', 'lib'))
  paths << self_lib if not paths.include? self_lib

  ENV["RUBYLIB"] = paths.join(":")
  ENV["RUBYOPT"] = '-rbixby-client/script'
end

Instance Method Details

#manager_ws_uriString

Get the WebSocket API URI

Returns:

  • (String)

    uri



88
89
90
91
92
93
94
# File 'lib/bixby-agent/agent.rb', line 88

def manager_ws_uri
  # convert manager uri to websocket
  uri = URI.parse(manager_uri)
  uri.scheme = (uri.scheme == "https" ? "wss" : "ws")
  uri.path = "/wsapi"
  return uri.to_s
end