This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
/
apt_repository.py
182 lines (146 loc) · 4.89 KB
/
apt_repository.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# -*- coding: utf-8 -*-
# author: Bruno Clermont <patate@fastmail.cn>
'''
APT repository states
=====================
Handle Debian, Ubuntu and other Debian based distribution APT repositories
'''
from __future__ import absolute_import
import urlparse
from salt import exceptions, utils
def __virtual__():
'''
Verify apt is installed.
'''
try:
utils.check_or_die('apt-key')
return 'apt_repository'
except exceptions.CommandNotFoundError:
return False
def present(address, components, distribution=None, source=False, key_id=None,
key_server=None, in_sources_list_d=True, filename=None):
'''
Manage a APT repository such as an Ubuntu PPA
.. code-block:: yaml
rabbitmq-server:
apt_repository:
- present
- address: http://www.rabbitmq.com/debian/
- components:
- main
- distribution: testing
- key_server: pgp.mit.edu
- key_id: 056E8E56
address
Repository address, usually a HTTP or HTTPs URL
components
List of repository components, such as 'main'
distribution:
Set this to use a different distribution than the one the host that run
this state.
source
Add source "deb-src" statement? not the default.
key_id
GnuPG/PGP key ID used to authenticate packages of this repository.
key_server
The address of the PGP key server.
This argument is ignored if key_id is unset.
in_sources_list_d
In many distribution, there is a directory /etc/apt/sources.list.d/
that is included when you run apt-get command.
Create a file there instead of change /etc/apt/sources.list
This is used by default.
'''
if distribution is None:
distribution = __salt__['grains.item']('oscodename')['oscodename']
if filename is None:
url = urlparse.urlparse(address)
if not url.scheme:
return {'name': address, 'result': False, 'changes': {},
'comment': "Invalid address '{0}'".format(address)}
filename = '-'.join((
# address without port
url.netloc.split(':')[0],
# path with _ instead of /
url.path.lstrip('/').rstrip('/').replace('/', '_'),
distribution
))
# deb http://ppa.launchpad.net/mercurial-ppa/releases/ubuntu precise main
# without the deb
line_content = [address, distribution]
line_content.extend(components)
if in_sources_list_d:
apt_file = '/etc/apt/sources.list.d/{0}.list'.format(filename)
else:
apt_file = '/etc/apt/sources.list'
text = [' '.join(['deb'] + line_content)]
if source:
text.append(' '.join(['deb-src'] + line_content))
data = {
filename: {
'file': [
'append',
{
'name': apt_file
},
{
'text': text
},
{
'makedirs': True
}
]
}
}
if key_id:
add_command = ['apt-key', 'adv', '--recv-keys']
if key_server:
add_command.extend(['--keyserver', key_server])
add_command.extend([key_id])
data[filename]['cmd'] = [
'run',
{'name': ' '.join(add_command)},
{'unless': 'apt-key list | grep -q {0}'.format(key_id)}
]
output = __salt__['state.high'](data)
file_result, cmd_result = output.values()
ret = {
'name': filename,
'result': file_result['result'] == cmd_result['result'] is True,
'changes': file_result['changes'],
'comment': ' and '.join((file_result['comment'], cmd_result['comment']))
}
if ret['result'] and ret['changes']:
__salt__['pkg.refresh_db']()
ret['changes'].update(cmd_result['changes'])
return ret
def ubuntu_ppa(user, name, key_id, source=False, distribution=None):
'''
Manage an Ubuntu PPA repository
user
Launchpad username
name
Repository name owned by this user
key_id
Launchpad PGP key ID
source
Add source "deb-src" statement? not the default.
distribution:
Set this to use a different Ubuntu distribution than the host that run
this state.
For this PPA: https://launchpad.net/~pitti/+archive/postgresql
the state must be:
.. code-block:: yaml
postgresql:
apt_repository.ubuntu_ppa:
- user: pitti
- name: postgresql
- key_id: 8683D8A2
'''
address = 'http://ppa.launchpad.net/{0}/{1}/ubuntu'.format(user, name)
filename = '{0}-{1}-{2}'.format(
user, name,
__salt__['grains.item']('lsb_codename')['lsb_codename']
)
return present(address, ('main',), distribution, source, key_id,
'keyserver.ubuntu.com', True, filename)