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
|
#!/usr/bin/env python
# common.py - Common bits for API examples
# 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.
"""
API Examples
This is a collection of common functions used by the example scripts.
It may also be run directly as a script to do basic testing of itself.
common.object_parser() provides the common set of command-line arguments
used in the library CLIs for setting up authentication. This should make
playing with the example scripts against a running OpenStack simpler.
common.configure_logging() provides the same basic logging control as
the OSC shell.
common.make_session() does the minimal loading of a Keystone authentication
plugin and creates a Keystone client Session.
"""
import argparse
import logging
import os
import sys
import traceback
from keystoneauth1 import session as ks_session
from osc_lib.api import auth
CONSOLE_MESSAGE_FORMAT = '%(levelname)s: %(name)s %(message)s'
DEFAULT_VERBOSE_LEVEL = 1
USER_AGENT = 'osc-examples'
PARSER_DESCRIPTION = 'A demonstration framework'
DEFAULT_IDENTITY_API_VERSION = '2.0'
_logger = logging.getLogger(__name__)
# --debug sets this True
dump_stack_trace = False
# Generally useful stuff often found in a utils module
def env(*vars, **kwargs):
"""Search for the first defined of possibly many env vars
Returns the first environment variable defined in vars, or
returns the default defined in kwargs.
"""
for v in vars:
value = os.environ.get(v, None)
if value:
return value
return kwargs.get('default', '')
# Common Example functions
def base_parser(parser):
"""Set up some of the common CLI options
These are the basic options that match the library CLIs so
command-line/environment setups for those also work with these
demonstration programs.
"""
# Global arguments
parser.add_argument(
'--os-cloud',
metavar='<cloud-config-name>',
dest='cloud',
default=env('OS_CLOUD'),
help='Cloud name in clouds.yaml (Env: OS_CLOUD)',
)
parser.add_argument(
'--os-region-name',
metavar='<auth-region-name>',
dest='region_name',
default=env('OS_REGION_NAME'),
help='Authentication region name (Env: OS_REGION_NAME)',
)
parser.add_argument(
'--os-cacert',
metavar='<ca-bundle-file>',
dest='cacert',
default=env('OS_CACERT'),
help='CA certificate bundle file (Env: OS_CACERT)',
)
parser.add_argument(
'--os-default-domain',
metavar='<auth-domain>',
default='default',
help='Default domain ID, default=default (Env: OS_DEFAULT_DOMAIN)',
)
verify_group = parser.add_mutually_exclusive_group()
verify_group.add_argument(
'--verify',
action='store_true',
help='Verify server certificate (default)',
)
verify_group.add_argument(
'--insecure',
action='store_true',
help='Disable server certificate verification',
)
parser.add_argument(
'--timing',
default=False,
action='store_true',
help="Print API call timing info",
)
parser.add_argument(
'-v',
'--verbose',
action='count',
dest='verbose_level',
default=1,
help='Increase verbosity of output. Can be repeated.',
)
parser.add_argument(
'--debug',
default=False,
action='store_true',
help='show tracebacks on errors',
)
parser.add_argument(
'rest',
nargs='*',
help='the rest of the args',
)
return parser
def configure_logging(opts):
"""Typical app logging setup
Based on OSC/cliff
"""
global dump_stack_trace
root_logger = logging.getLogger('')
# Requests logs some stuff at INFO that we don't want
# unless we have DEBUG
requests_log = logging.getLogger("requests")
requests_log.setLevel(logging.ERROR)
# Other modules we don't want DEBUG output for so
# don't reset them below
iso8601_log = logging.getLogger("iso8601")
iso8601_log.setLevel(logging.ERROR)
# Always send higher-level messages to the console via stderr
console = logging.StreamHandler(sys.stderr)
formatter = logging.Formatter(CONSOLE_MESSAGE_FORMAT)
console.setFormatter(formatter)
root_logger.addHandler(console)
# Set logging to the requested level
dump_stack_trace = False
if opts.verbose_level == 0:
# --quiet
root_logger.setLevel(logging.ERROR)
elif opts.verbose_level == 1:
# This is the default case, no --debug, --verbose or --quiet
root_logger.setLevel(logging.WARNING)
elif opts.verbose_level == 2:
# One --verbose
root_logger.setLevel(logging.INFO)
elif opts.verbose_level >= 3:
# Two or more --verbose
root_logger.setLevel(logging.DEBUG)
requests_log.setLevel(logging.DEBUG)
if opts.debug:
# --debug forces traceback
dump_stack_trace = True
root_logger.setLevel(logging.DEBUG)
requests_log.setLevel(logging.DEBUG)
return
def make_session(opts, **kwargs):
"""Create our base session using simple auth from ksc plugins
The arguments required in opts varies depending on the auth plugin
that is used. This example assumes Identity v2 will be used
and selects token auth if both os_url and os_token have been
provided, otherwise it uses password.
:param Namespace opts:
A parser options Namespace containing the authentication
options to be used
:param dict kwargs:
Additional options passed directly to Session constructor
"""
# If no auth type is named by the user, select one based on
# the supplied options
auth_plugin_name = auth.select_auth_plugin(opts)
(auth_plugin, auth_params) = auth.build_auth_params(
auth_plugin_name,
opts,
)
auth_p = auth_plugin.load_from_options(**auth_params)
session = ks_session.Session(auth=auth_p, **kwargs)
return session
# Top-level functions
def run(opts):
"""Default run command"""
# Do some basic testing here
sys.stdout.write("Default run command\n")
sys.stdout.write("Verbose level: %s\n" % opts.verbose_level)
sys.stdout.write("Debug: %s\n" % opts.debug)
sys.stdout.write("dump_stack_trace: %s\n" % dump_stack_trace)
def setup():
"""Parse command line and configure logging"""
opts = base_parser(
auth.build_auth_plugins_option_parser(
argparse.ArgumentParser(description='Object API Example')
)
).parse_args()
configure_logging(opts)
return opts
def main(opts, run):
try:
return run(opts)
except Exception as e:
if dump_stack_trace:
_logger.error(traceback.format_exc(e))
else:
_logger.error('Exception raised: ' + str(e))
return 1
if __name__ == "__main__":
opts = setup()
sys.exit(main(opts, run))
|