/
__init__.py
607 lines (503 loc) · 18.5 KB
/
__init__.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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
import contextlib
import copy
import logging
import os
import subprocess
import yaml
from teuthology import misc as teuthology
from teuthology import contextutil, packaging
from teuthology.parallel import parallel
from teuthology.orchestra import run
from teuthology.task import ansible
from distutils.version import LooseVersion
from .util import (
_get_builder_project, get_flavor, ship_utilities,
)
from . import rpm, deb, redhat
log = logging.getLogger(__name__)
def get_upgrade_version(ctx, config, remote):
builder = _get_builder_project(ctx, remote, config)
version = builder.version
return version
def verify_package_version(ctx, config, remote):
"""
Ensures that the version of package installed is what
was asked for in the config.
For most cases this is for ceph, but we also install samba
for example.
"""
# Do not verify the version if the ceph-deploy task is being used to
# install ceph. Verifying the ceph installed by ceph-deploy should work,
# but the qa suites will need reorganized first to run ceph-deploy
# before the install task.
# see: http://tracker.ceph.com/issues/11248
if config.get("extras"):
log.info("Skipping version verification...")
return True
builder = _get_builder_project(ctx, remote, config)
version = builder.version
pkg_to_check = builder.project
installed_ver = packaging.get_package_version(remote, pkg_to_check)
if installed_ver and version in installed_ver:
msg = "The correct {pkg} version {ver} is installed.".format(
ver=version,
pkg=pkg_to_check
)
log.info(msg)
else:
raise RuntimeError(
"{pkg} version {ver} was not installed, found {installed}.".format(
ver=version,
installed=installed_ver,
pkg=pkg_to_check
)
)
def purge_data(ctx):
"""
Purge /var/lib/ceph on every remote in ctx.
:param ctx: the argparse.Namespace object
"""
with parallel() as p:
for remote in ctx.cluster.remotes.iterkeys():
p.spawn(_purge_data, remote)
def _purge_data(remote):
"""
Purge /var/lib/ceph on remote.
:param remote: the teuthology.orchestra.remote.Remote object
"""
log.info('Purging /var/lib/ceph on %s', remote)
remote.run(args=[
'sudo',
'rm', '-rf', '--one-file-system', '--', '/var/lib/ceph',
run.Raw('||'),
'true',
run.Raw(';'),
'test', '-d', '/var/lib/ceph',
run.Raw('&&'),
'sudo',
'find', '/var/lib/ceph',
'-mindepth', '1',
'-maxdepth', '2',
'-type', 'd',
'-exec', 'umount', '{}', ';',
run.Raw(';'),
'sudo', 'umount', '/var/lib/ceph',
run.Raw('||'),
'true',
run.Raw(';'),
'sudo',
'rm', '-rf', '--one-file-system', '--', '/var/lib/ceph',
])
def install_packages(ctx, pkgs, config):
"""
Installs packages on each remote in ctx.
:param ctx: the argparse.Namespace object
:param pkgs: list of packages names to install
:param config: the config dict
"""
install_pkgs = {
"deb": deb._update_package_list_and_install,
"rpm": rpm._update_package_list_and_install,
}
with parallel() as p:
for remote in ctx.cluster.remotes.iterkeys():
system_type = teuthology.get_system_type(remote)
p.spawn(
install_pkgs[system_type],
ctx, remote, pkgs[system_type], config)
for remote in ctx.cluster.remotes.iterkeys():
# verifies that the install worked as expected
verify_package_version(ctx, config, remote)
def remove_packages(ctx, config, pkgs):
"""
Removes packages from each remote in ctx.
:param ctx: the argparse.Namespace object
:param config: the config dict
:param pkgs: list of packages names to remove
"""
remove_pkgs = {
"deb": deb._remove,
"rpm": rpm._remove,
}
with parallel() as p:
for remote in ctx.cluster.remotes.iterkeys():
system_type = teuthology.get_system_type(remote)
p.spawn(remove_pkgs[
system_type], ctx, config, remote, pkgs[system_type])
def remove_sources(ctx, config):
"""
Removes repo source files from each remote in ctx.
:param ctx: the argparse.Namespace object
:param config: the config dict
"""
remove_sources_pkgs = {
'deb': deb._remove_sources_list,
'rpm': rpm._remove_sources_list,
}
with parallel() as p:
project = config.get('project', 'ceph')
log.info("Removing {proj} sources lists".format(
proj=project))
for remote in ctx.cluster.remotes.iterkeys():
remove_fn = remove_sources_pkgs[remote.os.package_type]
p.spawn(remove_fn, ctx, config, remote)
def get_package_list(ctx, config):
debug = config.get('debuginfo', False)
project = config.get('project', 'ceph')
yaml_path = None
# Look for <suite_path>/packages/packages.yaml
if hasattr(ctx, 'config') and 'suite_path' in ctx.config:
suite_packages_path = os.path.join(
ctx.config['suite_path'],
'packages',
'packages.yaml',
)
if os.path.exists(suite_packages_path):
yaml_path = suite_packages_path
# If packages.yaml isn't found in the suite_path, potentially use
# teuthology's
yaml_path = yaml_path or os.path.join(
os.path.dirname(__file__),
'packages.yaml',
)
default_packages = yaml.safe_load(open(yaml_path))
default_debs = default_packages.get(project, dict()).get('deb', [])
default_rpms = default_packages.get(project, dict()).get('rpm', [])
# If a custom deb and/or rpm list is provided via the task config, use
# that. Otherwise, use the list from whichever packages.yaml was found
# first
debs = config.get('packages', dict()).get('deb', default_debs)
rpms = config.get('packages', dict()).get('rpm', default_rpms)
# Optionally include or exclude debug packages
if not debug:
debs = filter(lambda p: not p.endswith('-dbg'), debs)
rpms = filter(lambda p: not p.endswith('-debuginfo'), rpms)
excluded_packages = config.get('exclude_packages', list())
if excluded_packages:
log.debug("Excluding packages: {}".format(excluded_packages))
def exclude(pkgs):
return list(set(pkgs).difference(set(excluded_packages)))
debs = exclude(debs)
rpms = exclude(rpms)
package_list = dict(deb=debs, rpm=rpms)
log.debug("Package list is: {}".format(package_list))
return package_list
@contextlib.contextmanager
def install(ctx, config):
"""
The install task. Installs packages for a given project on all hosts in
ctx. May work for projects besides ceph, but may not. Patches welcomed!
:param ctx: the argparse.Namespace object
:param config: the config dict
"""
project = config.get('project', 'ceph')
package_list = get_package_list(ctx, config)
debs = package_list['deb']
rpm = package_list['rpm']
# pull any additional packages out of config
extra_pkgs = config.get('extra_packages')
log.info('extra packages: {packages}'.format(packages=extra_pkgs))
debs += extra_pkgs
rpm += extra_pkgs
# When extras is in the config we want to purposely not install ceph.
# This is typically used on jobs that use ceph-deploy to install ceph
# or when we are testing ceph-deploy directly. The packages being
# installed are needed to properly test ceph as ceph-deploy won't
# install these. 'extras' might not be the best name for this.
extras = config.get('extras')
if extras is not None:
debs = ['ceph-test', 'ceph-fuse',
'librados2', 'librbd1',
'python-ceph']
rpm = ['ceph-fuse', 'librbd1', 'librados2', 'ceph-test', 'python-ceph']
package_list = dict(deb=debs, rpm=rpm)
install_packages(ctx, package_list, config)
try:
yield
finally:
remove_packages(ctx, config, package_list)
remove_sources(ctx, config)
if project == 'ceph':
purge_data(ctx)
def upgrade_old_style(ctx, node, remote, pkgs, system_type):
"""
Handle the upgrade using methods in use prior to ceph-deploy.
"""
if system_type == 'deb':
deb._upgrade_packages(ctx, node, remote, pkgs)
elif system_type == 'rpm':
rpm._upgrade_packages(ctx, node, remote, pkgs)
def upgrade_with_ceph_deploy(ctx, node, remote, pkgs, sys_type):
"""
Upgrade using ceph-deploy
"""
dev_table = ['branch', 'tag', 'dev']
ceph_dev_parm = ''
ceph_rel_parm = ''
for entry in node.keys():
if entry in dev_table:
ceph_dev_parm = node[entry]
if entry == 'release':
ceph_rel_parm = node[entry]
params = []
if ceph_dev_parm:
params += ['--dev', ceph_dev_parm]
if ceph_rel_parm:
params += ['--release', ceph_rel_parm]
params.append(remote.name)
subprocess.call(['ceph-deploy', 'install'] + params)
remote.run(args=['sudo', 'restart', 'ceph-all'])
def upgrade_remote_to_config(ctx, config):
assert config is None or isinstance(config, dict), \
"install.upgrade only supports a dictionary for configuration"
project = config.get('project', 'ceph')
# use 'install' overrides here, in case the upgrade target is left
# unspecified/implicit.
install_overrides = ctx.config.get(
'overrides', {}).get('install', {}).get(project, {})
log.info('project %s config %s overrides %s', project, config,
install_overrides)
# build a normalized remote -> config dict
remotes = {}
if 'all' in config:
for remote in ctx.cluster.remotes.iterkeys():
remotes[remote] = config.get('all')
else:
for role in config.keys():
remotes_dict = ctx.cluster.only(role).remotes
if not remotes_dict:
# This is a regular config argument, not a role
continue
remote = remotes_dict.keys()[0]
if remote in remotes:
log.warn('remote %s came up twice (role %s)', remote, role)
continue
remotes[remote] = config.get(role)
result = {}
for remote, node in remotes.iteritems():
if not node:
node = {}
this_overrides = copy.deepcopy(install_overrides)
if 'sha1' in node or 'tag' in node or 'branch' in node:
log.info("config contains sha1|tag|branch, "
"removing those keys from override")
this_overrides.pop('sha1', None)
this_overrides.pop('tag', None)
this_overrides.pop('branch', None)
teuthology.deep_merge(node, this_overrides)
log.info('remote %s config %s', remote, node)
node['project'] = project
result[remote] = node
return result
def _upgrade_is_downgrade(installed_version, upgrade_version):
assert installed_version, "installed_version is empty"
assert upgrade_version, "upgrade_version is empty"
return LooseVersion(installed_version) > LooseVersion(upgrade_version)
def upgrade_common(ctx, config, deploy_style):
"""
Common code for upgrading
"""
remotes = upgrade_remote_to_config(ctx, config)
project = config.get('project', 'ceph')
# FIXME: extra_pkgs is not distro-agnostic
extra_pkgs = config.get('extra_packages', [])
log.info('extra packages: {packages}'.format(packages=extra_pkgs))
for remote, node in remotes.iteritems():
system_type = teuthology.get_system_type(remote)
assert system_type in ('deb', 'rpm')
pkgs = get_package_list(ctx, config)[system_type]
log.info("Upgrading {proj} {system_type} packages: {pkgs}".format(
proj=project, system_type=system_type, pkgs=', '.join(pkgs)))
# FIXME: again, make extra_pkgs distro-agnostic
pkgs += extra_pkgs
installed_version = packaging.get_package_version(remote, 'ceph-common')
upgrade_version = get_upgrade_version(ctx, node, remote)
log.info("Ceph {s} upgrade from {i} to {u}".format(
s=system_type,
i=installed_version,
u=upgrade_version
))
if _upgrade_is_downgrade(installed_version, upgrade_version):
raise RuntimeError(
"An attempt to upgrade from a higher version to a lower one "
"will always fail. Hint: check tags in the target git branch."
)
deploy_style(ctx, node, remote, pkgs, system_type)
verify_package_version(ctx, node, remote)
return len(remotes)
docstring_for_upgrade = """"
Upgrades packages for a given project.
For example::
tasks:
- install.{cmd_parameter}:
all:
branch: end
or specify specific roles::
tasks:
- install.{cmd_parameter}:
mon.a:
branch: end
osd.0:
branch: other
or rely on the overrides for the target version::
overrides:
install:
ceph:
sha1: ...
tasks:
- install.{cmd_parameter}:
all:
(HACK: the overrides will *only* apply the sha1/branch/tag if those
keys are not present in the config.)
It is also possible to attempt to exclude packages from the upgrade set:
tasks:
- install.{cmd_parameter}:
exclude_packages: ['ceph-test', 'ceph-test-dbg']
:param ctx: the argparse.Namespace object
:param config: the config dict
"""
#
# __doc__ strings for upgrade and ceph_deploy_upgrade are set from
# the same string so that help(upgrade) and help(ceph_deploy_upgrade)
# look the same.
#
@contextlib.contextmanager
def upgrade(ctx, config):
upgrade_common(ctx, config, upgrade_old_style)
yield
upgrade.__doc__ = docstring_for_upgrade.format(cmd_parameter='upgrade')
@contextlib.contextmanager
def ceph_deploy_upgrade(ctx, config):
upgrade_common(ctx, config, upgrade_with_ceph_deploy)
yield
ceph_deploy_upgrade.__doc__ = docstring_for_upgrade.format(
cmd_parameter='ceph_deploy_upgrade')
@contextlib.contextmanager
def task(ctx, config):
"""
Install packages for a given project.
tasks:
- install:
project: ceph
branch: bar
- install:
project: samba
branch: foo
extra_packages: ['samba']
- install:
rhbuild: 1.3.0
playbook: downstream_setup.yml
vars:
yum_repos:
- url: "http://location.repo"
name: "ceph_repo"
Overrides are project specific:
overrides:
install:
ceph:
sha1: ...
Debug packages may optionally be installed:
overrides:
install:
ceph:
debuginfo: true
Default package lists (which come from packages.yaml) may be overridden:
overrides:
install:
ceph:
packages:
deb:
- ceph-osd
- ceph-mon
rpm:
- ceph-devel
- rbd-fuse
When tag, branch and sha1 do not reference the same commit hash, the
tag takes precedence over the branch and the branch takes precedence
over the sha1.
When the overrides have a sha1 that is different from the sha1 of
the project to be installed, it will be a noop if the project has
a branch or tag, because they take precedence over the sha1. For
instance:
overrides:
install:
ceph:
sha1: 1234
tasks:
- install:
project: ceph
sha1: 4567
branch: foobar # which has sha1 4567
The override will transform the tasks as follows:
tasks:
- install:
project: ceph
sha1: 1234
branch: foobar # which has sha1 4567
But the branch takes precedence over the sha1 and foobar
will be installed. The override of the sha1 has no effect.
When passed 'rhbuild' as a key, it will attempt to install an rh ceph build
using ceph-deploy
Reminder regarding teuthology-suite side effects:
The teuthology-suite command always adds the following:
overrides:
install:
ceph:
sha1: 1234
where sha1 matches the --ceph argument. For instance if
teuthology-suite is called with --ceph master, the sha1 will be
the tip of master. If called with --ceph v0.94.1, the sha1 will be
the v0.94.1 (as returned by git rev-parse v0.94.1 which is not to
be confused with git rev-parse v0.94.1^{commit})
:param ctx: the argparse.Namespace object
:param config: the config dict
"""
if config is None:
config = {}
assert isinstance(config, dict), \
"task install only supports a dictionary for configuration"
project, = config.get('project', 'ceph'),
log.debug('project %s' % project)
overrides = ctx.config.get('overrides')
if overrides:
install_overrides = overrides.get('install', {})
teuthology.deep_merge(config, install_overrides.get(project, {}))
log.debug('config %s' % config)
rhbuild = None
if config.get('rhbuild'):
rhbuild = config.get('rhbuild')
log.info("Build is %s " % rhbuild)
flavor = get_flavor(config)
log.info("Using flavor: %s", flavor)
ctx.summary['flavor'] = flavor
nested_tasks = [lambda: redhat.install(ctx=ctx, config=config),
lambda: ship_utilities(ctx=ctx, config=None)]
if config.get('rhbuild'):
if config.get('playbook'):
ansible_config = dict(config)
# remove key not required by ansible task
del ansible_config['rhbuild']
nested_tasks.insert(
0,
lambda: ansible.CephLab(ctx, config=ansible_config)
)
with contextutil.nested(*nested_tasks):
yield
else:
with contextutil.nested(
lambda: install(ctx=ctx, config=dict(
branch=config.get('branch'),
tag=config.get('tag'),
sha1=config.get('sha1'),
debuginfo=config.get('debuginfo'),
flavor=flavor,
extra_packages=config.get('extra_packages', []),
exclude_packages=config.get('exclude_packages', []),
extras=config.get('extras', None),
wait_for_package=config.get('wait_for_package', False),
project=project,
packages=config.get('packages', dict()),
)),
lambda: ship_utilities(ctx=ctx, config=None),
):
yield