2
# vim: tabstop=4 shiftwidth=4 softtabstop=4
4
# Copyright [2010] [Anso Labs, LLC]
6
# Licensed under the Apache License, Version 2.0 (the "License");
7
# you may not use this file except in compliance with the License.
8
# You may obtain a copy of the License at
10
# http://www.apache.org/licenses/LICENSE-2.0
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS,
14
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
# See the License for the specific language governing permissions and
16
# limitations under the License.
18
CLI interface for nova management.
19
Connects to the running ADMIN api in the api daemon.
24
from nova import flags
25
from nova import utils
26
from nova.auth import users
27
from nova.compute import model
28
from nova.endpoint import cloud
34
class UserCommands(object):
36
self.manager = users.UserManager.instance()
38
def __print_export(self, user):
39
print 'export EC2_ACCESS_KEY=%s' % user.access
40
print 'export EC2_SECRET_KEY=%s' % user.secret
42
def admin(self, name, access=None, secret=None):
43
"""creates a new admin and prints exports
44
arguments: name [access] [secret]"""
45
user = self.manager.create_user(name, access, secret, True)
46
self.__print_export(user)
48
def create(self, name, access=None, secret=None):
49
"""creates a new user and prints exports
50
arguments: name [access] [secret]"""
51
user = self.manager.create_user(name, access, secret, False)
52
self.__print_export(user)
54
def delete(self, name):
55
"""deletes an existing user
57
self.manager.delete_user(name)
59
def exports(self, name):
60
"""prints access and secrets for user in export format
62
user = self.manager.get_user(name)
64
self.__print_export(user)
66
print "User %s doesn't exist" % name
71
for user in self.manager.get_users():
74
def zip(self, name, filename='nova.zip'):
75
"""exports credentials for user to a zip file
76
arguments: name [filename='nova.zip]"""
77
user = self.manager.get_user(name)
79
with open(filename, 'w') as f:
80
f.write(user.get_credentials())
82
print "User %s doesn't exist" % name
85
def usage(script_name):
86
print script_name + " category action [<args>]"
90
('user', UserCommands),
94
def lazy_match(name, key_value_tuples):
95
"""finds all objects that have a key that case insensitively contains [name]
96
key_value_tuples is a list of tuples of the form (key, value)
97
returns a list of tuples of the form (key, value)"""
98
return [(k, v) for (k, v) in key_value_tuples if k.lower().find(name.lower()) == 0]
102
"""get all callable methods of an object that don't start with underscore
103
returns a list of tuples of the form (method_name, method)"""
104
return [(i, getattr(obj, i)) for i in dir(obj) if callable(getattr(obj, i)) and not i.startswith('_')]
107
if __name__ == '__main__':
108
utils.default_flagfile()
109
argv = FLAGS(sys.argv)
110
script_name = argv.pop(0)
113
print "Available categories:"
114
for k, v in categories:
117
category = argv.pop(0)
118
matches = lazy_match(category, categories)
119
if len(matches) == 0:
120
print "%s does not match any categories:" % category
121
for k, v in categories:
125
print "%s matched multiple categories:" % category
129
# instantiate the command group object
130
category, fn = matches[0]
131
command_object = fn()
132
actions = methods_of(command_object)
135
print "Available actions for %s category:" % category
140
matches = lazy_match(action, actions)
141
if len(matches) == 0:
142
print "%s does not match any actions" % action
147
print "%s matched multiple actions:" % action
151
action, fn = matches[0]
152
# call the action with the remaining arguments
156
print "Wrong number of arguments supplied"
157
print "%s %s: %s" % (category, action, fn.__doc__)