mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Ensure temp file is actually removed
Forgot to add this when I was doing the os.close()
This commit is contained in:
parent
072fd823f7
commit
0bf520e089
1 changed files with 202 additions and 188 deletions
|
@ -1801,110 +1801,117 @@ def scp_file(dest_path, contents=None, kwargs=None, local_file=None):
|
|||
'''
|
||||
Use scp or sftp to copy a file to a server
|
||||
'''
|
||||
if contents is None:
|
||||
file_to_upload = None
|
||||
else:
|
||||
try:
|
||||
tmpfd, file_to_upload = tempfile.mkstemp()
|
||||
os.write(tmpfd, contents)
|
||||
finally:
|
||||
file_to_upload = None
|
||||
try:
|
||||
if contents is not None:
|
||||
try:
|
||||
os.close(tmpfd)
|
||||
except OSError as exc:
|
||||
if exc.errno != errno.EBADF:
|
||||
raise exc
|
||||
tmpfd, file_to_upload = tempfile.mkstemp()
|
||||
os.write(tmpfd, contents)
|
||||
finally:
|
||||
try:
|
||||
os.close(tmpfd)
|
||||
except OSError as exc:
|
||||
if exc.errno != errno.EBADF:
|
||||
raise exc
|
||||
|
||||
log.debug('Uploading {0} to {1}'.format(dest_path, kwargs['hostname']))
|
||||
log.debug('Uploading {0} to {1}'.format(dest_path, kwargs['hostname']))
|
||||
|
||||
ssh_args = [
|
||||
# Don't add new hosts to the host key database
|
||||
'-oStrictHostKeyChecking=no',
|
||||
# Set hosts key database path to /dev/null, i.e., non-existing
|
||||
'-oUserKnownHostsFile=/dev/null',
|
||||
# Don't re-use the SSH connection. Less failures.
|
||||
'-oControlPath=none'
|
||||
]
|
||||
ssh_args = [
|
||||
# Don't add new hosts to the host key database
|
||||
'-oStrictHostKeyChecking=no',
|
||||
# Set hosts key database path to /dev/null, i.e., non-existing
|
||||
'-oUserKnownHostsFile=/dev/null',
|
||||
# Don't re-use the SSH connection. Less failures.
|
||||
'-oControlPath=none'
|
||||
]
|
||||
|
||||
if local_file is not None:
|
||||
file_to_upload = local_file
|
||||
if os.path.isdir(local_file):
|
||||
ssh_args.append('-r')
|
||||
if local_file is not None:
|
||||
file_to_upload = local_file
|
||||
if os.path.isdir(local_file):
|
||||
ssh_args.append('-r')
|
||||
|
||||
if 'key_filename' in kwargs:
|
||||
# There should never be both a password and an ssh key passed in, so
|
||||
ssh_args.extend([
|
||||
# tell SSH to skip password authentication
|
||||
'-oPasswordAuthentication=no',
|
||||
'-oChallengeResponseAuthentication=no',
|
||||
# Make sure public key authentication is enabled
|
||||
'-oPubkeyAuthentication=yes',
|
||||
# do only use the provided identity file
|
||||
'-oIdentitiesOnly=yes',
|
||||
# No Keyboard interaction!
|
||||
'-oKbdInteractiveAuthentication=no',
|
||||
# Also, specify the location of the key file
|
||||
'-i {0}'.format(kwargs['key_filename'])
|
||||
])
|
||||
if 'key_filename' in kwargs:
|
||||
# There should never be both a password and an ssh key passed in, so
|
||||
ssh_args.extend([
|
||||
# tell SSH to skip password authentication
|
||||
'-oPasswordAuthentication=no',
|
||||
'-oChallengeResponseAuthentication=no',
|
||||
# Make sure public key authentication is enabled
|
||||
'-oPubkeyAuthentication=yes',
|
||||
# do only use the provided identity file
|
||||
'-oIdentitiesOnly=yes',
|
||||
# No Keyboard interaction!
|
||||
'-oKbdInteractiveAuthentication=no',
|
||||
# Also, specify the location of the key file
|
||||
'-i {0}'.format(kwargs['key_filename'])
|
||||
])
|
||||
|
||||
if 'port' in kwargs:
|
||||
ssh_args.append('-oPort={0}'.format(kwargs['port']))
|
||||
if 'port' in kwargs:
|
||||
ssh_args.append('-oPort={0}'.format(kwargs['port']))
|
||||
|
||||
if 'ssh_gateway' in kwargs:
|
||||
ssh_gateway = kwargs['ssh_gateway']
|
||||
ssh_gateway_port = 22
|
||||
ssh_gateway_key = ''
|
||||
ssh_gateway_user = 'root'
|
||||
if ':' in ssh_gateway:
|
||||
ssh_gateway, ssh_gateway_port = ssh_gateway.split(':')
|
||||
if 'ssh_gateway_port' in kwargs:
|
||||
ssh_gateway_port = kwargs['ssh_gateway_port']
|
||||
if 'ssh_gateway_key' in kwargs:
|
||||
ssh_gateway_key = '-i {0}'.format(kwargs['ssh_gateway_key'])
|
||||
if 'ssh_gateway_user' in kwargs:
|
||||
ssh_gateway_user = kwargs['ssh_gateway_user']
|
||||
if 'ssh_gateway' in kwargs:
|
||||
ssh_gateway = kwargs['ssh_gateway']
|
||||
ssh_gateway_port = 22
|
||||
ssh_gateway_key = ''
|
||||
ssh_gateway_user = 'root'
|
||||
if ':' in ssh_gateway:
|
||||
ssh_gateway, ssh_gateway_port = ssh_gateway.split(':')
|
||||
if 'ssh_gateway_port' in kwargs:
|
||||
ssh_gateway_port = kwargs['ssh_gateway_port']
|
||||
if 'ssh_gateway_key' in kwargs:
|
||||
ssh_gateway_key = '-i {0}'.format(kwargs['ssh_gateway_key'])
|
||||
if 'ssh_gateway_user' in kwargs:
|
||||
ssh_gateway_user = kwargs['ssh_gateway_user']
|
||||
|
||||
ssh_args.append(
|
||||
# Setup ProxyCommand
|
||||
'-oProxyCommand="ssh {0} {1} {2} {3} {4}@{5} -p {6} nc -q0 %h %p"'.format(
|
||||
# Don't add new hosts to the host key database
|
||||
'-oStrictHostKeyChecking=no',
|
||||
# Set hosts key database path to /dev/null, i.e., non-existing
|
||||
'-oUserKnownHostsFile=/dev/null',
|
||||
# Don't re-use the SSH connection. Less failures.
|
||||
'-oControlPath=none',
|
||||
ssh_gateway_key,
|
||||
ssh_gateway_user,
|
||||
ssh_gateway,
|
||||
ssh_gateway_port
|
||||
ssh_args.append(
|
||||
# Setup ProxyCommand
|
||||
'-oProxyCommand="ssh {0} {1} {2} {3} {4}@{5} -p {6} nc -q0 %h %p"'.format(
|
||||
# Don't add new hosts to the host key database
|
||||
'-oStrictHostKeyChecking=no',
|
||||
# Set hosts key database path to /dev/null, i.e., non-existing
|
||||
'-oUserKnownHostsFile=/dev/null',
|
||||
# Don't re-use the SSH connection. Less failures.
|
||||
'-oControlPath=none',
|
||||
ssh_gateway_key,
|
||||
ssh_gateway_user,
|
||||
ssh_gateway,
|
||||
ssh_gateway_port
|
||||
)
|
||||
)
|
||||
|
||||
try:
|
||||
if socket.inet_pton(socket.AF_INET6, kwargs['hostname']):
|
||||
ipaddr = '[{0}]'.format(kwargs['hostname'])
|
||||
else:
|
||||
ipaddr = kwargs['hostname']
|
||||
except socket.error:
|
||||
ipaddr = kwargs['hostname']
|
||||
|
||||
if file_to_upload is None:
|
||||
log.warning(
|
||||
'No source file to upload. Please make sure that either file '
|
||||
'contents or the path to a local file are provided.'
|
||||
)
|
||||
cmd = (
|
||||
'scp {0} {1} {2[username]}@{4}:{3} || '
|
||||
'echo "put {1} {3}" | sftp {0} {2[username]}@{4} || '
|
||||
'rsync -avz -e "ssh {0}" {1} {2[username]}@{2[hostname]}:{3}'.format(
|
||||
' '.join(ssh_args), file_to_upload, kwargs, dest_path, ipaddr
|
||||
)
|
||||
)
|
||||
|
||||
try:
|
||||
if socket.inet_pton(socket.AF_INET6, kwargs['hostname']):
|
||||
ipaddr = '[{0}]'.format(kwargs['hostname'])
|
||||
else:
|
||||
ipaddr = kwargs['hostname']
|
||||
except socket.error:
|
||||
ipaddr = kwargs['hostname']
|
||||
|
||||
if file_to_upload is None:
|
||||
log.warning(
|
||||
'No source file to upload. Please make sure that either file '
|
||||
'contents or the path to a local file are provided.'
|
||||
)
|
||||
cmd = (
|
||||
'scp {0} {1} {2[username]}@{4}:{3} || '
|
||||
'echo "put {1} {3}" | sftp {0} {2[username]}@{4} || '
|
||||
'rsync -avz -e "ssh {0}" {1} {2[username]}@{2[hostname]}:{3}'.format(
|
||||
' '.join(ssh_args), file_to_upload, kwargs, dest_path, ipaddr
|
||||
)
|
||||
)
|
||||
|
||||
log.debug('SCP command: \'{0}\''.format(cmd))
|
||||
retcode = _exec_ssh_cmd(cmd,
|
||||
error_msg='Failed to upload file \'{0}\': {1}\n{2}',
|
||||
password_retries=3,
|
||||
**kwargs)
|
||||
log.debug('SCP command: \'{0}\''.format(cmd))
|
||||
retcode = _exec_ssh_cmd(cmd,
|
||||
error_msg='Failed to upload file \'{0}\': {1}\n{2}',
|
||||
password_retries=3,
|
||||
**kwargs)
|
||||
finally:
|
||||
if contents is not None:
|
||||
try:
|
||||
os.remove(file_to_upload)
|
||||
except OSError as exc:
|
||||
if exc.errno != errno.ENOENT:
|
||||
raise exc
|
||||
return retcode
|
||||
|
||||
|
||||
|
@ -1927,104 +1934,111 @@ def sftp_file(dest_path, contents=None, kwargs=None, local_file=None):
|
|||
if kwargs is None:
|
||||
kwargs = {}
|
||||
|
||||
if contents is None:
|
||||
file_to_upload = None
|
||||
else:
|
||||
try:
|
||||
tmpfd, file_to_upload = tempfile.mkstemp()
|
||||
os.write(tmpfd, contents)
|
||||
finally:
|
||||
try:
|
||||
os.close(tmpfd)
|
||||
except OSError as exc:
|
||||
if exc.errno != errno.EBADF:
|
||||
raise exc
|
||||
|
||||
if local_file is not None:
|
||||
file_to_upload = local_file
|
||||
if os.path.isdir(local_file):
|
||||
put_args = ['-r']
|
||||
|
||||
log.debug('Uploading {0} to {1} (sftp)'.format(dest_path, kwargs.get('hostname')))
|
||||
|
||||
ssh_args = [
|
||||
# Don't add new hosts to the host key database
|
||||
'-oStrictHostKeyChecking=no',
|
||||
# Set hosts key database path to /dev/null, i.e., non-existing
|
||||
'-oUserKnownHostsFile=/dev/null',
|
||||
# Don't re-use the SSH connection. Less failures.
|
||||
'-oControlPath=none'
|
||||
]
|
||||
if 'key_filename' in kwargs:
|
||||
# There should never be both a password and an ssh key passed in, so
|
||||
ssh_args.extend([
|
||||
# tell SSH to skip password authentication
|
||||
'-oPasswordAuthentication=no',
|
||||
'-oChallengeResponseAuthentication=no',
|
||||
# Make sure public key authentication is enabled
|
||||
'-oPubkeyAuthentication=yes',
|
||||
# do only use the provided identity file
|
||||
'-oIdentitiesOnly=yes',
|
||||
# No Keyboard interaction!
|
||||
'-oKbdInteractiveAuthentication=no',
|
||||
# Also, specify the location of the key file
|
||||
'-oIdentityFile={0}'.format(kwargs['key_filename'])
|
||||
])
|
||||
|
||||
if 'port' in kwargs:
|
||||
ssh_args.append('-oPort={0}'.format(kwargs['port']))
|
||||
|
||||
if 'ssh_gateway' in kwargs:
|
||||
ssh_gateway = kwargs['ssh_gateway']
|
||||
ssh_gateway_port = 22
|
||||
ssh_gateway_key = ''
|
||||
ssh_gateway_user = 'root'
|
||||
if ':' in ssh_gateway:
|
||||
ssh_gateway, ssh_gateway_port = ssh_gateway.split(':')
|
||||
if 'ssh_gateway_port' in kwargs:
|
||||
ssh_gateway_port = kwargs['ssh_gateway_port']
|
||||
if 'ssh_gateway_key' in kwargs:
|
||||
ssh_gateway_key = '-i {0}'.format(kwargs['ssh_gateway_key'])
|
||||
if 'ssh_gateway_user' in kwargs:
|
||||
ssh_gateway_user = kwargs['ssh_gateway_user']
|
||||
|
||||
ssh_args.append(
|
||||
# Setup ProxyCommand
|
||||
'-oProxyCommand="ssh {0} {1} {2} {3} {4}@{5} -p {6} nc -q0 %h %p"'.format(
|
||||
# Don't add new hosts to the host key database
|
||||
'-oStrictHostKeyChecking=no',
|
||||
# Set hosts key database path to /dev/null, i.e., non-existing
|
||||
'-oUserKnownHostsFile=/dev/null',
|
||||
# Don't re-use the SSH connection. Less failures.
|
||||
'-oControlPath=none',
|
||||
ssh_gateway_key,
|
||||
ssh_gateway_user,
|
||||
ssh_gateway,
|
||||
ssh_gateway_port
|
||||
)
|
||||
)
|
||||
|
||||
file_to_upload = None
|
||||
try:
|
||||
if socket.inet_pton(socket.AF_INET6, kwargs['hostname']):
|
||||
ipaddr = '[{0}]'.format(kwargs['hostname'])
|
||||
else:
|
||||
ipaddr = kwargs['hostname']
|
||||
except socket.error:
|
||||
ipaddr = kwargs['hostname']
|
||||
if contents is not None:
|
||||
try:
|
||||
tmpfd, file_to_upload = tempfile.mkstemp()
|
||||
os.write(tmpfd, contents)
|
||||
finally:
|
||||
try:
|
||||
os.close(tmpfd)
|
||||
except OSError as exc:
|
||||
if exc.errno != errno.EBADF:
|
||||
raise exc
|
||||
|
||||
if file_to_upload is None:
|
||||
log.warning(
|
||||
'No source file to upload. Please make sure that either file '
|
||||
'contents or the path to a local file are provided.'
|
||||
if local_file is not None:
|
||||
file_to_upload = local_file
|
||||
if os.path.isdir(local_file):
|
||||
put_args = ['-r']
|
||||
|
||||
log.debug('Uploading {0} to {1} (sftp)'.format(dest_path, kwargs.get('hostname')))
|
||||
|
||||
ssh_args = [
|
||||
# Don't add new hosts to the host key database
|
||||
'-oStrictHostKeyChecking=no',
|
||||
# Set hosts key database path to /dev/null, i.e., non-existing
|
||||
'-oUserKnownHostsFile=/dev/null',
|
||||
# Don't re-use the SSH connection. Less failures.
|
||||
'-oControlPath=none'
|
||||
]
|
||||
if 'key_filename' in kwargs:
|
||||
# There should never be both a password and an ssh key passed in, so
|
||||
ssh_args.extend([
|
||||
# tell SSH to skip password authentication
|
||||
'-oPasswordAuthentication=no',
|
||||
'-oChallengeResponseAuthentication=no',
|
||||
# Make sure public key authentication is enabled
|
||||
'-oPubkeyAuthentication=yes',
|
||||
# do only use the provided identity file
|
||||
'-oIdentitiesOnly=yes',
|
||||
# No Keyboard interaction!
|
||||
'-oKbdInteractiveAuthentication=no',
|
||||
# Also, specify the location of the key file
|
||||
'-oIdentityFile={0}'.format(kwargs['key_filename'])
|
||||
])
|
||||
|
||||
if 'port' in kwargs:
|
||||
ssh_args.append('-oPort={0}'.format(kwargs['port']))
|
||||
|
||||
if 'ssh_gateway' in kwargs:
|
||||
ssh_gateway = kwargs['ssh_gateway']
|
||||
ssh_gateway_port = 22
|
||||
ssh_gateway_key = ''
|
||||
ssh_gateway_user = 'root'
|
||||
if ':' in ssh_gateway:
|
||||
ssh_gateway, ssh_gateway_port = ssh_gateway.split(':')
|
||||
if 'ssh_gateway_port' in kwargs:
|
||||
ssh_gateway_port = kwargs['ssh_gateway_port']
|
||||
if 'ssh_gateway_key' in kwargs:
|
||||
ssh_gateway_key = '-i {0}'.format(kwargs['ssh_gateway_key'])
|
||||
if 'ssh_gateway_user' in kwargs:
|
||||
ssh_gateway_user = kwargs['ssh_gateway_user']
|
||||
|
||||
ssh_args.append(
|
||||
# Setup ProxyCommand
|
||||
'-oProxyCommand="ssh {0} {1} {2} {3} {4}@{5} -p {6} nc -q0 %h %p"'.format(
|
||||
# Don't add new hosts to the host key database
|
||||
'-oStrictHostKeyChecking=no',
|
||||
# Set hosts key database path to /dev/null, i.e., non-existing
|
||||
'-oUserKnownHostsFile=/dev/null',
|
||||
# Don't re-use the SSH connection. Less failures.
|
||||
'-oControlPath=none',
|
||||
ssh_gateway_key,
|
||||
ssh_gateway_user,
|
||||
ssh_gateway,
|
||||
ssh_gateway_port
|
||||
)
|
||||
)
|
||||
|
||||
try:
|
||||
if socket.inet_pton(socket.AF_INET6, kwargs['hostname']):
|
||||
ipaddr = '[{0}]'.format(kwargs['hostname'])
|
||||
else:
|
||||
ipaddr = kwargs['hostname']
|
||||
except socket.error:
|
||||
ipaddr = kwargs['hostname']
|
||||
|
||||
if file_to_upload is None:
|
||||
log.warning(
|
||||
'No source file to upload. Please make sure that either file '
|
||||
'contents or the path to a local file are provided.'
|
||||
)
|
||||
cmd = 'echo "put {0} {1} {2}" | sftp {3} {4[username]}@{5}'.format(
|
||||
' '.join(put_args), file_to_upload, dest_path, ' '.join(ssh_args), kwargs, ipaddr
|
||||
)
|
||||
cmd = 'echo "put {0} {1} {2}" | sftp {3} {4[username]}@{5}'.format(
|
||||
' '.join(put_args), file_to_upload, dest_path, ' '.join(ssh_args), kwargs, ipaddr
|
||||
)
|
||||
log.debug('SFTP command: \'{0}\''.format(cmd))
|
||||
retcode = _exec_ssh_cmd(cmd,
|
||||
error_msg='Failed to upload file \'{0}\': {1}\n{2}',
|
||||
password_retries=3,
|
||||
**kwargs)
|
||||
log.debug('SFTP command: \'{0}\''.format(cmd))
|
||||
retcode = _exec_ssh_cmd(cmd,
|
||||
error_msg='Failed to upload file \'{0}\': {1}\n{2}',
|
||||
password_retries=3,
|
||||
**kwargs)
|
||||
finally:
|
||||
if contents is not None:
|
||||
try:
|
||||
os.remove(file_to_upload)
|
||||
except OSError as exc:
|
||||
if exc.errno != errno.ENOENT:
|
||||
raise exc
|
||||
return retcode
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue