lib/bixby-agent/app.rb
def load_agent

    begin
      agent = Agent.create(@config[:directory])
    rescue Exception => ex
      if ex.message =~ /manager URI/ then
        # if unable to load from config and no/bad uri passed, bail!
        $stderr.puts "ERROR: a valid manager URI is required on first run"
        $stderr.puts
        $stderr.puts @opt_parser.help()
        exit 1
      end
      raise ex
    end

    # TODO disable mac detection for now; it doesn't work in certain cases
    #      e.g., when you stop/start an instance on EC2 a new mac is issued
    #
    # if not agent.new? and agent.mac_changed? then
    #   # loaded from config and mac has changed
    #   agent = Agent.create(opts, false)
    # end

    if agent.new? then

      if !@config[:register] then
        # --register not passed, bail out
        if File.exists? agent.config_file then
          $stderr.puts "Unable to load agent config from #{agent.config_file}; pass --register to reinitialize"
        else
          $stderr.puts "Unable to load agent from BIXBY_HOME=#{ENV['BIXBY_HOME']}; pass --register to initialize"
        end
        exit 1
      end

      # validate uri
      uri = @argv.shift || @config[:register]
      begin
        if uri.nil? or URI.parse(uri).nil? or URI.join(uri, "/api").nil? then
          raise ConfigException, "Missing manager URI", caller
        end
      rescue URI::Error => ex
        raise ConfigException, "Bad manager URI: '#{uri}'"
      end

      # register
      $stdout.puts "Going to register with manager: #{uri}"
      if (ret = agent.register_agent(uri, @config[:token], @config[:tags])).fail? then
        $stderr.puts "error: failed to register with manager!"
        $stderr.puts "reason:"
        if ret.message =~ /900 seconds old/ then
          Help::SystemTime.print()
        else
          $stderr.puts "  #{ret.message}"
        end
        exit 1
      end
      agent.save_config()
      ARGV.clear # make sure it's empty so daemon starts properly
      $stdout.puts "Registration successful; launching bixby-agent into background"
    end
    agent
  end
def load_agent

    begin
      agent = Agent.create(@config[:directory])
    rescue Exception => ex
      if ex.message =~ /manager URI/ then
        # if unable to load from config and no/bad uri passed, bail!
        $stderr.puts "ERROR: a valid manager URI is required on first run"
        $stderr.puts
        $stderr.puts @opt_parser.help()
        exit 1
      end
      raise ex
    end

    # TODO disable mac detection for now; it doesn't work in certain cases
    #      e.g., when you stop/start an instance on EC2 a new mac is issued
    #
    # if not agent.new? and agent.mac_changed? then
    #   # loaded from config and mac has changed
    #   agent = Agent.create(opts, false)
    # end

    if agent.new? then

      if !@config[:register] then
        # --register not passed, bail out
        if File.exists? agent.config_file then
          $stderr.puts "Unable to load agent config from #{agent.config_file}; pass --register to reinitialize"
        else
          $stderr.puts "Unable to load agent from BIXBY_HOME=#{ENV['BIXBY_HOME']}; pass --register to initialize"
        end
        exit 1
      end

      # validate uri
      uri = @argv.shift || @config[:register]
      begin
        if uri.nil? or URI.parse(uri).nil? or URI.join(uri, "/api").nil? then
          raise ConfigException, "Missing manager URI", caller
        end
      rescue URI::Error => ex
        raise ConfigException, "Bad manager URI: '#{uri}'"
      end

      # register
      $stdout.puts "Going to register with manager: #{uri}"
      if (ret = agent.register_agent(uri, @config[:token], @config[:tags])).fail? then
        $stderr.puts "error: failed to register with manager!"
        $stderr.puts "reason:"
        if ret.message =~ /900 seconds old/ then
          Help::SystemTime.print()
        else
          $stderr.puts "  #{ret.message}"
        end
        exit 1
      end
      agent.save_config()
      ARGV.clear # make sure it's empty so daemon starts properly
      $stdout.puts "Registration successful; launching bixby-agent into background"
    end
    agent
  end
def load_agent begin agent = Agent . create ( @config [ :directory ] ) rescue Exception => ex if ex . message =~ / / then # if unable to load from config and no/bad uri passed, bail! $stderr . puts "ERROR: a valid manager URI is required on first run" $stderr . puts $stderr . puts @opt_parser . help ( ) exit 1 end raise ex end # TODO disable mac detection for now; it doesn't work in certain cases #      e.g., when you stop/start an instance on EC2 a new mac is issued # # if not agent.new? and agent.mac_changed? then #   # loaded from config and mac has changed #   agent = Agent.create(opts, false) # end if agent . new? then if ! @config [ :register ] then # --register not passed, bail out if File . exists? agent . config_file then $stderr . puts "Unable to load agent config from #{agent.config_file}; pass --register to reinitialize" else $stderr . puts "Unable to load agent from BIXBY_HOME=#{ENV['BIXBY_HOME']}; pass --register to initialize" end exit 1 end # validate uri uri = @argv . shift || @config [ :register ] begin if uri . nil? or URI . parse ( uri ) . nil? or URI . join ( uri , "/api" ) . nil? then raise ConfigException , "Missing manager URI" , caller end rescue URI :: Error => ex raise ConfigException , "Bad manager URI: '#{uri}'" end # register $stdout . puts "Going to register with manager: #{uri}" if ( ret = agent . register_agent ( uri , @config [ :token ] , @config [ :tags ] ) ) . fail? then $stderr . puts "error: failed to register with manager!" $stderr . puts "reason:" if ret . message =~ / / then Help :: SystemTime . print ( ) else $stderr . puts "  #{ret.message}" end exit 1 end agent . save_config ( ) ARGV . clear # make sure it's empty so daemon starts properly $stdout . puts "Registration successful; launching bixby-agent into background" end agent end
Load Agent

 Load the agent from $BIXBY_HOME. If no existing configuration was found,
 try to register with the server if we have the correct parameters.
Load Agent