class Envoi::Mam::Agent

Constants

VERSION

Attributes

api_client[RW]
config[RW]
initial_args[RW]
logger[RW]

Public Class Methods

load_config_and_init(args) click to toggle source
# File lib/envoi/mam/agent.rb, line 121
def self.load_config_and_init(args)
  if args[:config_service_app_id] && args[:config_service_app_token]
    load_from_config_service(args)
  else args[:config_file_path]
    load_from_config_file(args)
  end
end
load_config_from_file(args) click to toggle source
# File lib/envoi/mam/agent.rb, line 89
def self.load_config_from_file(args)
  config_file_paths = args[:config_file_path]
  config_file_path = [*config_file_paths].find { |v| File.exists?(File.expand_path(v)) }
  raise "Configuration file not found. Searched '#{config_file_paths}'" unless config_file_path && !config_file_path.empty? && File.exists?(config_file_path)

  begin
    config = JSON.parse(File.read(config_file_path))
  rescue => e
    raise "Failed to Load Config File. '#{config_file_path}' '#{e.message}'"
  end
  config
end
load_config_from_service(args) click to toggle source
# File lib/envoi/mam/agent.rb, line 102
def self.load_config_from_service(args)
  args_out = { }
  args_out[:app_id] = args[:config_service_app_id]
  args_out[:token] = args[:config_service_app_token]
  args_out[:api_url] = args[:config_service_app_url]
  config = Envoi::Mam::Agent::ConfigServiceClient.config_get(args_out)
  config
end
load_from_config_file(args) click to toggle source
# File lib/envoi/mam/agent.rb, line 111
def self.load_from_config_file(args)
  config = load_config_from_file(args)
  self.new(args.merge({ :config => config }))
end
load_from_config_service(args) click to toggle source
# File lib/envoi/mam/agent.rb, line 116
def self.load_from_config_service(args)
  config = load_config_from_service(args)
  self.new(args.merge({ :config => config }))
end
new(args = { }) click to toggle source
# File lib/envoi/mam/agent.rb, line 13
def initialize(args = { })
  @initial_args = args.clone
  @config = args[:config] || { }
  @notifiers = args[:notifiers] || [ ]

  @dry_run = args.fetch(:dry_run, false)

  initialize_logger(args)
  initialize_api_client(args) # if self.respond_to?(:initialize_api_client)
  after_initialize
end

Public Instance Methods

after_initialize() click to toggle source
# File lib/envoi/mam/agent.rb, line 25
def after_initialize

end
dry_run?() click to toggle source
# File lib/envoi/mam/agent.rb, line 33
def dry_run?; @dry_run end
initialize_api_client(args = { }) click to toggle source

@param [Hash] args {} @option args [Object] :api_client Will usually be overridden by child class

# File lib/envoi/mam/agent.rb, line 51
def initialize_api_client(args = { })
  @api_client = args[:api_client] || begin

  end
end
initialize_logger(args = { }) click to toggle source
# File lib/envoi/mam/agent.rb, line 29
def initialize_logger(args = { })
  @logger = args[:logger] || Logger.new(STDOUT)
end
notify(message, args = { }) click to toggle source
# File lib/envoi/mam/agent.rb, line 35
def notify(message, args = { })
  return if @notifiers.empty?
  args[:level] ||= :info
  args[:message] ||= message
  @notifiers.each do |notifier|
    begin
      notifier.notify(args) rescue nil
    rescue => e
      logger.warn { "Notification Failed: #{e.message} "}
    end
  end
end
run_operation() click to toggle source
# File lib/envoi/mam/agent.rb, line 130
def run_operation
  case initial_args[:operation]
  when :upload; upload(initial_args) if self.respond_to?(:upload)
  when :download; download(initial_args) if self.respond_to?(:download)
  end
end
shell_execute(command, dry_run = @dry_run) click to toggle source

@param [String] command @param [Boolean] dry_run @return [Hash]

# File lib/envoi/mam/agent.rb, line 60
def shell_execute(command, dry_run = @dry_run)
  if dry_run
    logger.debug { "Skipping Execution of Command: '#{command}' " }
    return { }
  end
  logger.debug { "Executing Command: '#{command}'" }

  success = false
  Open3.popen3(command) do |stdin, stdout, stderr, thread|
    # stdin.sync = true
    # stdout.sync = true
    # stderr.sync = true

    output = ''
    until thread.stop?
      output << stdout.read #rescue nil
      output << stderr.read # rescue nil
      unless output.empty?
        logger.debug { output }
        output.clear
      end
    end

    success = thread.value == 0 ? true : false
  end

  { success: success }
end