29
29
- Add or remove an APT repositories in Ubuntu and Debian.
31
- This module works on Debian and Ubuntu and requires C(python-apt) and C(python-pycurl) packages.
31
- This module works on Debian and Ubuntu and requires C(python-apt).
32
32
- This module supports Debian Squeeze (version 6) as well as its successors.
33
33
- This module treats Debian and Ubuntu distributions separately. So PPA could be installed only on Ubuntu machines.
45
45
- A source string state.
50
- The octal mode for newly created files in sources.list.d
48
- Run the equivalent of C(apt-get update) if has changed.
54
- Run the equivalent of C(apt-get update) when a change occurs. Cache updates are run after making changes.
51
57
choices: [ "yes", "no" ]
52
58
author: Alexander Saltanov
53
59
version_added: "0.7"
54
requirements: [ python-apt, python-pycurl ]
60
requirements: [ python-apt ]
87
89
except ImportError:
88
90
HAVE_PYTHON_APT = False
96
93
VALID_SOURCE_TYPES = ('deb', 'deb-src')
103
def body_callback(self, buf):
104
self.contents = self.contents + buf
95
def install_python_apt(module):
97
if not module.check_mode:
98
apt_get_path = module.get_bin_path('apt-get')
100
rc, so, se = module.run_command('%s update && %s install python-apt -y -q' % (apt_get_path, apt_get_path))
107
106
class InvalidSource(Exception):
140
139
def _suggest_filename(self, line):
141
140
def _cleanup_filename(s):
142
141
return '_'.join(re.sub('[^a-zA-Z0-9]', ' ', s).split())
142
def _strip_username_password(s):
144
148
# Drop options and protocols.
145
149
line = re.sub('\[[^\]]+\]', '', line)
146
150
line = re.sub('\w+://', '', line)
152
# split line into valid keywords
148
153
parts = [part for part in line.split() if part not in VALID_SOURCE_TYPES]
155
# Drop usernames and passwords
156
parts[0] = _strip_username_password(parts[0])
149
158
return '%s.list' % _cleanup_filename(' '.join(parts[:1]))
151
160
def _parse(self, line, raise_if_invalid_or_disabled=False):
215
224
d, fn = os.path.split(filename)
216
225
fd, tmp_path = tempfile.mkstemp(prefix=".%s-" % fn, dir=d)
217
os.chmod(os.path.join(fd, tmp_path), 0644)
227
# allow the user to override the default mode
228
this_mode = module.params['mode']
229
module.set_mode_if_different(tmp_path, this_mode, False)
219
231
f = os.fdopen(fd, 'w')
220
232
for n, valid, enabled, source, comment in sources:
291
303
class UbuntuSourcesList(SourcesList):
293
LP_API = 'https://launchpad.net/api/1.0/~%s/+archive/%s'
305
LP_API = 'https://launchpad.net/api/1.0/~%s/+archive/%s'
295
def __init__(self, add_ppa_signing_keys_callback=None):
307
def __init__(self, module, add_ppa_signing_keys_callback=None):
296
309
self.add_ppa_signing_keys_callback = add_ppa_signing_keys_callback
297
310
super(UbuntuSourcesList, self).__init__()
299
312
def _get_ppa_info(self, owner_name, ppa_name):
300
# we can not use urllib2 here as it does not do cert verification
301
313
lp_api = self.LP_API % (owner_name, ppa_name)
302
return self._get_ppa_info_curl(lp_api)
304
def _get_ppa_info_curl(self, lp_api):
305
callback = CurlCallback()
307
curl.setopt(pycurl.SSL_VERIFYPEER, 1)
308
curl.setopt(pycurl.SSL_VERIFYHOST, 2)
309
curl.setopt(pycurl.WRITEFUNCTION, callback.body_callback)
310
curl.setopt(pycurl.URL, str(lp_api))
311
curl.setopt(pycurl.HTTPHEADER, ["Accept: application/json"])
314
lp_page = callback.contents
315
return json.loads(lp_page)
315
headers = dict(Accept='application/json')
316
response, info = fetch_url(self.module, lp_api, headers=headers)
317
return json.load(response)
317
319
def _expand_ppa(self, path):
318
320
ppa = path.split(':')[1]
363
365
argument_spec=dict(
364
366
repo=dict(required=True),
365
367
state=dict(choices=['present', 'absent'], default='present'),
368
mode=dict(required=False, default=0644),
366
369
update_cache = dict(aliases=['update-cache'], type='bool', default='yes'),
370
# this should not be needed, but exists as a failsafe
371
install_python_apt=dict(required=False, default="yes", type='bool'),
368
373
supports_check_mode=True,
371
if not HAVE_PYTHON_APT:
372
module.fail_json(msg='Could not import python modules: apt_pkg. Please install python-apt package.')
375
module.fail_json(msg='Could not import python modules: pycurl. Please install python-pycurl package.')
376
params = module.params
377
if params['install_python_apt'] and not HAVE_PYTHON_APT and not module.check_mode:
378
install_python_apt(module)
377
380
repo = module.params['repo']
378
381
state = module.params['state']
380
383
sourceslist = None
382
385
if isinstance(distro, aptsources.distro.UbuntuDistribution):
383
sourceslist = UbuntuSourcesList(add_ppa_signing_keys_callback=get_add_ppa_signing_key_callback(module))
386
sourceslist = UbuntuSourcesList(module,
387
add_ppa_signing_keys_callback=get_add_ppa_signing_key_callback(module))
384
388
elif isinstance(distro, aptsources.distro.DebianDistribution) or \
385
389
isinstance(distro, aptsources.distro.Distribution):
386
390
sourceslist = SourcesList()