#!/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 # deal with global options OptionParser.new do |opts| opts.banner = "Cloud 66 Agent v#{Cloud66::Utils::Version.current} (#{$config.is_agent_configured? ? 'Configured' : 'Not Configured'})" opts.on('--version', 'Agent version', '-v') { |v| puts "Cloud 66 Agent v#{Cloud66::Utils::Version.current} (#{$config.is_agent_configured? ? 'Configured' : 'Not Configured'})"; exit 0 } opts.on('--log [LOG]', 'Log output') { |v| $config.log = (v == "STDOUT") ? STDOUT : v } opts.on('--log-level [LOG_LEVEL]', 'Log level (int)') { |v| $config.log_level = v.to_i } end.order! command = ARGV.shift # ensure we have a command if command.nil? puts "Cloud 66 Agent v#{Cloud66::Utils::Version.current} (#{$config.is_agent_configured? ? 'Configured' : 'Not Configured'})" exit 0 else command = command.downcase end # prepare the global logger (can't have stdout logging for job_start command - as the stdout result is used) $config.log = "/var/log/cloud66_agent.log" if command == 'job_start' && $config.log == STDOUT $logger = Logger.new($config.log) $logger.level = $config.log_level # nothing allowed while disabled if command != 'configure' if $config.disabled $logger.error "This agent had been disabled. Please contact Cloud 66 if you think this is in error" exit 86 elsif !$config.is_agent_configured? # no other commands allowed while not configured $logger.error "Can only do command \"configure\" (until its been configured!)" exit -1 end end # handle commands $logger.info "Attempting: \"#{command}\"" if command == 'configure' OptionParser.new do |opts| opts.on('--api-url [URL]', 'API URL') { |v| $config.api_url = v } opts.on('--api-key API_KEY', 'API key') { |v| $config.api_key = v } opts.on('--secret-key SECRET_KEY', 'Secret key') { |v| $config.secret_key = v } opts.on('--server-uid SERVER_UID', 'Server UID') { |v| @server_uid = v } end.order! Cloud66Agent.configure(@server_uid) elsif command == 'job_start' OptionParser.new do |opts| opts.on('--job-uid JOB_UID', 'Job UID') { |v| @job_uid = v } end.order! Cloud66Agent.job_start(@job_uid) elsif command == 'job_end' OptionParser.new do |opts| opts.on('--job-uid JOB_UID', 'Job UID') { |v| @job_uid = v } opts.on('--run-uid RUN_UID', 'Run UID') { |v| @run_uid = v } opts.on('--run-status RUN_STATUS', 'Status') { |v| @run_status = v } opts.on('--run-time RUN_TIME', 'Execution time') { |v| @run_time = v } opts.on('--results-file RESULTS_FILE', 'Results file') { |v| @results_file = v } end.order! Cloud66Agent.job_end(@job_uid, @run_uid, @run_status, @run_time, @results_file) elsif command == 'fail2ban' OptionParser.new do |opts| opts.on('--is-banned IS_BANNED', 'Is this a ban action') { |v| @is_banned = v } opts.on('--ip-address IP_ADDRESS', 'IP address to act on') { |v| @ip_address = v } opts.on('--attack ATTACK', 'Attack type') { |v| @attack = v } opts.on('--port PORT', 'Attack port') { |v| @port = v } end.order! Cloud66Agent.fail2ban(@is_banned, @ip_address, @attack, @port) else begin Cloud66Agent.send command rescue $logger.error "Invalid command: #{command}" exit -1 end end