3 # Licensed to the Apache Software Foundation (ASF) under one
4 # or more contributor license agreements. See the NOTICE file
5 # distributed with this work for additional information
6 # regarding copyright ownership. The ASF licenses this file
7 # to you under the Apache License, Version 2.0 (the
8 # "License"); you may not use this file except in compliance
9 # with the License. You may obtain a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
19 # File passed to org.jruby.Main by bin/hbase. Pollutes jirb with hbase imports
20 # and hbase commands and then loads jirb. Outputs a banner that tells user
21 # where to find help, shell version, and loads up a custom hirb.
23 # In noninteractive mode, runs commands from stdin until completion or an error.
24 # On success will exit with status 0, on any problem will exit non-zero. Callers
25 # should only rely on "not equal to 0", because the current error exit code of 1
26 # will likely be updated to diffentiate e.g. invalid commands, incorrect args,
29 # TODO: Interrupt a table creation or a connection to a bad master. Currently
30 # has to time out. Below we've set down the retries for rpc and hbase but
31 # still can be annoying (And there seem to be times when we'll retry for
33 # TODO: Add support for listing and manipulating catalog tables, etc.
34 # TODO: Encoding; need to know how to go from ruby String to UTF-8 bytes
36 # Run the java magic include and import basic HBase types that will help ease
40 # Some goodies for hirb. Should these be left up to the user's discretion?
41 require 'irb/completion'
44 # Add the directory names in hbase.jruby.sources commandline option
45 # to the ruby load path so I can load up my HBase ruby modules
46 sources = java.lang.System.getProperty('hbase.ruby.sources')
47 $LOAD_PATH.unshift Pathname.new(sources)
50 # FIXME: Switch args processing to getopt
52 # See if there are args for this shell. If any, read and then strip from ARGV
53 # so they don't go through to irb. Output shell 'usage' if user types '--help'
54 cmdline_help = <<HERE # HERE document output as shell usage
55 Usage: shell [OPTIONS] [SCRIPTFILE [ARGUMENTS]]
57 --format=OPTION Formatter for outputting results.
58 Valid options are: console, html.
61 -d | --debug Set DEBUG log levels.
62 -h | --help This help.
63 -n | --noninteractive Do not run within an IRB session
64 and exit with non-zero status on
70 log_level = org.apache.log4j.Level::ERROR
74 if arg =~ /^--format=(.+)/i
76 if format =~ /^html$/i
77 raise NoMethodError.new("Not yet implemented")
78 elsif format =~ /^console$/i
81 raise ArgumentError.new("Unsupported format " + arg)
84 elsif arg == '-h' || arg == '--help'
87 elsif arg == '-d' || arg == '--debug'
88 log_level = org.apache.log4j.Level::DEBUG
92 puts "Setting DEBUG log level..."
93 elsif arg == '-n' || arg == '--noninteractive'
97 # Presume it a script. Save it off for running later below
98 # after we've set up some environment.
101 # Presume that any other args are meant for the script.
106 # Delete all processed args
107 found.each { |arg| ARGV.delete(arg) }
108 # Make sure debug flag gets back to IRB
113 # Set logging level to avoid verboseness
114 org.apache.log4j.Logger.getLogger("org.apache.zookeeper").setLevel(log_level)
115 org.apache.log4j.Logger.getLogger("org.apache.hadoop.hbase").setLevel(log_level)
117 # Require HBase now after setting log levels
124 require 'shell/formatter'
126 # Presume console format.
127 # Formatter takes an :output_stream parameter, if you don't want STDOUT.
128 @formatter = Shell::Formatter::Console.new
130 # Setup the HBase module. Create a configuration.
131 @hbase = Hbase::Hbase.new
134 @shell = Shell::Shell.new(@hbase, @formatter, interactive)
135 @shell.debug = @shell_debug
137 # Add commands to this namespace
138 # TODO avoid polluting main namespace by using a binding
139 @shell.export_commands(self)
142 def help(command = nil)
146 # Backwards compatibility method
148 @shell.help_group('tools')
155 conf.back_trace_limit = 0
156 log_level = org.apache.log4j.Level::ERROR
159 conf.back_trace_limit = 100
160 log_level = org.apache.log4j.Level::DEBUG
162 org.apache.log4j.Logger.getLogger("org.apache.zookeeper").setLevel(log_level)
163 org.apache.log4j.Logger.getLogger("org.apache.hadoop.hbase").setLevel(log_level)
168 puts "Debug mode is #{@shell_debug ? 'ON' : 'OFF'}\n\n"
172 # Include hbase constants
173 include HBaseConstants
175 # If script2run, try running it. If we're in interactive mode, will go on to run the shell unless
176 # script calls 'exit' or 'exit 0' or 'exit errcode'.
177 load(script2run) if script2run
180 # Output a banner message that tells users where to go for help
187 def self.start(ap_path = nil)
188 $0 = File::basename(ap_path, ".rb") if ap_path
191 @CONF[:IRB_NAME] = 'hbase'
192 @CONF[:AP_NAME] = 'hbase'
193 @CONF[:BACK_TRACE_LIMIT] = 0 unless $fullBackTrace
196 hirb = HIRB.new(nil, @CONF[:SCRIPT])
201 @CONF[:IRB_RC].call(hirb.context) if @CONF[:IRB_RC]
202 @CONF[:MAIN_CONTEXT] = hirb.context
213 # Noninteractive mode: if there is input on stdin, do a simple REPL.
214 # XXX Note that this purposefully uses STDIN and not Kernel.gets
215 # in order to maintain compatibility with previous behavior where
216 # a user could pass in script2run and then still pipe commands on
218 require "irb/ruby-lex"
219 require "irb/workspace"
220 workspace = IRB::WorkSpace.new(binding())
221 scanner = RubyLex.new
222 scanner.set_input(STDIN)
223 scanner.each_top_level_statement do |statement, linenum|
224 puts(workspace.evaluate(nil, statement, 'stdin', linenum))
226 # XXX We're catching Exception on purpose, because we want to include
227 # unwrapped java exceptions, syntax errors, eval failures, etc.
228 rescue Exception => exception
229 message = exception.to_s
230 # exception unwrapping in shell means we'll have to handle Java exceptions
231 # as a special case in order to format them properly.
232 if exception.kind_of? java.lang.Exception
233 $stderr.puts "java exception"
234 message = exception.get_message
236 # Include the 'ERROR' string to try to make transition easier for scripts that
237 # may have already been relying on grepping output.
238 puts "ERROR #{exception.class}: #{message}"
240 # re-raising the will include a backtrace and exit.