root / kamaki / cli / commands / __init__.py @ 362adf50
History | View | Annotate | Download (5.9 kB)
1 | 5eae854d | Stavros Sachtouris | # Copyright 2011-2012 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 | 362adf50 | Stavros Sachtouris | from kamaki.cli.errors import CLIError |
38 | 362adf50 | Stavros Sachtouris | from kamaki.clients import Client |
39 | 6069b53b | Stavros Sachtouris | |
40 | 0d4a6d0a | Stavros Sachtouris | log = get_logger(__name__) |
41 | 6069b53b | Stavros Sachtouris | |
42 | 234954d1 | Stavros Sachtouris | |
43 | 5eae854d | Stavros Sachtouris | class _command_init(object): |
44 | 36526b3c | Stavros Sachtouris | |
45 | 362adf50 | Stavros Sachtouris | def __init__(self, arguments={}, auth_base_or_remote=None): |
46 | e15d78e2 | Stavros Sachtouris | if hasattr(self, 'arguments'): |
47 | e15d78e2 | Stavros Sachtouris | arguments.update(self.arguments)
|
48 | 915b99b5 | Stavros Sachtouris | if isinstance(self, _optional_output_cmd): |
49 | 915b99b5 | Stavros Sachtouris | arguments.update(self.oo_arguments)
|
50 | 545c6c29 | Stavros Sachtouris | if isinstance(self, _optional_json): |
51 | 545c6c29 | Stavros Sachtouris | arguments.update(self.oj_arguments)
|
52 | e15d78e2 | Stavros Sachtouris | self.arguments = dict(arguments) |
53 | 5eae854d | Stavros Sachtouris | try:
|
54 | e15d78e2 | Stavros Sachtouris | self.config = self['config'] |
55 | 5eae854d | Stavros Sachtouris | except KeyError: |
56 | 5eae854d | Stavros Sachtouris | pass
|
57 | 362adf50 | Stavros Sachtouris | if isinstance(auth_base_or_remote, Client): |
58 | 362adf50 | Stavros Sachtouris | self.auth_base = auth_base_or_remote
|
59 | 362adf50 | Stavros Sachtouris | elif not getattr(self, 'auth_base', None): |
60 | 362adf50 | Stavros Sachtouris | self.remote = auth_base_or_remote
|
61 | 362adf50 | Stavros Sachtouris | if not self.remote: |
62 | 362adf50 | Stavros Sachtouris | raise CLIError('CRITICAL: No cloud specified', 3) |
63 | 5eae854d | Stavros Sachtouris | |
64 | f47417e7 | Stavros Sachtouris | def _set_log_params(self): |
65 | b3bb083f | Stavros Sachtouris | try:
|
66 | 5fdccdec | Stavros Sachtouris | self.client.LOG_TOKEN, self.client.LOG_DATA = ( |
67 | 362adf50 | Stavros Sachtouris | self['config'].get_global('log_token').lower() == 'on', |
68 | 362adf50 | Stavros Sachtouris | self['config'].get_global('log_data').lower() == 'on') |
69 | c5b9380c | Stavros Sachtouris | except Exception as e: |
70 | 0d4a6d0a | Stavros Sachtouris | log.warning('Failed to read custom log settings:'
|
71 | 0d4a6d0a | Stavros Sachtouris | '%s\n defaults for token and data logging are off' % e)
|
72 | c5b9380c | Stavros Sachtouris | |
73 | c5b9380c | Stavros Sachtouris | def _update_max_threads(self): |
74 | c5b9380c | Stavros Sachtouris | try:
|
75 | 362adf50 | Stavros Sachtouris | max_threads = int(self['config'].get_global('max_threads')) |
76 | c5b9380c | Stavros Sachtouris | assert max_threads > 0 |
77 | c5b9380c | Stavros Sachtouris | self.client.MAX_THREADS = max_threads
|
78 | c5b9380c | Stavros Sachtouris | except Exception as e: |
79 | 0d4a6d0a | Stavros Sachtouris | log.warning('Failed to read custom thread settings: '
|
80 | 0d4a6d0a | Stavros Sachtouris | '%s, use default max threads (%s)' % (
|
81 | 0d4a6d0a | Stavros Sachtouris | e, self.client.MAX_THREADS))
|
82 | 5fdccdec | Stavros Sachtouris | |
83 | 5a673575 | Stavros Sachtouris | def _safe_progress_bar(self, msg, arg='progress_bar'): |
84 | 5a673575 | Stavros Sachtouris | """Try to get a progress bar, but do not raise errors"""
|
85 | 5a673575 | Stavros Sachtouris | try:
|
86 | 5a673575 | Stavros Sachtouris | progress_bar = self.arguments[arg]
|
87 | 5a673575 | Stavros Sachtouris | gen = progress_bar.get_generator(msg) |
88 | 5a673575 | Stavros Sachtouris | except Exception: |
89 | 5a673575 | Stavros Sachtouris | return (None, None) |
90 | 5a673575 | Stavros Sachtouris | return (progress_bar, gen)
|
91 | 5a673575 | Stavros Sachtouris | |
92 | 5a673575 | Stavros Sachtouris | def _safe_progress_bar_finish(self, progress_bar): |
93 | 5a673575 | Stavros Sachtouris | try:
|
94 | 5a673575 | Stavros Sachtouris | progress_bar.finish() |
95 | 5a673575 | Stavros Sachtouris | except Exception: |
96 | 5a673575 | Stavros Sachtouris | pass
|
97 | 5a673575 | Stavros Sachtouris | |
98 | 5a37a189 | Stavros Sachtouris | def __getitem__(self, argterm): |
99 | 5a37a189 | Stavros Sachtouris | """
|
100 | 5a37a189 | Stavros Sachtouris | :param argterm: (str) the name/label of an argument in self.arguments
|
101 | 5a37a189 | Stavros Sachtouris |
|
102 | b113e74b | Stavros Sachtouris | :returns: the value of the corresponding Argument (not the argument
|
103 | b113e74b | Stavros Sachtouris | object)
|
104 | 5a37a189 | Stavros Sachtouris |
|
105 | 5a37a189 | Stavros Sachtouris | :raises KeyError: if argterm not in self.arguments of this object
|
106 | 5a37a189 | Stavros Sachtouris | """
|
107 | b113e74b | Stavros Sachtouris | return self.arguments[argterm].value |
108 | 5a37a189 | Stavros Sachtouris | |
109 | 5a37a189 | Stavros Sachtouris | def __setitem__(self, argterm, arg): |
110 | 5a37a189 | Stavros Sachtouris | """Install an argument as argterm
|
111 | 5a37a189 | Stavros Sachtouris | If argterm points to another argument, the other argument is lost
|
112 | 5a37a189 | Stavros Sachtouris |
|
113 | 5a37a189 | Stavros Sachtouris | :param argterm: (str)
|
114 | 5a37a189 | Stavros Sachtouris |
|
115 | 5a37a189 | Stavros Sachtouris | :param arg: (Argument)
|
116 | 5a37a189 | Stavros Sachtouris | """
|
117 | 5a37a189 | Stavros Sachtouris | if not hasattr(self, 'arguments'): |
118 | 5a37a189 | Stavros Sachtouris | self.arguments = {}
|
119 | 5a37a189 | Stavros Sachtouris | self.arguments[argterm] = arg
|
120 | 5a37a189 | Stavros Sachtouris | |
121 | b113e74b | Stavros Sachtouris | def get_argument_object(self, argterm): |
122 | b113e74b | Stavros Sachtouris | """
|
123 | b113e74b | Stavros Sachtouris | :param argterm: (str) the name/label of an argument in self.arguments
|
124 | b113e74b | Stavros Sachtouris |
|
125 | b113e74b | Stavros Sachtouris | :returns: the arument object
|
126 | b113e74b | Stavros Sachtouris |
|
127 | b113e74b | Stavros Sachtouris | :raises KeyError: if argterm not in self.arguments of this object
|
128 | b113e74b | Stavros Sachtouris | """
|
129 | b113e74b | Stavros Sachtouris | return self.arguments[argterm] |
130 | b113e74b | Stavros Sachtouris | |
131 | 5eae854d | Stavros Sachtouris | def get_argument(self, argterm): |
132 | 5a37a189 | Stavros Sachtouris | """
|
133 | 5a37a189 | Stavros Sachtouris | :param argterm: (str) the name/label of an argument in self.arguments
|
134 | 5a37a189 | Stavros Sachtouris |
|
135 | 5a37a189 | Stavros Sachtouris | :returns: the value of the arument object
|
136 | 5a37a189 | Stavros Sachtouris |
|
137 | 5a37a189 | Stavros Sachtouris | :raises KeyError: if argterm not in self.arguments of this object
|
138 | 5a37a189 | Stavros Sachtouris | """
|
139 | e15d78e2 | Stavros Sachtouris | return self[argterm] |
140 | 915b99b5 | Stavros Sachtouris | |
141 | 915b99b5 | Stavros Sachtouris | |
142 | 545c6c29 | Stavros Sachtouris | # feature classes - inherit them to get special features for your commands
|
143 | 545c6c29 | Stavros Sachtouris | |
144 | 545c6c29 | Stavros Sachtouris | |
145 | 915b99b5 | Stavros Sachtouris | class _optional_output_cmd(object): |
146 | 915b99b5 | Stavros Sachtouris | |
147 | 915b99b5 | Stavros Sachtouris | oo_arguments = dict(
|
148 | 915b99b5 | Stavros Sachtouris | with_output=FlagArgument('show response headers', ('--with-output')), |
149 | 915b99b5 | Stavros Sachtouris | json_output=FlagArgument('show headers in json', ('-j', '--json')) |
150 | 915b99b5 | Stavros Sachtouris | ) |
151 | 915b99b5 | Stavros Sachtouris | |
152 | 915b99b5 | Stavros Sachtouris | def _optional_output(self, r): |
153 | 915b99b5 | Stavros Sachtouris | if self['json_output']: |
154 | 915b99b5 | Stavros Sachtouris | print_json(r) |
155 | 915b99b5 | Stavros Sachtouris | elif self['with_output']: |
156 | 915b99b5 | Stavros Sachtouris | print_items([r] if isinstance(r, dict) else r) |
157 | 545c6c29 | Stavros Sachtouris | |
158 | 545c6c29 | Stavros Sachtouris | |
159 | 545c6c29 | Stavros Sachtouris | class _optional_json(object): |
160 | 545c6c29 | Stavros Sachtouris | |
161 | 545c6c29 | Stavros Sachtouris | oj_arguments = dict(
|
162 | 545c6c29 | Stavros Sachtouris | json_output=FlagArgument('show headers in json', ('-j', '--json')) |
163 | 545c6c29 | Stavros Sachtouris | ) |
164 | 545c6c29 | Stavros Sachtouris | |
165 | 545c6c29 | Stavros Sachtouris | def _print(self, output, print_method=print_items, **print_method_kwargs): |
166 | 545c6c29 | Stavros Sachtouris | if self['json_output']: |
167 | 545c6c29 | Stavros Sachtouris | print_json(output) |
168 | 545c6c29 | Stavros Sachtouris | else:
|
169 | 545c6c29 | Stavros Sachtouris | print_method(output, **print_method_kwargs) |