2
# vim: tabstop=4 shiftwidth=4 softtabstop=4
4
# Copyright 2010 United States Government as represented by the
5
# Administrator of the National Aeronautics and Space Administration.
8
# Licensed under the Apache License, Version 2.0 (the "License"); you may
9
# not use this file except in compliance with the License. You may obtain
10
# a copy of the License at
12
# http://www.apache.org/licenses/LICENSE-2.0
14
# Unless required by applicable law or agreed to in writing, software
15
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17
# License for the specific language governing permissions and limitations
20
"""CLI for the Direct API."""
23
eventlet.monkey_patch()
32
# If ../nova/__init__.py exists, add ../ to Python search path, so that
33
# it will override what happens to be installed in /usr/(local/)lib/python...
34
possible_topdir = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
37
if os.path.exists(os.path.join(possible_topdir, 'nova', '__init__.py')):
38
sys.path.insert(0, possible_topdir)
41
from nova import utils
45
gflags.DEFINE_string('host', '127.0.0.1', 'Direct API host')
46
gflags.DEFINE_integer('port', 8001, 'Direct API host')
47
gflags.DEFINE_string('user', 'user1', 'Direct API username')
48
gflags.DEFINE_string('project', 'proj1', 'Direct API project')
51
USAGE = """usage: stack [options] <controller> <method> [arg1=value arg2=value]
53
`stack help` should output the list of available controllers
54
`stack <controller>` should output the available methods for that controller
55
`stack help <controller>` should do the same
56
`stack help <controller> <method>` should output info for a method
61
"""Format help text, keys are labels and values are descriptions."""
62
indent = max([len(k) for k in d])
64
for k, v in d.iteritems():
65
t = textwrap.TextWrapper(initial_indent=' %s ' % k.ljust(indent),
66
subsequent_indent=' ' * (indent + 6))
72
rv = do_request('reflect', 'get_controllers')
74
return (USAGE + str(FLAGS.MainModuleHelp()) +
75
'\n\nAvailable controllers:\n' +
76
'\n'.join(out) + '\n')
79
def help_controller(controller):
80
rv = do_request('reflect', 'get_methods')
81
methods = dict([(k.split('/')[2], v) for k, v in rv.iteritems()
82
if k.startswith('/%s' % controller)])
83
return ('Available methods for %s:\n' % controller +
84
'\n'.join(format_help(methods)))
87
def help_method(controller, method):
88
rv = do_request('reflect',
90
{'method': '/%s/%s' % (controller, method)})
92
sig = '%s(%s):' % (method, ', '.join(['='.join(x) for x in rv['args']]))
93
out = textwrap.wrap(sig, subsequent_indent=' ' * len('%s(' % method))
94
out.append('\n' + rv['doc'])
98
def do_request(controller, method, params=None):
100
data = urllib.urlencode(params)
104
url = 'http://%s:%s/%s/%s' % (FLAGS.host, FLAGS.port, controller, method)
105
headers = {'X-OpenStack-User': FLAGS.user,
106
'X-OpenStack-Project': FLAGS.project}
108
req = urllib2.Request(url, data, headers)
109
resp = urllib2.urlopen(req)
110
return utils.loads(resp.read())
113
if __name__ == '__main__':
114
args = FLAGS(sys.argv)
126
params.append(args.pop(0))
127
action = help_controller
129
params.append(args.pop(0))
131
print action(*params)
136
print help_controller(controller)
142
key, value = x.split('=', 1)
145
pprint.pprint(do_request(controller, method, params))