Class: Locd::CLI::Command::Agent

Inherits:
Base
  • Object
show all
Defined in:
lib/locd/cli/command/agent.rb,
lib/locd/cli/command/agent/ls.rb,
lib/locd/cli/command/agent/rm.rb,
lib/locd/cli/command/agent/add.rb,
lib/locd/cli/command/agent/open.rb,
lib/locd/cli/command/agent/stop.rb,
lib/locd/cli/command/agent/tail.rb,
lib/locd/cli/command/agent/plist.rb,
lib/locd/cli/command/agent/start.rb,
lib/locd/cli/command/agent/shared.rb,
lib/locd/cli/command/agent/status.rb,
lib/locd/cli/command/agent/update.rb,
lib/locd/cli/command/agent/restart.rb,
lib/locd/cli/command/agent/truncate_logs.rb

Overview

Definitions

Direct Known Subclasses

Job, Proxy, Site

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.agent_classObject

Helpers



40
41
42
# File 'lib/locd/cli/command/agent.rb', line 40

def self.agent_class
  Locd::Agent
end

.agent_typeObject

.agent_class



44
45
46
# File 'lib/locd/cli/command/agent.rb', line 44

def self.agent_type
  agent_class.name.split( '::' ).last.downcase
end

Instance Method Details

#add(*cmd_template, **kwds) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/locd/cli/command/agent/add.rb', line 44

def add *cmd_template, **kwds
  logger.trace __method__.to_s,
    cmd_template: cmd_template,
    kwds: kwds,
    options: options
  
  # Merge all the keywords together into the format needed to call
  # {Locd::Agent.add}
  kwds.merge! **option_kwds( :force, groups: [ :write ] ),
              cmd_template: cmd_template
  
  # Check args
  
  # `:cmd_template` can not be empty at this point
  if kwds[:cmd_template].empty? || kwds[:cmd_template].all?( &:empty? )
    raise Thor::RequiredArgumentMissingError,
      "CMD_TEMPLATE argument is required to add an agent"
  end
  
  # Need a `:label` too
  unless t.non_empty_str === kwds[:label]
    raise Thor::RequiredArgumentMissingError,
      "--label=LABEL option is required to add an agent"
  end
  
  # Do the add
  agent = agent_class.add **kwds
  
  # Reload (unless we were told not to... usually you want to reload)
  agent.reload if options[:load]
  
  respond agent
end

#lsObject



38
39
40
41
42
43
44
45
46
# File 'lib/locd/cli/command/agent/ls.rb', line 38

def ls
  results = if pattern.nil?
    agent_class.all
  else
    agent_class.list pattern, **option_kwds( groups: :pattern )
  end
  
  respond results.values.sort
end

#openObject



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/locd/cli/command/agent/open.rb', line 31

def open
  Locd::Agent::Proxy.get!.ensure_running
  
  find_multi!( pattern ).each do |agent|
    if options[:start]
      agent.ensure_running( **option_kwds( groups: :start ) )
    end
    
    Cmds! "open %s", agent.url
    
    logger.info "Opened agent `#{ agent.label }` at #{ agent.url }"
  end
end

#plistObject



26
27
28
29
30
31
32
33
34
# File 'lib/locd/cli/command/agent/plist.rb', line 26

def plist
  agent = find_only! pattern
  
  if options[:json] || options[:yaml]
    respond agent.plist
  else
    respond agent.path.read
  end
end

#restartObject



28
29
30
31
32
33
# File 'lib/locd/cli/command/agent/restart.rb', line 28

def restart
  find_multi!( pattern ).
    each { |agent|
      agent.restart **option_kwds( groups: [ :restart, :stop, :start ] )
    }
end

#rmObject



33
34
35
36
# File 'lib/locd/cli/command/agent/rm.rb', line 33

def rm
  kwds = option_kwds :logs
  find_multi!( pattern ).each { |agent| agent.remove **kwds }
end

#startObject



25
26
27
28
# File 'lib/locd/cli/command/agent/start.rb', line 25

def start
  find_multi!( pattern ).
    each { |agent| agent.start **option_kwds( groups: :start ) }
end

#statusObject



26
27
28
29
30
31
# File 'lib/locd/cli/command/agent/status.rb', line 26

def status
  agent = find_only! pattern
  respond \
    label:  agent.label,
    status: agent.status.to_h( compact: false )
end

#stopObject



26
27
28
29
# File 'lib/locd/cli/command/agent/stop.rb', line 26

def stop
  kwds = option_kwds groups: :stop
  find_multi!( pattern ).each { |agent| agent.stop **kwds }
end

#tail(*tail_options) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/locd/cli/command/agent/tail.rb', line 37

def tail          *tail_options
  agent = find_only! pattern
  
  path = case options[:stream]
  when nil
    paths = agent.log_paths
    
    unless paths.length == 1
      raise Thor::RequiredArgumentMissingError.new binding.erb "        Agent `<%= agent.label %>` has multiple log files.\n        \n        out: <%= agent.out_path.to_s %>\n        err: <%= agent.err_path.to_s %>\n        \n        Must specify one via the `--stream` option.\n        \n      END\n    end\n    \n    paths[0]\n    \n  when 'out'\n    agent.out_path\n    \n  when 'err'\n    agent.err_path\n    \n  else\n    raise \"WTF\"\n  end\n  \n  cmd = ['tail']\n  cmd += ['-F'] if options[:follow]\n  \n  exec *cmd, *tail_options, path.to_s\nend\n"

#truncate_logsObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/locd/cli/command/agent/truncate_logs.rb', line 31

def truncate_logs
  find_multi!( pattern ).each do |agent|
    log_paths = [agent.out_path, agent.err_path].compact.uniq
    
    unless log_paths.empty?
      restart = options[:restart] && agent.running?
      
      agent.stop if restart
      
      log_paths.each do |log_path|
        begin
          log_path.open( 'w' ) { |f| f.truncate 0 }
        rescue Exception => error
          logger.error "Failed to truncate #{ log_path }", error
        else
          logger.info "Truncated",
            'file' => log_path.to_s,
            'agent.label' => agent.label
        end
      end # each log_path
      
      agent.start if restart
    end # unless log_paths.empty?
  end # each agent
end

#update(*cmd_template) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/locd/cli/command/agent/update.rb', line 29

def update        *cmd_template
  agent = find_only! pattern
  
  new_agent = agent.update \
    cmd_template: cmd_template,
    **option_kwds( groups: :write )
  
  logger.info "Agent `#{ agent.label }` updated"
  
  respond agent
end