Add ability to download deb from url

if :// in deb, will try to download from url.
This commit is contained in:
Jonathan Mainguy 2015-12-02 23:24:23 -05:00
parent 1229adb36c
commit b0db35a369

32
packaging/os/apt.py Executable file → Normal file
View file

@ -92,6 +92,7 @@ options:
deb:
description:
- Path to a .deb package on the remote machine.
- If :// in the path, ansible will attempt to download deb before installing. (Version added 2.1)
required: false
version_added: "1.6"
requirements: [ python-apt, aptitude ]
@ -137,6 +138,9 @@ EXAMPLES = '''
# Install the build dependencies for package "foo"
- apt: pkg=foo state=build-dep
# Install a .deb package from the internet.
- apt: deb=https://example.com/python-ppq_0.1-1_all.deb
'''
RETURN = '''
@ -542,6 +546,31 @@ def upgrade(m, mode="yes", force=False, default_release=None,
m.exit_json(changed=False, msg=out, stdout=out, stderr=err)
m.exit_json(changed=True, msg=out, stdout=out, stderr=err)
def download(module, deb):
tempdir = os.path.dirname(__file__)
package = os.path.join(tempdir, str(deb.rsplit('/', 1)[1]))
# When downloading a deb, how much of the deb to download before
# saving to a tempfile (64k)
BUFSIZE = 65536
try:
rsp, info = fetch_url(module, deb)
f = open(package, 'w')
# Read 1kb at a time to save on ram
while True:
data = rsp.read(BUFSIZE)
if data == "":
break # End of file, break while loop
f.write(data)
f.close()
deb = package
except Exception, e:
module.fail_json(msg="Failure downloading %s, %s" % (deb, e))
return deb
def main():
module = AnsibleModule(
argument_spec = dict(
@ -648,6 +677,8 @@ def main():
if p['deb']:
if p['state'] != 'present':
module.fail_json(msg="deb only supports state=present")
if '://' in p['deb']:
p['deb'] = download(module, p['deb'])
install_deb(module, p['deb'], cache,
install_recommends=install_recommends,
force=force_yes, dpkg_options=p['dpkg_options'])
@ -689,6 +720,7 @@ def main():
# import module snippets
from ansible.module_utils.basic import *
from ansible.module_utils.urls import *
if __name__ == "__main__":
main()