root / snf-pithos-app / pithos / api / manage_accounts / cli / __init__.py @ 3046f62c
History | View | Annotate | Download (7.7 kB)
1 |
# Copyright 2012 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
from pithos.api.manage_accounts import ManageAccounts |
35 |
from synnefo.webproject.management.utils import pprint_table |
36 |
|
37 |
import argparse |
38 |
|
39 |
import os |
40 |
import sys |
41 |
|
42 |
|
43 |
def _double_list_str(l): |
44 |
return '\n'.join(', '.join(sublist) for sublist in l) |
45 |
|
46 |
|
47 |
def list(args): |
48 |
try:
|
49 |
utils = ManageAccounts() |
50 |
if args.only_duplicate:
|
51 |
accounts = utils.duplicate_accounts() |
52 |
else:
|
53 |
accounts = utils.existing_accounts() |
54 |
headers = ['uuid']
|
55 |
table = [(a,) for a in accounts] |
56 |
if args.output_format != "json" and not args.headers: |
57 |
headers = None
|
58 |
pprint_table(sys.stdout, table, headers, args.output_format) |
59 |
except Exception, e: |
60 |
sys.stderr.write('%s\n' % e)
|
61 |
finally:
|
62 |
utils.cleanup() |
63 |
|
64 |
|
65 |
def delete(args): |
66 |
try:
|
67 |
utils = ManageAccounts() |
68 |
utils.delete_account(args.delete_account, only_stats=True)
|
69 |
|
70 |
confirm = raw_input(
|
71 |
"Type 'yes' if you are sure you want "
|
72 |
"to remove those entries: "
|
73 |
) |
74 |
if not confirm == 'yes': |
75 |
return
|
76 |
else:
|
77 |
utils.delete_account( |
78 |
args.delete_account, only_stats=False, dry=args.dry
|
79 |
) |
80 |
except Exception, e: |
81 |
sys.stderr.write('%s\n' % e)
|
82 |
finally:
|
83 |
utils.cleanup() |
84 |
|
85 |
|
86 |
def merge(args): |
87 |
try:
|
88 |
utils = ManageAccounts() |
89 |
utils.merge_account( |
90 |
args.src_account, args.dest_account, only_stats=True
|
91 |
) |
92 |
|
93 |
confirm = raw_input(
|
94 |
"Type 'yes' if you are sure you want"
|
95 |
" to move those entries to %s: " % args.dest_account
|
96 |
) |
97 |
if not confirm == 'yes': |
98 |
return
|
99 |
else:
|
100 |
utils.merge_account( |
101 |
args.src_account, args.dest_account, only_stats=False,
|
102 |
dry=args.dry |
103 |
) |
104 |
except Exception, e: |
105 |
sys.stderr.write('%s\n' % e)
|
106 |
finally:
|
107 |
utils.cleanup() |
108 |
|
109 |
|
110 |
def export_quota(args): |
111 |
try:
|
112 |
utils = ManageAccounts() |
113 |
d = utils.backend.node.node_account_quotas() |
114 |
|
115 |
location = os.path.abspath(os.path.expanduser(args.location)) |
116 |
f = open(location, 'w') |
117 |
|
118 |
for uuid, capacity in d.iteritems(): |
119 |
f.write(' '.join([uuid, 'pithos.diskspace', capacity])) |
120 |
f.write('\n')
|
121 |
except Exception, e: |
122 |
sys.stderr.write('%s\n' % e)
|
123 |
finally:
|
124 |
utils.cleanup() |
125 |
|
126 |
|
127 |
def set_container_quota(args): |
128 |
try:
|
129 |
utils = ManageAccounts() |
130 |
try:
|
131 |
quota = int(args.quota)
|
132 |
except:
|
133 |
raise ValueError('Invalid quota') |
134 |
|
135 |
accounts = [args.account] if args.account \
|
136 |
else utils.existing_accounts()
|
137 |
|
138 |
failed = [] |
139 |
|
140 |
def update_container_policy(account): |
141 |
trans = utils.backend.wrapper.conn.begin() |
142 |
try:
|
143 |
utils.backend.update_container_policy( |
144 |
account, account, args.container, {'quota': quota}
|
145 |
) |
146 |
if args.dry:
|
147 |
print "Skipping database commit." |
148 |
trans.rollback() |
149 |
else:
|
150 |
trans.commit() |
151 |
except Exception, e: |
152 |
failed.append((account, e)) |
153 |
|
154 |
map(update_container_policy, accounts)
|
155 |
if failed:
|
156 |
sys.stdout.write( |
157 |
'Failed for the following accounts:\n'
|
158 |
) |
159 |
pprint_table(sys.stdout, failed, headers=[]) |
160 |
except Exception, e: |
161 |
sys.stderr.write('%s\n' % e)
|
162 |
finally:
|
163 |
utils.cleanup() |
164 |
|
165 |
|
166 |
def main(argv=None): |
167 |
# create the top-level parser
|
168 |
parser = argparse.ArgumentParser() |
169 |
subparsers = parser.add_subparsers() |
170 |
|
171 |
# create the parser for the "list" command
|
172 |
parser_list = subparsers.add_parser( |
173 |
'list', description="List existing accounts" |
174 |
) |
175 |
parser_list.add_argument( |
176 |
'--dublicate', dest='only_duplicate', action="store_true", |
177 |
default=False, help="Display only case insensitive duplicate accounts." |
178 |
) |
179 |
parser_list.add_argument( |
180 |
"--no-headers", dest="headers", action="store_false", default=True, |
181 |
help="Do not display headers"
|
182 |
) |
183 |
parser_list.add_argument( |
184 |
"--output-format", dest="output_format", metavar="[pretty, csv, json]", |
185 |
default="pretty", choices=["pretty", "csv", "json"], |
186 |
help="Select the output format: pretty [the default], tabs"
|
187 |
" [tab-separated output], csv [comma-separated output]")
|
188 |
parser_list.set_defaults(func=list)
|
189 |
|
190 |
# create the parser for the "delete" command
|
191 |
parser_delete = subparsers.add_parser('delete')
|
192 |
parser_delete.add_argument('delete_account')
|
193 |
parser_delete.add_argument( |
194 |
'--dry', action="store_true", default=False, |
195 |
help="Do not commit database changes."
|
196 |
) |
197 |
parser_delete.set_defaults(func=delete) |
198 |
|
199 |
# create the parser for the "merge" command
|
200 |
parser_merge = subparsers.add_parser('merge')
|
201 |
parser_merge.add_argument('src_account')
|
202 |
parser_merge.add_argument('dest_account')
|
203 |
parser_merge.add_argument( |
204 |
'--dry', action="store_true", default=False, |
205 |
help="Do not commit database changes."
|
206 |
) |
207 |
parser_merge.set_defaults(func=merge) |
208 |
|
209 |
# create the parser for the "export-quota" command
|
210 |
parser_export_quota = subparsers.add_parser('export-quota')
|
211 |
parser_export_quota.add_argument( |
212 |
'--location', dest='location', required=True, |
213 |
help="Where to save the exported quotas"
|
214 |
) |
215 |
parser_export_quota.set_defaults(func=export_quota) |
216 |
|
217 |
# create the parser for the "set-container-quota" command
|
218 |
parser_set_container_quota = subparsers.add_parser( |
219 |
'set-container-quota',
|
220 |
description="Set container quota for all or a specific account"
|
221 |
) |
222 |
parser_set_container_quota.add_argument( |
223 |
'--account', dest='account', |
224 |
help="Set container quota for a specific account"
|
225 |
) |
226 |
parser_set_container_quota.add_argument( |
227 |
'--dry', action="store_true", default=False, |
228 |
help="Do not commit database changes."
|
229 |
) |
230 |
parser_set_container_quota.add_argument('container')
|
231 |
parser_set_container_quota.add_argument('quota')
|
232 |
parser_set_container_quota.set_defaults(func=set_container_quota) |
233 |
|
234 |
args = parser.parse_args() |
235 |
args.func(args) |
236 |
|
237 |
if __name__ == '__main__': |
238 |
main(sys.argv) |