Class: LogStash::Runner

Inherits:
Object
  • Object
show all
Includes:
Program
Defined in:
lib/logstash/runner.rb

Instance Method Summary collapse

Methods included from Program

#exit

Instance Method Details

#main(args) ⇒ Object



59
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
88
89
90
91
92
93
94
# File 'lib/logstash/runner.rb', line 59

def main(args)
  require "logstash/util"
  require "stud/trap"
  require "stud/task"
  @startup_interruption_trap = Stud::trap("INT") { puts "Interrupted"; exit 0 }

  LogStash::Util::set_thread_name(self.class.name)
  #$LOAD_PATH << File.join(File.dirname(__FILE__), "..")

  if RUBY_VERSION < "1.9.2"
    $stderr.puts "Ruby 1.9.2 or later is required. (You are running: " + RUBY_VERSION + ")"
    return 1
  end

  Stud::untrap("INT", @startup_interruption_trap)

  args = [nil] if args.empty?

  @runners = []
  while args != nil && !args.empty?
    args = run(args)
  end

  status = []
  @runners.each do |r|
    #$stderr.puts "Waiting on #{r.wait.inspect}"
    status << r.wait
  end

  # Avoid running test/unit's at_exit crap
  if status.empty? || status.first.nil?
    exit(0)
  else
    exit(status.first)
  end
end

#run(args) ⇒ Object

def self.main



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/logstash/runner.rb', line 96

def run(args)
  command = args.shift
  commands = {
    "version" => lambda do
      require "logstash/agent"
      agent_args = ["--version"]
      if args.include?("--verbose") 
        agent_args << "--verbose"
      end
      LogStash::Agent.run($0, agent_args)
      return []
    end,
    "web" => lambda do
      # Give them kibana.
      require "logstash/kibana"
      kibana = LogStash::Kibana::Runner.new
      @runners << kibana
      return kibana.run(args)
    end,
    "test" => lambda do
      $LOAD_PATH << File.join(File.dirname(__FILE__), "..", "..", "test")
      require "logstash/test"
      test = LogStash::Test.new
      @runners << test
      return test.run(args)
    end,
    "rspec" => lambda do
      require "rspec/core/runner"
      require "rspec"
      fixedargs = args.collect do |arg|
        # if the arg ends in .rb or has a "/" in it, assume it's a path.
        if arg =~ /\.rb$/ || arg =~ /\//
          # check if it's a file, if not, try inside the jar if we are in it.
          if !File.exists?(arg) && __FILE__ =~ /file:.*\.jar!\//
            # Try inside the jar.
            jar_root = __FILE__.gsub(/!.*/,"!")
            newpath = File.join(jar_root, arg)

            # Strip leading 'jar:' path (JRUBY_6970)
            newpath.gsub!(/^jar:/, "")
            if File.exists?(newpath)
              # Add the 'spec' dir to the load path so specs can run
              specpath = File.join(jar_root, "spec")
              $LOAD_PATH << specpath unless $LOAD_PATH.include?(specpath)
              next newpath
            end
          end
        end
        next arg
      end # args.collect

      # Hack up a runner
      runner = Class.new do
        def initialize(args)
          @args = args
        end
        def run
          @thread = Thread.new do
            @result = RSpec::Core::Runner.run(@args)
          end
        end
        def wait
          @thread.join
          return @result
        end
      end

      $LOAD_PATH << File.expand_path("#{File.dirname(__FILE__)}/../../spec")
      require "test_utils"
      #p :args => fixedargs
      rspec = runner.new(fixedargs)
      rspec.run
      @runners << rspec
      return []
    end,
    "irb" => lambda do
      require "irb"
      IRB.start(__FILE__)
      return []
    end,
    "ruby" => lambda do
      require(args[0])
      return []
    end,
    "pry" => lambda do
      require "pry"
      return binding.pry
    end,
    "agent" => lambda do
      require "logstash/agent"
      # Hack up a runner
      agent = LogStash::Agent.new($0)
      begin
        agent.parse(args)
      rescue Clamp::HelpWanted => e
        puts e.command.help
      rescue Clamp::UsageError => e
        # If 'too many arguments' then give the arguments to
        # the next command. Otherwise it's a real error.
        raise if e.message != "too many arguments"
        remaining = agent.remaining_arguments
      end

      #require "pry"
      #binding.pry
      @runners << Stud::Task.new { agent.execute }
      return remaining
    end
  } # commands

  if commands.include?(command)
    args = commands[command].call
  else
    if command.nil?
      $stderr.puts "No command given"
    else
      if !%w(--help -h help).include?(command)
        # Emit 'no such command' if it's not someone asking for help.
        $stderr.puts "No such command #{command.inspect}"
      end
    end
    $stderr.puts "Usage: logstash <command> [command args]"
    $stderr.puts "Run a command with the --help flag to see the arguments."
    $stderr.puts "For example: logstash agent --help"
    $stderr.puts
    # hardcode the available commands to reduce confusion.
    $stderr.puts "Available commands:"
    $stderr.puts "  agent - runs the logstash agent"
    $stderr.puts "  version - emits version info about this logstash"
    $stderr.puts "  web - runs the logstash web ui (called Kibana)"
    $stderr.puts "  rspec - runs tests"
    #$stderr.puts commands.keys.map { |s| "  #{s}" }.join("\n")
    exit 1
  end

  return args
end