require_relative "rrake/version" require 'aws-sdk-v1' require 'rest_client' require 'json' require 'logger' module Rrake class Raker def initialize @awsAccessKeyId = ENV['AWS_ACCESS_KEY'] @awsSecretAccessKey = ENV['AWS_SECRET_ACCESS_KEY'] @rrakeQueue = ENV['RRAKE_QUEUE'] logfile = ENV['RRAKE_LOGFILE_PATH'].nil? ? STDOUT : File.open(ENV['RRAKE_LOGFILE_PATH'], 'a') @logger = Logger.new logfile @stop = false @logger.fatal "Please ensure AWS_ACCESS_KEY is set." if @awsAccessKeyId.nil? or @awsAccessKeyId.empty? @logger.fatal "Please ensure AWS_SECRET_ACCESS_KEY is set." if @awsSecretAccessKey.nil? or @awsSecretAccessKey.empty? @logger.fatal "Please ensure RRAKE_QUEUE is set." if @rrakeQueue.nil? or @rrakeQueue.empty? @logger.info "RRake is ready to do your remote bidding." end def stop @stop = true end def process_messages @logger.info "RRake awaiting first command." begin sqs = AWS::SQS.new(region: 'us-east-1', access_key_id: @awsAccessKeyId, secret_access_key: @awsSecretAccessKey) sqs.queues[@rrakeQueue].poll do |message| if block_given? yield message, @options else handle_received_message(message) end return if @stop end rescue Exception => ex @logger.error "Something cause an SQS failure." @logger.error ex end end def handle_received_message(message) sns_msg = JSON.parse(message.body) rake_cmd = "rake #{sns_msg['Message']}" @logger.info %x[ #{rake_cmd} ] end end class IssueRrake def initialize @awsAccessKeyId = ENV['AWS_ACCESS_KEY'] @awsSecretAccessKey = ENV['AWS_SECRET_ACCESS_KEY'] @rrakeTopic = ENV['RRAKE_TOPIC'] logfile = ENV['RRAKE_LOGFILE_PATH'].nil? ? STDOUT : File.open(ENV['RRAKE_LOGFILE_PATH'], 'a') @logger = Logger.new logfile @logger.fatal "Please ensure AWS_ACCESS_KEY is set." if @awsAccessKeyId.nil? or @awsAccessKeyId.empty? @logger.fatal "Please ensure AWS_SECRET_ACCESS_KEY is set." if @awsSecretAccessKey.nil? or @awsSecretAccessKey.empty? @logger.fatal "Please ensure RRAKE_TOPIC is set." if @rrakeTopic.nil? or @rrakeTopic.empty? end def issue_command(command) @logger.info "Issueing Command: #{command}" sns = AWS::SNS.new(region: 'us-east-1', access_key_id: @awsAccessKeyId, secret_access_key: @awsSecretAccessKey) @logger.info sns.topics[@rrakeTopic].publish(command) rescue Exception => ex @logger.error "Something cause an SQS failure." @logger.error ex end end end