#!/usr/bin/env ruby

require_relative '../lib/kontena-agent'

Docker.options[:read_timeout] = (60*60)
Docker.options[:write_timeout] = (60*60)
Docker.validate_version!

$stdout.sync = true
$0 = 'kontena-agent'

api_token = ENV['KONTENA_TOKEN']
api_uri = ENV['KONTENA_URI'] || 'ws://api.kontena.io'

if !api_token
  exit('KONTENA_TOKEN is required')
end
if !api_uri
  exit('KONTENA_URI is required')
end
if api_uri.match(/^http.*/)
  api_uri = api_uri.sub('http', 'ws')
end

if ENV['LOG_LEVEL']
  log_level = ENV['LOG_LEVEL'].to_i
else
  log_level = Logger::INFO
end
Kontena::Logging.initialize_logger(STDOUT, log_level)

agent = Kontena::Agent.new(
  api_uri: api_uri,
  api_token: api_token
)
agent.run!
