2010-03-26 18:45:48 -04:00
|
|
|
#
|
|
|
|
#
|
|
|
|
# Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
# or more contributor license agreements. See the NOTICE file
|
|
|
|
# distributed with this work for additional information
|
|
|
|
# regarding copyright ownership. The ASF licenses this file
|
|
|
|
# to you under the Apache License, Version 2.0 (the
|
|
|
|
# "License"); you may not use this file except in compliance
|
|
|
|
# with the License. You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
#
|
2008-06-13 01:50:00 -04:00
|
|
|
# File passed to org.jruby.Main by bin/hbase. Pollutes jirb with hbase imports
|
|
|
|
# and hbase commands and then loads jirb. Outputs a banner that tells user
|
|
|
|
# where to find help, shell version, and loads up a custom hirb.
|
2014-08-08 16:54:53 -04:00
|
|
|
#
|
|
|
|
# In noninteractive mode, runs commands from stdin until completion or an error.
|
|
|
|
# On success will exit with status 0, on any problem will exit non-zero. Callers
|
|
|
|
# should only rely on "not equal to 0", because the current error exit code of 1
|
|
|
|
# will likely be updated to diffentiate e.g. invalid commands, incorrect args,
|
|
|
|
# permissions, etc.
|
2008-05-30 15:19:46 -04:00
|
|
|
|
2008-06-13 01:50:00 -04:00
|
|
|
# TODO: Interrupt a table creation or a connection to a bad master. Currently
|
2008-06-18 18:24:34 -04:00
|
|
|
# has to time out. Below we've set down the retries for rpc and hbase but
|
|
|
|
# still can be annoying (And there seem to be times when we'll retry for
|
|
|
|
# ever regardless)
|
2008-06-13 01:50:00 -04:00
|
|
|
# TODO: Add support for listing and manipulating catalog tables, etc.
|
2008-06-17 19:58:05 -04:00
|
|
|
# TODO: Encoding; need to know how to go from ruby String to UTF-8 bytes
|
2008-05-30 01:13:58 -04:00
|
|
|
|
2008-06-12 02:00:35 -04:00
|
|
|
# Run the java magic include and import basic HBase types that will help ease
|
|
|
|
# hbase hacking.
|
2008-05-30 01:13:58 -04:00
|
|
|
include Java
|
|
|
|
|
2008-05-30 15:19:46 -04:00
|
|
|
# Some goodies for hirb. Should these be left up to the user's discretion?
|
|
|
|
require 'irb/completion'
|
2012-05-26 01:56:04 -04:00
|
|
|
require 'pathname'
|
2008-05-30 15:19:46 -04:00
|
|
|
|
2012-05-26 01:56:04 -04:00
|
|
|
# Add the directory names in hbase.jruby.sources commandline option
|
2010-03-11 20:36:04 -05:00
|
|
|
# to the ruby load path so I can load up my HBase ruby modules
|
2012-05-26 01:56:04 -04:00
|
|
|
sources = java.lang.System.getProperty('hbase.ruby.sources')
|
|
|
|
$LOAD_PATH.unshift Pathname.new(sources)
|
2008-06-12 02:00:35 -04:00
|
|
|
|
2010-03-11 20:36:04 -05:00
|
|
|
#
|
|
|
|
# FIXME: Switch args processing to getopt
|
|
|
|
#
|
2008-06-13 01:50:00 -04:00
|
|
|
# See if there are args for this shell. If any, read and then strip from ARGV
|
|
|
|
# so they don't go through to irb. Output shell 'usage' if user types '--help'
|
|
|
|
cmdline_help = <<HERE # HERE document output as shell usage
|
2011-12-28 17:57:30 -05:00
|
|
|
Usage: shell [OPTIONS] [SCRIPTFILE [ARGUMENTS]]
|
|
|
|
|
|
|
|
--format=OPTION Formatter for outputting results.
|
|
|
|
Valid options are: console, html.
|
|
|
|
(Default: console)
|
|
|
|
|
|
|
|
-d | --debug Set DEBUG log levels.
|
|
|
|
-h | --help This help.
|
2014-08-08 16:54:53 -04:00
|
|
|
-n | --noninteractive Do not run within an IRB session
|
|
|
|
and exit with non-zero status on
|
|
|
|
first error.
|
2008-06-12 02:00:35 -04:00
|
|
|
HERE
|
|
|
|
found = []
|
2008-06-18 18:24:34 -04:00
|
|
|
format = 'console'
|
2009-06-25 19:53:04 -04:00
|
|
|
script2run = nil
|
2010-03-11 20:36:04 -05:00
|
|
|
log_level = org.apache.log4j.Level::ERROR
|
2012-10-27 21:37:04 -04:00
|
|
|
@shell_debug = false
|
2014-08-08 16:54:53 -04:00
|
|
|
interactive = true
|
2008-06-12 02:00:35 -04:00
|
|
|
for arg in ARGV
|
2009-06-03 01:11:48 -04:00
|
|
|
if arg =~ /^--format=(.+)/i
|
2017-07-19 13:05:26 -04:00
|
|
|
format = Regexp.last_match(1)
|
2008-06-12 02:00:35 -04:00
|
|
|
if format =~ /^html$/i
|
2017-07-19 13:05:26 -04:00
|
|
|
raise NoMethodError, 'Not yet implemented'
|
2008-06-12 02:00:35 -04:00
|
|
|
elsif format =~ /^console$/i
|
|
|
|
# This is default
|
|
|
|
else
|
2017-07-19 13:05:26 -04:00
|
|
|
raise ArgumentError, 'Unsupported format ' + arg
|
2008-06-12 02:00:35 -04:00
|
|
|
end
|
2008-06-18 18:24:34 -04:00
|
|
|
found.push(arg)
|
2008-06-12 02:00:35 -04:00
|
|
|
elsif arg == '-h' || arg == '--help'
|
2008-06-13 01:50:00 -04:00
|
|
|
puts cmdline_help
|
2008-06-12 02:00:35 -04:00
|
|
|
exit
|
2009-12-07 15:54:58 -05:00
|
|
|
elsif arg == '-d' || arg == '--debug'
|
2010-03-11 20:36:04 -05:00
|
|
|
log_level = org.apache.log4j.Level::DEBUG
|
2009-12-07 15:54:58 -05:00
|
|
|
$fullBackTrace = true
|
2012-10-27 21:37:04 -04:00
|
|
|
@shell_debug = true
|
2014-08-04 15:47:54 -04:00
|
|
|
found.push(arg)
|
2017-07-19 13:05:26 -04:00
|
|
|
puts 'Setting DEBUG log level...'
|
2014-08-08 16:54:53 -04:00
|
|
|
elsif arg == '-n' || arg == '--noninteractive'
|
|
|
|
interactive = false
|
|
|
|
found.push(arg)
|
2008-10-08 20:03:50 -04:00
|
|
|
else
|
2009-06-25 19:53:04 -04:00
|
|
|
# Presume it a script. Save it off for running later below
|
|
|
|
# after we've set up some environment.
|
|
|
|
script2run = arg
|
|
|
|
found.push(arg)
|
2009-07-10 00:20:48 -04:00
|
|
|
# Presume that any other args are meant for the script.
|
|
|
|
break
|
2008-06-12 02:00:35 -04:00
|
|
|
end
|
|
|
|
end
|
2009-12-07 15:54:58 -05:00
|
|
|
|
2010-03-11 20:36:04 -05:00
|
|
|
# Delete all processed args
|
|
|
|
found.each { |arg| ARGV.delete(arg) }
|
2014-08-04 15:47:54 -04:00
|
|
|
# Make sure debug flag gets back to IRB
|
2017-07-19 13:05:26 -04:00
|
|
|
ARGV.unshift('-d') if @shell_debug
|
2008-06-12 02:00:35 -04:00
|
|
|
|
2009-12-07 15:54:58 -05:00
|
|
|
# Set logging level to avoid verboseness
|
2017-07-19 13:05:26 -04:00
|
|
|
org.apache.log4j.Logger.getLogger('org.apache.zookeeper').setLevel(log_level)
|
|
|
|
org.apache.log4j.Logger.getLogger('org.apache.hadoop.hbase').setLevel(log_level)
|
2009-01-29 11:51:26 -05:00
|
|
|
|
2010-03-11 20:36:04 -05:00
|
|
|
# Require HBase now after setting log levels
|
2015-12-28 17:50:50 -05:00
|
|
|
require 'hbase_constants'
|
2009-06-16 23:46:12 -04:00
|
|
|
|
2010-03-11 20:36:04 -05:00
|
|
|
# Load hbase shell
|
|
|
|
require 'shell'
|
2008-06-01 01:19:08 -04:00
|
|
|
|
2010-03-11 20:36:04 -05:00
|
|
|
# Require formatter
|
|
|
|
require 'shell/formatter'
|
2008-06-17 19:58:05 -04:00
|
|
|
|
2010-03-11 20:36:04 -05:00
|
|
|
# Setup the HBase module. Create a configuration.
|
|
|
|
@hbase = Hbase::Hbase.new
|
2008-08-12 14:30:33 -04:00
|
|
|
|
2010-03-11 20:36:04 -05:00
|
|
|
# Setup console
|
2015-12-28 17:50:50 -05:00
|
|
|
@shell = Shell::Shell.new(@hbase, interactive)
|
2013-08-21 16:21:14 -04:00
|
|
|
@shell.debug = @shell_debug
|
2009-01-05 20:12:36 -05:00
|
|
|
|
2010-03-11 20:36:04 -05:00
|
|
|
# Add commands to this namespace
|
2014-08-08 16:54:53 -04:00
|
|
|
# TODO avoid polluting main namespace by using a binding
|
2010-03-11 20:36:04 -05:00
|
|
|
@shell.export_commands(self)
|
2009-01-05 20:12:36 -05:00
|
|
|
|
2010-03-11 20:36:04 -05:00
|
|
|
# Add help command
|
|
|
|
def help(command = nil)
|
|
|
|
@shell.help(command)
|
2009-01-05 20:12:36 -05:00
|
|
|
end
|
|
|
|
|
2010-03-11 20:36:04 -05:00
|
|
|
# Backwards compatibility method
|
|
|
|
def tools
|
|
|
|
@shell.help_group('tools')
|
2009-01-05 20:12:36 -05:00
|
|
|
end
|
|
|
|
|
2010-03-16 13:43:09 -04:00
|
|
|
# Debugging method
|
|
|
|
def debug
|
2012-10-27 21:37:04 -04:00
|
|
|
if @shell_debug
|
|
|
|
@shell_debug = false
|
2010-03-16 13:43:09 -04:00
|
|
|
conf.back_trace_limit = 0
|
2012-10-27 21:37:04 -04:00
|
|
|
log_level = org.apache.log4j.Level::ERROR
|
2010-03-16 13:43:09 -04:00
|
|
|
else
|
2012-10-27 21:37:04 -04:00
|
|
|
@shell_debug = true
|
2010-03-16 13:43:09 -04:00
|
|
|
conf.back_trace_limit = 100
|
2012-10-27 21:37:04 -04:00
|
|
|
log_level = org.apache.log4j.Level::DEBUG
|
2010-03-16 13:43:09 -04:00
|
|
|
end
|
2017-07-19 13:05:26 -04:00
|
|
|
org.apache.log4j.Logger.getLogger('org.apache.zookeeper').setLevel(log_level)
|
|
|
|
org.apache.log4j.Logger.getLogger('org.apache.hadoop.hbase').setLevel(log_level)
|
2010-03-16 13:43:09 -04:00
|
|
|
debug?
|
|
|
|
end
|
|
|
|
|
|
|
|
def debug?
|
2012-10-27 21:37:04 -04:00
|
|
|
puts "Debug mode is #{@shell_debug ? 'ON' : 'OFF'}\n\n"
|
2010-03-16 13:43:09 -04:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2010-03-11 20:36:04 -05:00
|
|
|
# Include hbase constants
|
|
|
|
include HBaseConstants
|
2009-09-22 23:10:49 -04:00
|
|
|
|
2014-08-08 16:54:53 -04:00
|
|
|
# If script2run, try running it. If we're in interactive mode, will go on to run the shell unless
|
2009-09-22 23:10:49 -04:00
|
|
|
# script calls 'exit' or 'exit 0' or 'exit errcode'.
|
|
|
|
load(script2run) if script2run
|
|
|
|
|
2014-08-08 16:54:53 -04:00
|
|
|
if interactive
|
|
|
|
# Output a banner message that tells users where to go for help
|
|
|
|
@shell.print_banner
|
2008-06-01 01:19:08 -04:00
|
|
|
|
2017-07-19 13:05:26 -04:00
|
|
|
require 'irb'
|
2014-08-08 16:54:53 -04:00
|
|
|
require 'irb/hirb'
|
2008-06-01 01:19:08 -04:00
|
|
|
|
2014-08-08 16:54:53 -04:00
|
|
|
module IRB
|
|
|
|
def self.start(ap_path = nil)
|
2017-07-19 13:05:26 -04:00
|
|
|
$0 = File.basename(ap_path, '.rb') if ap_path
|
2008-06-01 01:19:08 -04:00
|
|
|
|
2014-08-08 16:54:53 -04:00
|
|
|
IRB.setup(ap_path)
|
|
|
|
@CONF[:IRB_NAME] = 'hbase'
|
|
|
|
@CONF[:AP_NAME] = 'hbase'
|
|
|
|
@CONF[:BACK_TRACE_LIMIT] = 0 unless $fullBackTrace
|
2010-03-11 20:36:04 -05:00
|
|
|
|
2017-07-19 13:05:26 -04:00
|
|
|
hirb = if @CONF[:SCRIPT]
|
|
|
|
HIRB.new(nil, @CONF[:SCRIPT])
|
|
|
|
else
|
|
|
|
HIRB.new
|
|
|
|
end
|
2008-06-01 01:19:08 -04:00
|
|
|
|
2014-08-08 16:54:53 -04:00
|
|
|
@CONF[:IRB_RC].call(hirb.context) if @CONF[:IRB_RC]
|
|
|
|
@CONF[:MAIN_CONTEXT] = hirb.context
|
2008-06-01 01:19:08 -04:00
|
|
|
|
2014-08-08 16:54:53 -04:00
|
|
|
catch(:IRB_EXIT) do
|
|
|
|
hirb.eval_input
|
|
|
|
end
|
2008-06-01 01:19:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-08 16:54:53 -04:00
|
|
|
IRB.start
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
# Noninteractive mode: if there is input on stdin, do a simple REPL.
|
|
|
|
# XXX Note that this purposefully uses STDIN and not Kernel.gets
|
|
|
|
# in order to maintain compatibility with previous behavior where
|
|
|
|
# a user could pass in script2run and then still pipe commands on
|
|
|
|
# stdin.
|
2017-07-19 13:05:26 -04:00
|
|
|
require 'irb/ruby-lex'
|
|
|
|
require 'irb/workspace'
|
|
|
|
workspace = IRB::WorkSpace.new(binding)
|
2014-08-08 16:54:53 -04:00
|
|
|
scanner = RubyLex.new
|
2017-07-19 13:17:02 -04:00
|
|
|
|
|
|
|
# RubyLex claims to take an IO but really wants an InputMethod
|
|
|
|
module IOExtensions
|
|
|
|
def encoding
|
|
|
|
external_encoding
|
|
|
|
end
|
|
|
|
end
|
|
|
|
IO.include IOExtensions
|
|
|
|
|
2014-08-08 16:54:53 -04:00
|
|
|
scanner.set_input(STDIN)
|
|
|
|
scanner.each_top_level_statement do |statement, linenum|
|
2017-07-19 13:05:26 -04:00
|
|
|
puts(workspace.evaluate(nil, statement, 'stdin', linenum))
|
2014-08-08 16:54:53 -04:00
|
|
|
end
|
|
|
|
# XXX We're catching Exception on purpose, because we want to include
|
|
|
|
# unwrapped java exceptions, syntax errors, eval failures, etc.
|
|
|
|
rescue Exception => exception
|
|
|
|
message = exception.to_s
|
|
|
|
# exception unwrapping in shell means we'll have to handle Java exceptions
|
|
|
|
# as a special case in order to format them properly.
|
2017-07-19 13:05:26 -04:00
|
|
|
if exception.is_a? java.lang.Exception
|
|
|
|
$stderr.puts 'java exception'
|
2014-08-08 16:54:53 -04:00
|
|
|
message = exception.get_message
|
|
|
|
end
|
|
|
|
# Include the 'ERROR' string to try to make transition easier for scripts that
|
|
|
|
# may have already been relying on grepping output.
|
|
|
|
puts "ERROR #{exception.class}: #{message}"
|
|
|
|
if $fullBacktrace
|
|
|
|
# re-raising the will include a backtrace and exit.
|
|
|
|
raise exception
|
|
|
|
else
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|