This repository was archived by the owner on Dec 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathaptitude.py
More file actions
332 lines (256 loc) · 11.1 KB
/
aptitude.py
File metadata and controls
332 lines (256 loc) · 11.1 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
Roles in this namespace are meant to provision packages installed via the Aptitude package manager for Debian distributions.
'''
from base64 import b64encode
from os.path import join
from datetime import datetime, timedelta
import re
from urlparse import urlparse
from fabric.api import settings
from provy.core import Role
class AptitudeRole(Role):
'''
This role provides package management operations with Aptitude within Debian distributions.
Example:
::
from provy.core import Role
from provy.more.debian import AptitudeRole
class MySampleRole(Role):
def provision(self):
with self.using(AptitudeRole) as role:
role.ensure_package_installed('nginx')
'''
time_format = "%d-%m-%y %H:%M:%S"
key = 'aptitude-up-to-date'
aptitude = 'aptitude'
def provision(self):
'''
Installs Aptitude dependencies. This method should be called upon if overriden in base classes, or Aptitude won't work properly in the remote server.
Example:
::
from provy.core import Role
from provy.more.debian import AptitudeRole
class MySampleRole(Role):
def provision(self):
self.provision_role(AptitudeRole) # does not need to be called if using with block.
'''
if not self.is_package_installed('aptitude'):
self.execute('apt-get update', stdout=False, sudo=True)
self.execute('apt-get install aptitude -y', stdout=False, sudo=True)
self.ensure_package_installed('curl')
def ensure_gpg_key(self, url):
'''
Ensures that the specified gpg key is imported into aptitude.
:param url: URL of the gpg key file.
:type url: :class:`str`
Example:
::
from provy.core import Role
from provy.more.debian import AptitudeRole
class MySampleRole(Role):
def provision(self):
with self.using(AptitudeRole) as role:
role.ensure_gpg_key('http://some.url.com/to/key.gpg')
'''
command = "curl %s | apt-key add -" % url
self.execute(command, stdout=False, sudo=True)
def has_source(self, source_string):
'''
Returns :data:`True` if the specified repository is in aptitude's list of repositories.
:param source_string: Repository string.
:type source_string: :class:`str`
:return: Whether the repository string is present or not.
:rtype: :class:`bool`
Example:
::
from provy.core import Role
from provy.more.debian import AptitudeRole
class MySampleRole(Role):
def provision(self):
with self.using(AptitudeRole) as role:
if role.has_source('deb http://www.las.ic.unicamp.br/pub/ubuntu/ natty main restricted'):
pass
'''
result = self.execute('grep -ilR \'^%s\' /etc/apt/sources.list /etc/apt/sources.list.d | wc -l' % source_string, stdout=False, sudo=True)
return int(result) != 0
def ensure_aptitude_source(self, source_string):
'''
Ensures that the specified repository is in aptitude's list of repositories.
:param source_string: Repository string.
:type source_string: :class:`str`
:return: Whether the repository had to be added or not.
:rtype: :class:`bool`
Example:
::
from provy.core import Role
from provy.more.debian import AptitudeRole
class MySampleRole(Role):
def provision(self):
with self.using(AptitudeRole) as role:
role.ensure_aptitude_source('deb http://www.las.ic.unicamp.br/pub/ubuntu/ natty main restricted')
'''
if self.has_source(source_string):
return False
self.log("Aptitude source %s not found! Adding it..." % source_string)
url = self.__parse_source_string(source_string)['uri']
domain = urlparse(url).netloc
source_file = '%s_%s' % (b64encode(source_string)[:12], domain)
command = 'echo "%s" >> /etc/apt/sources.list.d/%s.list' % (source_string, source_file)
self.execute(command, stdout=False, sudo=True)
return True
def __parse_source_string(self, source_string):
parts = re.split('\s+', source_string, 3)
return {'type': parts[0], 'uri': parts[1], 'distribution': parts[2], 'components': parts[3]}
@property
def update_date_file(self):
'''
Returns the path for the file that contains the last update date to aptitudes's list of packages.
:return: The path to the file.
:rtype: :class:`str`
Example:
::
from provy.core import Role
from provy.more.debian import AptitudeRole
class MySampleRole(Role):
def provision(self):
with self.using(AptitudeRole) as role:
file_path = role.update_date_file
'''
return join(self.remote_temp_dir(), 'last_aptitude_update')
def store_update_date(self):
'''
Updates the date in the :meth:`update_date_file`.
Example:
::
from provy.core import Role
from provy.more.debian import AptitudeRole
class MySampleRole(Role):
def provision(self):
with self.using(AptitudeRole) as role:
role.store_update_date()
'''
self.execute('echo "%s" > %s' % (datetime.now().strftime(self.time_format), self.update_date_file), stdout=False)
def get_last_update_date(self):
'''
Returns the date in the :meth:`update_date_file`.
Example:
::
from provy.core import Role
from provy.more.debian import AptitudeRole
class MySampleRole(Role):
def provision(self):
with self.using(AptitudeRole) as role:
last_update = role.get_last_update_date()
'''
if not self.remote_exists(self.update_date_file):
return None
date = datetime.strptime(self.read_remote_file(self.update_date_file), self.time_format)
return date
def ensure_up_to_date(self):
'''
Makes sure aptitude's repository is updated if it hasn't been updated in the last 30 minutes.
Example:
::
from provy.core import Role
from provy.more.debian import AptitudeRole
class MySampleRole(Role):
def provision(self):
with self.using(AptitudeRole) as role:
role.ensure_up_to_date()
'''
last_updated = self.get_last_update_date()
if not self.key in self.context and (not last_updated or (datetime.now() - last_updated > timedelta(minutes=30))):
self.force_update()
def force_update(self):
'''
Forces an update to aptitude's repository.
Example:
::
from provy.core import Role
from provy.more.debian import AptitudeRole
class MySampleRole(Role):
def provision(self):
with self.using(AptitudeRole) as role:
role.force_update()
'''
self.log('Updating aptitude sources...')
self.execute('%s update' % self.aptitude, stdout=False, sudo=True)
self.store_update_date()
self.log('Aptitude sources up-to-date')
self.context[self.key] = True
def is_package_installed(self, package_name):
'''
Returns :data:`True` if the given package is installed via aptitude, :data:`False` otherwise.
:param package_name: Name of the package to verify.
:type package_name: :class:`str`
:return: Whether the package is installed or not.
:rtype: :class:`bool`
Example:
::
from provy.core import Role
from provy.more.debian import AptitudeRole
class MySampleRole(Role):
def provision(self):
with self.using(AptitudeRole) as role:
if role.is_package_installed('nginx'):
pass
'''
with settings(warn_only=True):
return package_name in self.execute("dpkg -l | egrep 'ii[ ]*%s\\b'" % package_name, stdout=False, sudo=True)
def ensure_package_installed(self, package_name, stdout=False, sudo=True):
'''
Ensures that the given package is installed via aptitude.
:param package_name: Name of the package to install.
:type package_name: :class:`str`
:param stdout: Indicates whether install progress should be shown to stdout. Defaults to :data:`False`.
:type stdout: :class:`bool`
:param sudo: Indicates whether the package should be installed with the super user. Defaults to :data:`True`.
:type sudo: :class:`bool`
:return: Whether the package had to be installed or not.
:rtype: :class:`bool`
:raise: :class:`provy.more.debian.PackageNotFound <provy.more.debian.package.aptitude.PackageNotFound>` if the package is not found in the repositories.
Example:
::
from provy.core import Role
from provy.more.debian import AptitudeRole
class MySampleRole(Role):
def provision(self):
with self.using(AptitudeRole) as role:
role.ensure_package_installed('nginx')
'''
if not self.is_package_installed(package_name):
self.ensure_up_to_date()
self.__check_before_install(package_name)
self.log('%s is not installed (via aptitude)! Installing...' % package_name)
self.execute('%s install -y %s' % (self.aptitude, package_name), stdout=stdout, sudo=sudo)
self.log('%s is installed (via aptitude).' % package_name)
return True
return False
def __check_before_install(self, package_name):
if not self.package_exists(package_name):
raise PackageNotFound('Package "%s" not found in repositories' % package_name)
def package_exists(self, package):
'''
Checks if the given package exists.
:param package: Name of the package to check.
:type package: :class:`str`
:return: Whether the package exists or not.
:rtype: :class:`bool`
Example:
::
from provy.core import Role
from provy.more.debian import AptitudeRole
class MySampleRole(Role):
def provision(self):
with self.using(AptitudeRole) as role:
role.package_exists('nginx') # True
'''
try:
self.ensure_up_to_date()
return bool(self.execute('%s show %s' % (self.aptitude, package), stdout=False))
except SystemExit:
return False
class PackageNotFound(Exception):
'''Should be raised when a package doesn't exist.'''