76
76
default: ['https://updates.jenkins.io', 'http://mirrors.jenkins.io']
77
77
updates_url_username:
78
78
description:
79
- - If using a custom O(updates_url), set this as the username of the user with access to the url .
79
+ - If using a custom O(updates_url), set this as the username of the user with access to the URL .
80
80
- If the custom O(updates_url) does not require authentication, this can be left empty.
81
81
type: str
82
+ version_added: 11.1.0
82
83
updates_url_password:
83
84
description:
84
- - If using a custom O(updates_url), set this as the password of the user with access to the url .
85
+ - If using a custom O(updates_url), set this as the password of the user with access to the URL .
85
86
- If the custom O(updates_url) does not require authentication, this can be left empty.
86
87
type: str
88
+ version_added: 11.1.0
87
89
update_json_url_segment:
88
90
type: list
89
91
elements: str
122
124
with_dependencies:
123
125
description:
124
126
- Defines whether to install plugin dependencies.
127
+ - In earlier versions, this option had no effect when a specific C(version) was set.
128
+ - Since community.general 11.1.0, dependencies are also installed for versioned plugins.
125
129
type: bool
126
130
default: true
127
131
325
329
import os
326
330
import tempfile
327
331
import time
328
- import base64
329
332
from collections import OrderedDict
330
333
331
334
from ansible .module_utils .basic import AnsibleModule , to_bytes
332
335
from ansible .module_utils .six .moves import http_cookiejar as cookiejar
333
336
from ansible .module_utils .six .moves .urllib .parse import urlencode
334
- from ansible .module_utils .urls import fetch_url , url_argument_spec , open_url
337
+ from ansible .module_utils .urls import fetch_url , url_argument_spec , basic_auth_header
335
338
from ansible .module_utils .six import text_type , binary_type
336
339
from ansible .module_utils .common .text .converters import to_native
337
340
@@ -355,18 +358,14 @@ def __init__(self, module):
355
358
# Authentication for non-Jenkins calls
356
359
self .updates_url_credentials = {}
357
360
if self .params .get ('updates_url_username' ) and self .params .get ('updates_url_password' ):
358
- auth = "{}:{}" .format (self .params ['updates_url_username' ], self .params ['updates_url_password' ]).encode ("utf-8" )
359
- b64_auth = base64 .b64encode (auth ).decode ("ascii" )
360
- self .updates_url_credentials ["Authorization" ] = "Basic {}" .format (b64_auth )
361
+ self .updates_url_credentials ["Authorization" ] = basic_auth_header (self .params ['updates_url_username' ], self .params ['updates_url_password' ])
361
362
362
363
# Crumb
363
364
self .crumb = {}
364
365
365
366
# Authentication for Jenkins calls
366
367
if self .params .get ('url_username' ) and self .params .get ('url_password' ):
367
- auth = "{}:{}" .format (self .params ['url_username' ], self .params ['url_password' ]).encode ("utf-8" )
368
- b64_auth = base64 .b64encode (auth ).decode ("ascii" )
369
- self .crumb ["Authorization" ] = "Basic {}" .format (b64_auth )
368
+ self .crumb ["Authorization" ] = basic_auth_header (self .params ['url_username' ], self .params ['url_password' ])
370
369
371
370
# Cookie jar for crumb session
372
371
self .cookies = None
@@ -418,16 +417,18 @@ def _get_urls_data(self, urls, what=None, msg_status=None, msg_exception=None, *
418
417
self .module .debug ("fetching url: %s" % url )
419
418
420
419
is_jenkins_call = url .startswith (self .url )
420
+ self .module .params ['force_basic_auth' ] = is_jenkins_call
421
421
422
- response = open_url (
423
- url , timeout = self .timeout ,
424
- cookies = self .cookies if is_jenkins_call else None ,
425
- headers = self . crumb if is_jenkins_call else self . updates_url_credentials , ** kwargs )
426
- if response . getcode () == 200 :
422
+ response , info = fetch_url (
423
+ self . module , url , timeout = self .timeout , cookies = self . cookies ,
424
+ headers = self .crumb if is_jenkins_call else self . updates_url_credentials or self . crumb ,
425
+ ** kwargs )
426
+ if info [ 'status' ] == 200 :
427
427
return response
428
428
else :
429
429
err_msg = ("%s. fetching url %s failed. response code: %s" % (msg_status , url , response .getcode ()))
430
-
430
+ if info ['status' ] > 400 : # extend error message
431
+ err_msg = "%s. response body: %s" % (err_msg , info ['body' ])
431
432
except Exception as e :
432
433
err_msg = "%s. fetching url %s failed. error msg: %s" % (msg_status , url , to_native (e ))
433
434
finally :
@@ -451,19 +452,18 @@ def _get_url_data(
451
452
# Get the URL data
452
453
try :
453
454
is_jenkins_call = url .startswith (self .url )
454
- response = open_url (
455
- url , timeout = self .timeout ,
456
- cookies = self .cookies if is_jenkins_call else None ,
457
- headers = self .crumb if is_jenkins_call else self .updates_url_credentials , ** kwargs )
455
+ self .module .params ['force_basic_auth' ] = is_jenkins_call
456
+
457
+ response , info = fetch_url (
458
+ self .module , url , timeout = self .timeout , cookies = self .cookies ,
459
+ headers = self .crumb if is_jenkins_call else self .updates_url_credentials or self .crumb ,
460
+ ** kwargs )
458
461
459
- if response . getcode () != 200 :
462
+ if info [ 'status' ] != 200 :
460
463
if dont_fail :
461
464
raise FailedInstallingWithPluginManager ("HTTP {}" .format (response .getcode ()))
462
465
else :
463
- self .module .fail_json (
464
- msg = msg_status ,
465
- details = "Received status code {} from {}" .format (response .getcode (), url )
466
- )
466
+ self .module .fail_json (msg = msg_status , details = info ['msg' ], info = info )
467
467
except Exception as e :
468
468
if dont_fail :
469
469
raise FailedInstallingWithPluginManager (e )
@@ -679,6 +679,7 @@ def _get_latest_plugin_urls(self):
679
679
680
680
def _get_latest_compatible_plugin_version (self , plugin_name = None ):
681
681
if not hasattr (self , 'jenkins_version' ):
682
+ self .module .params ['force_basic_auth' ] = True
682
683
resp , info = fetch_url (self .module , self .url )
683
684
raw_version = info .get ("x-jenkins" )
684
685
self .jenkins_version = self .parse_version (raw_version )
@@ -694,9 +695,9 @@ def _get_latest_compatible_plugin_version(self, plugin_name=None):
694
695
else :
695
696
raise FileNotFoundError ("Cache file is outdated." )
696
697
except Exception :
697
- response = open_url ( "https://updates.jenkins.io/current/plugin-versions.json" ) # Get list of plugins and their dependencies
698
+ response , info = fetch_url ( self . module , "https://updates.jenkins.io/current/plugin-versions.json" ) # Get list of plugins and their dependencies
698
699
699
- if response . getcode () != 200 :
700
+ if info [ 'status' ] != 200 :
700
701
self .module .fail_json (msg = "Failed to fetch plugin-versions.json" , details = info )
701
702
702
703
try :
@@ -949,9 +950,6 @@ def main():
949
950
supports_check_mode = True ,
950
951
)
951
952
952
- # Force basic authentication
953
- module .params ['force_basic_auth' ] = True
954
-
955
953
# Convert timeout to float
956
954
try :
957
955
module .params ['timeout' ] = float (module .params ['timeout' ])
0 commit comments