forked from norcams/himlarcli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
159 lines (153 loc) · 6.18 KB
/
utils.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
import getopt
import sys
import os.path
import argparse
def get_options(desc, config=True, debug=True, dry_run=False, hosts='+'):
parser = argparse.ArgumentParser(description=desc)
if debug:
parser.add_argument('-d',
dest='debug',
action='store_const',
const=True,
default=False,
help='verbose debug mode')
if config:
parser.add_argument('-c',
dest='config',
metavar='config.ini',
action='store',
default='/etc/himlarcli/config.ini',
help='path to ini file with config')
if dry_run:
parser.add_argument('--dry-run',
dest='dry_run',
action='store_const',
const=True,
default=False,
help='dry run script')
if hosts:
parser.add_argument('host',
metavar='FQDN',
type=str,
nargs=hosts,
help='nova compute host')
return parser.parse_args()
def get_host_action_options(desc, actions, hosts=True, config=True, debug=True):
parser = argparse.ArgumentParser(description=desc)
if debug:
parser.add_argument('-d',
dest='debug',
action='store_const',
const=True,
default=False,
help='verbose debug mode')
if config:
parser.add_argument('-c',
dest='config',
metavar='config.ini',
action='store',
default='/etc/himlarcli/config.ini',
help='path to ini file with config')
if hosts:
parser.add_argument('host',
metavar='FQDN',
action='store',
help='nova compute host')
parser.add_argument('action',
metavar='actions',
choices=actions,
type=str,
nargs=1,
help='|'.join(actions))
return parser.parse_args()
def get_node_action_options(desc, actions, nodes=True, dry_run=False, config=True, debug=True):
parser = argparse.ArgumentParser(description=desc)
if debug:
parser.add_argument('-d',
dest='debug',
action='store_const',
const=True,
default=False,
help='verbose debug mode')
if config:
parser.add_argument('-c',
dest='config',
metavar='config.ini',
action='store',
default='/etc/himlarcli/config.ini',
help='path to ini file with config')
if dry_run:
parser.add_argument('--dry-run',
dest='dry_run',
action='store_const',
const=True,
default=False,
help='dry run script')
if nodes:
parser.add_argument('-n',
dest='node',
metavar='nodename',
action='store',
help='nodename (mandatory for some actions)',
default=None)
parser.add_argument('action',
metavar='actions',
choices=actions,
type=str,
nargs=1,
help='|'.join(actions))
return parser.parse_args()
def get_action_options(desc, actions, dry_run=False, config=True, debug=True, opt_args={}):
parser = argparse.ArgumentParser(description=desc)
if debug:
parser.add_argument('-d',
dest='debug',
action='store_const',
const=True,
default=False,
help='verbose debug mode')
if config:
parser.add_argument('-c',
dest='config',
metavar='config.ini',
action='store',
default='/etc/himlarcli/config.ini',
help='path to ini file with config')
if dry_run:
parser.add_argument('--dry-run',
dest='dry_run',
action='store_const',
const=True,
default=False,
help='dry run script')
for name, arg in opt_args.items():
if not 'dest' in arg:
print('missing dest in opt_args')
continue
if not 'default' in arg:
arg['default'] = False
if not 'action' in arg:
arg['action'] = 'store'
if not 'help' in arg:
arg['help'] = arg['dest']
if not 'metavar' in arg:
arg['metavar'] = arg['dest']
if not 'const' in arg:
arg['const'] = None
if not 'required' in arg:
arg['required'] = False
parser.add_argument(name,
dest=arg['dest'],
action=arg['action'],
default=arg['default'],
metavar=arg['metavar'],
const=arg['const'],
required=arg['required'],
help=arg['help'])
parser.add_argument('action',
metavar='actions',
choices=actions,
type=str,
nargs=1,
help='|'.join(actions))
return parser.parse_args()