-
Notifications
You must be signed in to change notification settings - Fork 6
/
oci-tools.py
116 lines (92 loc) · 4.1 KB
/
oci-tools.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
#!/usr/bin/env python
from oci_tools import __version__
from oci_tools import oci_config, training_tools
import sys, argparse, logging
def print_help(args):
parser.print_help()
def resource_manager(args):
"""
Entry point for the oci resource manager
"""
conf = oci_config.OCIConfig(args.config,
profile=args.profile,
operation=args.operation,
use_yaml_format=args.use_yaml_format,
output_file=args.output_file,
auto_approve=args.auto_approve
)
training_tools.run(conf)
def setup_log(args):
"""
setup log
"""
log_handlers = []
if args.log == 'console' or args.log == 'all':
sh = logging.StreamHandler()
sh.setFormatter(logging.Formatter("%(message)s"))
log_handlers.append(sh)
if args.log == 'file' or args.log == 'all':
log_handlers.append(logging.FileHandler("{0}".format(args.log_output)))
logging.basicConfig(
level=logging.getLevelName(args.log_level.upper()),
format="%(asctime)s [%(levelname)-5.5s] %(message)s",
handlers=log_handlers,
)
if args.log_level.upper() != 'DEBUG':
logging.getLogger("oci").setLevel(logging.WARNING)
#########################
# command line parser init
parser = argparse.ArgumentParser(description="OCI Toolkit")
parser.set_defaults(func=print_help)
parser.add_argument('--log',
dest='log',
choices=['console', 'file', 'all', 'none'],
default='console')
parser.add_argument('--log-level',
dest='log_level',
choices=['debug', 'info', 'warn'],
default='info')
parser.add_argument('--log-output',
dest='log_output',
default='log/oci-tools.log')
sub01 = parser.add_subparsers(help="OCI toolkit")
resource_manager_parser = sub01.add_parser('resource-manager',
help="utility to manage oci environments")
resource_manager_parser.set_defaults(func=resource_manager)
resource_manager_parser.add_argument('--config',
help='OCI configuration file',
dest='config',
default='./config/config')
resource_manager_parser.add_argument('-o', '--operation',
dest='operation',
default='list',
choices=['list', 'cleanup', 'dryrun'])
resource_manager_parser.add_argument('--profile',
help='OCI configuration profile',
dest='profile',
default='DEFAULT')
resource_manager_parser.add_argument('--auto-approve',
help='force the cleanup operation without asking for confirmation',
action='store_true',
dest='auto_approve')
resource_manager_parser.add_argument('--json',
help='print output in json format [DEFAULT]',
action='store_true',
dest='use_json_format')
resource_manager_parser.add_argument('--yaml',
help='print output in json format',
action='store_true',
dest='use_yaml_format')
resource_manager_parser.add_argument('--output',
help='output file',
dest='output_file')
def main():
import sys
if sys.version_info[0] < 3:
raise Exception("I'm sorry, you're Python version must be >= 3.6")
print(' ** oci-tools v.{} **'.format(__version__))
args=parser.parse_args()
setup_log(args)
args.func(args)
if __name__ == '__main__':
main()