Class: Pazuzu::CommandLine::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/pazuzu/command_line/controller.rb

Instance Method Summary collapse

Constructor Details

#initializeController

Returns a new instance of Controller.



6
7
# File 'lib/pazuzu/command_line/controller.rb', line 6

def initialize
end

Instance Method Details

#format_uptime(uptime) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/pazuzu/command_line/controller.rb', line 128

def format_uptime(uptime)
  s = 'running'
  if uptime
    s << ' for '
    days, hours, minutes, seconds =
      (uptime / 1.day).floor,
      (uptime / 1.hour).floor,
      (uptime / 1.minute).floor,
      (uptime / 1.second).floor
    if days > 0
      s << "#{days}d, #{hours}h"
    elsif hours > 0 or minutes > 0
      s << "#{hours}h, " if hours > 0
      s << "#{minutes}m"
    else
      s << "#{seconds}s"
    end
  end
  s
end


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/pazuzu/command_line/controller.rb', line 104

def print_applications(applications)
  puts "Process                 State                           Command"
  puts "----------------------  ------------------------------  ----------------------------"
  applications.each do |application|
    (application['workers'] || []).each do |worker|
      (worker['instances'] || []).each do |instance|
        state = instance['state']
        if state == 'running'
          state = format_uptime(instance['uptime'])
        end
        recovery_count = instance['recovery_count']
        if recovery_count and recovery_count > 0
          state << " (#{recovery_count} recoveries)"
        end
        puts [
          "%-22s" % instance['name'],
          "%-30s" % state,
          instance['command']
        ].join("  ")
      end
    end
  end
end


93
94
95
96
97
98
99
100
101
102
# File 'lib/pazuzu/command_line/controller.rb', line 93

def print_help(items)
  puts "Command                    Description"
  puts "-------------------------  ------------------------------"
  items.each do |item|
    puts [
      "%-25s" % item['syntax'],
      "%-30s" % item['description']
    ].join("  ")          
  end
end


83
84
85
86
87
88
89
90
91
# File 'lib/pazuzu/command_line/controller.rb', line 83

def print_log(entries)
  entries.each do |entry|
    puts [
      entry['time'],
      entry['source'],
      entry['message']
    ].join("\t")
  end
end

#run!(arguments) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/pazuzu/command_line/controller.rb', line 9

def run!(arguments)
  config_path, socket_path, format = nil, nil, 'text'

  OptionParser.new do |opts|
    opts.banner = "#{File.basename($0)} [OPTIONS] COMMAND [...]"
    opts.separator ""
    opts.on('--socket PATH', 'Connect to socket PATH') do |value|
      socket_path = File.expand_path(value)
    end
    opts.on("-c FILE", "--config FILE", 'Read configuration from FILE') do |value|
      config_path = File.expand_path(value)
    end
    opts.on('-f FORMAT', '--format FORMAT') do |value|
      format = value
    end
    opts.on("-h", "--help", "Show this help.") do
      puts opts
      exit
    end
    opts.parse!(arguments)
  end

  if arguments.empty?
    abort "No command specified. Run with 'help' for list of commands."
  end

  unless %w(text json).include?(format)
    abort "Invalid format '#{format}'."
  end

  if config_path
    configuration = YAML.load(File.open(config_path))
    socket_path = configuration['socket_path']
  end
  socket_path ||= Supervisor::DEFAULT_SOCKET_PATH

  command, command_arguments = arguments[0], arguments[1..-1]

  begin
    client = Control::SocketClient.new(socket_path)
  rescue Errno::ECONNREFUSED
    abort "Connection refused on #{socket_path}. Server might not be running."
  else
    response = client.execute(command, *command_arguments)
    if response
      case format
        when 'json'
          puts JSON.dump(response, :pretty => true)
        when 'text'
          if response['success']
            puts(response['success'])
          elsif response['error']
            abort(response['error'])
          elsif response['results']
            case command
              when 'log'
                print_log(response['results'])
              when 'help'
                print_help(response['results'])
              when 'list'
                print_applications(response['results'])
              else
                abort("Don't know how to print results from '#{command}'.")
            end
          else
            abort("Unrecognized response from server: #{JSON.dump response}")
          end
      end
    else
      exit(1)
    end
  end
end