-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathnrmig
executable file
·132 lines (103 loc) · 3.29 KB
/
nrmig
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
#!/usr/bin/env python3
"""New Relic Account Migration Utility
This script can be used to run account migration tasks.
"""
import argparse
import configparser
import os
import library.migrationlogger as m_logger
import library.utils as m_utils
import migratepolicies
import migrateconditions
logger = m_logger.get_logger(os.path.basename(__file__))
def create_argument_parser() -> argparse.ArgumentParser:
"""Create and configure the nrmig argument parser
:returns: a configured argument parser
:rtype: argparse.ArgumentParser
"""
# Create the global options group parser
global_options_parser = argparse.ArgumentParser(add_help=False)
global_options_parser.add_argument(
'-c',
'--config',
type=str,
default='config.ini',
help='the config file to use'
)
global_options_parser.add_argument(
'-d',
'--debug',
default=False,
action='store_true',
help='debug mode'
)
# Create the top-level parser
parser = argparse.ArgumentParser(
prog='nrmig',
parents=[global_options_parser],
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
subparsers = parser.add_subparsers(
required=True,
dest='command',
title='commands',
metavar='<command>'
)
# Create the parser for the "migrate" command
migrate_parser = subparsers.add_parser(
'migrate',
help='perform a migration',
parents=[global_options_parser],
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
migrate_subparsers = migrate_parser.add_subparsers(
required=True,
dest='migrate_command',
title='sub-commands',
metavar='<migrate-command>'
)
# Let each of the commands configure their parsing rules
for clazz in [
migratepolicies.MigratePoliciesCommand,
migrateconditions.MigrateConditionsCommand,
]:
command = clazz()
command.configure_parser(migrate_subparsers, global_options_parser)
return parser
def parse_config(config_file_path: str) -> configparser.ConfigParser:
"""Parse the configuration file
:param config_file_path: the configuraiton file
:type config_file_path: str
:returns: an initialized configuration parser
:rtype: configparser.ConfigParser
"""
logger.info('Parsing configuration from file %s...' % config_file_path)
config = configparser.ConfigParser()
try:
with open(config_file_path, 'r') as config_file:
config.read_file(config_file)
except OSError as err:
m_utils.error_message_and_exit(
'The file %s could not be opened: %s' % (
config_file_path,
err.strerror
)
)
return config
def main():
"""Parse the CLI arguments and run the appropriate commands.
"""
logger.info('Starting migration...')
# Create the argument parser
parser = create_argument_parser()
# Parse the command line
args = parser.parse_args()
# Configure logger
m_utils.configure_loglevel(args)
# Parse the configuration file
config = parse_config(args.config)
# Run the appropriate command function
args.func(config, args)
logger.info('Done.')
if __name__ == "__main__":
main()