def get_required(self, key): value = self.get(key) if value is None: utility.abort('Configuration parameter "%s" was not found.' % key, 'Set parameters using the "config" command, for example:', ['%s config %s=VALUE' % (environment.command_name, key)]) return value
def abort(self, *msgs): """ Display errors (optional) and abort execution. """ utility.error('Fatal error in "%s" command.' % self.verb.name, *msgs) self.help() utility.abort()
def go(self, verb, runner): if self.needs_catalog: if runner.opts.replica: self.subcommand = 'replica' if self.supports_live: if runner.opts.block: final_args = [self.subcommand] else: final_args = ['live', self.subcommand] else: final_args = [self.subcommand] if self.safemode_available: if runner.opts.safemode: final_args.extend(['safemode']) if self.needs_catalog: catalog = runner.opts.catalog if not catalog: catalog = runner.config.get('volt.catalog') if catalog is None: utility.abort('A catalog path is required.') final_args.extend(['catalog', catalog]) if runner.opts.deployment: final_args.extend(['deployment', runner.opts.deployment]) if runner.opts.host: final_args.extend(['host', runner.opts.host]) else: utility.abort('host is required.') if runner.opts.clientport: final_args.extend(['port', runner.opts.clientport]) if runner.opts.adminport: final_args.extend(['adminport', runner.opts.adminport]) if runner.opts.httpport: final_args.extend(['httpport', runner.opts.httpport]) if runner.opts.license: final_args.extend(['license', runner.opts.license]) if runner.opts.internalinterface: final_args.extend(['internalinterface', runner.opts.internalinterface]) if runner.opts.internalport: final_args.extend(['internalport', runner.opts.internalport]) if runner.opts.replicationport: final_args.extend(['replicationport', runner.opts.replicationport]) if runner.opts.zkport: final_args.extend(['zkport', runner.opts.zkport]) if runner.opts.externalinterface: final_args.extend(['externalinterface', runner.opts.externalinterface]) if runner.args: final_args.extend(runner.args) kwargs = {} if self.supports_daemon and runner.opts.daemon: # Provide a default description if not specified. daemon_description = self.daemon_description if daemon_description is None: daemon_description = "VoltDB server" # Initialize all the daemon-related keyword arguments. runner.setup_daemon_kwargs(kwargs, name=self.daemon_name, description=daemon_description, output=self.daemon_output) self.run_java(verb, runner, *final_args, **kwargs)
def __init__(self, name, function, **kwargs): CommandVerb.__init__(self, name, function, **kwargs) self.modifiers = utility.kwargs_get_list(kwargs, 'modifiers', default = []) if not self.modifiers: utility.abort('Multi-command "%s" must provide a "modifiers" list.' % self.name) valid_modifiers = '|'.join([mod.name for mod in self.modifiers]) has_args = 0 rows = [] for mod in self.modifiers: if mod.arg_name: usage = '%s %s [ %s ... ]' % (self.name, mod.name, mod.arg_name) has_args += 1 else: usage = '%s %s' % (self.name, mod.name) rows.append((usage, mod.description)) caption = '"%s" Command Variations' % self.name other_info = utility.format_table(rows, caption = caption, separator = ' ') self.set_defaults(other_info = other_info.strip()) args = [ cli.StringArgument('modifier', 'command modifier (valid modifiers: %s)' % valid_modifiers)] if has_args > 0: if has_args == len(self.modifiers): arg_desc = 'optional arguments(s)' else: arg_desc = 'optional arguments(s) (where applicable)' args.append(cli.StringArgument('arg', arg_desc, min_count = 0, max_count = None)) self.add_arguments(*args)
def go(self, runner): gofound = False for bundle in self.bundles: if hasattr(bundle, "go"): bundle.go(self, runner) gofound = True if not gofound: utility.abort("go() method is not implemented by any bundle or %s." % self.__class__.__name__)
def call_proc(self, sysproc_name, types, args, check_status = True): utility.verbose_info('Call procedure: %s%s' % (sysproc_name, tuple(args))) proc = voltdbclient.VoltProcedure(self.client, sysproc_name, types) response = proc.call(params = args) if check_status and response.status != 1: utility.abort('"%s" procedure call failed.' % sysproc_name, (response,)) utility.verbose_info(response) return utility.VoltResponseWrapper(response)
def postprocess_value(self, value): if type(value) is not int: try: converted = int(value.strip()) except ValueError: utility.abort('Bad "%s" integer value: %s' % (self.get_dest().upper(), value)) return converted return value
def add_verb_options(self, verb): """ Add options for verb command line. """ for option in verb.iter_options(): try: self.add_option(*option.get_option_names(), **option.kwargs) except Exception, e: utility.abort('Exception initializing options for verb "%s".' % verb.name, e)
def go(self): """ Invoke the default function provided by a Verb object. """ if self.default_func is None: utility.abort('Verb "%s" (class %s) does not provide a default go() function.' % (self.verb.name, self.verb.__class__.__name__)) else: self.default_func(self)
def process_outputfile_args(runner): if runner.opts.output and runner.opts.prefix: utility.abort('Cannot specify both --output and --prefix. Please use --output option.') if runner.opts.output: runner.args.extend(['--outputFile=' + runner.opts.output]) elif runner.opts.prefix: utility.warning('Specifying prefix for outputfile name is deprecated. Consider using --output option to specify' ' output file name.') runner.args.extend(['--prefix=' + runner.opts.prefix])
def start(self, verb, runner): try: kwargs = {} if runner.opts.username: kwargs['username'] = runner.opts.username if runner.opts.password: kwargs['password'] = runner.opts.password runner.client = FastSerializer(runner.opts.host.host, runner.opts.host.port, **kwargs) except Exception, e: utility.abort(e)
def collect(runner): if int(runner.opts.days) == 0: utility.abort(' \'0\' is invalid entry for option --days') process_voltdbroot_args(runner) process_outputfile_args(runner) runner.args.extend(['--dryrun=' + str(runner.opts.dryrun), '--skipheapdump=' + str(runner.opts.skipheapdump), '--days=' + str(runner.opts.days), '--force=' + str(runner.opts.force)]) runner.java_execute('org.voltdb.utils.Collector', None, *runner.args)
def call_proc(self, sysproc_name, types, args, check_status = True): if self.client is None: utility.abort('Command is not set up as a client.', 'Add an appropriate admin or client bundle to @VOLT.Command().') utility.verbose_info('Call procedure: %s%s' % (sysproc_name, tuple(args))) proc = voltdbclient.VoltProcedure(self.client, sysproc_name, types) response = proc.call(params = args) if check_status and response.status != 1: utility.abort('"%s" procedure call failed.' % sysproc_name, (response,)) utility.verbose_info(response) return utility.VoltResponseWrapper(response)
def __init__(self, runner, config): self.runner = runner self.config = config if config.source_type != 'mysql': utility.abort('Unsupported source type "%s".' % config.source_type, 'Only "mysql" is valid.') output_files = [config.ddl_file, config.deployment_file, config.run_script] overwrites = [p for p in output_files if os.path.exists(p)] if overwrites and not runner.opts.overwrite: utility.abort('Output files exist, delete or use the overwrite option.', overwrites) utility.FileGenerator.__init__(self, self, **config)
def postprocess_value(self, value): bad = [] converted = [] for v in value.split(','): try: converted.append(int(v.strip())) except ValueError: bad.append(v.strip()) if bad: utility.abort('Bad "%s" integer list value(s):' % self.get_dest().upper(), bad) return converted
def find_resource(self, name, required=False): """ Find a resource file. """ if self.verbspace.scan_dirs: for scan_dir in self.verbspace.scan_dirs: path = os.path.join(scan_dir, name) if os.path.exists(path): return path if required: utility.abort('Resource file "%s" is missing.' % name) return None
def main(command_name, command_dir, version, description, *args, **kwargs): #=============================================================================== """ Called by running script to execute command with command line arguments. """ # The "package" keyword flags when running from a package zip __main__.py. package = utility.kwargs_get_boolean(kwargs, 'package', default=False) # The "standalone" keyword allows environment.py to skip the library search. standalone = utility.kwargs_get_boolean(kwargs, 'standalone', default=False) # The "state_directory" keyword overrides ~/.<command_name> as the # directory used for runtime state files. state_directory = utility.kwargs_get_string(kwargs, 'state_directory', default=None) try: # Pre-scan for verbose, debug, and dry-run options so that early code # can display verbose and debug messages, and obey dry-run. opts = cli.preprocess_options(base_cli_spec.options, args) utility.set_verbose(opts.verbose) utility.set_debug(opts.debug) # Load the configuration and state permanent_path = os.path.join(os.getcwd(), environment.config_name) local_path = os.path.join(os.getcwd(), environment.config_name_local) config = VoltConfig(permanent_path, local_path) # Initialize the environment environment.initialize(standalone, command_name, command_dir, version) # Initialize the state directory (for runtime state files). if state_directory is None: state_directory = '~/.%s' % environment.command_name state_directory = os.path.expandvars(os.path.expanduser(state_directory)) utility.set_state_directory(state_directory) # Search for modules based on both this file's and the calling script's location. verbspace = load_verbspace(command_name, command_dir, config, version, description, package) # Make internal commands available to user commands via runner.verbspace(). internal_verbspaces = {} if command_name not in internal_commands: for internal_command in internal_commands: internal_verbspace = load_verbspace(internal_command, None, config, version, 'Internal "%s" command' % internal_command, package) internal_verbspaces[internal_command] = internal_verbspace # Run the command run_command(verbspace, internal_verbspaces, config, *args) except KeyboardInterrupt: sys.stderr.write('\n') utility.abort('break')
def process_verb_arguments(self, verb, verb_args, verb_opts): """ Validate the verb arguments. Check that required arguments are present and populate verb_opts attributes with scalar values or lists (for trailing arguments with max_count > 1). """ # Add fixed arguments passed in through the decorator to the verb object. args = copy.copy(verb_args) + verb.command_arguments # Set attributes for required arguments. missing = [] exceptions = [] iarg = 0 nargs = verb.get_argument_count() for arg in verb.iter_arguments(): # It's missing if we've exhausted all the arguments before # exhausting all the argument specs, unless it's the last argument # spec and it's optional. if iarg > len(args) or (iarg == len(args) and arg.min_count > 0): if not arg.optional: missing.append((arg.name, arg.help)) else: setattr(verb_opts, arg.name, None) iarg +=1 else: value = None # The last argument can have repeated arguments. If more than # one are allowed the values are put into a list. if iarg == nargs - 1 and arg.max_count > 1: if len(args) - iarg < arg.min_count: utility.abort('A minimum of %d %s arguments are required.' % (arg.min_count, arg.name.upper())) if len(args) - iarg > arg.max_count: utility.abort('A maximum of %d %s arguments are allowed.' % (arg.max_count, arg.name.upper())) # Pass through argument class get() for validation, conversion, etc.. # Skip bad values and report on them at the end. value = [] for v in args[iarg:]: try: value.append(arg.get(v)) except ArgumentException, e: exceptions.append(e) iarg = len(args) elif len(args) > 0: # All other arguments are treated as scalars. # Pass through argument class get() for validation, conversion, etc.. try: value = arg.get(args[iarg]) except ArgumentException, e: exceptions.append(e) iarg += 1
def check_missing_items(type_name, missing_items): #=============================================================================== """ Look at item list with (name, description) pairs and abort with a useful error message if the list isn't empty. """ if missing_items: if len(missing_items) > 1: plural = 's' else: plural = '' fmt = '%%-%ds %%s' % max([len(o) for (o, h) in missing_items]) utility.abort('Missing required %s%s:' % (type_name, plural), (fmt % (o.upper(), h) for (o, h) in missing_items))
def voltdb_connect(self, host, port, username=None, password=None): """ Create a VoltDB client connection. """ self.voltdb_disconnect() try: kwargs = {} if username: kwargs['username'] = username if password: kwargs['password'] = password self.client = FastSerializer(host, port, **kwargs) except Exception, e: utility.abort(e)
def config(runner): bad = [] for arg in runner.opts.keyvalue: if arg.find('=') == -1: bad.append(arg) if bad: utility.abort('Bad arguments (must be KEY=VALUE format):', bad) for arg in runner.opts.keyvalue: key, value = [s.strip() for s in arg.split('=', 1)] # Default to 'volt.' if simple name is given. if key.find('.') == -1: key = 'volt.%s' % key runner.config.set_local(key, value) utility.info('Configuration: %s=%s' % (key, value))
def get(runner): runner.args.extend(['get']) if runner.opts.resource in ('deployment', 'schema', 'classes'): runner.args.extend([runner.opts.resource]) else: utility.abort('Invalid argument \'%s\' for Get command. Valid arguments are deployment, schema and classes' % runner.opts.resource) if runner.opts.output: runner.args.extend(['file', runner.opts.output]) if runner.opts.directory_spec: runner.args.extend(['getvoltdbroot', runner.opts.directory_spec]) if runner.opts.force: runner.args.extend(['forceget']) runner.java_execute('org.voltdb.VoltDB', None, *runner.args)
def test_java_version(output): if 'JAVA_HOME' in os.environ: java = os.path.join(os.environ['JAVA_HOME'], 'bin', 'java') jar = os.path.join(os.environ['JAVA_HOME'], 'bin', 'jar') else: java = utility.find_in_path('java') jar = utility.find_in_path('jar') if not java: utility.abort('Could not find java in environment, set JAVA_HOME or put java in the path.') javaVersion = utility.get_java_version(javaHome=java, verbose=True) if '1.8.' in javaVersion: output['Java'] = ["PASS", javaVersion.strip()] elif len(javaVersion) > 0: output['Java'] = ["FAIL", "Unsupported " + javaVersion + " Check if Java has been installed properly and JAVA_HOME has been setup correctly."] else: output['Java'] = ["FAIL", "Please check if Java has been installed properly and JAVA_HOME has been setup correctly."]
def voltdb_connect(self, host, port, username=None, password=None): """ Create a VoltDB client connection. """ self.voltdb_disconnect() try: kwargs = {} if username: kwargs['username'] = username if password: kwargs['password'] = password else: """ If a username was specified and a password was not, prompt the user for the pwd. """ kwargs['password'] = getpass('Enter your password: ') self.client = FastSerializer(host, port, **kwargs) except Exception, e: utility.abort(e)
def go(self, runner): final_args = [self.server_subcommand] catalog = runner.opts.catalog if not catalog: catalog = runner.config.get('volt.catalog') if catalog is None: utility.abort('A catalog path is required.') final_args.extend(['catalog', catalog]) if runner.opts.deployment: final_args.extend(['deployment', runner.opts.deployment]) if runner.opts.host: final_args.extend(['host', runner.opts.host.host]) if runner.opts.host.port is not None: final_args.extend(['port', runner.opts.host.port]) if runner.opts.license: final_args.extend(['license', runner.opts.license]) if runner.args: final_args.extend(runner.args) self.run_java(runner, *final_args)
def process_voltdbroot_args(runner) : if (runner.opts.directory_spec) and (runner.opts.voltdbroot): utility.abort('Cannot specify both --dir and command line argument. Please use --dir option.') os.environ['PATH'] += os.pathsep + os.pathsep.join(s for s in sys.path if os.path.join('voltdb', 'bin') in s) # If database directory is given, derive voltdbroot path to store results of systemcheck in voltdbroot directory if runner.opts.directory_spec: if os.path.isdir(runner.opts.directory_spec) and os.access(runner.opts.directory_spec, os.R_OK|os.W_OK|os.X_OK): voltdbrootDir = os.path.join(runner.opts.directory_spec, 'voltdbroot') else: utility.abort('Specified database directory is not valid', runner.opts.directory_spec) elif runner.opts.voltdbroot: utility.warning('Specifying voltdbroot directory using command argument is deprecated. Consider using --dir ' 'option to specify database directory.'); voltdbrootDir = runner.opts.voltdbroot else: voltdbrootDir = os.path.join(os.getcwd(), 'voltdbroot') runner.args.extend(['--voltdbroot=' + voltdbrootDir]) performSystemCheck(runner, voltdbrootDir)
def package(self, output_dir_in, force, *args): """ Create python-runnable package/zip file. """ if output_dir_in is None: output_dir = '' else: output_dir = output_dir_in if not os.path.exists(output_dir): os.makedirs(output_dir) if args: # Package other verbspaces. for name in args: if name not in self.internal_verbspaces: utility.abort('Unknown base command "%s" specified for packaging.' % name) verbspace = self.internal_verbspaces[name] self._create_package(output_dir, verbspace.name, verbspace.version, verbspace.description, force) else: # Package the active verbspace. self._create_package(output_dir, self.verbspace.name, self.verbspace.version, self.verbspace.description, force) # Warn for Python version < 2.6. compat_msg = ('''\ The program package requires Python version 2.6 or greater. It will crash with older Python versions that can't detect and run zip packages. If a newer Python is not the default you can run by passing the package file to an explicit python version, e.g. python2.6 %s''' % self.verbspace.name) if sys.version_info[0] == 2 and sys.version_info[1] < 6: utility.warning(compat_msg) # Generate README.<tool> file. readme_path = os.path.join(output_dir, 'README.%s' % self.verbspace.name) readme_file = utility.File(readme_path, mode = 'w') readme_file.open() try: readme_file.write('%s\n\nWARNING: %s\n' % (self.get_usage(), compat_msg)) finally: readme_file.close()
def go(self, verb, runner): if self.needs_catalog: if runner.opts.replica: self.subcommand = 'replica' if self.supports_live: if runner.opts.block: final_args = [self.subcommand] else: final_args = ['live', self.subcommand] else: final_args = [self.subcommand] if self.needs_catalog: catalog = runner.opts.catalog if not catalog: catalog = runner.config.get('volt.catalog') if catalog is None: utility.abort('A catalog path is required.') final_args.extend(['catalog', catalog]) if runner.opts.deployment: final_args.extend(['deployment', runner.opts.deployment]) if runner.opts.host: final_args.extend(['host', runner.opts.host]) else: utility.abort('host is required.') if runner.opts.clientport: final_args.extend(['port', runner.opts.clientport]) if runner.opts.license: final_args.extend(['license', runner.opts.license]) if runner.opts.internalinterface: final_args.extend(['internalinterface', runner.opts.internalinterface]) if runner.opts.internalport: final_args.extend(['internalport', runner.opts.internalport]) if runner.opts.zkport: final_args.extend(['zkport', runner.opts.zkport]) if runner.opts.externalinterface: final_args.extend(['externalinterface', runner.opts.externalinterface]) if runner.args: final_args.extend(runner.args) self.run_java(verb, runner, *final_args)
def package(self, output_dir_in, force, *args): """ Create python-runnable package/zip file. """ if output_dir_in is None: output_dir = '' else: output_dir = output_dir_in if not os.path.exists(output_dir): os.makedirs(output_dir) if args: # Package other verbspaces. for name in args: if name not in self.internal_verbspaces: utility.abort('Unknown base command "%s" specified for packaging.' % name) verbspace = self.internal_verbspaces[name] self._create_package(output_dir, verbspace.name, verbspace.version, verbspace.description, force) else: # Package the active verbspace. self._create_package(output_dir, self.verbspace.name, self.verbspace.version, self.verbspace.description, force) # Warn for Python version < 2.6. compat_msg = compatibility_warning % dict(name = self.verbspace.name) if sys.version_info[0] == 2 and sys.version_info[1] < 6: utility.warning(compat_msg) # Generate README.<tool> file. readme_path = os.path.join(output_dir, 'README.%s' % self.verbspace.name) readme_file = utility.File(readme_path, mode = 'w') readme_file.open() try: readme_file.write(readme_template % dict(name = self.verbspace.name, usage = self.get_usage(), warning = compat_msg)) finally: readme_file.close()
def check_argument(cli_spec_arg, is_last): if cli_spec_arg.min_count < 0 or cli_spec_arg.max_count < 0: utility.abort('%s argument (%s) has a negative min or max count declared.' % (self.name, self.cli_spec_arg.name)) if cli_spec_arg.min_count == 0 and cli_spec_arg.max_count == 0: utility.abort('%s argument (%s) has zero min and max counts declared.' % (self.name, self.cli_spec_arg.name)) if not is_last and (cli_spec_arg.min_count != 1 or cli_spec_arg.max_count != 1): utility.abort('%s argument (%s) is not the last argument, ' 'but has min/max counts declared.' % (self.name, self.cli_spec_arg.name))
def _add_verb(self, verb): if verb.name in self.verbs: utility.abort('Verb "%s" is declared more than once.' % verb.name) self.verbs[verb.name] = verb
def go(self, verb, runner): if self.check_environment_config: incompatible_options = checkconfig.test_hard_requirements() for k, v in incompatible_options.items(): state = v[0] if state == 'PASS': pass elif state == "WARN": utility.warning(v[1]) elif state == 'FAIL': if k in checkconfig.skippableRequirements.keys( ) and runner.opts.skip_requirements and checkconfig.skippableRequirements[ k] in runner.opts.skip_requirements: utility.warning(v[1]) else: utility.abort(v[1]) else: utility.error(v[1]) final_args = None if self.subcommand in ('create', 'recover', 'probe'): if runner.opts.replica: final_args = [self.subcommand, 'replica'] if self.supports_live: if runner.opts.block: final_args = [self.subcommand] else: final_args = ['live', self.subcommand] elif final_args == None: final_args = [self.subcommand] if self.safemode_available: if runner.opts.safemode: final_args.extend(['safemode']) if self.needs_catalog: catalog = runner.opts.catalog if not catalog: catalog = runner.config.get('volt.catalog') if not catalog is None: final_args.extend(['catalog', catalog]) if self.is_legacy_verb and runner.opts.deployment: final_args.extend(['deployment', runner.opts.deployment]) if runner.opts.placementgroup: final_args.extend(['placementgroup', runner.opts.placementgroup]) if self.is_legacy_verb and runner.opts.host: final_args.extend(['host', runner.opts.host]) elif not self.subcommand in ('initialize', 'probe'): utility.abort('host is required.') if runner.opts.clientport: final_args.extend(['port', runner.opts.clientport]) if runner.opts.adminport: final_args.extend(['adminport', runner.opts.adminport]) if runner.opts.httpport: final_args.extend(['httpport', runner.opts.httpport]) if runner.opts.license: final_args.extend(['license', runner.opts.license]) if runner.opts.internalinterface: final_args.extend( ['internalinterface', runner.opts.internalinterface]) if runner.opts.internalport: final_args.extend(['internalport', runner.opts.internalport]) if runner.opts.replicationport: final_args.extend(['replicationport', runner.opts.replicationport]) if runner.opts.zkport: final_args.extend(['zkport', runner.opts.zkport]) if runner.opts.externalinterface: final_args.extend( ['externalinterface', runner.opts.externalinterface]) if runner.opts.publicinterface: final_args.extend(['publicinterface', runner.opts.publicinterface]) if self.subcommand in ('create', 'initialize'): if runner.opts.force: final_args.extend(['force']) if self.subcommand in ('create', 'probe', 'recover'): if runner.opts.paused: final_args.extend(['paused']) if runner.args: final_args.extend(runner.args) kwargs = {} if self.supports_daemon and runner.opts.daemon: # Provide a default description if not specified. daemon_description = self.daemon_description if daemon_description is None: daemon_description = "VoltDB server" # Initialize all the daemon-related keyword arguments. runner.setup_daemon_kwargs(kwargs, name=self.daemon_name, description=daemon_description, output=self.daemon_output) else: # Replace the Python process. kwargs['exec'] = True self.run_java(verb, runner, *final_args, **kwargs)
def get(self, value): utility.abort( 'BaseArgument subclass must implement a get(value) method: %s' % self.__class__.__name__)
def on_format_epilog(self): utility.abort( 'ExtendedHelpOptionParser subclass must override on_format_epilog(): %s' % self.__class__.__name__)
def get_dest(self): if 'dest' not in self.kwargs: utility.abort('%s must specify a "dest" property.' % self.__class__.__name__) return self.kwargs['dest']
def postprocess_value(self, value): if value not in self.values: utility.abort( 'EnumOption "%s" value "%s" is not one of the following:' % (self.get_dest(), value), self.values) return value
def execute(self, runner): utility.abort( '%s "%s" object does not implement the required execute() method.' % (self.__class__.__name__, self.name))