def ssh(host, opts, command): tries = 0 cmd = ssh_command(opts) + ['-t', '-t', '%s@%s' % (opts.user, host), stringify_command(command)] while True: process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) stdout = process.communicate()[0] code = process.returncode if code != 0: if tries > 2: print_error("SSH failure, returning error") raise Exception(stdout) else: time.sleep(3) tries += 1 else: return
def ssh(host, opts, command): tries = 0 while True: try: return subprocess.check_call( ssh_command(opts) + ['-t', '-t', '%s@%s' % (opts.user, host), stringify_command(command)]) except subprocess.CalledProcessError as e: if (tries > 5): # If this was an ssh failure, provide the user with hints. if e.returncode == 255: raise UsageError( "Failed to SSH to remote host {0}.\n" + "Please check that you have provided the correct --identity-file and " + "--key-pair parameters and try again.".format(host)) else: raise e print >> stderr, \ "Error executing remote command, retrying after 30 seconds: {0}".format(e) time.sleep(30) tries = tries + 1