Port layman to fetch_url

reviewable/pr18780/r1
Toshio Kuratomi 9 years ago
parent 79173ac18d
commit e08a2e84da

@ -20,7 +20,6 @@
import shutil import shutil
from os import path from os import path
from urllib2 import Request, urlopen, URLError
DOCUMENTATION = ''' DOCUMENTATION = '''
--- ---
@ -52,6 +51,15 @@ options:
required: false required: false
default: present default: present
choices: [present, absent, updated] 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 = ''' EXAMPLES = '''
@ -89,11 +97,12 @@ def init_layman(config=None):
:param config: the layman's configuration to use (optional) :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) return LaymanAPI(config)
def download_url(url, dest): def download_url(module, url, dest):
''' '''
:param url: the URL to download :param url: the URL to download
:param dest: the absolute path of where to save the downloaded content to; :param dest: the absolute path of where to save the downloaded content to;
@ -101,14 +110,13 @@ def download_url(url, dest):
:raises ModuleError :raises ModuleError
''' '''
request = Request(url)
request.add_header('User-agent', USERAGENT)
try: # Hack to add params in the form that fetch_url expects
response = urlopen(request) module.params['http_agent'] = USERAGENT
except URLError, e: response, info = fetch_url(module, url)
raise ModuleError("Failed to get %s: %s" % (url, str(e))) if info['status'] != 200:
raise ModuleError("Failed to get %s: %s" % (url, info['msg']))
try: try:
with open(dest, 'w') as f: with open(dest, 'w') as f:
shutil.copyfileobj(response, f) shutil.copyfileobj(response, f)
@ -116,7 +124,7 @@ def download_url(url, dest):
raise ModuleError("Failed to write: %s" % str(e)) 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, '''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 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 fetched and saved under ``%(overlay_defs)/%(name.xml)`` (location of the
@ -138,18 +146,20 @@ def install_overlay(name, list_url=None):
return False return False
if not layman.is_repo(name): 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) "overlays and URL of the remote list was not provided." % name)
overlay_defs = layman_conf.get_option('overlay_defs') overlay_defs = layman_conf.get_option('overlay_defs')
dest = path.join(overlay_defs, name + '.xml') dest = path.join(overlay_defs, name + '.xml')
download_url(list_url, dest) download_url(module, list_url, dest)
# reload config # reload config
layman = init_layman() 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 return True
@ -201,11 +211,12 @@ def sync_overlays():
def main(): def main():
# define module # define module
module = AnsibleModule( module = AnsibleModule(
argument_spec = { argument_spec = dict(
'name': { 'required': True }, name = dict(required=True),
'list_url': { 'aliases': ['url'] }, list_url = dict(aliases=['url']),
'state': { 'default': "present", 'choices': ['present', 'absent', 'updated'] }, state = dict(default="present", choices=['present', 'absent', 'updated']),
} validate_certs = dict(required=False, default=True, type='bool'),
)
) )
if not HAS_LAYMAN_API: if not HAS_LAYMAN_API:
@ -216,12 +227,12 @@ def main():
changed = False changed = False
try: try:
if state == 'present': if state == 'present':
changed = install_overlay(name, url) changed = install_overlay(module, name, url)
elif state == 'updated': elif state == 'updated':
if name == 'ALL': if name == 'ALL':
sync_overlays() sync_overlays()
elif install_overlay(name, url): elif install_overlay(module, name, url):
changed = True changed = True
else: else:
sync_overlay(name) sync_overlay(name)
@ -236,4 +247,6 @@ def main():
# import module snippets # import module snippets
from ansible.module_utils.basic import * from ansible.module_utils.basic import *
main() from ansible.module_utils.urls import *
if __name__ == '__main__':
main()

Loading…
Cancel
Save