Class: Envoi::Mam::Agent

Inherits:
Object
  • Object
show all
Defined in:
lib/envoi/mam/agent.rb,
lib/envoi/mam/agent/cli.rb,
lib/envoi/mam/agent/version.rb,
lib/envoi/mam/agent/base_daemon.rb,
lib/envoi/mam/agent/transfer_client.rb,
lib/envoi/mam/agent/transfer_client/s3.rb,
lib/envoi/mam/agent/config_service_client.rb,
lib/envoi/mam/agent/daemon/windows_service.rb,
lib/envoi/mam/agent/transfer_client/aspera.rb,
lib/envoi/mam/agent/watch_folder_utility/foreman.rb

Defined Under Namespace

Classes: BaseDaemon, CLI, ConfigServiceClient, Daemon, TransferClient, WatchFolderUtility

Constant Summary collapse

VERSION =
'1.1.0'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = { }) ⇒ Agent

Returns a new instance of Agent.



13
14
15
16
17
18
19
20
21
22
23
# 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

Instance Attribute Details

#api_clientObject

Returns the value of attribute api_client.



11
12
13
# File 'lib/envoi/mam/agent.rb', line 11

def api_client
  @api_client
end

#configObject

Returns the value of attribute config.



11
12
13
# File 'lib/envoi/mam/agent.rb', line 11

def config
  @config
end

#initial_argsObject

Returns the value of attribute initial_args.



11
12
13
# File 'lib/envoi/mam/agent.rb', line 11

def initial_args
  @initial_args
end

#loggerObject

Returns the value of attribute logger.



11
12
13
# File 'lib/envoi/mam/agent.rb', line 11

def logger
  @logger
end

Class Method Details

.load_config_and_init(args) ⇒ Object



121
122
123
124
125
126
127
# 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) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/envoi/mam/agent.rb', line 89

def self.load_config_from_file(args)
  config_file_path = args[:config_file_path]
  config_file_path = [*config_file_path].find { |v| File.exists?(File.expand_path(v)) }
  raise "Missing Config File. '#{config_file_path}'" 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) ⇒ Object



102
103
104
105
106
107
108
109
# 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) ⇒ Object



111
112
113
114
# 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) ⇒ Object



116
117
118
119
# 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

Instance Method Details

#after_initializeObject



25
26
27
# File 'lib/envoi/mam/agent.rb', line 25

def after_initialize

end

#dry_run?Boolean

Returns:

  • (Boolean)


33
# File 'lib/envoi/mam/agent.rb', line 33

def dry_run?; @dry_run end

#initialize_api_client(args = { }) ⇒ Object

Will usually be overridden by child class

Parameters:

  • args (Hash) (defaults to: { })

    {}

Options Hash (args):

  • :api_client (Object)


51
52
53
54
55
# File 'lib/envoi/mam/agent.rb', line 51

def initialize_api_client(args = { })
  @api_client = args[:api_client] || begin

  end
end

#initialize_logger(args = { }) ⇒ Object



29
30
31
# File 'lib/envoi/mam/agent.rb', line 29

def initialize_logger(args = { })
  @logger = args[:logger] || Logger.new(STDOUT)
end

#notify(message, args = { }) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# 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_operationObject



130
131
132
133
134
135
# 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) ⇒ Hash

Parameters:

  • command (String)
  • dry_run (Boolean) (defaults to: @dry_run)

Returns:

  • (Hash)


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# 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