|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" Run a migration on a running deployment """ |
| 3 | + |
| 4 | +import argparse |
| 5 | +import site |
| 6 | +import os |
| 7 | +from pathlib import Path |
| 8 | +import sys |
| 9 | +file_name = Path(__file__) |
| 10 | +current_file_path = file_name.parent.resolve() |
| 11 | +root_path = [parent_path for parent_path in current_file_path.parents if (parent_path / 'README.md').exists()][0] |
| 12 | +dependencies_dir = os.path.join(root_path, 'lib', 'dependencies') |
| 13 | +# Insert lib folders to python path |
| 14 | +sys.path.insert(0, str(root_path)) |
| 15 | +sys.path.insert(1, str(dependencies_dir) + site.USER_SITE.replace(site.USER_BASE, '')) |
| 16 | + |
| 17 | +from lib.python.ensure_configuration_is_valid_or_exit import ensure_configuration_is_valid_or_exit, \ |
| 18 | + print_how_to_install_dependencies |
| 19 | +from lib.python.defaults import SNAPSHOT_ROLE_NAME |
| 20 | + |
| 21 | +# First ensure configure has been executed |
| 22 | +try: |
| 23 | + ensure_configuration_is_valid_or_exit() |
| 24 | +except Exception as e: |
| 25 | + try: |
| 26 | + print(f'[error] {str(e)}') |
| 27 | + except: |
| 28 | + raise e |
| 29 | + sys.exit(1) |
| 30 | + |
| 31 | +try: |
| 32 | + import yaml |
| 33 | + from mergedeep import merge |
| 34 | +except: |
| 35 | + print_how_to_install_dependencies() |
| 36 | +import lib.python.common as common |
| 37 | +import lib.python.utils as utils |
| 38 | +import lib.python.migrations.sa2hs as sa2hs |
| 39 | + |
| 40 | + |
| 41 | +class MigrateFormatter(argparse.ArgumentDefaultsHelpFormatter, argparse.RawDescriptionHelpFormatter): |
| 42 | + pass |
| 43 | + |
| 44 | + |
| 45 | +if __name__ == '__main__': |
| 46 | + PROG = 'forgeops migrate' |
| 47 | + DESC = "Run a migration on a running deployment." |
| 48 | + |
| 49 | + migrations = ['sa2hs'] |
| 50 | + common_args = common.setup_args() |
| 51 | + |
| 52 | + parser = argparse.ArgumentParser(description=DESC, |
| 53 | + prog=PROG, |
| 54 | + formatter_class=MigrateFormatter) |
| 55 | + |
| 56 | + subparsers = parser.add_subparsers( |
| 57 | + title="Migrations", |
| 58 | + required=True, |
| 59 | + dest="migration") |
| 60 | + sa2hs.setup_args(subparsers, common_args) |
| 61 | + |
| 62 | + args = parser.parse_args() |
| 63 | + |
| 64 | + # Setup defaults for values that can be set in forgeops.conf |
| 65 | + overrides = utils.process_overrides(root_path, |
| 66 | + getattr(args, 'helm_path', None), |
| 67 | + getattr(args, 'kustomize_path', None), |
| 68 | + getattr(args, 'build_path', None), |
| 69 | + getattr(args, 'no_helm', False), |
| 70 | + getattr(args, 'no_kustomize', False), |
| 71 | + getattr(args, 'releases_src', None), |
| 72 | + getattr(args, 'pull_policy', None), |
| 73 | + getattr(args, 'source', None), |
| 74 | + getattr(args, 'ssl_secretname', None), |
| 75 | + args.debug) |
| 76 | + |
| 77 | + config = {} |
| 78 | + config['root_path'] = root_path |
| 79 | + if args.debug: |
| 80 | + print(f"root_path = {config['root_path']}") |
| 81 | + |
| 82 | + config = merge(config, overrides) |
| 83 | + |
| 84 | + if getattr(args, 'namespace', None): |
| 85 | + config['namespace_opt'] = f'-n {args.namespace}' |
| 86 | + else: |
| 87 | + ns = utils.get_namespace() |
| 88 | + print(f"No namespace given, getting it from your context. ns = {ns}") |
| 89 | + config['namespace_opt'] = f'-n {ns}' |
| 90 | + |
| 91 | + if args.migration == 'sa2hs': |
| 92 | + sa2hs.run(args, config) |
0 commit comments