root / kamaki / cli / commands / __init__.py @ f1e5b343
History | View | Annotate | Download (7 kB)
1 | e3f01d64 | Stavros Sachtouris | # Copyright 2011-2013 GRNET S.A. All rights reserved.
|
---|---|---|---|
2 | 5eae854d | Stavros Sachtouris | #
|
3 | 5eae854d | Stavros Sachtouris | # Redistribution and use in source and binary forms, with or
|
4 | 5eae854d | Stavros Sachtouris | # without modification, are permitted provided that the following
|
5 | 5eae854d | Stavros Sachtouris | # conditions are met:
|
6 | 5eae854d | Stavros Sachtouris | #
|
7 | 5eae854d | Stavros Sachtouris | # 1. Redistributions of source code must retain the above
|
8 | 5eae854d | Stavros Sachtouris | # copyright notice, this list of conditions and the following
|
9 | 5eae854d | Stavros Sachtouris | # disclaimer.
|
10 | 5eae854d | Stavros Sachtouris | #
|
11 | 5eae854d | Stavros Sachtouris | # 2. Redistributions in binary form must reproduce the above
|
12 | 5eae854d | Stavros Sachtouris | # copyright notice, this list of conditions and the following
|
13 | 5eae854d | Stavros Sachtouris | # disclaimer in the documentation and/or other materials
|
14 | 5eae854d | Stavros Sachtouris | # provided with the distribution.
|
15 | 5eae854d | Stavros Sachtouris | #
|
16 | 5eae854d | Stavros Sachtouris | # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 | 5eae854d | Stavros Sachtouris | # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 | 5eae854d | Stavros Sachtouris | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 | 5eae854d | Stavros Sachtouris | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 | 5eae854d | Stavros Sachtouris | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 | 5eae854d | Stavros Sachtouris | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 | 5eae854d | Stavros Sachtouris | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 | 5eae854d | Stavros Sachtouris | # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 | 5eae854d | Stavros Sachtouris | # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 | 5eae854d | Stavros Sachtouris | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 | 5eae854d | Stavros Sachtouris | # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 | 5eae854d | Stavros Sachtouris | # POSSIBILITY OF SUCH DAMAGE.
|
28 | 5eae854d | Stavros Sachtouris | #
|
29 | 5eae854d | Stavros Sachtouris | # The views and conclusions contained in the software and
|
30 | 5eae854d | Stavros Sachtouris | # documentation are those of the authors and should not be
|
31 | 5eae854d | Stavros Sachtouris | # interpreted as representing official policies, either expressed
|
32 | 5eae854d | Stavros Sachtouris | # or implied, of GRNET S.A.command
|
33 | 5eae854d | Stavros Sachtouris | |
34 | 137c51f5 | Stavros Sachtouris | from kamaki.cli.logger import get_logger |
35 | 915b99b5 | Stavros Sachtouris | from kamaki.cli.utils import print_json, print_items |
36 | 915b99b5 | Stavros Sachtouris | from kamaki.cli.argument import FlagArgument |
37 | 6069b53b | Stavros Sachtouris | |
38 | 0d4a6d0a | Stavros Sachtouris | log = get_logger(__name__) |
39 | 6069b53b | Stavros Sachtouris | |
40 | 234954d1 | Stavros Sachtouris | |
41 | b4f69041 | Stavros Sachtouris | def DontRaiseKeyError(foo): |
42 | b4f69041 | Stavros Sachtouris | def wrap(*args, **kwargs): |
43 | b4f69041 | Stavros Sachtouris | try:
|
44 | b4f69041 | Stavros Sachtouris | return foo(*args, **kwargs)
|
45 | b4f69041 | Stavros Sachtouris | except KeyError: |
46 | b4f69041 | Stavros Sachtouris | return None |
47 | b4f69041 | Stavros Sachtouris | return wrap
|
48 | b4f69041 | Stavros Sachtouris | |
49 | b4f69041 | Stavros Sachtouris | |
50 | b4f69041 | Stavros Sachtouris | def addLogSettings(foo): |
51 | b4f69041 | Stavros Sachtouris | def wrap(self, *args, **kwargs): |
52 | b4f69041 | Stavros Sachtouris | try:
|
53 | b4f69041 | Stavros Sachtouris | return foo(self, *args, **kwargs) |
54 | b4f69041 | Stavros Sachtouris | finally:
|
55 | b4f69041 | Stavros Sachtouris | self._set_log_params()
|
56 | b4f69041 | Stavros Sachtouris | self._update_max_threads
|
57 | b4f69041 | Stavros Sachtouris | return wrap
|
58 | b4f69041 | Stavros Sachtouris | |
59 | b4f69041 | Stavros Sachtouris | |
60 | 5eae854d | Stavros Sachtouris | class _command_init(object): |
61 | 36526b3c | Stavros Sachtouris | |
62 | 844a6bdb | Stavros Sachtouris | def __init__(self, arguments={}, auth_base=None, cloud=None): |
63 | e15d78e2 | Stavros Sachtouris | if hasattr(self, 'arguments'): |
64 | e15d78e2 | Stavros Sachtouris | arguments.update(self.arguments)
|
65 | 915b99b5 | Stavros Sachtouris | if isinstance(self, _optional_output_cmd): |
66 | 915b99b5 | Stavros Sachtouris | arguments.update(self.oo_arguments)
|
67 | 545c6c29 | Stavros Sachtouris | if isinstance(self, _optional_json): |
68 | 545c6c29 | Stavros Sachtouris | arguments.update(self.oj_arguments)
|
69 | 60c42f9f | Stavros Sachtouris | try:
|
70 | 60c42f9f | Stavros Sachtouris | arguments.update(self.wait_arguments)
|
71 | 60c42f9f | Stavros Sachtouris | except AttributeError: |
72 | 60c42f9f | Stavros Sachtouris | pass
|
73 | e15d78e2 | Stavros Sachtouris | self.arguments = dict(arguments) |
74 | 5eae854d | Stavros Sachtouris | try:
|
75 | e15d78e2 | Stavros Sachtouris | self.config = self['config'] |
76 | 5eae854d | Stavros Sachtouris | except KeyError: |
77 | 5eae854d | Stavros Sachtouris | pass
|
78 | 844a6bdb | Stavros Sachtouris | self.auth_base = auth_base or getattr(self, 'auth_base', None) |
79 | 844a6bdb | Stavros Sachtouris | self.cloud = cloud or getattr(self, 'cloud', None) |
80 | 5eae854d | Stavros Sachtouris | |
81 | b4f69041 | Stavros Sachtouris | @DontRaiseKeyError
|
82 | b4f69041 | Stavros Sachtouris | def _custom_url(self, service): |
83 | 144b3551 | Stavros Sachtouris | return self.config.get_cloud(self.cloud, '%s_url' % service) |
84 | b4f69041 | Stavros Sachtouris | |
85 | b4f69041 | Stavros Sachtouris | @DontRaiseKeyError
|
86 | b4f69041 | Stavros Sachtouris | def _custom_token(self, service): |
87 | 144b3551 | Stavros Sachtouris | return self.config.get_cloud(self.cloud, '%s_token' % service) |
88 | b4f69041 | Stavros Sachtouris | |
89 | b4f69041 | Stavros Sachtouris | @DontRaiseKeyError
|
90 | b4f69041 | Stavros Sachtouris | def _custom_type(self, service): |
91 | 144b3551 | Stavros Sachtouris | return self.config.get_cloud(self.cloud, '%s_type' % service) |
92 | b4f69041 | Stavros Sachtouris | |
93 | b4f69041 | Stavros Sachtouris | @DontRaiseKeyError
|
94 | b4f69041 | Stavros Sachtouris | def _custom_version(self, service): |
95 | 144b3551 | Stavros Sachtouris | return self.config.get_cloud(self.cloud, '%s_version' % service) |
96 | b4f69041 | Stavros Sachtouris | |
97 | f1e5b343 | Stavros Sachtouris | def _uuids2usernames(self, uuids): |
98 | f1e5b343 | Stavros Sachtouris | return self.auth_base.post_user_catalogs(uuids).json['uuid_catalog'] |
99 | f1e5b343 | Stavros Sachtouris | |
100 | f1e5b343 | Stavros Sachtouris | def _usernames2uuids(self, username): |
101 | f1e5b343 | Stavros Sachtouris | return self.auth_base.post_user_catalogs( |
102 | f1e5b343 | Stavros Sachtouris | displaynames=username).json['displayname_catalog']
|
103 | f1e5b343 | Stavros Sachtouris | |
104 | 95641ecc | Stavros Sachtouris | def _uuid2username(self, uuid): |
105 | f1e5b343 | Stavros Sachtouris | return self._uuids2usernames([uuid]).get(uuid, None) |
106 | 95641ecc | Stavros Sachtouris | |
107 | 95641ecc | Stavros Sachtouris | def _username2uuid(self, username): |
108 | f1e5b343 | Stavros Sachtouris | return self._usernames2uuids([username]).get(username, None) |
109 | 95641ecc | Stavros Sachtouris | |
110 | f47417e7 | Stavros Sachtouris | def _set_log_params(self): |
111 | b3bb083f | Stavros Sachtouris | try:
|
112 | 5fdccdec | Stavros Sachtouris | self.client.LOG_TOKEN, self.client.LOG_DATA = ( |
113 | 362adf50 | Stavros Sachtouris | self['config'].get_global('log_token').lower() == 'on', |
114 | 362adf50 | Stavros Sachtouris | self['config'].get_global('log_data').lower() == 'on') |
115 | c5b9380c | Stavros Sachtouris | except Exception as e: |
116 | 47f37f7c | Stavros Sachtouris | log.debug('Failed to read custom log settings:'
|
117 | 0d4a6d0a | Stavros Sachtouris | '%s\n defaults for token and data logging are off' % e)
|
118 | c5b9380c | Stavros Sachtouris | |
119 | c5b9380c | Stavros Sachtouris | def _update_max_threads(self): |
120 | b4f69041 | Stavros Sachtouris | if getattr(self, 'client', None): |
121 | 362adf50 | Stavros Sachtouris | max_threads = int(self['config'].get_global('max_threads')) |
122 | c5b9380c | Stavros Sachtouris | assert max_threads > 0 |
123 | c5b9380c | Stavros Sachtouris | self.client.MAX_THREADS = max_threads
|
124 | 5fdccdec | Stavros Sachtouris | |
125 | 5a673575 | Stavros Sachtouris | def _safe_progress_bar(self, msg, arg='progress_bar'): |
126 | 5a673575 | Stavros Sachtouris | """Try to get a progress bar, but do not raise errors"""
|
127 | 5a673575 | Stavros Sachtouris | try:
|
128 | 5a673575 | Stavros Sachtouris | progress_bar = self.arguments[arg]
|
129 | 5a673575 | Stavros Sachtouris | gen = progress_bar.get_generator(msg) |
130 | 5a673575 | Stavros Sachtouris | except Exception: |
131 | 5a673575 | Stavros Sachtouris | return (None, None) |
132 | 5a673575 | Stavros Sachtouris | return (progress_bar, gen)
|
133 | 5a673575 | Stavros Sachtouris | |
134 | 5a673575 | Stavros Sachtouris | def _safe_progress_bar_finish(self, progress_bar): |
135 | 5a673575 | Stavros Sachtouris | try:
|
136 | 5a673575 | Stavros Sachtouris | progress_bar.finish() |
137 | 5a673575 | Stavros Sachtouris | except Exception: |
138 | 5a673575 | Stavros Sachtouris | pass
|
139 | 5a673575 | Stavros Sachtouris | |
140 | 5a37a189 | Stavros Sachtouris | def __getitem__(self, argterm): |
141 | 5a37a189 | Stavros Sachtouris | """
|
142 | 5a37a189 | Stavros Sachtouris | :param argterm: (str) the name/label of an argument in self.arguments
|
143 | 5a37a189 | Stavros Sachtouris |
|
144 | b113e74b | Stavros Sachtouris | :returns: the value of the corresponding Argument (not the argument
|
145 | b113e74b | Stavros Sachtouris | object)
|
146 | 5a37a189 | Stavros Sachtouris |
|
147 | 5a37a189 | Stavros Sachtouris | :raises KeyError: if argterm not in self.arguments of this object
|
148 | 5a37a189 | Stavros Sachtouris | """
|
149 | b113e74b | Stavros Sachtouris | return self.arguments[argterm].value |
150 | 5a37a189 | Stavros Sachtouris | |
151 | 5a37a189 | Stavros Sachtouris | def __setitem__(self, argterm, arg): |
152 | 5a37a189 | Stavros Sachtouris | """Install an argument as argterm
|
153 | 5a37a189 | Stavros Sachtouris | If argterm points to another argument, the other argument is lost
|
154 | 5a37a189 | Stavros Sachtouris |
|
155 | 5a37a189 | Stavros Sachtouris | :param argterm: (str)
|
156 | 5a37a189 | Stavros Sachtouris |
|
157 | 5a37a189 | Stavros Sachtouris | :param arg: (Argument)
|
158 | 5a37a189 | Stavros Sachtouris | """
|
159 | 5a37a189 | Stavros Sachtouris | if not hasattr(self, 'arguments'): |
160 | 5a37a189 | Stavros Sachtouris | self.arguments = {}
|
161 | 5a37a189 | Stavros Sachtouris | self.arguments[argterm] = arg
|
162 | 5a37a189 | Stavros Sachtouris | |
163 | b113e74b | Stavros Sachtouris | def get_argument_object(self, argterm): |
164 | b113e74b | Stavros Sachtouris | """
|
165 | b113e74b | Stavros Sachtouris | :param argterm: (str) the name/label of an argument in self.arguments
|
166 | b113e74b | Stavros Sachtouris |
|
167 | b113e74b | Stavros Sachtouris | :returns: the arument object
|
168 | b113e74b | Stavros Sachtouris |
|
169 | b113e74b | Stavros Sachtouris | :raises KeyError: if argterm not in self.arguments of this object
|
170 | b113e74b | Stavros Sachtouris | """
|
171 | b113e74b | Stavros Sachtouris | return self.arguments[argterm] |
172 | b113e74b | Stavros Sachtouris | |
173 | 5eae854d | Stavros Sachtouris | def get_argument(self, argterm): |
174 | 5a37a189 | Stavros Sachtouris | """
|
175 | 5a37a189 | Stavros Sachtouris | :param argterm: (str) the name/label of an argument in self.arguments
|
176 | 5a37a189 | Stavros Sachtouris |
|
177 | 5a37a189 | Stavros Sachtouris | :returns: the value of the arument object
|
178 | 5a37a189 | Stavros Sachtouris |
|
179 | 5a37a189 | Stavros Sachtouris | :raises KeyError: if argterm not in self.arguments of this object
|
180 | 5a37a189 | Stavros Sachtouris | """
|
181 | e15d78e2 | Stavros Sachtouris | return self[argterm] |
182 | 915b99b5 | Stavros Sachtouris | |
183 | 915b99b5 | Stavros Sachtouris | |
184 | 545c6c29 | Stavros Sachtouris | # feature classes - inherit them to get special features for your commands
|
185 | 545c6c29 | Stavros Sachtouris | |
186 | 545c6c29 | Stavros Sachtouris | |
187 | 915b99b5 | Stavros Sachtouris | class _optional_output_cmd(object): |
188 | 915b99b5 | Stavros Sachtouris | |
189 | 915b99b5 | Stavros Sachtouris | oo_arguments = dict(
|
190 | 915b99b5 | Stavros Sachtouris | with_output=FlagArgument('show response headers', ('--with-output')), |
191 | 915b99b5 | Stavros Sachtouris | json_output=FlagArgument('show headers in json', ('-j', '--json')) |
192 | 915b99b5 | Stavros Sachtouris | ) |
193 | 915b99b5 | Stavros Sachtouris | |
194 | 915b99b5 | Stavros Sachtouris | def _optional_output(self, r): |
195 | 915b99b5 | Stavros Sachtouris | if self['json_output']: |
196 | 915b99b5 | Stavros Sachtouris | print_json(r) |
197 | 915b99b5 | Stavros Sachtouris | elif self['with_output']: |
198 | 915b99b5 | Stavros Sachtouris | print_items([r] if isinstance(r, dict) else r) |
199 | 545c6c29 | Stavros Sachtouris | |
200 | 545c6c29 | Stavros Sachtouris | |
201 | 545c6c29 | Stavros Sachtouris | class _optional_json(object): |
202 | 545c6c29 | Stavros Sachtouris | |
203 | 545c6c29 | Stavros Sachtouris | oj_arguments = dict(
|
204 | 545c6c29 | Stavros Sachtouris | json_output=FlagArgument('show headers in json', ('-j', '--json')) |
205 | 545c6c29 | Stavros Sachtouris | ) |
206 | 545c6c29 | Stavros Sachtouris | |
207 | 545c6c29 | Stavros Sachtouris | def _print(self, output, print_method=print_items, **print_method_kwargs): |
208 | 545c6c29 | Stavros Sachtouris | if self['json_output']: |
209 | 545c6c29 | Stavros Sachtouris | print_json(output) |
210 | 545c6c29 | Stavros Sachtouris | else:
|
211 | 545c6c29 | Stavros Sachtouris | print_method(output, **print_method_kwargs) |