Merge pull request #740 from ansible/fetch_url-layman

Port layman to fetch_url
This commit is contained in:
Toshio Kuratomi 2015-07-28 11:19:18 -07:00
commit 27e7f03d57

View file

@ -20,7 +20,6 @@
import shutil
from os import path
from urllib2 import Request, urlopen, URLError
DOCUMENTATION = '''
---
@ -52,6 +51,15 @@ options:
required: false
default: present
choices: [present, absent, updated]
validate_certs:
description:
- If C(no), SSL certificates will not be validated. This should only be
set to C(no) when no other option exists. Prior to 1.9.3 the code
defaulted to C(no).
required: false
default: 'yes'
choices: ['yes', 'no']
version_added: '1.9.3'
'''
EXAMPLES = '''
@ -89,11 +97,12 @@ def init_layman(config=None):
:param config: the layman's configuration to use (optional)
'''
if config is None: config = BareConfig(read_configfile=True, quietness=1)
if config is None:
config = BareConfig(read_configfile=True, quietness=1)
return LaymanAPI(config)
def download_url(url, dest):
def download_url(module, url, dest):
'''
:param url: the URL to download
:param dest: the absolute path of where to save the downloaded content to;
@ -101,13 +110,12 @@ def download_url(url, dest):
:raises ModuleError
'''
request = Request(url)
request.add_header('User-agent', USERAGENT)
try:
response = urlopen(request)
except URLError, e:
raise ModuleError("Failed to get %s: %s" % (url, str(e)))
# Hack to add params in the form that fetch_url expects
module.params['http_agent'] = USERAGENT
response, info = fetch_url(module, url)
if info['status'] != 200:
raise ModuleError("Failed to get %s: %s" % (url, info['msg']))
try:
with open(dest, 'w') as f:
@ -116,7 +124,7 @@ def download_url(url, dest):
raise ModuleError("Failed to write: %s" % str(e))
def install_overlay(name, list_url=None):
def install_overlay(module, name, list_url=None):
'''Installs the overlay repository. If not on the central overlays list,
then :list_url of an alternative list must be provided. The list will be
fetched and saved under ``%(overlay_defs)/%(name.xml)`` (location of the
@ -138,18 +146,20 @@ def install_overlay(name, list_url=None):
return False
if not layman.is_repo(name):
if not list_url: raise ModuleError("Overlay '%s' is not on the list of known " \
if not list_url:
raise ModuleError("Overlay '%s' is not on the list of known " \
"overlays and URL of the remote list was not provided." % name)
overlay_defs = layman_conf.get_option('overlay_defs')
dest = path.join(overlay_defs, name + '.xml')
download_url(list_url, dest)
download_url(module, list_url, dest)
# reload config
layman = init_layman()
if not layman.add_repos(name): raise ModuleError(layman.get_errors())
if not layman.add_repos(name):
raise ModuleError(layman.get_errors())
return True
@ -201,11 +211,12 @@ def sync_overlays():
def main():
# define module
module = AnsibleModule(
argument_spec = {
'name': { 'required': True },
'list_url': { 'aliases': ['url'] },
'state': { 'default': "present", 'choices': ['present', 'absent', 'updated'] },
}
argument_spec = dict(
name = dict(required=True),
list_url = dict(aliases=['url']),
state = dict(default="present", choices=['present', 'absent', 'updated']),
validate_certs = dict(required=False, default=True, type='bool'),
)
)
if not HAS_LAYMAN_API:
@ -216,12 +227,12 @@ def main():
changed = False
try:
if state == 'present':
changed = install_overlay(name, url)
changed = install_overlay(module, name, url)
elif state == 'updated':
if name == 'ALL':
sync_overlays()
elif install_overlay(name, url):
elif install_overlay(module, name, url):
changed = True
else:
sync_overlay(name)
@ -236,4 +247,6 @@ def main():
# import module snippets
from ansible.module_utils.basic import *
main()
from ansible.module_utils.urls import *
if __name__ == '__main__':
main()