Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _log_connect_error(host, message, data):
logger.error('{0}{1} ({2})'.format(
host.print_prefix,
click.style(message, 'red'),
data,
))
print_output=state.print_output,
return_combined_output=True,
)
except (timeout_error, socket_error, SSHException) as e:
log_host_command_error(
host,
e,
timeout=op_meta['timeout'],
)
# If we failed and have no already printed the stderr, print it
if status is False and not state.print_output:
for type_, line in combined_output_lines:
if type_ == 'stderr':
logger.error('{0}{1}'.format(
host.print_prefix,
click.style(line, 'red'),
))
else:
logger.error('{0}{1}'.format(
host.print_prefix,
line,
))
else:
raise TypeError('{0} is an invalid pyinfra command!'.format(command))
# Break the loop to trigger a failure
if status is False:
break
else:
state.results[host]['commands'] += 1
try:
_get_file(host, temp_file, filename_or_io)
# Ensure that, even if we encounter an error, we (attempt to) remove the
# temporary copy of the file.
finally:
remove_status, _, stderr = run_shell_command(
state, host, 'rm -f {0}'.format(temp_file),
sudo=sudo, sudo_user=sudo_user, su_user=su_user,
print_output=print_output,
**command_kwargs
)
if remove_status is False:
logger.error('File download remove temp error: {0}'.format('\n'.join(stderr)))
return False
else:
_get_file(host, remote_filename, filename_or_io)
if print_output:
print('{0}file downloaded: {1}'.format(host.print_prefix, remote_filename))
return True
def _log_connect_error(host, message, data):
logger.error('{0}{1} ({2})'.format(
host.print_prefix,
click.style(message, 'red'),
data,
))
def log_host_command_error(host, e, timeout=0):
if isinstance(e, timeout_error):
logger.error('{0}{1}'.format(
host.print_prefix,
click.style('Command timed out after {0}s'.format(
timeout,
), 'red'),
))
elif isinstance(e, (socket_error, SSHException)):
logger.error('{0}{1}'.format(
host.print_prefix,
click.style('Command socket/SSH error: {0}'.format(
format_exception(e)), 'red',
),
))
elif isinstance(e, IOError):
logger.error('{0}{1}'.format(
host.print_prefix,
click.style('Command IO error: {0}'.format(
format_exception(e)), 'red',
),
))
# Still here? Re-raise!
else:
# Move it to the su_user if present
if su_user:
command = '{0} && chown {1} {2}'.format(command, su_user, remote_file)
# Otherwise any sudo_user
elif sudo_user:
command = '{0} && chown {1} {2}'.format(command, sudo_user, remote_file)
status, _, stderr = run_shell_command(
state, host, command,
sudo=sudo, sudo_user=sudo_user, su_user=su_user,
print_output=print_output,
)
if status is False:
logger.error('File error: {0}'.format('\n'.join(stderr)))
return False
# No sudo and no su_user, so just upload it!
else:
_put_file(host, filename_or_io, remote_file)
if print_output:
print('{0}file uploaded: {1}'.format(host.print_prefix, remote_file))
# Tuples stand for callbacks & file uploads
if isinstance(command, tuple):
# If first element is function, it's a callback
if isinstance(command[0], FunctionType):
func, args, kwargs = command
try:
status = func(
state, host,
*args, **kwargs
)
# Custom functions could do anything, so expect anything!
except Exception as e:
logger.error('{0}{1}'.format(
host.print_prefix,
click.style(
'Unexpected error in Python callback: {0}'.format(
format_exception(e),
),
'red',
),
))
# Non-function mean files to copy
else:
method_type, first_file, second_file = command
if method_type == 'upload':
method = host.put_file
elif method_type == 'download':