mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Merge pull request #1546 from dagwieers/get_url-nonthirsty-fix
Don't require thirsty mode for directory destination
This commit is contained in:
commit
612aed55bb
1 changed files with 8 additions and 19 deletions
|
@ -98,24 +98,14 @@ def url_do_get(module, url, dest):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
USERAGENT = 'ansible-httpget'
|
USERAGENT = 'ansible-httpget'
|
||||||
info = dict(url=url)
|
info = dict(url=url, dest=dest)
|
||||||
r = None
|
r = None
|
||||||
actualdest = None
|
|
||||||
|
|
||||||
if os.path.isdir(dest):
|
|
||||||
urlfilename = url_filename(url)
|
|
||||||
actualdest = "%s/%s" % (dest, urlfilename)
|
|
||||||
module.params['path'] = actualdest
|
|
||||||
else:
|
|
||||||
actualdest = dest
|
|
||||||
|
|
||||||
info['actualdest'] = actualdest
|
|
||||||
|
|
||||||
request = urllib2.Request(url)
|
request = urllib2.Request(url)
|
||||||
request.add_header('User-agent', USERAGENT)
|
request.add_header('User-agent', USERAGENT)
|
||||||
|
|
||||||
if os.path.exists(actualdest):
|
if os.path.exists(dest):
|
||||||
t = datetime.datetime.utcfromtimestamp(os.path.getmtime(actualdest))
|
t = datetime.datetime.utcfromtimestamp(os.path.getmtime(dest))
|
||||||
tstamp = t.strftime('%a, %d %b %Y %H:%M:%S +0000')
|
tstamp = t.strftime('%a, %d %b %Y %H:%M:%S +0000')
|
||||||
request.add_header('If-Modified-Since', tstamp)
|
request.add_header('If-Modified-Since', tstamp)
|
||||||
|
|
||||||
|
@ -144,12 +134,11 @@ def url_get(module, url, dest):
|
||||||
|
|
||||||
# TODO: should really handle 304, but how? src file could exist (and be newer) but empty
|
# TODO: should really handle 304, but how? src file could exist (and be newer) but empty
|
||||||
if info['status'] == 304:
|
if info['status'] == 304:
|
||||||
module.exit_json(url=url, dest=info.get('actualdest', dest), changed=False, msg=info.get('msg', ''))
|
module.exit_json(url=url, dest=dest, changed=False, msg=info.get('msg', ''))
|
||||||
|
|
||||||
# create a temporary file and copy content to do md5-based replacement
|
# create a temporary file and copy content to do md5-based replacement
|
||||||
if info['status'] != 200:
|
if info['status'] != 200:
|
||||||
module.fail_json(msg="Request failed", status_code=info['status'], response=info['msg'], url=url)
|
module.fail_json(msg="Request failed", status_code=info['status'], response=info['msg'], url=url, dest=dest)
|
||||||
actualdest = info['actualdest']
|
|
||||||
|
|
||||||
fd, tempname = tempfile.mkstemp()
|
fd, tempname = tempfile.mkstemp()
|
||||||
f = os.fdopen(fd, 'wb')
|
f = os.fdopen(fd, 'wb')
|
||||||
|
@ -187,9 +176,10 @@ def main():
|
||||||
dest = os.path.expanduser(module.params['dest'])
|
dest = os.path.expanduser(module.params['dest'])
|
||||||
thirsty = module.boolean(module.params['thirsty'])
|
thirsty = module.boolean(module.params['thirsty'])
|
||||||
|
|
||||||
if not thirsty:
|
|
||||||
if os.path.isdir(dest):
|
if os.path.isdir(dest):
|
||||||
module.fail_json(msg="non-thirsty mode needs a filename for a destination, not a directory")
|
dest = os.path.join(dest, url_filename(url))
|
||||||
|
|
||||||
|
if not thirsty:
|
||||||
if os.path.exists(dest):
|
if os.path.exists(dest):
|
||||||
module.exit_json(msg="file already exists", dest=dest, url=url, changed=False)
|
module.exit_json(msg="file already exists", dest=dest, url=url, changed=False)
|
||||||
|
|
||||||
|
@ -197,7 +187,6 @@ def main():
|
||||||
tmpsrc, info = url_get(module, url, dest)
|
tmpsrc, info = url_get(module, url, dest)
|
||||||
md5sum_src = None
|
md5sum_src = None
|
||||||
md5sum_dest = None
|
md5sum_dest = None
|
||||||
dest = info['actualdest']
|
|
||||||
|
|
||||||
# raise an error if there is no tmpsrc file
|
# raise an error if there is no tmpsrc file
|
||||||
if not os.path.exists(tmpsrc):
|
if not os.path.exists(tmpsrc):
|
||||||
|
|
Loading…
Reference in a new issue