~ntt-pf-lab/nova/monkey_patch_notification

« back to all changes in this revision

Viewing changes to bin/nova-manage

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
3
 
 
4
# Copyright [2010] [Anso Labs, LLC]
 
5
#
 
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
 
9
#
 
10
#        http://www.apache.org/licenses/LICENSE-2.0
 
11
#
 
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.
 
17
"""
 
18
  CLI interface for nova management.
 
19
  Connects to the running ADMIN api in the api daemon.
 
20
"""
 
21
 
 
22
import sys
 
23
 
 
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
 
29
import time
 
30
 
 
31
FLAGS = flags.FLAGS
 
32
 
 
33
 
 
34
class UserCommands(object):
 
35
    def __init__(self):
 
36
        self.manager = users.UserManager.instance()
 
37
 
 
38
    def __print_export(self, user):
 
39
        print 'export EC2_ACCESS_KEY=%s' % user.access
 
40
        print 'export EC2_SECRET_KEY=%s' % user.secret
 
41
 
 
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)
 
47
 
 
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)
 
53
 
 
54
    def delete(self, name):
 
55
        """deletes an existing user
 
56
        arguments: name"""
 
57
        self.manager.delete_user(name)
 
58
 
 
59
    def exports(self, name):
 
60
        """prints access and secrets for user in export format
 
61
        arguments: name"""
 
62
        user = self.manager.get_user(name)
 
63
        if user:
 
64
            self.__print_export(user)
 
65
        else:
 
66
            print "User %s doesn't exist" % name
 
67
 
 
68
    def list(self):
 
69
        """lists all users
 
70
        arguments: <none>"""
 
71
        for user in self.manager.get_users():
 
72
            print user.name
 
73
 
 
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)
 
78
        if user:
 
79
            with open(filename, 'w') as f:
 
80
                f.write(user.get_credentials())
 
81
        else:
 
82
            print "User %s doesn't exist" % name
 
83
 
 
84
 
 
85
def usage(script_name):
 
86
    print script_name + " category action [<args>]"
 
87
 
 
88
 
 
89
categories = [
 
90
    ('user', UserCommands),
 
91
]
 
92
 
 
93
 
 
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]
 
99
 
 
100
 
 
101
def methods_of(obj):
 
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('_')]
 
105
 
 
106
 
 
107
if __name__ == '__main__':
 
108
    utils.default_flagfile()
 
109
    argv = FLAGS(sys.argv)
 
110
    script_name = argv.pop(0)
 
111
    if len(argv) < 1:
 
112
        usage(script_name)
 
113
        print "Available categories:"
 
114
        for k, v in categories:
 
115
            print "\t%s" % k
 
116
        sys.exit(2)
 
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:
 
122
            print "\t%s" % k
 
123
        sys.exit(2)
 
124
    if len(matches) > 1:
 
125
        print "%s matched multiple categories:" % category
 
126
        for k, v in matches:
 
127
            print "\t%s" % k
 
128
        sys.exit(2)
 
129
    # instantiate the command group object
 
130
    category, fn = matches[0]
 
131
    command_object = fn()
 
132
    actions = methods_of(command_object)
 
133
    if len(argv) < 1:
 
134
        usage(script_name)
 
135
        print "Available actions for %s category:" % category
 
136
        for k, v in actions:
 
137
            print "\t%s" % k
 
138
        sys.exit(2)
 
139
    action = argv.pop(0)
 
140
    matches = lazy_match(action, actions)
 
141
    if len(matches) == 0:
 
142
        print "%s does not match any actions" % action
 
143
        for k, v in actions:
 
144
            print "\t%s" % k
 
145
        sys.exit(2)
 
146
    if len(matches) > 1:
 
147
        print "%s matched multiple actions:" % action
 
148
        for k, v in matches:
 
149
            print "\t%s" % k
 
150
        sys.exit(2)
 
151
    action, fn = matches[0]
 
152
    # call the action with the remaining arguments
 
153
    try:
 
154
        fn(*argv)
 
155
    except TypeError:
 
156
        print "Wrong number of arguments supplied"
 
157
        print "%s %s: %s" % (category, action, fn.__doc__)
 
158