#!/usr/bin/env ruby require 'optparse' require 'logger' require 'cloud66_agent/utils/config' require 'cloud66_agent/utils/version' require 'cloud66_agent' # global config variable $config = Cloud66::Utils::Config.new OptionParser.new do |opts| opts.banner = "Cloud 66 Agent v#{Cloud66::Utils::Version.current}" # following opts only for registration opts.on('--url URL', 'Server URL') do |v| $config.url = v end opts.on('--sockets SOCKETS', 'Sockets URL') do |v| $config.faye_url = v end opts.on('--api-key APIKEY', 'API key') do |v| $config.api_key = v end opts.on('--secret-key SECRETKEY', 'Secret Key') do |v| $config.secret_key = v end opts.on('--server SERVERUID', 'Server id') do |v| @server_uid = v end # respond to version requests opts.on('--version') do |v| puts "Cloud 66 Agent #{Cloud66::Utils::Version.current}" exit 0 end # logging configuration opts.on('--log LOG', 'Log file path') do |v| v == "STDOUT" ? $config.log_path = STDOUT : $config.log_path = v end opts.on('--log_level LOGLEVEL', 'Log level (int)') do |v| $config.log_level = v.to_i end end.parse! #pick up the command used command = ARGV[0].downcase unless ARGV[0].nil? # prepare the global logger (can't have stdout logging for job_start command - as the stdout result is used) $config.log_path = "/var/log/cloud66/cloud66_agent.log" if command == 'job_start' && $config.log_path == STDOUT $logger = Logger.new($config.log_path) $logger.level = $config.log_level if $config.disabled # no other commands allowed while disabled $logger.error "This agent had been disabled. Please contact Cloud 66 if you think this is in error" exit -1 end if command.nil? || command.empty? $logger.debug("Cloud 66 Agent v#{Cloud66::Utils::Version.current} (#{$config.is_agent_configured? ? 'Configured' : 'Not Configured'})") exit 0 end if !$config.is_agent_configured? && command != 'configure' # no other commands allowed while not configured $logger.error "Can only do command \"configure\" (until its been configured!)" exit -1 end # handle commands $logger.info "Performing: \"#{command}\"" if command == "configure" Cloud66Agent.configure @server_uid else begin arguments = ARGV[1..-1] if arguments.empty? Cloud66Agent.send command else Cloud66Agent.send(command, arguments) end rescue ArgumentError $logger.error "Invalid command/arguments: #{command} #{arguments}" exit -1 end end exit 0