-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
cli.py
694 lines (549 loc) · 24.3 KB
/
cli.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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
# Copyright 2012 New Dream Network, LLC (DreamHost)
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import copy
from logging import config as logging_config
import os
from alembic import command as alembic_command
from alembic import config as alembic_config
from alembic import environment
from alembic import migration as alembic_migration
from alembic import script as alembic_script
from alembic import util as alembic_util
from oslo_config import cfg
from oslo_utils import fileutils
from oslo_utils import importutils
import pkg_resources
import six
from neutron._i18n import _
from neutron.common import utils
from neutron.db import migration
from neutron.db.migration.connection import DBConnection
HEAD_FILENAME = 'HEAD'
HEADS_FILENAME = 'HEADS'
CONTRACT_HEAD_FILENAME = 'CONTRACT_HEAD'
EXPAND_HEAD_FILENAME = 'EXPAND_HEAD'
CURRENT_RELEASE = migration.NEWTON
RELEASES = (
migration.LIBERTY,
migration.MITAKA,
migration.NEWTON,
)
EXPAND_BRANCH = 'expand'
CONTRACT_BRANCH = 'contract'
MIGRATION_BRANCHES = (EXPAND_BRANCH, CONTRACT_BRANCH)
MIGRATION_ENTRYPOINTS = 'neutron.db.alembic_migrations'
migration_entrypoints = {
entrypoint.name: entrypoint
for entrypoint in pkg_resources.iter_entry_points(MIGRATION_ENTRYPOINTS)
}
neutron_alembic_ini = os.path.join(os.path.dirname(__file__), 'alembic.ini')
INSTALLED_SUBPROJECTS = [project_ for project_ in migration_entrypoints]
_core_opts = [
cfg.StrOpt('subproject',
choices=INSTALLED_SUBPROJECTS,
help=(_("The subproject to execute the command against. "
"Can be one of: '%s'.")
% "', '".join(INSTALLED_SUBPROJECTS))),
cfg.BoolOpt('split_branches',
default=True,
deprecated_for_removal=True,
help=_("DEPRECATED. Alembic environments integrating with "
"Neutron must implement split (contract and expand) "
"branches file structure."))
]
_db_opts = [
cfg.StrOpt('connection',
default='',
secret=True,
help=_('URL to database')),
cfg.StrOpt('engine',
default='',
help=_('Database engine for which script will be generated '
'when using offline migration.')),
]
CONF = cfg.ConfigOpts()
CONF.register_cli_opts(_core_opts)
CONF.register_cli_opts(_db_opts, 'database')
def do_alembic_command(config, cmd, revision=None, desc=None, **kwargs):
project = config.get_main_option('neutron_project')
args = []
if revision:
# We use unique branch labels from Newton onwards.
if revision.split('@')[0] in MIGRATION_BRANCHES:
revision = '-'.join([project, revision])
args.append(revision)
if desc:
alembic_util.msg(_('Running %(cmd)s (%(desc)s) for %(project)s ...') %
{'cmd': cmd, 'desc': desc, 'project': project})
else:
alembic_util.msg(_('Running %(cmd)s for %(project)s ...') %
{'cmd': cmd, 'project': project})
try:
getattr(alembic_command, cmd)(config, *args, **kwargs)
except alembic_util.CommandError as e:
alembic_util.err(six.text_type(e))
alembic_util.msg(_('OK'))
def _get_alembic_entrypoint(project):
if project not in migration_entrypoints:
alembic_util.err(_('Sub-project %s not installed.') % project)
return migration_entrypoints[project]
def do_generic_show(config, cmd):
kwargs = {'verbose': CONF.command.verbose}
do_alembic_command(config, cmd, **kwargs)
def do_check_migration(config, cmd):
do_alembic_command(config, 'branches')
validate_revisions(config)
validate_head_files(config)
def add_alembic_subparser(sub, cmd):
return sub.add_parser(cmd, help=getattr(alembic_command, cmd).__doc__)
def add_branch_options(parser):
group = parser.add_mutually_exclusive_group()
group.add_argument('--expand', action='store_true')
group.add_argument('--contract', action='store_true')
return group
def _find_milestone_revisions(config, milestone, branch=None):
"""Return the revision(s) for a given milestone."""
script = alembic_script.ScriptDirectory.from_config(config)
milestone_revisions = []
for m in _get_revisions(script):
for branch_label in (m.branch_labels or [None]):
if milestone in getattr(m.module, 'neutron_milestone', []):
if branch is None or branch_label.endswith(branch):
milestone_revisions.append((m.revision, branch))
return milestone_revisions
def do_upgrade(config, cmd):
branch = None
if ((CONF.command.revision or CONF.command.delta) and
(CONF.command.expand or CONF.command.contract)):
raise SystemExit(_(
'Phase upgrade options do not accept revision specification'))
if CONF.command.expand:
branch = EXPAND_BRANCH
revision = _get_branch_head(config, EXPAND_BRANCH)
elif CONF.command.contract:
branch = CONTRACT_BRANCH
revision = _get_branch_head(config, CONTRACT_BRANCH)
elif not CONF.command.revision and not CONF.command.delta:
raise SystemExit(_('You must provide a revision or relative delta'))
else:
revision = CONF.command.revision or ''
if '-' in revision:
raise SystemExit(_('Negative relative revision (downgrade) not '
'supported'))
delta = CONF.command.delta
if delta:
if '+' in revision:
raise SystemExit(_('Use either --delta or relative revision, '
'not both'))
if delta < 0:
raise SystemExit(_('Negative delta (downgrade) not supported'))
revision = '%s+%d' % (revision, delta)
# leave branchless 'head' revision request backward compatible by
# applying all heads in all available branches.
if revision == 'head':
revision = 'heads'
if revision in migration.NEUTRON_MILESTONES:
expand_revisions = _find_milestone_revisions(config, revision,
EXPAND_BRANCH)
contract_revisions = _find_milestone_revisions(config, revision,
CONTRACT_BRANCH)
# Expand revisions must be run before contract revisions
revisions = expand_revisions + contract_revisions
else:
revisions = [(revision, branch)]
for revision, branch in revisions:
if not CONF.command.sql:
run_sanity_checks(config, revision)
do_alembic_command(config, cmd, revision=revision,
desc=branch, sql=CONF.command.sql)
def no_downgrade(config, cmd):
raise SystemExit(_("Downgrade no longer supported"))
def do_stamp(config, cmd):
do_alembic_command(config, cmd,
revision=CONF.command.revision,
sql=CONF.command.sql)
def _get_branch_head(config, branch):
'''Get the latest @head specification for a branch.'''
script_dir = alembic_script.ScriptDirectory.from_config(config)
revs = script_dir.revision_map.get_revisions('heads')
for rev in revs:
for branch_label in rev.branch_labels:
# For forwards and backwards compatibility we handle branch
# names of either
# 'contract/expand'
# or
# 'subproject-contract/subproject-expand'.
if branch_label.endswith(branch):
branch = branch_label
break
return '%s@head' % branch
def _check_bootstrap_new_branch(config, branch, version_path, addn_kwargs):
addn_kwargs['version_path'] = version_path
addn_kwargs['head'] = _get_branch_head(config, branch)
if not os.path.exists(version_path):
# Bootstrap initial directory structure
utils.ensure_dir(version_path)
def do_revision(config, cmd):
kwargs = {
'message': CONF.command.message,
'autogenerate': CONF.command.autogenerate,
'sql': CONF.command.sql,
}
branches = []
if CONF.command.expand:
kwargs['head'] = 'expand@head'
branches.append(EXPAND_BRANCH)
elif CONF.command.contract:
kwargs['head'] = 'contract@head'
branches.append(CONTRACT_BRANCH)
else:
branches = MIGRATION_BRANCHES
if not CONF.command.autogenerate:
for branch in branches:
args = copy.copy(kwargs)
version_path = _get_version_branch_path(
config, release=CURRENT_RELEASE, branch=branch)
_check_bootstrap_new_branch(config, branch, version_path, args)
do_alembic_command(config, cmd, **args)
else:
# autogeneration code will take care of enforcing proper directories
do_alembic_command(config, cmd, **kwargs)
update_head_files(config)
def _compare_labels(revision, expected_labels):
# validate that the script has expected labels only
expected_branch_labels = set()
for label in revision.branch_labels:
for expected_label in expected_labels:
if label.endswith(expected_label):
expected_branch_labels.add(label)
bad_labels = revision.branch_labels - expected_branch_labels
if bad_labels:
script_name = os.path.basename(revision.path)
alembic_util.err(
_('Unexpected label for script %(script_name)s: %(labels)s') %
{'script_name': script_name,
'labels': bad_labels}
)
def _validate_single_revision_labels(script_dir, revision, label=None):
expected_labels = set()
if label is not None:
expected_labels.add(label)
_compare_labels(revision, expected_labels)
# if it's not the root element of the branch, expect the parent of the
# script to have the same label
if revision.down_revision is not None:
down_revision = script_dir.get_revision(revision.down_revision)
_compare_labels(down_revision, expected_labels)
def _validate_revision(script_dir, revision):
for branch in MIGRATION_BRANCHES:
if branch in revision.path:
_validate_single_revision_labels(
script_dir, revision, label=branch)
return
# validate script from branchless part of migration rules
_validate_single_revision_labels(script_dir, revision)
def validate_revisions(config):
script_dir = alembic_script.ScriptDirectory.from_config(config)
revisions = _get_revisions(script_dir)
for revision in revisions:
_validate_revision(script_dir, revision)
branchpoints = _get_branch_points(script_dir)
if len(branchpoints) > 1:
branchpoints = ', '.join(p.revision for p in branchpoints)
alembic_util.err(
_('Unexpected number of alembic branch points: %(branchpoints)s') %
{'branchpoints': branchpoints}
)
def _get_revisions(script):
return list(script.walk_revisions(base='base', head='heads'))
def _get_branch_type(revision):
for branch_label in revision.branch_labels:
if branch_label.endswith(CONTRACT_BRANCH):
return CONTRACT_BRANCH
if branch_label.endswith(EXPAND_BRANCH):
return EXPAND_BRANCH
def _get_branch_points(script):
branchpoints = []
for revision in _get_revisions(script):
if revision.is_branch_point:
branchpoints.append(revision)
return branchpoints
def _get_heads_map(config):
script = alembic_script.ScriptDirectory.from_config(config)
heads = script.get_heads()
head_map = {}
for head in heads:
head_map[_get_branch_type(script.get_revision(head))] = head
return head_map
def _check_head(branch_name, head_file, head):
try:
with open(head_file) as file_:
observed_head = file_.read().strip()
except IOError:
pass
else:
if observed_head != head:
alembic_util.err(
_('%(branch)s HEAD file does not match migration timeline '
'head, expected: %(head)s') % {'branch': branch_name.title(),
'head': head})
def validate_head_files(config):
'''Check that HEAD files contain the latest head for the branch.'''
contract_head = _get_contract_head_file_path(config)
expand_head = _get_expand_head_file_path(config)
if not os.path.exists(contract_head) or not os.path.exists(expand_head):
alembic_util.warn(_("Repository does not contain HEAD files for "
"contract and expand branches."))
return
head_map = _get_heads_map(config)
_check_head(CONTRACT_BRANCH, contract_head, head_map[CONTRACT_BRANCH])
_check_head(EXPAND_BRANCH, expand_head, head_map[EXPAND_BRANCH])
def update_head_files(config):
'''Update HEAD files with the latest branch heads.'''
head_map = _get_heads_map(config)
contract_head = _get_contract_head_file_path(config)
expand_head = _get_expand_head_file_path(config)
with open(contract_head, 'w+') as f:
f.write(head_map[CONTRACT_BRANCH] + '\n')
with open(expand_head, 'w+') as f:
f.write(head_map[EXPAND_BRANCH] + '\n')
old_head_file = _get_head_file_path(config)
old_heads_file = _get_heads_file_path(config)
for file_ in (old_head_file, old_heads_file):
fileutils.delete_if_exists(file_)
def _get_current_database_heads(config):
with DBConnection(config.neutron_config.database.connection) as conn:
opts = {
'version_table': get_alembic_version_table(config)
}
context = alembic_migration.MigrationContext.configure(
conn, opts=opts)
return context.get_current_heads()
def has_offline_migrations(config, cmd):
heads_map = _get_heads_map(config)
if heads_map[CONTRACT_BRANCH] not in _get_current_database_heads(config):
# If there is at least one contract revision not applied to database,
# it means we should shut down all neutron-server instances before
# proceeding with upgrade.
project = config.get_main_option('neutron_project')
alembic_util.msg(_('Need to apply migrations from %(project)s '
'contract branch. This will require all Neutron '
'server instances to be shutdown before '
'proceeding with the upgrade.') %
{"project": project})
return True
return False
def add_command_parsers(subparsers):
for name in ['current', 'history', 'branches', 'heads']:
parser = add_alembic_subparser(subparsers, name)
parser.set_defaults(func=do_generic_show)
parser.add_argument('--verbose',
action='store_true',
help='Display more verbose output for the '
'specified command')
help_text = (getattr(alembic_command, 'branches').__doc__ +
' and validate head file')
parser = subparsers.add_parser('check_migration', help=help_text)
parser.set_defaults(func=do_check_migration)
parser = add_alembic_subparser(subparsers, 'upgrade')
parser.add_argument('--delta', type=int)
parser.add_argument('--sql', action='store_true')
parser.add_argument('revision', nargs='?')
parser.add_argument('--mysql-engine',
default='',
help='Change MySQL storage engine of current '
'existing tables')
add_branch_options(parser)
parser.set_defaults(func=do_upgrade)
parser = subparsers.add_parser('downgrade', help="(No longer supported)")
parser.add_argument('None', nargs='?', help="Downgrade not supported")
parser.set_defaults(func=no_downgrade)
parser = add_alembic_subparser(subparsers, 'stamp')
parser.add_argument('--sql', action='store_true')
parser.add_argument('revision')
parser.set_defaults(func=do_stamp)
parser = add_alembic_subparser(subparsers, 'revision')
parser.add_argument('-m', '--message')
parser.add_argument('--sql', action='store_true')
group = add_branch_options(parser)
group.add_argument('--autogenerate', action='store_true')
parser.set_defaults(func=do_revision)
parser = subparsers.add_parser(
'has_offline_migrations',
help='Determine whether there are pending migration scripts that '
'require full shutdown for all services that directly access '
'database.')
parser.set_defaults(func=has_offline_migrations)
command_opt = cfg.SubCommandOpt('command',
title='Command',
help=_('Available commands'),
handler=add_command_parsers)
CONF.register_cli_opt(command_opt)
def _get_project_base(config):
'''Return the base python namespace name for a project.'''
script_location = config.get_main_option('script_location')
return script_location.split(':')[0].split('.')[0]
def _get_package_root_dir(config):
root_module = importutils.try_import(_get_project_base(config))
if not root_module:
project = config.get_main_option('neutron_project')
alembic_util.err(_("Failed to locate source for %s.") % project)
# The root_module.__file__ property is a path like
# '/opt/stack/networking-foo/networking_foo/__init__.py'
# We return just
# '/opt/stack/networking-foo'
return os.path.dirname(os.path.dirname(root_module.__file__))
def _get_root_versions_dir(config):
'''Return root directory that contains all migration rules.'''
root_dir = _get_package_root_dir(config)
script_location = config.get_main_option('script_location')
# Script location is something like:
# 'project_base.db.migration:alembic_migrations'
# Convert it to:
# 'project_base/db/migration/alembic_migrations/versions'
part1, part2 = script_location.split(':')
parts = part1.split('.') + part2.split('.') + ['versions']
# Return the absolute path to the versions dir
return os.path.join(root_dir, *parts)
def _get_head_file_path(config):
'''Return the path of the file that contains single head.'''
return os.path.join(
_get_root_versions_dir(config),
HEAD_FILENAME)
def _get_heads_file_path(config):
'''
Return the path of the file that was once used to maintain the list of
latest heads.
'''
return os.path.join(
_get_root_versions_dir(config),
HEADS_FILENAME)
def _get_contract_head_file_path(config):
'''
Return the path of the file that is used to maintain contract head
'''
return os.path.join(
_get_root_versions_dir(config),
CONTRACT_HEAD_FILENAME)
def _get_expand_head_file_path(config):
'''
Return the path of the file that is used to maintain expand head
'''
return os.path.join(
_get_root_versions_dir(config),
EXPAND_HEAD_FILENAME)
def _get_version_branch_path(config, release=None, branch=None):
version_path = _get_root_versions_dir(config)
if branch and release:
return os.path.join(version_path, release, branch)
return version_path
def _set_version_locations(config):
'''Make alembic see all revisions in all migration branches.'''
split_branches = False
version_paths = [_get_version_branch_path(config)]
for release in RELEASES:
for branch in MIGRATION_BRANCHES:
version_path = _get_version_branch_path(config, release, branch)
if split_branches or os.path.exists(version_path):
split_branches = True
version_paths.append(version_path)
config.set_main_option('version_locations', ' '.join(version_paths))
def _get_installed_entrypoint(subproject):
'''Get the entrypoint for the subproject, which must be installed.'''
if subproject not in migration_entrypoints:
alembic_util.err(_('Package %s not installed') % subproject)
return migration_entrypoints[subproject]
def _get_subproject_script_location(subproject):
'''Get the script location for the installed subproject.'''
entrypoint = _get_installed_entrypoint(subproject)
return ':'.join([entrypoint.module_name, entrypoint.attrs[0]])
def _get_subproject_base(subproject):
'''Get the import base name for the installed subproject.'''
entrypoint = _get_installed_entrypoint(subproject)
return entrypoint.module_name.split('.')[0]
def get_alembic_version_table(config):
script_dir = alembic_script.ScriptDirectory.from_config(config)
alembic_version_table = [None]
def alembic_version_table_from_env(rev, context):
alembic_version_table[0] = context.version_table
return []
with environment.EnvironmentContext(config, script_dir,
fn=alembic_version_table_from_env):
script_dir.run_env()
return alembic_version_table[0]
def get_alembic_configs():
'''Return a list of alembic configs, one per project.
'''
# Get the script locations for the specified or installed projects.
# Which projects to get script locations for is determined by the CLI
# options as follows:
# --subproject P # only subproject P (where P can be neutron)
# (none specified) # neutron and all installed subprojects
script_locations = {}
if CONF.subproject:
script_location = _get_subproject_script_location(CONF.subproject)
script_locations[CONF.subproject] = script_location
else:
for subproject, ep in migration_entrypoints.items():
script_locations[subproject] = _get_subproject_script_location(
subproject)
# Return a list of alembic configs from the projects in the
# script_locations dict. If neutron is in the list it is first.
configs = []
project_seq = sorted(script_locations.keys())
# Core neutron must be the first project if there is more than one
if len(project_seq) > 1 and 'neutron' in project_seq:
project_seq.insert(0, project_seq.pop(project_seq.index('neutron')))
for project in project_seq:
config = alembic_config.Config(neutron_alembic_ini)
config.set_main_option('neutron_project', project)
script_location = script_locations[project]
config.set_main_option('script_location', script_location)
_set_version_locations(config)
config.neutron_config = CONF
configs.append(config)
return configs
def get_neutron_config():
# Neutron's alembic config is always the first one
return get_alembic_configs()[0]
def run_sanity_checks(config, revision):
script_dir = alembic_script.ScriptDirectory.from_config(config)
def check_sanity(rev, context):
# TODO(ihrachyshka): here we use internal API for alembic; we may need
# alembic to expose implicit_base= argument into public
# iterate_revisions() call
for script in script_dir.revision_map.iterate_revisions(
revision, rev, implicit_base=True):
if hasattr(script.module, 'check_sanity'):
script.module.check_sanity(context.connection)
return []
with environment.EnvironmentContext(config, script_dir,
fn=check_sanity,
starting_rev=None,
destination_rev=revision):
script_dir.run_env()
def get_engine_config():
return [obj for obj in _db_opts if obj.name == 'engine']
def main():
# Interpret the config file for Python logging.
# This line sets up loggers basically.
logging_config.fileConfig(neutron_alembic_ini)
CONF(project='neutron')
return_val = False
for config in get_alembic_configs():
#TODO(gongysh) enable logging
return_val |= bool(CONF.command.func(config, CONF.command.name))
if CONF.command.name == 'has_offline_migrations' and not return_val:
alembic_util.msg(_('No offline migrations pending.'))
return return_val