def test_host_string_denormalization(self): username = _get_system_username() for description, string1, string2 in ( ("Sanity check: equal strings remain equal", "localhost", "localhost"), ("Empty username is same as get_system_username", "localhost:22", username + "@localhost:22"), ("Empty port is same as port 22", "user@localhost", "user@localhost:22"), ("Both username and port", "localhost", username + "@localhost:22"), ("IPv6 address", "2001:DB8::1", username + "@[2001:DB8::1]:22"), ): eq_.description = "Host-string denormalization: %s" % description yield eq_, denormalize(string1), denormalize(string2) del eq_.description
def disconnect(self, log=False): for key in connections.keys(): if output.status: connections[key].close() del connections[key] if output.status: if log: self._logger.info('Disconnected from {0}'.format( denormalize(key))) else: self._logger.warning( 'Failed to disconnect from {0}'.format( denormalize(key)))
def test_host_string_denormalization(): username = _get_system_username() for description, string1, string2 in ( ("Sanity check: equal strings remain equal", 'localhost', 'localhost'), ("Empty username is same as get_system_username", 'localhost:22', username + '@localhost:22'), ("Empty port is same as port 22", 'user@localhost', 'user@localhost:22'), ("Both username and port", 'localhost', username + '@localhost:22'), ): eq_.description = "Host-string denormalization: %s" % description yield eq_, denormalize(string1), denormalize(string2) del eq_.description
def test_host_string_denormalization(self): username = _get_system_username() for description, string1, string2 in ( ("Sanity check: equal strings remain equal", 'localhost', 'localhost'), ("Empty username is same as get_system_username", 'localhost:22', username + '@localhost:22'), ("Empty port is same as port 22", 'user@localhost', 'user@localhost:22'), ("Both username and port", 'localhost', username + '@localhost:22'), ): eq_.description = "Host-string denormalization: %s" % description yield eq_, denormalize(string1), denormalize(string2) del eq_.description
def main(): """ Main command-line execution loop. """ try: # Parse command line options parser, options, arguments = parse_options() # Handle regular args vs -- args arguments = parser.largs remainder_arguments = parser.rargs # Update env with any overridden option values # NOTE: This needs to remain the first thing that occurs # post-parsing, since so many things hinge on the values in env. for option in env_options: state.env[option.dest] = getattr(options, option.dest) # Handle --hosts, --roles (comma separated string => list) for key in ['hosts', 'roles']: if key in state.env and isinstance(state.env[key], str): state.env[key] = state.env[key].split(',') # Handle output control level show/hide update_output_levels(show=options.show, hide=options.hide) # Handle version number option if options.show_version: print("Fabric %s" % state.env.version) sys.exit(0) # Load settings from user settings file, into shared env dict. state.env.update(load_settings(state.env.rcfile)) # Find local fabfile path or abort fabfile = find_fabfile() if not fabfile: abort("Couldn't find any fabfiles!") # Store absolute path to fabfile in case anyone needs it state.env.real_fabfile = fabfile # Load fabfile (which calls its module-level code, including # tweaks to env values) and put its commands in the shared commands # dict commands.update(load_fabfile(fabfile)) # Abort if no commands found if not commands and not remainder_arguments: abort("Fabfile didn't contain any commands!") # Now that we're settled on a fabfile, inform user. if state.output.debug: print("Using fabfile '%s'" % fabfile) # Handle list-commands option (now that commands are loaded) if options.list_commands: list_commands() # Handle show (command-specific help) option if options.display: display_command(options.display) # If user didn't specify any commands to run, show help if not (arguments or remainder_arguments): parser.print_help() sys.exit(0) # Or should it exit with error (1)? # Parse arguments into commands to run (plus args/kwargs/hosts) commands_to_run = parse_arguments(arguments) # Parse remainders into a faux "command" to execute remainder_command = parse_remainder(remainder_arguments) # Figure out if any specified task names are invalid unknown_commands = [] for tup in commands_to_run: if tup[0] not in commands: unknown_commands.append(tup[0]) # Abort if any unknown commands were specified if unknown_commands: abort("Command(s) not found:\n%s" \ % indent(unknown_commands)) # Generate remainder command and insert into commands, commands_to_run if remainder_command: r = '<remainder>' commands[r] = lambda: api.run(remainder_command) commands_to_run.append((r, [], {}, [], [])) # At this point all commands must exist, so execute them in order. for name, args, kwargs, cli_hosts, cli_roles in commands_to_run: # Get callable by itself command = commands[name] # Set current command name (used for some error messages) state.env.command = name # Set host list (also copy to env) state.env.all_hosts = hosts = get_hosts( command, cli_hosts, cli_roles) # If hosts found, execute the function on each host in turn for host in hosts: username, hostname, port = normalize(host) state.env.host_string = host state.env.host = hostname # Preserve user prev_user = state.env.user state.env.user = username state.env.port = port # Log to stdout if state.output.running: print("[%s] Executing task '%s'" % (host, name)) # Actually run command commands[name](*args, **kwargs) # Put old user back state.env.user = prev_user # If no hosts found, assume local-only and run once if not hosts: commands[name](*args, **kwargs) # If we got here, no errors occurred, so print a final note. if state.output.status: print("\nDone.") except SystemExit: # a number of internal functions might raise this one. raise except KeyboardInterrupt: if state.output.status: print >> sys.stderr, "\nStopped." sys.exit(1) except: sys.excepthook(*sys.exc_info()) # we might leave stale threads if we don't explicitly exit() sys.exit(1) finally: # Explicitly disconnect from all servers for key in connections.keys(): if state.output.status: print "Disconnecting from %s..." % denormalize(key), connections[key].close() if state.output.status: print "done." sys.exit(0)