forked from fabtools/fabtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deb.py
239 lines (179 loc) · 6.77 KB
/
deb.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
"""
Debian packages
===============
This module provides tools to manage Debian/Ubuntu packages
and repositories.
"""
from fabric.api import hide, run, settings
from fabtools.utils import run_as_root
from fabtools.files import getmtime, is_file
MANAGER = 'DEBIAN_FRONTEND=noninteractive apt-get'
def update_index(quiet=True):
"""
Update APT package definitions.
"""
options = "--quiet --quiet" if quiet else ""
run_as_root("%s %s update" % (MANAGER, options))
def upgrade(safe=True):
"""
Upgrade all packages.
"""
manager = MANAGER
if safe:
cmd = 'upgrade'
else:
cmd = 'dist-upgrade'
run_as_root("%(manager)s --assume-yes %(cmd)s" % locals(), pty=False)
def is_installed(pkg_name):
"""
Check if a package is installed.
"""
with settings(hide('running', 'stdout', 'stderr', 'warnings'), warn_only=True):
res = run("dpkg -s %(pkg_name)s" % locals())
for line in res.splitlines():
if line.startswith("Status: "):
status = line[8:]
if "installed" in status.split(' '):
return True
return False
def install(packages, update=False, options=None, version=None):
"""
Install one or more packages.
If *update* is ``True``, the package definitions will be updated
first, using :py:func:`~fabtools.deb.update_index`.
Extra *options* may be passed to ``apt-get`` if necessary.
Example::
import fabtools
# Update index, then install a single package
fabtools.deb.install('build-essential', update=True)
# Install multiple packages
fabtools.deb.install([
'python-dev',
'libxml2-dev',
])
# Install a specific version
fabtools.deb.install('emacs', version='23.3+1-1ubuntu9')
"""
manager = MANAGER
if update:
update_index()
if options is None:
options = []
if version is None:
version = ''
if version and not isinstance(packages, list):
version = '=' + version
if not isinstance(packages, basestring):
packages = " ".join(packages)
options.append("--quiet")
options.append("--assume-yes")
options = " ".join(options)
cmd = '%(manager)s install %(options)s %(packages)s%(version)s' % locals()
run_as_root(cmd, pty=False)
def uninstall(packages, purge=False, options=None):
"""
Remove one or more packages.
If *purge* is ``True``, the package configuration files will be
removed from the system.
Extra *options* may be passed to ``apt-get`` if necessary.
"""
manager = MANAGER
command = "purge" if purge else "remove"
if options is None:
options = []
if not isinstance(packages, basestring):
packages = " ".join(packages)
options.append("--assume-yes")
options = " ".join(options)
cmd = '%(manager)s %(command)s %(options)s %(packages)s' % locals()
run_as_root(cmd, pty=False)
def preseed_package(pkg_name, preseed):
"""
Enable unattended package installation by preseeding ``debconf``
parameters.
Example::
import fabtools
# Unattended install of Postfix mail server
fabtools.deb.preseed_package('postfix', {
'postfix/main_mailer_type': ('select', 'Internet Site'),
'postfix/mailname': ('string', 'example.com'),
'postfix/destinations': ('string', 'example.com, localhost.localdomain, localhost'),
})
fabtools.deb.install('postfix')
"""
for q_name, _ in preseed.items():
q_type, q_answer = _
run_as_root('echo "%(pkg_name)s %(q_name)s %(q_type)s %(q_answer)s" | debconf-set-selections' % locals())
def get_selections():
"""
Get the state of ``dkpg`` selections.
Returns a dict with state => [packages].
"""
with settings(hide('stdout')):
res = run_as_root('dpkg --get-selections')
selections = dict()
for line in res.splitlines():
package, status = line.split()
selections.setdefault(status, list()).append(package)
return selections
def apt_key_exists(keyid):
"""
Check if the given key id exists in apt keyring.
"""
# Command extracted from apt-key source
gpg_cmd = 'gpg --ignore-time-conflict --no-options --no-default-keyring --keyring /etc/apt/trusted.gpg'
with settings(hide('everything'), warn_only=True):
res = run('%(gpg_cmd)s --fingerprint %(keyid)s' % locals())
return res.succeeded
def _check_pgp_key(path, keyid):
with settings(hide('everything')):
return not run('gpg --with-colons %(path)s | cut -d: -f 5 | grep -q \'%(keyid)s$\'' % locals())
def add_apt_key(filename=None, url=None, keyid=None, keyserver='subkeys.pgp.net', update=False):
"""
Trust packages signed with this public key.
Example::
import fabtools
# Varnish signing key from URL and verify fingerprint)
fabtools.deb.add_apt_key(keyid='C4DEFFEB', url='http://repo.varnish-cache.org/debian/GPG-key.txt')
# Nginx signing key from default key server (subkeys.pgp.net)
fabtools.deb.add_apt_key(keyid='7BD9BF62')
# From custom key server
fabtools.deb.add_apt_key(keyid='7BD9BF62', keyserver='keyserver.ubuntu.com')
# From a file
fabtools.deb.add_apt_key(keyid='7BD9BF62', filename='nginx.asc'
"""
if keyid is None:
if filename is not None:
run_as_root('apt-key add %(filename)s' % locals())
elif url is not None:
run_as_root('wget %(url)s -O - | apt-key add -' % locals())
else:
raise ValueError('Either filename, url or keyid must be provided as argument')
else:
if filename is not None:
_check_pgp_key(filename, keyid)
run_as_root('apt-key add %(filename)s' % locals())
elif url is not None:
tmp_key = '/tmp/tmp.fabtools.key.%(keyid)s.key' % locals()
run_as_root('wget %(url)s -O %(tmp_key)s' % locals())
_check_pgp_key(tmp_key, keyid)
run_as_root('apt-key add %(tmp_key)s' % locals())
else:
keyserver_opt = '--keyserver %(keyserver)s' % locals() if keyserver is not None else ''
run_as_root('apt-key adv %(keyserver_opt)s --recv-keys %(keyid)s' % locals())
if update:
update_index()
def last_update_time():
"""
Get the time of last APT index update
This is the last modification time of ``/var/lib/apt/periodic/fabtools-update-success-stamp``.
Returns ``-1`` if there was no update before.
Example::
import fabtools
print(fabtools.deb.last_update_time())
# 1377603808.02
"""
STAMP = '/var/lib/apt/periodic/fabtools-update-success-stamp'
if not is_file(STAMP):
return -1
return getmtime(STAMP)