]> git.proxmox.com Git - ceph.git/blame - ceph/src/rgw/rgw_admin.cc
update sources to v12.1.2
[ceph.git] / ceph / src / rgw / rgw_admin.cc
CommitLineData
7c673cae
FG
1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2// vim: ts=8 sw=2 smarttab
3
4#include <errno.h>
5#include <iostream>
6#include <sstream>
7#include <string>
8
9#include <boost/optional.hpp>
10
11#include "auth/Crypto.h"
12
13#include "common/armor.h"
14#include "common/ceph_json.h"
15#include "common/config.h"
16#include "common/ceph_argparse.h"
17#include "common/Formatter.h"
18#include "common/errno.h"
19#include "common/safe_io.h"
20
21#include "cls/rgw/cls_rgw_client.h"
22
23#include "global/global_init.h"
24
25#include "include/utime.h"
26#include "include/str_list.h"
27
28#include "rgw_user.h"
29#include "rgw_bucket.h"
30#include "rgw_rados.h"
31#include "rgw_acl.h"
32#include "rgw_acl_s3.h"
33#include "rgw_lc.h"
34#include "rgw_log.h"
35#include "rgw_formats.h"
36#include "rgw_usage.h"
37#include "rgw_replica_log.h"
38#include "rgw_orphan.h"
39#include "rgw_sync.h"
40#include "rgw_data_sync.h"
41#include "rgw_rest_conn.h"
42#include "rgw_realm_watcher.h"
43#include "rgw_role.h"
31f18b77 44#include "rgw_reshard.h"
7c673cae
FG
45
46using namespace std;
47
48#define dout_context g_ceph_context
49#define dout_subsys ceph_subsys_rgw
50
51#define SECRET_KEY_LEN 40
52#define PUBLIC_ID_LEN 20
53
54static RGWRados *store = NULL;
55
56void _usage()
57{
58 cout << "usage: radosgw-admin <cmd> [options...]" << std::endl;
59 cout << "commands:\n";
60 cout << " user create create a new user\n" ;
61 cout << " user modify modify user\n";
62 cout << " user info get user info\n";
63 cout << " user rm remove user\n";
64 cout << " user suspend suspend a user\n";
65 cout << " user enable re-enable user after suspension\n";
66 cout << " user check check user info\n";
67 cout << " user stats show user stats as accounted by quota subsystem\n";
68 cout << " user list list users\n";
69 cout << " caps add add user capabilities\n";
70 cout << " caps rm remove user capabilities\n";
71 cout << " subuser create create a new subuser\n" ;
72 cout << " subuser modify modify subuser\n";
73 cout << " subuser rm remove subuser\n";
74 cout << " key create create access key\n";
75 cout << " key rm remove access key\n";
76 cout << " bucket list list buckets\n";
77 cout << " bucket limit check show bucket sharding stats\n";
78 cout << " bucket link link bucket to specified user\n";
79 cout << " bucket unlink unlink bucket from specified user\n";
80 cout << " bucket stats returns bucket statistics\n";
81 cout << " bucket rm remove bucket\n";
82 cout << " bucket check check bucket index\n";
83 cout << " bucket reshard reshard bucket\n";
c07f9fc5
FG
84 cout << " bucket sync disable disable bucket sync\n";
85 cout << " bucket sync enable enable bucket sync\n";
7c673cae
FG
86 cout << " bi get retrieve bucket index object entries\n";
87 cout << " bi put store bucket index object entries\n";
88 cout << " bi list list raw bucket index entries\n";
89 cout << " object rm remove object\n";
90 cout << " object stat stat an object for its metadata\n";
91 cout << " object unlink unlink object from bucket index\n";
92 cout << " objects expire run expired objects cleanup\n";
93 cout << " period delete delete a period\n";
94 cout << " period get get period info\n";
95 cout << " period get-current get current period info\n";
96 cout << " period pull pull a period\n";
97 cout << " period push push a period\n";
98 cout << " period list list all periods\n";
99 cout << " period update update the staging period\n";
100 cout << " period commit commit the staging period\n";
101 cout << " quota set set quota params\n";
102 cout << " quota enable enable quota\n";
103 cout << " quota disable disable quota\n";
104 cout << " global quota get view global quota params\n";
105 cout << " global quota set set global quota params\n";
106 cout << " global quota enable enable a global quota\n";
107 cout << " global quota disable disable a global quota\n";
108 cout << " realm create create a new realm\n";
109 cout << " realm delete delete a realm\n";
110 cout << " realm get show realm info\n";
111 cout << " realm get-default get default realm name\n";
112 cout << " realm list list realms\n";
113 cout << " realm list-periods list all realm periods\n";
114 cout << " realm remove remove a zonegroup from the realm\n";
115 cout << " realm rename rename a realm\n";
116 cout << " realm set set realm info (requires infile)\n";
117 cout << " realm default set realm as default\n";
118 cout << " realm pull pull a realm and its current period\n";
119 cout << " zonegroup add add a zone to a zonegroup\n";
120 cout << " zonegroup create create a new zone group info\n";
121 cout << " zonegroup default set default zone group\n";
122 cout << " zonegroup delete delete a zone group info\n";
123 cout << " zonegroup get show zone group info\n";
124 cout << " zonegroup modify modify an existing zonegroup\n";
125 cout << " zonegroup set set zone group info (requires infile)\n";
126 cout << " zonegroup remove remove a zone from a zonegroup\n";
127 cout << " zonegroup rename rename a zone group\n";
128 cout << " zonegroup list list all zone groups set on this cluster\n";
129 cout << " zonegroup placement list list zonegroup's placement targets\n";
130 cout << " zonegroup placement add add a placement target id to a zonegroup\n";
131 cout << " zonegroup placement modify modify a placement target of a specific zonegroup\n";
132 cout << " zonegroup placement rm remove a placement target from a zonegroup\n";
133 cout << " zonegroup placement default set a zonegroup's default placement target\n";
134 cout << " zone create create a new zone\n";
135 cout << " zone delete delete a zone\n";
136 cout << " zone get show zone cluster params\n";
137 cout << " zone modify modify an existing zone\n";
138 cout << " zone set set zone cluster params (requires infile)\n";
139 cout << " zone list list all zones set on this cluster\n";
140 cout << " zone rename rename a zone\n";
141 cout << " zone placement list list zone's placement targets\n";
142 cout << " zone placement add add a zone placement target\n";
143 cout << " zone placement modify modify a zone placement target\n";
144 cout << " zone placement rm remove a zone placement target\n";
145 cout << " pool add add an existing pool for data placement\n";
146 cout << " pool rm remove an existing pool from data placement set\n";
147 cout << " pools list list placement active set\n";
148 cout << " policy read bucket/object policy\n";
149 cout << " log list list log objects\n";
150 cout << " log show dump a log from specific object or (bucket + date\n";
151 cout << " + bucket-id)\n";
152 cout << " (NOTE: required to specify formatting of date\n";
153 cout << " to \"YYYY-MM-DD-hh\")\n";
154 cout << " log rm remove log object\n";
155 cout << " usage show show usage (by user, date range)\n";
156 cout << " usage trim trim usage (by user, date range)\n";
157 cout << " gc list dump expired garbage collection objects (specify\n";
158 cout << " --include-all to list all entries, including unexpired)\n";
159 cout << " gc process manually process garbage\n";
160 cout << " lc list list all bucket lifecycle progress\n";
161 cout << " lc process manually process lifecycle\n";
162 cout << " metadata get get metadata info\n";
163 cout << " metadata put put metadata info\n";
164 cout << " metadata rm remove metadata info\n";
165 cout << " metadata list list metadata info\n";
166 cout << " mdlog list list metadata log\n";
167 cout << " mdlog trim trim metadata log (use start-date, end-date or\n";
168 cout << " start-marker, end-marker)\n";
169 cout << " mdlog status read metadata log status\n";
170 cout << " bilog list list bucket index log\n";
171 cout << " bilog trim trim bucket index log (use start-marker, end-marker)\n";
172 cout << " datalog list list data log\n";
173 cout << " datalog trim trim data log\n";
174 cout << " datalog status read data log status\n";
175 cout << " opstate list list stateful operations entries (use client_id,\n";
176 cout << " op_id, object)\n";
177 cout << " opstate set set state on an entry (use client_id, op_id, object, state)\n";
178 cout << " opstate renew renew state on an entry (use client_id, op_id, object)\n";
179 cout << " opstate rm remove entry (use client_id, op_id, object)\n";
180 cout << " replicalog get get replica metadata log entry\n";
181 cout << " replicalog update update replica metadata log entry\n";
182 cout << " replicalog delete delete replica metadata log entry\n";
183 cout << " orphans find init and run search for leaked rados objects (use job-id, pool)\n";
184 cout << " orphans finish clean up search for leaked rados objects\n";
185 cout << " orphans list-jobs list the current job-ids for orphans search\n";
186 cout << " role create create a AWS role for use with STS\n";
187 cout << " role delete delete a role\n";
188 cout << " role get get a role\n";
189 cout << " role list list roles with specified path prefix\n";
190 cout << " role modify modify the assume role policy of an existing role\n";
191 cout << " role-policy put add/update permission policy to role\n";
192 cout << " role-policy list list policies attached to a role\n";
193 cout << " role-policy get get the specified inline policy document embedded with the given role\n";
194 cout << " role-policy delete delete policy attached to a role\n";
31f18b77
FG
195 cout << " reshard add schedule a resharding of a bucket\n";
196 cout << " reshard list list all bucket resharding or scheduled to be reshared\n";
197 cout << " reshard process process of scheduled reshard jobs\n";
198 cout << " reshard cancel cancel resharding a bucket\n";
7c673cae
FG
199 cout << "options:\n";
200 cout << " --tenant=<tenant> tenant name\n";
201 cout << " --uid=<id> user id\n";
202 cout << " --subuser=<name> subuser name\n";
203 cout << " --access-key=<key> S3 access key\n";
204 cout << " --email=<email>\n";
205 cout << " --secret/--secret-key=<key>\n";
206 cout << " specify secret key\n";
207 cout << " --gen-access-key generate random access key (for S3)\n";
208 cout << " --gen-secret generate random secret key\n";
209 cout << " --key-type=<type> key type, options are: swift, s3\n";
210 cout << " --temp-url-key[-2]=<key> temp url key\n";
211 cout << " --access=<access> Set access permissions for sub-user, should be one\n";
212 cout << " of read, write, readwrite, full\n";
213 cout << " --display-name=<name>\n";
c07f9fc5 214 cout << " --max-buckets max number of buckets for a user\n";
7c673cae
FG
215 cout << " --admin set the admin flag on the user\n";
216 cout << " --system set the system flag on the user\n";
217 cout << " --bucket=<bucket>\n";
218 cout << " --pool=<pool>\n";
219 cout << " --object=<object>\n";
220 cout << " --date=<date>\n";
221 cout << " --start-date=<date>\n";
222 cout << " --end-date=<date>\n";
223 cout << " --bucket-id=<bucket-id>\n";
224 cout << " --shard-id=<shard-id> optional for mdlog list\n";
225 cout << " required for: \n";
226 cout << " mdlog trim\n";
227 cout << " replica mdlog get/delete\n";
228 cout << " replica datalog get/delete\n";
229 cout << " --metadata-key=<key> key to retrieve metadata from with metadata get\n";
230 cout << " --remote=<remote> zone or zonegroup id of remote gateway\n";
231 cout << " --period=<id> period id\n";
232 cout << " --epoch=<number> period epoch\n";
233 cout << " --commit commit the period during 'period update'\n";
234 cout << " --staging get staging period info\n";
235 cout << " --master set as master\n";
236 cout << " --master-url master url\n";
237 cout << " --master-zonegroup=<id> master zonegroup id\n";
238 cout << " --master-zone=<id> master zone id\n";
239 cout << " --rgw-realm=<name> realm name\n";
240 cout << " --realm-id=<id> realm id\n";
241 cout << " --realm-new-name=<name> realm new name\n";
242 cout << " --rgw-zonegroup=<name> zonegroup name\n";
243 cout << " --zonegroup-id=<id> zonegroup id\n";
244 cout << " --zonegroup-new-name=<name>\n";
245 cout << " zonegroup new name\n";
246 cout << " --rgw-zone=<name> name of zone in which radosgw is running\n";
247 cout << " --zone-id=<id> zone id\n";
248 cout << " --zone-new-name=<name> zone new name\n";
249 cout << " --source-zone specify the source zone (for data sync)\n";
250 cout << " --default set entity (realm, zonegroup, zone) as default\n";
251 cout << " --read-only set zone as read-only (when adding to zonegroup)\n";
252 cout << " --placement-id placement id for zonegroup placement commands\n";
253 cout << " --tags=<list> list of tags for zonegroup placement add and modify commands\n";
254 cout << " --tags-add=<list> list of tags to add for zonegroup placement modify command\n";
255 cout << " --tags-rm=<list> list of tags to remove for zonegroup placement modify command\n";
256 cout << " --endpoints=<list> zone endpoints\n";
c07f9fc5 257 cout << " --index-pool=<pool> placement target index pool\n";
224ce89b
WB
258 cout << " --data-pool=<pool> placement target data pool\n";
259 cout << " --data-extra-pool=<pool> placement target data extra (non-ec) pool\n";
7c673cae
FG
260 cout << " --placement-index-type=<type>\n";
261 cout << " placement target index type (normal, indexless, or #id)\n";
262 cout << " --compression=<type> placement target compression type (plugin name or empty/none)\n";
263 cout << " --tier-type=<type> zone tier type\n";
264 cout << " --tier-config=<k>=<v>[,...]\n";
265 cout << " set zone tier config keys, values\n";
266 cout << " --tier-config-rm=<k>[,...]\n";
267 cout << " unset zone tier config keys\n";
268 cout << " --sync-from-all[=false] set/reset whether zone syncs from all zonegroup peers\n";
269 cout << " --sync-from=[zone-name][,...]\n";
270 cout << " set list of zones to sync from\n";
271 cout << " --sync-from-rm=[zone-name][,...]\n";
272 cout << " remove zones from list of zones to sync from\n";
273 cout << " --fix besides checking bucket index, will also fix it\n";
274 cout << " --check-objects bucket check: rebuilds bucket index according to\n";
275 cout << " actual objects state\n";
276 cout << " --format=<format> specify output format for certain operations: xml,\n";
277 cout << " json\n";
278 cout << " --purge-data when specified, user removal will also purge all the\n";
279 cout << " user data\n";
280 cout << " --purge-keys when specified, subuser removal will also purge all the\n";
281 cout << " subuser keys\n";
282 cout << " --purge-objects remove a bucket's objects before deleting it\n";
283 cout << " (NOTE: required to delete a non-empty bucket)\n";
284 cout << " --sync-stats option to 'user stats', update user stats with current\n";
285 cout << " stats reported by user's buckets indexes\n";
286 cout << " --show-log-entries=<flag> enable/disable dump of log entries on log show\n";
287 cout << " --show-log-sum=<flag> enable/disable dump of log summation on log show\n";
288 cout << " --skip-zero-entries log show only dumps entries that don't have zero value\n";
289 cout << " in one of the numeric field\n";
290 cout << " --infile=<file> specify a file to read in when setting data\n";
291 cout << " --state=<state string> specify a state for the opstate set command\n";
292 cout << " --replica-log-type replica log type (metadata, data, bucket), required for\n";
293 cout << " replica log operations\n";
294 cout << " --categories=<list> comma separated list of categories, used in usage show\n";
295 cout << " --caps=<caps> list of caps (e.g., \"usage=read, write; user=read\")\n";
296 cout << " --yes-i-really-mean-it required for certain operations\n";
297 cout << " --warnings-only when specified with bucket limit check, list\n";
298 cout << " only buckets nearing or over the current max\n";
299 cout << " objects per shard value\n";
300 cout << " --bypass-gc when specified with bucket deletion, triggers\n";
301 cout << " object deletions by not involving GC\n";
302 cout << " --inconsistent-index when specified with bucket deletion and bypass-gc set to true,\n";
303 cout << " ignores bucket index consistency\n";
304 cout << "\n";
305 cout << "<date> := \"YYYY-MM-DD[ hh:mm:ss]\"\n";
306 cout << "\nQuota options:\n";
307 cout << " --bucket specified bucket for quota command\n";
308 cout << " --max-objects specify max objects (negative value to disable)\n";
309 cout << " --max-size specify max size (in B/K/M/G/T, negative value to disable)\n";
310 cout << " --quota-scope scope of quota (bucket, user)\n";
311 cout << "\nOrphans search options:\n";
312 cout << " --pool data pool to scan for leaked rados objects in\n";
313 cout << " --num-shards num of shards to use for keeping the temporary scan info\n";
314 cout << " --orphan-stale-secs num of seconds to wait before declaring an object to be an orphan (default: 86400)\n";
315 cout << " --job-id set the job id (for orphans find)\n";
316 cout << " --max-concurrent-ios maximum concurrent ios for orphans find (default: 32)\n";
317 cout << "\nOrphans list-jobs options:\n";
318 cout << " --extra-info provide extra info in job list\n";
319 cout << "\nRole options:\n";
320 cout << " --role-name name of the role to create\n";
321 cout << " --path path to the role\n";
322 cout << " --assume-role-policy-doc the trust relationship policy document that grants an entity permission to assume the role\n";
323 cout << " --policy-name name of the policy document\n";
324 cout << " --policy-doc permission policy document\n";
325 cout << " --path-prefix path prefix for filtering roles\n";
326 cout << "\n";
327 generic_client_usage();
328}
329
330int usage()
331{
332 _usage();
333 return 1;
334}
335
336enum {
337 OPT_NO_CMD = 0,
338 OPT_USER_CREATE,
339 OPT_USER_INFO,
340 OPT_USER_MODIFY,
341 OPT_USER_RM,
342 OPT_USER_SUSPEND,
343 OPT_USER_ENABLE,
344 OPT_USER_CHECK,
345 OPT_USER_STATS,
346 OPT_USER_LIST,
347 OPT_SUBUSER_CREATE,
348 OPT_SUBUSER_MODIFY,
349 OPT_SUBUSER_RM,
350 OPT_KEY_CREATE,
351 OPT_KEY_RM,
352 OPT_BUCKETS_LIST,
31f18b77 353 OPT_BUCKET_LIMIT_CHECK,
7c673cae
FG
354 OPT_BUCKET_LINK,
355 OPT_BUCKET_UNLINK,
356 OPT_BUCKET_STATS,
357 OPT_BUCKET_CHECK,
358 OPT_BUCKET_SYNC_STATUS,
359 OPT_BUCKET_SYNC_INIT,
360 OPT_BUCKET_SYNC_RUN,
c07f9fc5
FG
361 OPT_BUCKET_SYNC_DISABLE,
362 OPT_BUCKET_SYNC_ENABLE,
7c673cae
FG
363 OPT_BUCKET_RM,
364 OPT_BUCKET_REWRITE,
365 OPT_BUCKET_RESHARD,
366 OPT_POLICY,
367 OPT_POOL_ADD,
368 OPT_POOL_RM,
369 OPT_POOLS_LIST,
370 OPT_LOG_LIST,
371 OPT_LOG_SHOW,
372 OPT_LOG_RM,
373 OPT_USAGE_SHOW,
374 OPT_USAGE_TRIM,
375 OPT_OBJECT_RM,
376 OPT_OBJECT_UNLINK,
377 OPT_OBJECT_STAT,
378 OPT_OBJECT_REWRITE,
379 OPT_OBJECTS_EXPIRE,
380 OPT_BI_GET,
381 OPT_BI_PUT,
382 OPT_BI_LIST,
383 OPT_BI_PURGE,
384 OPT_OLH_GET,
385 OPT_OLH_READLOG,
386 OPT_QUOTA_SET,
387 OPT_QUOTA_ENABLE,
388 OPT_QUOTA_DISABLE,
389 OPT_GC_LIST,
390 OPT_GC_PROCESS,
391 OPT_LC_LIST,
392 OPT_LC_PROCESS,
393 OPT_ORPHANS_FIND,
394 OPT_ORPHANS_FINISH,
395 OPT_ORPHANS_LIST_JOBS,
396 OPT_ZONEGROUP_ADD,
397 OPT_ZONEGROUP_CREATE,
398 OPT_ZONEGROUP_DEFAULT,
399 OPT_ZONEGROUP_DELETE,
400 OPT_ZONEGROUP_GET,
401 OPT_ZONEGROUP_MODIFY,
402 OPT_ZONEGROUP_SET,
403 OPT_ZONEGROUP_LIST,
404 OPT_ZONEGROUP_REMOVE,
405 OPT_ZONEGROUP_RENAME,
406 OPT_ZONEGROUP_PLACEMENT_ADD,
407 OPT_ZONEGROUP_PLACEMENT_MODIFY,
408 OPT_ZONEGROUP_PLACEMENT_RM,
409 OPT_ZONEGROUP_PLACEMENT_LIST,
410 OPT_ZONEGROUP_PLACEMENT_DEFAULT,
411 OPT_ZONE_CREATE,
412 OPT_ZONE_DELETE,
413 OPT_ZONE_GET,
414 OPT_ZONE_MODIFY,
415 OPT_ZONE_SET,
416 OPT_ZONE_LIST,
417 OPT_ZONE_RENAME,
418 OPT_ZONE_DEFAULT,
419 OPT_ZONE_PLACEMENT_ADD,
420 OPT_ZONE_PLACEMENT_MODIFY,
421 OPT_ZONE_PLACEMENT_RM,
422 OPT_ZONE_PLACEMENT_LIST,
423 OPT_CAPS_ADD,
424 OPT_CAPS_RM,
425 OPT_METADATA_GET,
426 OPT_METADATA_PUT,
427 OPT_METADATA_RM,
428 OPT_METADATA_LIST,
429 OPT_METADATA_SYNC_STATUS,
430 OPT_METADATA_SYNC_INIT,
431 OPT_METADATA_SYNC_RUN,
432 OPT_MDLOG_LIST,
433 OPT_MDLOG_AUTOTRIM,
434 OPT_MDLOG_TRIM,
435 OPT_MDLOG_FETCH,
436 OPT_MDLOG_STATUS,
437 OPT_SYNC_ERROR_LIST,
438 OPT_BILOG_LIST,
439 OPT_BILOG_TRIM,
440 OPT_BILOG_STATUS,
441 OPT_DATA_SYNC_STATUS,
442 OPT_DATA_SYNC_INIT,
443 OPT_DATA_SYNC_RUN,
444 OPT_DATALOG_LIST,
445 OPT_DATALOG_STATUS,
446 OPT_DATALOG_TRIM,
447 OPT_OPSTATE_LIST,
448 OPT_OPSTATE_SET,
449 OPT_OPSTATE_RENEW,
450 OPT_OPSTATE_RM,
451 OPT_REPLICALOG_GET,
452 OPT_REPLICALOG_UPDATE,
453 OPT_REPLICALOG_DELETE,
454 OPT_REALM_CREATE,
455 OPT_REALM_DELETE,
456 OPT_REALM_GET,
457 OPT_REALM_GET_DEFAULT,
458 OPT_REALM_LIST,
459 OPT_REALM_LIST_PERIODS,
460 OPT_REALM_REMOVE,
461 OPT_REALM_RENAME,
462 OPT_REALM_SET,
463 OPT_REALM_DEFAULT,
464 OPT_REALM_PULL,
465 OPT_PERIOD_DELETE,
466 OPT_PERIOD_GET,
467 OPT_PERIOD_GET_CURRENT,
468 OPT_PERIOD_PULL,
469 OPT_PERIOD_PUSH,
470 OPT_PERIOD_LIST,
471 OPT_PERIOD_UPDATE,
472 OPT_PERIOD_COMMIT,
473 OPT_GLOBAL_QUOTA_GET,
474 OPT_GLOBAL_QUOTA_SET,
475 OPT_GLOBAL_QUOTA_ENABLE,
476 OPT_GLOBAL_QUOTA_DISABLE,
477 OPT_SYNC_STATUS,
478 OPT_ROLE_CREATE,
479 OPT_ROLE_DELETE,
480 OPT_ROLE_GET,
481 OPT_ROLE_MODIFY,
482 OPT_ROLE_LIST,
483 OPT_ROLE_POLICY_PUT,
484 OPT_ROLE_POLICY_LIST,
485 OPT_ROLE_POLICY_GET,
486 OPT_ROLE_POLICY_DELETE,
31f18b77
FG
487 OPT_RESHARD_ADD,
488 OPT_RESHARD_LIST,
489 OPT_RESHARD_STATUS,
490 OPT_RESHARD_PROCESS,
491 OPT_RESHARD_CANCEL,
7c673cae
FG
492};
493
494static int get_cmd(const char *cmd, const char *prev_cmd, const char *prev_prev_cmd, bool *need_more)
495{
496 *need_more = false;
497 // NOTE: please keep the checks in alphabetical order !!!
498 if (strcmp(cmd, "bi") == 0 ||
499 strcmp(cmd, "bilog") == 0 ||
7c673cae
FG
500 strcmp(cmd, "buckets") == 0 ||
501 strcmp(cmd, "caps") == 0 ||
502 strcmp(cmd, "data") == 0 ||
503 strcmp(cmd, "datalog") == 0 ||
504 strcmp(cmd, "error") == 0 ||
505 strcmp(cmd, "gc") == 0 ||
506 strcmp(cmd, "global") == 0 ||
507 strcmp(cmd, "key") == 0 ||
508 strcmp(cmd, "log") == 0 ||
509 strcmp(cmd, "lc") == 0 ||
510 strcmp(cmd, "mdlog") == 0 ||
511 strcmp(cmd, "metadata") == 0 ||
512 strcmp(cmd, "object") == 0 ||
513 strcmp(cmd, "objects") == 0 ||
514 strcmp(cmd, "olh") == 0 ||
515 strcmp(cmd, "opstate") == 0 ||
516 strcmp(cmd, "orphans") == 0 ||
517 strcmp(cmd, "period") == 0 ||
518 strcmp(cmd, "placement") == 0 ||
519 strcmp(cmd, "pool") == 0 ||
520 strcmp(cmd, "pools") == 0 ||
521 strcmp(cmd, "quota") == 0 ||
522 strcmp(cmd, "realm") == 0 ||
523 strcmp(cmd, "replicalog") == 0 ||
524 strcmp(cmd, "role") == 0 ||
525 strcmp(cmd, "role-policy") == 0 ||
526 strcmp(cmd, "subuser") == 0 ||
527 strcmp(cmd, "sync") == 0 ||
528 strcmp(cmd, "usage") == 0 ||
529 strcmp(cmd, "user") == 0 ||
530 strcmp(cmd, "zone") == 0 ||
531 strcmp(cmd, "zonegroup") == 0 ||
31f18b77
FG
532 strcmp(cmd, "zonegroups") == 0) {
533 *need_more = true;
534 return 0;
535 }
536
537 /*
538 * can do both radosgw-admin bucket reshard, and radosgw-admin reshard bucket
539 */
540 if (strcmp(cmd, "reshard") == 0 &&
541 !(prev_cmd && strcmp(prev_cmd, "bucket") == 0)) {
542 *need_more = true;
543 return 0;
544 }
545 if (strcmp(cmd, "bucket") == 0 &&
546 !(prev_cmd && strcmp(prev_cmd, "reshard") == 0)) {
7c673cae
FG
547 *need_more = true;
548 return 0;
549 }
550
551 if (strcmp(cmd, "policy") == 0)
552 return OPT_POLICY;
553
554 if (!prev_cmd)
555 return -EINVAL;
556
557 if (strcmp(prev_cmd, "user") == 0) {
558 if (strcmp(cmd, "create") == 0)
559 return OPT_USER_CREATE;
560 if (strcmp(cmd, "info") == 0)
561 return OPT_USER_INFO;
562 if (strcmp(cmd, "modify") == 0)
563 return OPT_USER_MODIFY;
564 if (strcmp(cmd, "rm") == 0)
565 return OPT_USER_RM;
566 if (strcmp(cmd, "suspend") == 0)
567 return OPT_USER_SUSPEND;
568 if (strcmp(cmd, "enable") == 0)
569 return OPT_USER_ENABLE;
570 if (strcmp(cmd, "check") == 0)
571 return OPT_USER_CHECK;
572 if (strcmp(cmd, "stats") == 0)
573 return OPT_USER_STATS;
574 if (strcmp(cmd, "list") == 0)
575 return OPT_USER_LIST;
576 } else if (strcmp(prev_cmd, "subuser") == 0) {
577 if (strcmp(cmd, "create") == 0)
578 return OPT_SUBUSER_CREATE;
579 if (strcmp(cmd, "modify") == 0)
580 return OPT_SUBUSER_MODIFY;
581 if (strcmp(cmd, "rm") == 0)
582 return OPT_SUBUSER_RM;
583 } else if (strcmp(prev_cmd, "key") == 0) {
584 if (strcmp(cmd, "create") == 0)
585 return OPT_KEY_CREATE;
586 if (strcmp(cmd, "rm") == 0)
587 return OPT_KEY_RM;
588 } else if (strcmp(prev_cmd, "buckets") == 0) {
589 if (strcmp(cmd, "list") == 0)
590 return OPT_BUCKETS_LIST;
7c673cae
FG
591 } else if (strcmp(prev_cmd, "bucket") == 0) {
592 if (strcmp(cmd, "list") == 0)
593 return OPT_BUCKETS_LIST;
594 if (strcmp(cmd, "link") == 0)
595 return OPT_BUCKET_LINK;
596 if (strcmp(cmd, "unlink") == 0)
597 return OPT_BUCKET_UNLINK;
598 if (strcmp(cmd, "stats") == 0)
599 return OPT_BUCKET_STATS;
600 if (strcmp(cmd, "rm") == 0)
601 return OPT_BUCKET_RM;
602 if (strcmp(cmd, "rewrite") == 0)
603 return OPT_BUCKET_REWRITE;
604 if (strcmp(cmd, "reshard") == 0)
605 return OPT_BUCKET_RESHARD;
606 if (strcmp(cmd, "check") == 0)
607 return OPT_BUCKET_CHECK;
608 if (strcmp(cmd, "sync") == 0) {
609 *need_more = true;
610 return 0;
611 }
31f18b77
FG
612 if (strcmp(cmd, "limit") == 0) {
613 *need_more = true;
614 return 0;
615 }
7c673cae
FG
616 } else if (prev_prev_cmd && strcmp(prev_prev_cmd, "bucket") == 0) {
617 if (strcmp(prev_cmd, "sync") == 0) {
618 if (strcmp(cmd, "status") == 0)
c07f9fc5 619 return OPT_BUCKET_SYNC_STATUS;
7c673cae 620 if (strcmp(cmd, "init") == 0)
c07f9fc5 621 return OPT_BUCKET_SYNC_INIT;
7c673cae 622 if (strcmp(cmd, "run") == 0)
c07f9fc5
FG
623 return OPT_BUCKET_SYNC_RUN;
624 if (strcmp(cmd, "disable") == 0)
625 return OPT_BUCKET_SYNC_DISABLE;
626 if (strcmp(cmd, "enable") == 0)
627 return OPT_BUCKET_SYNC_ENABLE;
7c673cae
FG
628 } else if ((strcmp(prev_cmd, "limit") == 0) &&
629 (strcmp(cmd, "check") == 0)) {
31f18b77 630 return OPT_BUCKET_LIMIT_CHECK;
7c673cae
FG
631 }
632 } else if (strcmp(prev_cmd, "log") == 0) {
633 if (strcmp(cmd, "list") == 0)
634 return OPT_LOG_LIST;
635 if (strcmp(cmd, "show") == 0)
636 return OPT_LOG_SHOW;
637 if (strcmp(cmd, "rm") == 0)
638 return OPT_LOG_RM;
639 } else if (strcmp(prev_cmd, "usage") == 0) {
640 if (strcmp(cmd, "show") == 0)
641 return OPT_USAGE_SHOW;
642 if (strcmp(cmd, "trim") == 0)
643 return OPT_USAGE_TRIM;
644 } else if (strcmp(prev_cmd, "caps") == 0) {
645 if (strcmp(cmd, "add") == 0)
646 return OPT_CAPS_ADD;
647 if (strcmp(cmd, "rm") == 0)
648 return OPT_CAPS_RM;
649 } else if (strcmp(prev_cmd, "pool") == 0) {
650 if (strcmp(cmd, "add") == 0)
651 return OPT_POOL_ADD;
652 if (strcmp(cmd, "rm") == 0)
653 return OPT_POOL_RM;
654 if (strcmp(cmd, "list") == 0)
655 return OPT_POOLS_LIST;
656 } else if (strcmp(prev_cmd, "pools") == 0) {
657 if (strcmp(cmd, "list") == 0)
658 return OPT_POOLS_LIST;
659 } else if (strcmp(prev_cmd, "object") == 0) {
660 if (strcmp(cmd, "rm") == 0)
661 return OPT_OBJECT_RM;
662 if (strcmp(cmd, "unlink") == 0)
663 return OPT_OBJECT_UNLINK;
664 if (strcmp(cmd, "stat") == 0)
665 return OPT_OBJECT_STAT;
666 if (strcmp(cmd, "rewrite") == 0)
667 return OPT_OBJECT_REWRITE;
668 } else if (strcmp(prev_cmd, "objects") == 0) {
669 if (strcmp(cmd, "expire") == 0)
670 return OPT_OBJECTS_EXPIRE;
671 } else if (strcmp(prev_cmd, "olh") == 0) {
672 if (strcmp(cmd, "get") == 0)
673 return OPT_OLH_GET;
674 if (strcmp(cmd, "readlog") == 0)
675 return OPT_OLH_READLOG;
676 } else if (strcmp(prev_cmd, "bi") == 0) {
677 if (strcmp(cmd, "get") == 0)
678 return OPT_BI_GET;
679 if (strcmp(cmd, "put") == 0)
680 return OPT_BI_PUT;
681 if (strcmp(cmd, "list") == 0)
682 return OPT_BI_LIST;
683 if (strcmp(cmd, "purge") == 0)
684 return OPT_BI_PURGE;
685 } else if ((prev_prev_cmd && strcmp(prev_prev_cmd, "global") == 0) &&
686 (strcmp(prev_cmd, "quota") == 0)) {
687 if (strcmp(cmd, "get") == 0)
688 return OPT_GLOBAL_QUOTA_GET;
689 if (strcmp(cmd, "set") == 0)
690 return OPT_GLOBAL_QUOTA_SET;
691 if (strcmp(cmd, "enable") == 0)
692 return OPT_GLOBAL_QUOTA_ENABLE;
693 if (strcmp(cmd, "disable") == 0)
694 return OPT_GLOBAL_QUOTA_DISABLE;
695 } else if (strcmp(prev_cmd, "period") == 0) {
696 if (strcmp(cmd, "delete") == 0)
697 return OPT_PERIOD_DELETE;
698 if (strcmp(cmd, "get") == 0)
699 return OPT_PERIOD_GET;
700 if (strcmp(cmd, "get-current") == 0)
701 return OPT_PERIOD_GET_CURRENT;
702 if (strcmp(cmd, "pull") == 0)
703 return OPT_PERIOD_PULL;
704 if (strcmp(cmd, "push") == 0)
705 return OPT_PERIOD_PUSH;
706 if (strcmp(cmd, "list") == 0)
707 return OPT_PERIOD_LIST;
708 if (strcmp(cmd, "update") == 0)
709 return OPT_PERIOD_UPDATE;
710 if (strcmp(cmd, "commit") == 0)
711 return OPT_PERIOD_COMMIT;
712 } else if (strcmp(prev_cmd, "realm") == 0) {
713 if (strcmp(cmd, "create") == 0)
714 return OPT_REALM_CREATE;
715 if (strcmp(cmd, "delete") == 0)
716 return OPT_REALM_DELETE;
717 if (strcmp(cmd, "get") == 0)
718 return OPT_REALM_GET;
719 if (strcmp(cmd, "get-default") == 0)
720 return OPT_REALM_GET_DEFAULT;
721 if (strcmp(cmd, "list") == 0)
722 return OPT_REALM_LIST;
723 if (strcmp(cmd, "list-periods") == 0)
724 return OPT_REALM_LIST_PERIODS;
725 if (strcmp(cmd, "remove") == 0)
726 return OPT_REALM_REMOVE;
727 if (strcmp(cmd, "rename") == 0)
728 return OPT_REALM_RENAME;
729 if (strcmp(cmd, "set") == 0)
730 return OPT_REALM_SET;
731 if (strcmp(cmd, "default") == 0)
732 return OPT_REALM_DEFAULT;
733 if (strcmp(cmd, "pull") == 0)
734 return OPT_REALM_PULL;
735 } else if ((prev_prev_cmd && strcmp(prev_prev_cmd, "zonegroup") == 0) &&
736 (strcmp(prev_cmd, "placement") == 0)) {
737 if (strcmp(cmd, "add") == 0)
738 return OPT_ZONEGROUP_PLACEMENT_ADD;
739 if (strcmp(cmd, "modify") == 0)
740 return OPT_ZONEGROUP_PLACEMENT_MODIFY;
741 if (strcmp(cmd, "rm") == 0)
742 return OPT_ZONEGROUP_PLACEMENT_RM;
743 if (strcmp(cmd, "list") == 0)
744 return OPT_ZONEGROUP_PLACEMENT_LIST;
745 if (strcmp(cmd, "default") == 0)
746 return OPT_ZONEGROUP_PLACEMENT_DEFAULT;
747 } else if (strcmp(prev_cmd, "zonegroup") == 0) {
748 if (strcmp(cmd, "add") == 0)
749 return OPT_ZONEGROUP_ADD;
750 if (strcmp(cmd, "create")== 0)
751 return OPT_ZONEGROUP_CREATE;
752 if (strcmp(cmd, "default") == 0)
753 return OPT_ZONEGROUP_DEFAULT;
754 if (strcmp(cmd, "delete") == 0)
755 return OPT_ZONEGROUP_DELETE;
756 if (strcmp(cmd, "get") == 0)
757 return OPT_ZONEGROUP_GET;
758 if (strcmp(cmd, "modify") == 0)
759 return OPT_ZONEGROUP_MODIFY;
760 if (strcmp(cmd, "list") == 0)
761 return OPT_ZONEGROUP_LIST;
762 if (strcmp(cmd, "set") == 0)
763 return OPT_ZONEGROUP_SET;
764 if (strcmp(cmd, "remove") == 0)
765 return OPT_ZONEGROUP_REMOVE;
766 if (strcmp(cmd, "rename") == 0)
767 return OPT_ZONEGROUP_RENAME;
768 } else if (strcmp(prev_cmd, "quota") == 0) {
769 if (strcmp(cmd, "set") == 0)
770 return OPT_QUOTA_SET;
771 if (strcmp(cmd, "enable") == 0)
772 return OPT_QUOTA_ENABLE;
773 if (strcmp(cmd, "disable") == 0)
774 return OPT_QUOTA_DISABLE;
775 } else if (strcmp(prev_cmd, "zonegroups") == 0) {
776 if (strcmp(cmd, "list") == 0)
777 return OPT_ZONEGROUP_LIST;
778 } else if ((prev_prev_cmd && strcmp(prev_prev_cmd, "zone") == 0) &&
779 (strcmp(prev_cmd, "placement") == 0)) {
780 if (strcmp(cmd, "add") == 0)
781 return OPT_ZONE_PLACEMENT_ADD;
782 if (strcmp(cmd, "modify") == 0)
783 return OPT_ZONE_PLACEMENT_MODIFY;
784 if (strcmp(cmd, "rm") == 0)
785 return OPT_ZONE_PLACEMENT_RM;
786 if (strcmp(cmd, "list") == 0)
787 return OPT_ZONE_PLACEMENT_LIST;
788 } else if (strcmp(prev_cmd, "zone") == 0) {
789 if (strcmp(cmd, "delete") == 0)
790 return OPT_ZONE_DELETE;
791 if (strcmp(cmd, "create") == 0)
792 return OPT_ZONE_CREATE;
793 if (strcmp(cmd, "get") == 0)
794 return OPT_ZONE_GET;
795 if (strcmp(cmd, "set") == 0)
796 return OPT_ZONE_SET;
797 if (strcmp(cmd, "list") == 0)
798 return OPT_ZONE_LIST;
799 if (strcmp(cmd, "modify") == 0)
800 return OPT_ZONE_MODIFY;
801 if (strcmp(cmd, "rename") == 0)
802 return OPT_ZONE_RENAME;
803 if (strcmp(cmd, "default") == 0)
804 return OPT_ZONE_DEFAULT;
805 } else if (strcmp(prev_cmd, "zones") == 0) {
806 if (strcmp(cmd, "list") == 0)
807 return OPT_ZONE_LIST;
808 } else if (strcmp(prev_cmd, "gc") == 0) {
809 if (strcmp(cmd, "list") == 0)
810 return OPT_GC_LIST;
811 if (strcmp(cmd, "process") == 0)
812 return OPT_GC_PROCESS;
813 } else if (strcmp(prev_cmd, "lc") == 0) {
814 if (strcmp(cmd, "list") == 0)
815 return OPT_LC_LIST;
816 if (strcmp(cmd, "process") == 0)
817 return OPT_LC_PROCESS;
818 } else if (strcmp(prev_cmd, "orphans") == 0) {
819 if (strcmp(cmd, "find") == 0)
820 return OPT_ORPHANS_FIND;
821 if (strcmp(cmd, "finish") == 0)
822 return OPT_ORPHANS_FINISH;
823 if (strcmp(cmd, "list-jobs") == 0)
824 return OPT_ORPHANS_LIST_JOBS;
825 } else if (strcmp(prev_cmd, "metadata") == 0) {
826 if (strcmp(cmd, "get") == 0)
827 return OPT_METADATA_GET;
828 if (strcmp(cmd, "put") == 0)
829 return OPT_METADATA_PUT;
830 if (strcmp(cmd, "rm") == 0)
831 return OPT_METADATA_RM;
832 if (strcmp(cmd, "list") == 0)
833 return OPT_METADATA_LIST;
834 if (strcmp(cmd, "sync") == 0) {
835 *need_more = true;
836 return 0;
837 }
838 } else if ((prev_prev_cmd && strcmp(prev_prev_cmd, "metadata") == 0) &&
839 (strcmp(prev_cmd, "sync") == 0)) {
840 if (strcmp(cmd, "status") == 0)
841 return OPT_METADATA_SYNC_STATUS;
842 if (strcmp(cmd, "init") == 0)
843 return OPT_METADATA_SYNC_INIT;
844 if (strcmp(cmd, "run") == 0)
845 return OPT_METADATA_SYNC_RUN;
846 } else if ((prev_prev_cmd && strcmp(prev_prev_cmd, "sync") == 0) &&
847 (strcmp(prev_cmd, "error") == 0)) {
848 if (strcmp(cmd, "list") == 0)
849 return OPT_SYNC_ERROR_LIST;
850 } else if (strcmp(prev_cmd, "mdlog") == 0) {
851 if (strcmp(cmd, "list") == 0)
852 return OPT_MDLOG_LIST;
853 if (strcmp(cmd, "autotrim") == 0)
854 return OPT_MDLOG_AUTOTRIM;
855 if (strcmp(cmd, "trim") == 0)
856 return OPT_MDLOG_TRIM;
857 if (strcmp(cmd, "fetch") == 0)
858 return OPT_MDLOG_FETCH;
859 if (strcmp(cmd, "status") == 0)
860 return OPT_MDLOG_STATUS;
861 } else if (strcmp(prev_cmd, "bilog") == 0) {
862 if (strcmp(cmd, "list") == 0)
863 return OPT_BILOG_LIST;
864 if (strcmp(cmd, "trim") == 0)
865 return OPT_BILOG_TRIM;
866 if (strcmp(cmd, "status") == 0)
867 return OPT_BILOG_STATUS;
868 } else if (strcmp(prev_cmd, "data") == 0) {
869 if (strcmp(cmd, "sync") == 0) {
870 *need_more = true;
871 return 0;
872 }
873 } else if (strcmp(prev_cmd, "datalog") == 0) {
874 if (strcmp(cmd, "list") == 0)
875 return OPT_DATALOG_LIST;
876 if (strcmp(cmd, "trim") == 0)
877 return OPT_DATALOG_TRIM;
878 if (strcmp(cmd, "status") == 0)
879 return OPT_DATALOG_STATUS;
880 } else if ((prev_prev_cmd && strcmp(prev_prev_cmd, "data") == 0) &&
881 (strcmp(prev_cmd, "sync") == 0)) {
882 if (strcmp(cmd, "status") == 0)
883 return OPT_DATA_SYNC_STATUS;
884 if (strcmp(cmd, "init") == 0)
885 return OPT_DATA_SYNC_INIT;
886 if (strcmp(cmd, "run") == 0)
887 return OPT_DATA_SYNC_RUN;
888 } else if (strcmp(prev_cmd, "opstate") == 0) {
889 if (strcmp(cmd, "list") == 0)
890 return OPT_OPSTATE_LIST;
891 if (strcmp(cmd, "set") == 0)
892 return OPT_OPSTATE_SET;
893 if (strcmp(cmd, "renew") == 0)
894 return OPT_OPSTATE_RENEW;
895 if (strcmp(cmd, "rm") == 0)
896 return OPT_OPSTATE_RM;
897 } else if (strcmp(prev_cmd, "replicalog") == 0) {
898 if (strcmp(cmd, "get") == 0)
899 return OPT_REPLICALOG_GET;
900 if (strcmp(cmd, "update") == 0)
901 return OPT_REPLICALOG_UPDATE;
902 if (strcmp(cmd, "delete") == 0)
903 return OPT_REPLICALOG_DELETE;
904 } else if (strcmp(prev_cmd, "sync") == 0) {
905 if (strcmp(cmd, "status") == 0)
906 return OPT_SYNC_STATUS;
907 } else if (strcmp(prev_cmd, "role") == 0) {
908 if (strcmp(cmd, "create") == 0)
909 return OPT_ROLE_CREATE;
910 if (strcmp(cmd, "delete") == 0)
911 return OPT_ROLE_DELETE;
912 if (strcmp(cmd, "get") == 0)
913 return OPT_ROLE_GET;
914 if (strcmp(cmd, "modify") == 0)
915 return OPT_ROLE_MODIFY;
916 if (strcmp(cmd, "list") == 0)
917 return OPT_ROLE_LIST;
918 } else if (strcmp(prev_cmd, "role-policy") == 0) {
919 if (strcmp(cmd, "put") == 0)
920 return OPT_ROLE_POLICY_PUT;
921 if (strcmp(cmd, "list") == 0)
922 return OPT_ROLE_POLICY_LIST;
923 if (strcmp(cmd, "get") == 0)
924 return OPT_ROLE_POLICY_GET;
925 if (strcmp(cmd, "delete") == 0)
926 return OPT_ROLE_POLICY_DELETE;
31f18b77
FG
927 } else if (strcmp(prev_cmd, "reshard") == 0) {
928 if (strcmp(cmd, "bucket") == 0)
929 return OPT_BUCKET_RESHARD;
930 if (strcmp(cmd, "add") == 0)
931 return OPT_RESHARD_ADD;
932 if (strcmp(cmd, "list") == 0)
933 return OPT_RESHARD_LIST;
934 if (strcmp(cmd, "status") == 0)
935 return OPT_RESHARD_STATUS;
936 if (strcmp(cmd, "execute") == 0)
937 return OPT_RESHARD_PROCESS;
938 if (strcmp(cmd, "cancel") == 0)
939 return OPT_RESHARD_CANCEL;
7c673cae
FG
940 }
941
942 return -EINVAL;
943}
944
945enum ReplicaLogType {
946 ReplicaLog_Invalid = 0,
947 ReplicaLog_Metadata,
948 ReplicaLog_Data,
949 ReplicaLog_Bucket,
950};
951
952ReplicaLogType get_replicalog_type(const string& name) {
953 if (name == "md" || name == "meta" || name == "metadata")
954 return ReplicaLog_Metadata;
955 if (name == "data")
956 return ReplicaLog_Data;
957 if (name == "bucket")
958 return ReplicaLog_Bucket;
959
960 return ReplicaLog_Invalid;
961}
962
963BIIndexType get_bi_index_type(const string& type_str) {
964 if (type_str == "plain")
965 return PlainIdx;
966 if (type_str == "instance")
967 return InstanceIdx;
968 if (type_str == "olh")
969 return OLHIdx;
970
971 return InvalidIdx;
972}
973
974void dump_bi_entry(bufferlist& bl, BIIndexType index_type, Formatter *formatter)
975{
976 bufferlist::iterator iter = bl.begin();
977 switch (index_type) {
978 case PlainIdx:
979 case InstanceIdx:
980 {
981 rgw_bucket_dir_entry entry;
982 ::decode(entry, iter);
983 encode_json("entry", entry, formatter);
984 }
985 break;
986 case OLHIdx:
987 {
988 rgw_bucket_olh_entry entry;
989 ::decode(entry, iter);
990 encode_json("entry", entry, formatter);
991 }
992 break;
993 default:
994 ceph_abort();
995 break;
996 }
997}
998
999static void show_user_info(RGWUserInfo& info, Formatter *formatter)
1000{
1001 encode_json("user_info", info, formatter);
1002 formatter->flush(cout);
1003 cout << std::endl;
1004}
1005
1006static void show_perm_policy(string perm_policy, Formatter* formatter)
1007{
1008 formatter->open_object_section("role");
1009 formatter->dump_string("Permission policy", perm_policy);
1010 formatter->close_section();
1011 formatter->flush(cout);
1012}
1013
1014static void show_policy_names(std::vector<string> policy_names, Formatter* formatter)
1015{
1016 formatter->open_array_section("PolicyNames");
1017 for (const auto& it : policy_names) {
1018 formatter->dump_string("policyname", it);
1019 }
1020 formatter->close_section();
1021 formatter->flush(cout);
1022}
1023
1024static void show_role_info(RGWRole& role, Formatter* formatter)
1025{
1026 formatter->open_object_section("role");
1027 role.dump(formatter);
1028 formatter->close_section();
1029 formatter->flush(cout);
1030}
1031
1032static void show_roles_info(vector<RGWRole>& roles, Formatter* formatter)
1033{
1034 formatter->open_array_section("Roles");
1035 for (const auto& it : roles) {
1036 formatter->open_object_section("role");
1037 it.dump(formatter);
1038 formatter->close_section();
1039 }
1040 formatter->close_section();
1041 formatter->flush(cout);
1042}
1043
7c673cae
FG
1044class StoreDestructor {
1045 RGWRados *store;
1046public:
1047 explicit StoreDestructor(RGWRados *_s) : store(_s) {}
1048 ~StoreDestructor() {
1049 RGWStoreManager::close_storage(store);
1050 }
1051};
1052
1053static int init_bucket(const string& tenant_name, const string& bucket_name, const string& bucket_id,
1054 RGWBucketInfo& bucket_info, rgw_bucket& bucket, map<string, bufferlist> *pattrs = nullptr)
1055{
1056 if (!bucket_name.empty()) {
1057 RGWObjectCtx obj_ctx(store);
1058 int r;
1059 if (bucket_id.empty()) {
1060 r = store->get_bucket_info(obj_ctx, tenant_name, bucket_name, bucket_info, nullptr, pattrs);
1061 } else {
1062 string bucket_instance_id = bucket_name + ":" + bucket_id;
1063 r = store->get_bucket_instance_info(obj_ctx, bucket_instance_id, bucket_info, NULL, pattrs);
1064 }
1065 if (r < 0) {
1066 cerr << "could not get bucket info for bucket=" << bucket_name << std::endl;
1067 return r;
1068 }
1069 bucket = bucket_info.bucket;
1070 }
1071 return 0;
1072}
1073
1074static int read_input(const string& infile, bufferlist& bl)
1075{
1076 int fd = 0;
1077 if (infile.size()) {
1078 fd = open(infile.c_str(), O_RDONLY);
1079 if (fd < 0) {
1080 int err = -errno;
1081 cerr << "error reading input file " << infile << std::endl;
1082 return err;
1083 }
1084 }
1085
1086#define READ_CHUNK 8196
1087 int r;
1088 int err;
1089
1090 do {
1091 char buf[READ_CHUNK];
1092
1093 r = safe_read(fd, buf, READ_CHUNK);
1094 if (r < 0) {
1095 err = -errno;
1096 cerr << "error while reading input" << std::endl;
1097 goto out;
1098 }
1099 bl.append(buf, r);
1100 } while (r > 0);
1101 err = 0;
1102
1103 out:
1104 if (infile.size()) {
1105 close(fd);
1106 }
1107 return err;
1108}
1109
1110template <class T>
1111static int read_decode_json(const string& infile, T& t)
1112{
1113 bufferlist bl;
1114 int ret = read_input(infile, bl);
1115 if (ret < 0) {
1116 cerr << "ERROR: failed to read input: " << cpp_strerror(-ret) << std::endl;
1117 return ret;
1118 }
1119 JSONParser p;
1120 if (!p.parse(bl.c_str(), bl.length())) {
1121 cout << "failed to parse JSON" << std::endl;
1122 return -EINVAL;
1123 }
1124
1125 try {
1126 decode_json_obj(t, &p);
1127 } catch (JSONDecoder::err& e) {
1128 cout << "failed to decode JSON input: " << e.message << std::endl;
1129 return -EINVAL;
1130 }
1131 return 0;
1132}
1133
1134template <class T, class K>
1135static int read_decode_json(const string& infile, T& t, K *k)
1136{
1137 bufferlist bl;
1138 int ret = read_input(infile, bl);
1139 if (ret < 0) {
1140 cerr << "ERROR: failed to read input: " << cpp_strerror(-ret) << std::endl;
1141 return ret;
1142 }
1143 JSONParser p;
1144 if (!p.parse(bl.c_str(), bl.length())) {
1145 cout << "failed to parse JSON" << std::endl;
1146 return -EINVAL;
1147 }
1148
1149 try {
1150 t.decode_json(&p, k);
1151 } catch (JSONDecoder::err& e) {
1152 cout << "failed to decode JSON input: " << e.message << std::endl;
1153 return -EINVAL;
1154 }
1155 return 0;
1156}
1157
1158static int parse_date_str(const string& date_str, utime_t& ut)
1159{
1160 uint64_t epoch = 0;
1161 uint64_t nsec = 0;
1162
1163 if (!date_str.empty()) {
1164 int ret = utime_t::parse_date(date_str, &epoch, &nsec);
1165 if (ret < 0) {
1166 cerr << "ERROR: failed to parse date: " << date_str << std::endl;
1167 return -EINVAL;
1168 }
1169 }
1170
1171 ut = utime_t(epoch, nsec);
1172
1173 return 0;
1174}
1175
1176template <class T>
1177static bool decode_dump(const char *field_name, bufferlist& bl, Formatter *f)
1178{
1179 T t;
1180
1181 bufferlist::iterator iter = bl.begin();
1182
1183 try {
1184 ::decode(t, iter);
1185 } catch (buffer::error& err) {
1186 return false;
1187 }
1188
1189 encode_json(field_name, t, f);
1190
1191 return true;
1192}
1193
1194static bool dump_string(const char *field_name, bufferlist& bl, Formatter *f)
1195{
1196 string val;
1197 if (bl.length() > 0) {
31f18b77 1198 val.assign(bl.c_str());
7c673cae
FG
1199 }
1200 f->dump_string(field_name, val);
1201
1202 return true;
1203}
1204
1205void set_quota_info(RGWQuotaInfo& quota, int opt_cmd, int64_t max_size, int64_t max_objects,
1206 bool have_max_size, bool have_max_objects)
1207{
1208 switch (opt_cmd) {
1209 case OPT_QUOTA_ENABLE:
1210 case OPT_GLOBAL_QUOTA_ENABLE:
1211 quota.enabled = true;
1212
1213 // falling through on purpose
1214
1215 case OPT_QUOTA_SET:
1216 case OPT_GLOBAL_QUOTA_SET:
1217 if (have_max_objects) {
1218 if (max_objects < 0) {
1219 quota.max_objects = -1;
1220 } else {
1221 quota.max_objects = max_objects;
1222 }
1223 }
1224 if (have_max_size) {
1225 if (max_size < 0) {
1226 quota.max_size = -1;
1227 } else {
1228 quota.max_size = rgw_rounded_kb(max_size) * 1024;
1229 }
1230 }
1231 break;
1232 case OPT_QUOTA_DISABLE:
1233 case OPT_GLOBAL_QUOTA_DISABLE:
1234 quota.enabled = false;
1235 break;
1236 }
1237}
1238
1239int set_bucket_quota(RGWRados *store, int opt_cmd,
1240 const string& tenant_name, const string& bucket_name,
1241 int64_t max_size, int64_t max_objects,
1242 bool have_max_size, bool have_max_objects)
1243{
1244 RGWBucketInfo bucket_info;
1245 map<string, bufferlist> attrs;
1246 RGWObjectCtx obj_ctx(store);
1247 int r = store->get_bucket_info(obj_ctx, tenant_name, bucket_name, bucket_info, NULL, &attrs);
1248 if (r < 0) {
1249 cerr << "could not get bucket info for bucket=" << bucket_name << ": " << cpp_strerror(-r) << std::endl;
1250 return -r;
1251 }
1252
1253 set_quota_info(bucket_info.quota, opt_cmd, max_size, max_objects, have_max_size, have_max_objects);
1254
1255 r = store->put_bucket_instance_info(bucket_info, false, real_time(), &attrs);
1256 if (r < 0) {
1257 cerr << "ERROR: failed writing bucket instance info: " << cpp_strerror(-r) << std::endl;
1258 return -r;
1259 }
1260 return 0;
1261}
1262
1263int set_user_bucket_quota(int opt_cmd, RGWUser& user, RGWUserAdminOpState& op_state, int64_t max_size, int64_t max_objects,
1264 bool have_max_size, bool have_max_objects)
1265{
1266 RGWUserInfo& user_info = op_state.get_user_info();
1267
1268 set_quota_info(user_info.bucket_quota, opt_cmd, max_size, max_objects, have_max_size, have_max_objects);
1269
1270 op_state.set_bucket_quota(user_info.bucket_quota);
1271
1272 string err;
1273 int r = user.modify(op_state, &err);
1274 if (r < 0) {
1275 cerr << "ERROR: failed updating user info: " << cpp_strerror(-r) << ": " << err << std::endl;
1276 return -r;
1277 }
1278 return 0;
1279}
1280
1281int set_user_quota(int opt_cmd, RGWUser& user, RGWUserAdminOpState& op_state, int64_t max_size, int64_t max_objects,
1282 bool have_max_size, bool have_max_objects)
1283{
1284 RGWUserInfo& user_info = op_state.get_user_info();
1285
1286 set_quota_info(user_info.user_quota, opt_cmd, max_size, max_objects, have_max_size, have_max_objects);
1287
1288 op_state.set_user_quota(user_info.user_quota);
1289
1290 string err;
1291 int r = user.modify(op_state, &err);
1292 if (r < 0) {
1293 cerr << "ERROR: failed updating user info: " << cpp_strerror(-r) << ": " << err << std::endl;
1294 return -r;
1295 }
1296 return 0;
1297}
1298
1299static bool bucket_object_check_filter(const string& name)
1300{
1301 rgw_obj_key k;
1302 string ns; /* empty namespace */
1303 return rgw_obj_key::oid_to_key_in_ns(name, &k, ns);
1304}
1305
1306int check_min_obj_stripe_size(RGWRados *store, RGWBucketInfo& bucket_info, rgw_obj& obj, uint64_t min_stripe_size, bool *need_rewrite)
1307{
1308 map<string, bufferlist> attrs;
1309 uint64_t obj_size;
1310
1311 RGWObjectCtx obj_ctx(store);
1312 RGWRados::Object op_target(store, bucket_info, obj_ctx, obj);
1313 RGWRados::Object::Read read_op(&op_target);
1314
1315 read_op.params.attrs = &attrs;
1316 read_op.params.obj_size = &obj_size;
1317
1318 int ret = read_op.prepare();
1319 if (ret < 0) {
1320 lderr(store->ctx()) << "ERROR: failed to stat object, returned error: " << cpp_strerror(-ret) << dendl;
1321 return ret;
1322 }
1323
1324 map<string, bufferlist>::iterator iter;
1325 iter = attrs.find(RGW_ATTR_MANIFEST);
1326 if (iter == attrs.end()) {
1327 *need_rewrite = (obj_size >= min_stripe_size);
1328 return 0;
1329 }
1330
1331 RGWObjManifest manifest;
1332
1333 try {
1334 bufferlist& bl = iter->second;
1335 bufferlist::iterator biter = bl.begin();
1336 ::decode(manifest, biter);
1337 } catch (buffer::error& err) {
1338 ldout(store->ctx(), 0) << "ERROR: failed to decode manifest" << dendl;
1339 return -EIO;
1340 }
1341
1342 map<uint64_t, RGWObjManifestPart>& objs = manifest.get_explicit_objs();
1343 map<uint64_t, RGWObjManifestPart>::iterator oiter;
1344 for (oiter = objs.begin(); oiter != objs.end(); ++oiter) {
1345 RGWObjManifestPart& part = oiter->second;
1346
1347 if (part.size >= min_stripe_size) {
1348 *need_rewrite = true;
1349 return 0;
1350 }
1351 }
1352 *need_rewrite = false;
1353
1354 return 0;
1355}
1356
1357
1358int check_obj_locator_underscore(RGWBucketInfo& bucket_info, rgw_obj& obj, rgw_obj_key& key, bool fix, bool remove_bad, Formatter *f) {
1359 f->open_object_section("object");
1360 f->open_object_section("key");
1361 f->dump_string("type", "head");
1362 f->dump_string("name", key.name);
1363 f->dump_string("instance", key.instance);
1364 f->close_section();
1365
1366 string oid;
1367 string locator;
1368
1369 get_obj_bucket_and_oid_loc(obj, oid, locator);
1370
1371 f->dump_string("oid", oid);
1372 f->dump_string("locator", locator);
1373
1374
1375 RGWObjectCtx obj_ctx(store);
1376
1377 RGWRados::Object op_target(store, bucket_info, obj_ctx, obj);
1378 RGWRados::Object::Read read_op(&op_target);
1379
1380 int ret = read_op.prepare();
1381 bool needs_fixing = (ret == -ENOENT);
1382
1383 f->dump_bool("needs_fixing", needs_fixing);
1384
1385 string status = (needs_fixing ? "needs_fixing" : "ok");
1386
1387 if ((needs_fixing || remove_bad) && fix) {
1388 ret = store->fix_head_obj_locator(bucket_info, needs_fixing, remove_bad, key);
1389 if (ret < 0) {
1390 cerr << "ERROR: fix_head_object_locator() returned ret=" << ret << std::endl;
1391 goto done;
1392 }
1393 status = "fixed";
1394 }
1395
1396done:
1397 f->dump_string("status", status);
1398
1399 f->close_section();
1400
1401 return 0;
1402}
1403
1404int check_obj_tail_locator_underscore(RGWBucketInfo& bucket_info, rgw_obj& obj, rgw_obj_key& key, bool fix, Formatter *f) {
1405 f->open_object_section("object");
1406 f->open_object_section("key");
1407 f->dump_string("type", "tail");
1408 f->dump_string("name", key.name);
1409 f->dump_string("instance", key.instance);
1410 f->close_section();
1411
1412 bool needs_fixing;
1413 string status;
1414
1415 int ret = store->fix_tail_obj_locator(bucket_info, key, fix, &needs_fixing);
1416 if (ret < 0) {
1417 cerr << "ERROR: fix_tail_object_locator_underscore() returned ret=" << ret << std::endl;
1418 status = "failed";
1419 } else {
1420 status = (needs_fixing && !fix ? "needs_fixing" : "ok");
1421 }
1422
1423 f->dump_bool("needs_fixing", needs_fixing);
1424 f->dump_string("status", status);
1425
1426 f->close_section();
1427
1428 return 0;
1429}
1430
1431int do_check_object_locator(const string& tenant_name, const string& bucket_name,
1432 bool fix, bool remove_bad, Formatter *f)
1433{
1434 if (remove_bad && !fix) {
1435 cerr << "ERROR: can't have remove_bad specified without fix" << std::endl;
1436 return -EINVAL;
1437 }
1438
1439 RGWBucketInfo bucket_info;
1440 rgw_bucket bucket;
1441 string bucket_id;
1442
1443 f->open_object_section("bucket");
1444 f->dump_string("bucket", bucket_name);
1445 int ret = init_bucket(tenant_name, bucket_name, bucket_id, bucket_info, bucket);
1446 if (ret < 0) {
1447 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
1448 return ret;
1449 }
1450 bool truncated;
1451 int count = 0;
1452
1453 int max_entries = 1000;
1454
1455 string prefix;
1456 string delim;
1457 vector<rgw_bucket_dir_entry> result;
1458 map<string, bool> common_prefixes;
1459 string ns;
1460
1461 RGWRados::Bucket target(store, bucket_info);
1462 RGWRados::Bucket::List list_op(&target);
1463
1464 string marker;
1465
1466 list_op.params.prefix = prefix;
1467 list_op.params.delim = delim;
1468 list_op.params.marker = rgw_obj_key(marker);
1469 list_op.params.ns = ns;
1470 list_op.params.enforce_ns = true;
1471 list_op.params.list_versions = true;
1472
1473 f->open_array_section("check_objects");
1474 do {
1475 ret = list_op.list_objects(max_entries - count, &result, &common_prefixes, &truncated);
1476 if (ret < 0) {
1477 cerr << "ERROR: store->list_objects(): " << cpp_strerror(-ret) << std::endl;
1478 return -ret;
1479 }
1480
1481 count += result.size();
1482
1483 for (vector<rgw_bucket_dir_entry>::iterator iter = result.begin(); iter != result.end(); ++iter) {
1484 rgw_obj_key key = iter->key;
1485 rgw_obj obj(bucket, key);
1486
1487 if (key.name[0] == '_') {
1488 ret = check_obj_locator_underscore(bucket_info, obj, key, fix, remove_bad, f);
1489
1490 if (ret >= 0) {
1491 ret = check_obj_tail_locator_underscore(bucket_info, obj, key, fix, f);
1492 }
1493 }
1494 }
1495 f->flush(cout);
1496 } while (truncated && count < max_entries);
1497 f->close_section();
1498 f->close_section();
1499
1500 f->flush(cout);
1501
1502 return 0;
1503}
1504
c07f9fc5
FG
1505int set_bucket_sync_enabled(RGWRados *store, int opt_cmd, const string& tenant_name, const string& bucket_name)
1506{
1507 RGWBucketInfo bucket_info;
1508 map<string, bufferlist> attrs;
1509 RGWObjectCtx obj_ctx(store);
1510
1511 int r = store->get_bucket_info(obj_ctx, tenant_name, bucket_name, bucket_info, NULL, &attrs);
1512 if (r < 0) {
1513 cerr << "could not get bucket info for bucket=" << bucket_name << ": " << cpp_strerror(-r) << std::endl;
1514 return -r;
1515 }
1516
1517 if (opt_cmd == OPT_BUCKET_SYNC_ENABLE) {
1518 bucket_info.flags &= ~BUCKET_DATASYNC_DISABLED;
1519 } else if (opt_cmd == OPT_BUCKET_SYNC_DISABLE) {
1520 bucket_info.flags |= BUCKET_DATASYNC_DISABLED;
1521 }
1522
1523 r = store->put_bucket_instance_info(bucket_info, false, real_time(), &attrs);
1524 if (r < 0) {
1525 cerr << "ERROR: failed writing bucket instance info: " << cpp_strerror(-r) << std::endl;
1526 return -r;
1527 }
1528
1529 int shards_num = bucket_info.num_shards? bucket_info.num_shards : 1;
1530 int shard_id = bucket_info.num_shards? 0 : -1;
1531
1532 if (opt_cmd == OPT_BUCKET_SYNC_DISABLE) {
1533 r = store->stop_bi_log_entries(bucket_info, -1);
1534 if (r < 0) {
1535 lderr(store->ctx()) << "ERROR: failed writing stop bilog" << dendl;
1536 return r;
1537 }
1538 } else {
1539 r = store->resync_bi_log_entries(bucket_info, -1);
1540 if (r < 0) {
1541 lderr(store->ctx()) << "ERROR: failed writing resync bilog" << dendl;
1542 return r;
1543 }
1544 }
1545
1546 for (int i = 0; i < shards_num; ++i, ++shard_id) {
1547 r = store->data_log->add_entry(bucket_info.bucket, shard_id);
1548 if (r < 0) {
1549 lderr(store->ctx()) << "ERROR: failed writing data log" << dendl;
1550 return r;
1551 }
1552 }
1553
1554 return 0;
1555}
1556
1557
7c673cae
FG
1558/// search for a matching zone/zonegroup id and return a connection if found
1559static boost::optional<RGWRESTConn> get_remote_conn(RGWRados *store,
1560 const RGWZoneGroup& zonegroup,
1561 const std::string& remote)
1562{
1563 boost::optional<RGWRESTConn> conn;
1564 if (remote == zonegroup.get_id()) {
1565 conn.emplace(store->ctx(), store, remote, zonegroup.endpoints);
1566 } else {
1567 for (const auto& z : zonegroup.zones) {
1568 const auto& zone = z.second;
1569 if (remote == zone.id) {
1570 conn.emplace(store->ctx(), store, remote, zone.endpoints);
1571 break;
1572 }
1573 }
1574 }
1575 return conn;
1576}
1577
1578/// search each zonegroup for a connection
1579static boost::optional<RGWRESTConn> get_remote_conn(RGWRados *store,
1580 const RGWPeriodMap& period_map,
1581 const std::string& remote)
1582{
1583 boost::optional<RGWRESTConn> conn;
1584 for (const auto& zg : period_map.zonegroups) {
1585 conn = get_remote_conn(store, zg.second, remote);
1586 if (conn) {
1587 break;
1588 }
1589 }
1590 return conn;
1591}
1592
224ce89b
WB
1593// we expect a very small response
1594static constexpr size_t MAX_REST_RESPONSE = 128 * 1024;
1595
7c673cae
FG
1596static int send_to_remote_gateway(RGWRESTConn* conn, req_info& info,
1597 bufferlist& in_data, JSONParser& parser)
1598{
224ce89b
WB
1599 if (!conn) {
1600 return -EINVAL;
1601 }
1602
1603 ceph::bufferlist response;
7c673cae 1604 rgw_user user;
224ce89b 1605 int ret = conn->forward(user, info, nullptr, MAX_REST_RESPONSE, &in_data, &response);
7c673cae
FG
1606
1607 int parse_ret = parser.parse(response.c_str(), response.length());
1608 if (parse_ret < 0) {
1609 cerr << "failed to parse response" << std::endl;
1610 return parse_ret;
1611 }
1612 return ret;
1613}
1614
1615static int send_to_url(const string& url, const string& access,
1616 const string& secret, req_info& info,
1617 bufferlist& in_data, JSONParser& parser)
1618{
1619 if (access.empty() || secret.empty()) {
1620 cerr << "An --access-key and --secret must be provided with --url." << std::endl;
1621 return -EINVAL;
1622 }
1623 RGWAccessKey key;
1624 key.id = access;
1625 key.key = secret;
1626
1627 param_vec_t params;
1628 RGWRESTSimpleRequest req(g_ceph_context, url, NULL, &params);
1629
1630 bufferlist response;
1631 int ret = req.forward_request(key, info, MAX_REST_RESPONSE, &in_data, &response);
1632
1633 int parse_ret = parser.parse(response.c_str(), response.length());
1634 if (parse_ret < 0) {
1635 cout << "failed to parse response" << std::endl;
1636 return parse_ret;
1637 }
1638 return ret;
1639}
1640
1641static int send_to_remote_or_url(RGWRESTConn *conn, const string& url,
1642 const string& access, const string& secret,
1643 req_info& info, bufferlist& in_data,
1644 JSONParser& parser)
1645{
1646 if (url.empty()) {
1647 return send_to_remote_gateway(conn, info, in_data, parser);
1648 }
1649 return send_to_url(url, access, secret, info, in_data, parser);
1650}
1651
1652static int commit_period(RGWRealm& realm, RGWPeriod& period,
1653 string remote, const string& url,
1654 const string& access, const string& secret,
1655 bool force)
1656{
1657 const string& master_zone = period.get_master_zone();
1658 if (master_zone.empty()) {
1659 cerr << "cannot commit period: period does not have a master zone of a master zonegroup" << std::endl;
1660 return -EINVAL;
1661 }
1662 // are we the period's master zone?
1663 if (store->get_zone_params().get_id() == master_zone) {
1664 // read the current period
1665 RGWPeriod current_period;
1666 int ret = current_period.init(g_ceph_context, store, realm.get_id());
1667 if (ret < 0) {
1668 cerr << "Error initializing current period: "
1669 << cpp_strerror(-ret) << std::endl;
1670 return ret;
1671 }
1672 // the master zone can commit locally
1673 ret = period.commit(realm, current_period, cerr, force);
1674 if (ret < 0) {
1675 cerr << "failed to commit period: " << cpp_strerror(-ret) << std::endl;
1676 }
1677 return ret;
1678 }
1679
1680 if (remote.empty() && url.empty()) {
1681 // use the new master zone's connection
1682 remote = master_zone;
1683 cout << "Sending period to new master zone " << remote << std::endl;
1684 }
1685 boost::optional<RGWRESTConn> conn;
1686 RGWRESTConn *remote_conn = nullptr;
1687 if (!remote.empty()) {
1688 conn = get_remote_conn(store, period.get_map(), remote);
1689 if (!conn) {
1690 cerr << "failed to find a zone or zonegroup for remote "
1691 << remote << std::endl;
1692 return -ENOENT;
1693 }
1694 remote_conn = &*conn;
1695 }
1696
1697 // push period to the master with an empty period id
1698 period.set_id("");
1699
1700 RGWEnv env;
1701 req_info info(g_ceph_context, &env);
1702 info.method = "POST";
1703 info.request_uri = "/admin/realm/period";
1704
1705 // json format into a bufferlist
1706 JSONFormatter jf(false);
1707 encode_json("period", period, &jf);
1708 bufferlist bl;
1709 jf.flush(bl);
1710
1711 JSONParser p;
1712 int ret = send_to_remote_or_url(remote_conn, url, access, secret, info, bl, p);
1713 if (ret < 0) {
1714 cerr << "request failed: " << cpp_strerror(-ret) << std::endl;
1715
1716 // did we parse an error message?
1717 auto message = p.find_obj("Message");
1718 if (message) {
1719 cerr << "Reason: " << message->get_data() << std::endl;
1720 }
1721 return ret;
1722 }
1723
1724 // decode the response and store it back
1725 try {
1726 decode_json_obj(period, &p);
1727 } catch (JSONDecoder::err& e) {
1728 cout << "failed to decode JSON input: " << e.message << std::endl;
1729 return -EINVAL;
1730 }
1731 if (period.get_id().empty()) {
1732 cerr << "Period commit got back an empty period id" << std::endl;
1733 return -EINVAL;
1734 }
1735 // the master zone gave us back the period that it committed, so it's
1736 // safe to save it as our latest epoch
1737 ret = period.store_info(false);
1738 if (ret < 0) {
1739 cerr << "Error storing committed period " << period.get_id() << ": "
1740 << cpp_strerror(ret) << std::endl;
1741 return ret;
1742 }
1743 ret = period.set_latest_epoch(period.get_epoch());
1744 if (ret < 0) {
1745 cerr << "Error updating period epoch: " << cpp_strerror(ret) << std::endl;
1746 return ret;
1747 }
1748 ret = period.reflect();
1749 if (ret < 0) {
1750 cerr << "Error updating local objects: " << cpp_strerror(ret) << std::endl;
1751 return ret;
1752 }
1753 realm.notify_new_period(period);
1754 return ret;
1755}
1756
1757static int update_period(const string& realm_id, const string& realm_name,
1758 const string& period_id, const string& period_epoch,
1759 bool commit, const string& remote, const string& url,
1760 const string& access, const string& secret,
1761 Formatter *formatter, bool force)
1762{
1763 RGWRealm realm(realm_id, realm_name);
1764 int ret = realm.init(g_ceph_context, store);
1765 if (ret < 0 ) {
1766 cerr << "Error initializing realm " << cpp_strerror(-ret) << std::endl;
1767 return ret;
1768 }
1769 epoch_t epoch = 0;
1770 if (!period_epoch.empty()) {
1771 epoch = atoi(period_epoch.c_str());
1772 }
1773 RGWPeriod period(period_id, epoch);
1774 ret = period.init(g_ceph_context, store, realm.get_id());
1775 if (ret < 0) {
1776 cerr << "period init failed: " << cpp_strerror(-ret) << std::endl;
1777 return ret;
1778 }
1779 period.fork();
1780 ret = period.update();
1781 if(ret < 0) {
1782 // Dropping the error message here, as both the ret codes were handled in
1783 // period.update()
1784 return ret;
1785 }
1786 ret = period.store_info(false);
1787 if (ret < 0) {
1788 cerr << "failed to store period: " << cpp_strerror(-ret) << std::endl;
1789 return ret;
1790 }
1791 if (commit) {
1792 ret = commit_period(realm, period, remote, url, access, secret, force);
1793 if (ret < 0) {
1794 cerr << "failed to commit period: " << cpp_strerror(-ret) << std::endl;
1795 return ret;
1796 }
1797 }
1798 encode_json("period", period, formatter);
1799 formatter->flush(cout);
1800 cout << std::endl;
1801 return 0;
1802}
1803
1804static int init_bucket_for_sync(const string& tenant, const string& bucket_name,
1805 const string& bucket_id, rgw_bucket& bucket)
1806{
1807 RGWBucketInfo bucket_info;
1808
1809 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
1810 if (ret < 0) {
1811 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
1812 return ret;
1813 }
1814
1815 return 0;
1816}
1817
1818static int do_period_pull(RGWRESTConn *remote_conn, const string& url,
1819 const string& access_key, const string& secret_key,
1820 const string& realm_id, const string& realm_name,
1821 const string& period_id, const string& period_epoch,
1822 RGWPeriod *period)
1823{
1824 RGWEnv env;
1825 req_info info(g_ceph_context, &env);
1826 info.method = "GET";
1827 info.request_uri = "/admin/realm/period";
1828
1829 map<string, string> &params = info.args.get_params();
1830 if (!realm_id.empty())
1831 params["realm_id"] = realm_id;
1832 if (!realm_name.empty())
1833 params["realm_name"] = realm_name;
1834 if (!period_id.empty())
1835 params["period_id"] = period_id;
1836 if (!period_epoch.empty())
1837 params["epoch"] = period_epoch;
1838
1839 bufferlist bl;
1840 JSONParser p;
1841 int ret = send_to_remote_or_url(remote_conn, url, access_key, secret_key,
1842 info, bl, p);
1843 if (ret < 0) {
1844 cerr << "request failed: " << cpp_strerror(-ret) << std::endl;
1845 return ret;
1846 }
1847 ret = period->init(g_ceph_context, store, false);
1848 if (ret < 0) {
1849 cerr << "faile to init period " << cpp_strerror(-ret) << std::endl;
1850 return ret;
1851 }
1852 try {
1853 decode_json_obj(*period, &p);
1854 } catch (JSONDecoder::err& e) {
1855 cout << "failed to decode JSON input: " << e.message << std::endl;
1856 return -EINVAL;
1857 }
1858 ret = period->store_info(false);
1859 if (ret < 0) {
1860 cerr << "Error storing period " << period->get_id() << ": " << cpp_strerror(ret) << std::endl;
1861 }
224ce89b
WB
1862 // store latest epoch (ignore errors)
1863 period->update_latest_epoch(period->get_epoch());
7c673cae
FG
1864 return 0;
1865}
1866
1867static int read_current_period_id(RGWRados* store, const std::string& realm_id,
1868 const std::string& realm_name,
1869 std::string* period_id)
1870{
1871 RGWRealm realm(realm_id, realm_name);
1872 int ret = realm.init(g_ceph_context, store);
1873 if (ret < 0) {
1874 std::cerr << "failed to read realm: " << cpp_strerror(-ret) << std::endl;
1875 return ret;
1876 }
1877 *period_id = realm.get_current_period();
1878 return 0;
1879}
1880
1881void flush_ss(stringstream& ss, list<string>& l)
1882{
1883 if (!ss.str().empty()) {
1884 l.push_back(ss.str());
1885 }
1886 ss.str("");
1887}
1888
1889stringstream& push_ss(stringstream& ss, list<string>& l, int tab = 0)
1890{
1891 flush_ss(ss, l);
1892 if (tab > 0) {
1893 ss << setw(tab) << "" << setw(1);
1894 }
1895 return ss;
1896}
1897
1898static void get_md_sync_status(list<string>& status)
1899{
1900 RGWMetaSyncStatusManager sync(store, store->get_async_rados());
1901
1902 int ret = sync.init();
1903 if (ret < 0) {
1904 status.push_back(string("failed to retrieve sync info: sync.init() failed: ") + cpp_strerror(-ret));
1905 return;
1906 }
1907
1908 rgw_meta_sync_status sync_status;
1909 ret = sync.read_sync_status(&sync_status);
1910 if (ret < 0) {
1911 status.push_back(string("failed to read sync status: ") + cpp_strerror(-ret));
1912 return;
1913 }
1914
1915 string status_str;
1916 switch (sync_status.sync_info.state) {
1917 case rgw_meta_sync_info::StateInit:
1918 status_str = "init";
1919 break;
1920 case rgw_meta_sync_info::StateBuildingFullSyncMaps:
1921 status_str = "preparing for full sync";
1922 break;
1923 case rgw_meta_sync_info::StateSync:
1924 status_str = "syncing";
1925 break;
1926 default:
1927 status_str = "unknown";
1928 }
1929
1930 status.push_back(status_str);
1931
1932 uint64_t full_total = 0;
1933 uint64_t full_complete = 0;
1934
1935 int num_full = 0;
1936 int num_inc = 0;
1937 int total_shards = 0;
1938
1939 for (auto marker_iter : sync_status.sync_markers) {
1940 full_total += marker_iter.second.total_entries;
1941 total_shards++;
1942 if (marker_iter.second.state == rgw_meta_sync_marker::SyncState::FullSync) {
1943 num_full++;
1944 full_complete += marker_iter.second.pos;
1945 } else {
1946 full_complete += marker_iter.second.total_entries;
1947 }
1948 if (marker_iter.second.state == rgw_meta_sync_marker::SyncState::IncrementalSync) {
1949 num_inc++;
1950 }
1951 }
1952
1953 stringstream ss;
1954 push_ss(ss, status) << "full sync: " << num_full << "/" << total_shards << " shards";
1955
1956 if (num_full > 0) {
1957 push_ss(ss, status) << "full sync: " << full_total - full_complete << " entries to sync";
1958 }
1959
1960 push_ss(ss, status) << "incremental sync: " << num_inc << "/" << total_shards << " shards";
1961
1962 rgw_mdlog_info log_info;
1963 ret = sync.read_log_info(&log_info);
1964 if (ret < 0) {
1965 status.push_back(string("failed to fetch local sync status: ") + cpp_strerror(-ret));
1966 return;
1967 }
1968
1969 map<int, RGWMetadataLogInfo> master_shards_info;
1970 string master_period = store->get_current_period_id();
1971
1972 ret = sync.read_master_log_shards_info(master_period, &master_shards_info);
1973 if (ret < 0) {
1974 status.push_back(string("failed to fetch master sync status: ") + cpp_strerror(-ret));
1975 return;
1976 }
1977
1978 map<int, string> shards_behind;
1979 if (sync_status.sync_info.period != master_period) {
1980 status.push_back(string("master is on a different period: master_period=" +
1981 master_period + " local_period=" + sync_status.sync_info.period));
1982 } else {
1983 for (auto local_iter : sync_status.sync_markers) {
1984 int shard_id = local_iter.first;
1985 auto iter = master_shards_info.find(shard_id);
1986
1987 if (iter == master_shards_info.end()) {
1988 /* huh? */
1989 derr << "ERROR: could not find remote sync shard status for shard_id=" << shard_id << dendl;
1990 continue;
1991 }
1992 auto master_marker = iter->second.marker;
1993 if (local_iter.second.state == rgw_meta_sync_marker::SyncState::IncrementalSync &&
1994 master_marker > local_iter.second.marker) {
1995 shards_behind[shard_id] = local_iter.second.marker;
1996 }
1997 }
1998 }
1999
2000 int total_behind = shards_behind.size() + (sync_status.sync_info.num_shards - num_inc);
2001 if (total_behind == 0) {
2002 push_ss(ss, status) << "metadata is caught up with master";
2003 } else {
2004 push_ss(ss, status) << "metadata is behind on " << total_behind << " shards";
2005
2006 map<int, rgw_mdlog_shard_data> master_pos;
2007 ret = sync.read_master_log_shards_next(sync_status.sync_info.period, shards_behind, &master_pos);
2008 if (ret < 0) {
2009 derr << "ERROR: failed to fetch master next positions (" << cpp_strerror(-ret) << ")" << dendl;
2010 } else {
2011 ceph::real_time oldest;
2012 for (auto iter : master_pos) {
2013 rgw_mdlog_shard_data& shard_data = iter.second;
2014
2015 if (!shard_data.entries.empty()) {
2016 rgw_mdlog_entry& entry = shard_data.entries.front();
2017 if (ceph::real_clock::is_zero(oldest)) {
2018 oldest = entry.timestamp;
2019 } else if (!ceph::real_clock::is_zero(entry.timestamp) && entry.timestamp < oldest) {
2020 oldest = entry.timestamp;
2021 }
2022 }
2023 }
2024
2025 if (!ceph::real_clock::is_zero(oldest)) {
2026 push_ss(ss, status) << "oldest incremental change not applied: " << oldest;
2027 }
2028 }
2029 }
2030
2031 flush_ss(ss, status);
2032}
2033
2034static void get_data_sync_status(const string& source_zone, list<string>& status, int tab)
2035{
2036 stringstream ss;
2037
2038 auto ziter = store->zone_by_id.find(source_zone);
2039 if (ziter == store->zone_by_id.end()) {
2040 push_ss(ss, status, tab) << string("zone not found");
2041 flush_ss(ss, status);
2042 return;
2043 }
2044 RGWZone& sz = ziter->second;
2045
2046 if (!store->zone_syncs_from(store->get_zone(), sz)) {
2047 push_ss(ss, status, tab) << string("not syncing from zone");
2048 flush_ss(ss, status);
2049 return;
2050 }
2051 RGWDataSyncStatusManager sync(store, store->get_async_rados(), source_zone);
2052
2053 int ret = sync.init();
2054 if (ret < 0) {
2055 push_ss(ss, status, tab) << string("failed to retrieve sync info: ") + cpp_strerror(-ret);
2056 flush_ss(ss, status);
2057 return;
2058 }
2059
2060 rgw_data_sync_status sync_status;
2061 ret = sync.read_sync_status(&sync_status);
2062 if (ret < 0 && ret != -ENOENT) {
2063 push_ss(ss, status, tab) << string("failed read sync status: ") + cpp_strerror(-ret);
2064 return;
2065 }
2066
2067 string status_str;
2068 switch (sync_status.sync_info.state) {
2069 case rgw_data_sync_info::StateInit:
2070 status_str = "init";
2071 break;
2072 case rgw_data_sync_info::StateBuildingFullSyncMaps:
2073 status_str = "preparing for full sync";
2074 break;
2075 case rgw_data_sync_info::StateSync:
2076 status_str = "syncing";
2077 break;
2078 default:
2079 status_str = "unknown";
2080 }
2081
2082 push_ss(ss, status, tab) << status_str;
2083
2084 uint64_t full_total = 0;
2085 uint64_t full_complete = 0;
2086
2087 int num_full = 0;
2088 int num_inc = 0;
2089 int total_shards = 0;
2090
2091 for (auto marker_iter : sync_status.sync_markers) {
2092 full_total += marker_iter.second.total_entries;
2093 total_shards++;
2094 if (marker_iter.second.state == rgw_data_sync_marker::SyncState::FullSync) {
2095 num_full++;
2096 full_complete += marker_iter.second.pos;
2097 } else {
2098 full_complete += marker_iter.second.total_entries;
2099 }
2100 if (marker_iter.second.state == rgw_data_sync_marker::SyncState::IncrementalSync) {
2101 num_inc++;
2102 }
2103 }
2104
2105 push_ss(ss, status, tab) << "full sync: " << num_full << "/" << total_shards << " shards";
2106
2107 if (num_full > 0) {
2108 push_ss(ss, status, tab) << "full sync: " << full_total - full_complete << " buckets to sync";
2109 }
2110
2111 push_ss(ss, status, tab) << "incremental sync: " << num_inc << "/" << total_shards << " shards";
2112
2113 rgw_datalog_info log_info;
2114 ret = sync.read_log_info(&log_info);
2115 if (ret < 0) {
2116 push_ss(ss, status, tab) << string("failed to fetch local sync status: ") + cpp_strerror(-ret);
2117 return;
2118 }
2119
2120
2121 map<int, RGWDataChangesLogInfo> source_shards_info;
2122
2123 ret = sync.read_source_log_shards_info(&source_shards_info);
2124 if (ret < 0) {
2125 push_ss(ss, status, tab) << string("failed to fetch source sync status: ") + cpp_strerror(-ret);
2126 return;
2127 }
2128
2129 map<int, string> shards_behind;
2130
2131 for (auto local_iter : sync_status.sync_markers) {
2132 int shard_id = local_iter.first;
2133 auto iter = source_shards_info.find(shard_id);
2134
2135 if (iter == source_shards_info.end()) {
2136 /* huh? */
2137 derr << "ERROR: could not find remote sync shard status for shard_id=" << shard_id << dendl;
2138 continue;
2139 }
2140 auto master_marker = iter->second.marker;
2141 if (local_iter.second.state == rgw_data_sync_marker::SyncState::IncrementalSync &&
2142 master_marker > local_iter.second.marker) {
2143 shards_behind[shard_id] = local_iter.second.marker;
2144 }
2145 }
2146
2147 int total_behind = shards_behind.size() + (sync_status.sync_info.num_shards - num_inc);
2148 if (total_behind == 0) {
2149 push_ss(ss, status, tab) << "data is caught up with source";
2150 } else {
2151 push_ss(ss, status, tab) << "data is behind on " << total_behind << " shards";
2152
2153 map<int, rgw_datalog_shard_data> master_pos;
2154 ret = sync.read_source_log_shards_next(shards_behind, &master_pos);
2155 if (ret < 0) {
2156 derr << "ERROR: failed to fetch next positions (" << cpp_strerror(-ret) << ")" << dendl;
2157 } else {
2158 ceph::real_time oldest;
2159 for (auto iter : master_pos) {
2160 rgw_datalog_shard_data& shard_data = iter.second;
2161
2162 if (!shard_data.entries.empty()) {
2163 rgw_datalog_entry& entry = shard_data.entries.front();
2164 if (ceph::real_clock::is_zero(oldest)) {
2165 oldest = entry.timestamp;
2166 } else if (!ceph::real_clock::is_zero(entry.timestamp) && entry.timestamp < oldest) {
2167 oldest = entry.timestamp;
2168 }
2169 }
2170 }
2171
2172 if (!ceph::real_clock::is_zero(oldest)) {
2173 push_ss(ss, status, tab) << "oldest incremental change not applied: " << oldest;
2174 }
2175 }
2176 }
2177
2178 flush_ss(ss, status);
2179}
2180
2181static void tab_dump(const string& header, int width, const list<string>& entries)
2182{
2183 string s = header;
2184
2185 for (auto e : entries) {
2186 cout << std::setw(width) << s << std::setw(1) << " " << e << std::endl;
2187 s.clear();
2188 }
2189}
2190
2191
2192static void sync_status(Formatter *formatter)
2193{
2194 RGWRealm& realm = store->realm;
2195 RGWZoneGroup& zonegroup = store->get_zonegroup();
2196 RGWZone& zone = store->get_zone();
2197
2198 int width = 15;
2199
2200 cout << std::setw(width) << "realm" << std::setw(1) << " " << realm.get_id() << " (" << realm.get_name() << ")" << std::endl;
2201 cout << std::setw(width) << "zonegroup" << std::setw(1) << " " << zonegroup.get_id() << " (" << zonegroup.get_name() << ")" << std::endl;
2202 cout << std::setw(width) << "zone" << std::setw(1) << " " << zone.id << " (" << zone.name << ")" << std::endl;
2203
2204 list<string> md_status;
2205
2206 if (store->is_meta_master()) {
2207 md_status.push_back("no sync (zone is master)");
2208 } else {
2209 get_md_sync_status(md_status);
2210 }
2211
2212 tab_dump("metadata sync", width, md_status);
2213
2214 list<string> data_status;
2215
2216 for (auto iter : store->zone_conn_map) {
2217 const string& source_id = iter.first;
2218 string zone_name;
2219 string source_str = "source: ";
2220 string s = source_str + source_id;
2221 auto siter = store->zone_by_id.find(source_id);
2222 if (siter != store->zone_by_id.end()) {
2223 s += string(" (") + siter->second.name + ")";
2224 }
2225 data_status.push_back(s);
2226 get_data_sync_status(source_id, data_status, source_str.size());
2227 }
2228
2229 tab_dump("data sync", width, data_status);
2230}
2231
31f18b77 2232static void parse_tier_config_param(const string& s, map<string, string, ltstr_nocase>& out)
7c673cae
FG
2233{
2234 list<string> confs;
2235 get_str_list(s, ",", confs);
2236 for (auto c : confs) {
2237 ssize_t pos = c.find("=");
2238 if (pos < 0) {
2239 out[c] = "";
2240 } else {
2241 out[c.substr(0, pos)] = c.substr(pos + 1);
2242 }
2243 }
2244}
2245
31f18b77
FG
2246int check_reshard_bucket_params(RGWRados *store,
2247 const string& bucket_name,
2248 const string& tenant,
2249 const string& bucket_id,
2250 bool num_shards_specified,
2251 int num_shards,
2252 int yes_i_really_mean_it,
2253 rgw_bucket& bucket,
2254 RGWBucketInfo& bucket_info,
2255 map<string, bufferlist>& attrs)
2256{
2257 if (bucket_name.empty()) {
2258 cerr << "ERROR: bucket not specified" << std::endl;
2259 return -EINVAL;
7c673cae
FG
2260 }
2261
31f18b77
FG
2262 if (!num_shards_specified) {
2263 cerr << "ERROR: --num-shards not specified" << std::endl;
2264 return -EINVAL;
7c673cae
FG
2265 }
2266
31f18b77
FG
2267 if (num_shards > (int)store->get_max_bucket_shards()) {
2268 cerr << "ERROR: num_shards too high, max value: " << store->get_max_bucket_shards() << std::endl;
2269 return -EINVAL;
7c673cae
FG
2270 }
2271
31f18b77
FG
2272 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket, &attrs);
2273 if (ret < 0) {
2274 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
2275 return -ret;
7c673cae 2276 }
7c673cae 2277
31f18b77 2278 int num_source_shards = (bucket_info.num_shards > 0 ? bucket_info.num_shards : 1);
7c673cae 2279
31f18b77
FG
2280 if (num_shards <= num_source_shards && !yes_i_really_mean_it) {
2281 cerr << "num shards is less or equal to current shards count" << std::endl
2282 << "do you really mean it? (requires --yes-i-really-mean-it)" << std::endl;
2283 return -EINVAL;
7c673cae 2284 }
31f18b77
FG
2285 return 0;
2286}
7c673cae 2287
31f18b77
FG
2288int create_new_bucket_instance(RGWRados *store,
2289 int new_num_shards,
2290 const RGWBucketInfo& bucket_info,
2291 map<string, bufferlist>& attrs,
2292 RGWBucketInfo& new_bucket_info)
2293{
7c673cae 2294
31f18b77
FG
2295 store->create_bucket_id(&new_bucket_info.bucket.bucket_id);
2296 new_bucket_info.bucket.oid.clear();
7c673cae 2297
31f18b77
FG
2298 new_bucket_info.num_shards = new_num_shards;
2299 new_bucket_info.objv_tracker.clear();
7c673cae 2300
31f18b77
FG
2301 int ret = store->init_bucket_index(new_bucket_info, new_bucket_info.num_shards);
2302 if (ret < 0) {
2303 cerr << "ERROR: failed to init new bucket indexes: " << cpp_strerror(-ret) << std::endl;
2304 return -ret;
7c673cae
FG
2305 }
2306
31f18b77
FG
2307 ret = store->put_bucket_instance_info(new_bucket_info, true, real_time(), &attrs);
2308 if (ret < 0) {
2309 cerr << "ERROR: failed to store new bucket instance info: " << cpp_strerror(-ret) << std::endl;
2310 return -ret;
7c673cae
FG
2311 }
2312
31f18b77
FG
2313 return 0;
2314}
2315
7c673cae
FG
2316
2317#ifdef BUILDING_FOR_EMBEDDED
2318extern "C" int cephd_rgw_admin(int argc, const char **argv)
2319#else
2320int main(int argc, const char **argv)
2321#endif
2322{
2323 vector<const char*> args;
2324 argv_to_vec(argc, (const char **)argv, args);
2325 env_to_vec(args);
2326
2327 auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT,
2328 CODE_ENVIRONMENT_UTILITY, 0);
2329
2330 // for region -> zonegroup conversion (must happen before common_init_finish())
2331 if (!g_conf->rgw_region.empty() && g_conf->rgw_zonegroup.empty()) {
2332 g_conf->set_val_or_die("rgw_zonegroup", g_conf->rgw_region.c_str());
2333 }
2334
2335 common_init_finish(g_ceph_context);
2336
2337 rgw_user user_id;
2338 string tenant;
2339 std::string access_key, secret_key, user_email, display_name;
2340 std::string bucket_name, pool_name, object;
2341 rgw_pool pool;
2342 std::string date, subuser, access, format;
2343 std::string start_date, end_date;
2344 std::string key_type_str;
2345 std::string period_id, period_epoch, remote, url;
2346 std::string master_zonegroup, master_zone;
2347 std::string realm_name, realm_id, realm_new_name;
2348 std::string zone_name, zone_id, zone_new_name;
2349 std::string zonegroup_name, zonegroup_id, zonegroup_new_name;
2350 std::string api_name;
2351 std::string role_name, path, assume_role_doc, policy_name, perm_policy_doc, path_prefix;
2352 list<string> endpoints;
2353 int tmp_int;
2354 int sync_from_all_specified = false;
2355 bool sync_from_all = false;
2356 list<string> sync_from;
2357 list<string> sync_from_rm;
2358 std::string master_url;
2359 int is_master_int;
2360 int set_default = 0;
2361 bool is_master = false;
2362 bool is_master_set = false;
2363 int read_only_int;
2364 bool read_only = false;
2365 int is_read_only_set = false;
2366 int commit = false;
2367 int staging = false;
2368 int key_type = KEY_TYPE_UNDEFINED;
2369 rgw_bucket bucket;
2370 uint32_t perm_mask = 0;
2371 RGWUserInfo info;
2372 int opt_cmd = OPT_NO_CMD;
2373 bool need_more;
2374 int gen_access_key = 0;
2375 int gen_secret_key = 0;
2376 bool set_perm = false;
2377 bool set_temp_url_key = false;
2378 map<int, string> temp_url_keys;
2379 string bucket_id;
2380 Formatter *formatter = NULL;
2381 int purge_data = false;
7c673cae
FG
2382 int pretty_format = false;
2383 int show_log_entries = true;
2384 int show_log_sum = true;
2385 int skip_zero_entries = false; // log show
2386 int purge_keys = false;
2387 int yes_i_really_mean_it = false;
2388 int delete_child_objects = false;
2389 int fix = false;
2390 int remove_bad = false;
2391 int check_head_obj_locator = false;
2392 int max_buckets = -1;
2393 bool max_buckets_specified = false;
2394 map<string, bool> categories;
2395 string caps;
2396 int check_objects = false;
2397 RGWUserAdminOpState user_op;
2398 RGWBucketAdminOpState bucket_op;
2399 string infile;
2400 string metadata_key;
2401 RGWObjVersionTracker objv_tracker;
2402 string marker;
2403 string start_marker;
2404 string end_marker;
2405 int max_entries = -1;
2406 int admin = false;
2407 bool admin_specified = false;
2408 int system = false;
2409 bool system_specified = false;
2410 int shard_id = -1;
2411 bool specified_shard_id = false;
2412 string daemon_id;
2413 bool specified_daemon_id = false;
2414 string client_id;
2415 string op_id;
2416 string state_str;
2417 string replica_log_type_str;
2418 ReplicaLogType replica_log_type = ReplicaLog_Invalid;
2419 string op_mask_str;
2420 string quota_scope;
2421 string object_version;
2422 string placement_id;
2423 list<string> tags;
2424 list<string> tags_add;
2425 list<string> tags_rm;
2426
2427 int64_t max_objects = -1;
2428 int64_t max_size = -1;
2429 bool have_max_objects = false;
2430 bool have_max_size = false;
2431 int include_all = false;
2432
2433 int sync_stats = false;
2434 int bypass_gc = false;
2435 int warnings_only = false;
2436 int inconsistent_index = false;
2437
2438 int verbose = false;
2439
2440 int extra_info = false;
2441
2442 uint64_t min_rewrite_size = 4 * 1024 * 1024;
2443 uint64_t max_rewrite_size = ULLONG_MAX;
2444 uint64_t min_rewrite_stripe_size = 0;
2445
2446 BIIndexType bi_index_type = PlainIdx;
2447
2448 string job_id;
2449 int num_shards = 0;
2450 bool num_shards_specified = false;
2451 int max_concurrent_ios = 32;
2452 uint64_t orphan_stale_secs = (24 * 3600);
2453
2454 std::string val;
2455 std::ostringstream errs;
2456 string err;
2457 long long tmp = 0;
2458
2459 string source_zone_name;
2460 string source_zone; /* zone id */
2461
2462 string tier_type;
2463 bool tier_type_specified = false;
2464
31f18b77
FG
2465 map<string, string, ltstr_nocase> tier_config_add;
2466 map<string, string, ltstr_nocase> tier_config_rm;
7c673cae
FG
2467
2468 boost::optional<string> index_pool;
2469 boost::optional<string> data_pool;
2470 boost::optional<string> data_extra_pool;
2471 RGWBucketIndexType placement_index_type = RGWBIType_Normal;
2472 bool index_type_specified = false;
2473
2474 boost::optional<std::string> compression_type;
2475
2476 for (std::vector<const char*>::iterator i = args.begin(); i != args.end(); ) {
2477 if (ceph_argparse_double_dash(args, i)) {
2478 break;
2479 } else if (ceph_argparse_flag(args, i, "-h", "--help", (char*)NULL)) {
2480 usage();
2481 return 0;
2482 } else if (ceph_argparse_witharg(args, i, &val, "-i", "--uid", (char*)NULL)) {
2483 user_id.from_str(val);
2484 } else if (ceph_argparse_witharg(args, i, &val, "--tenant", (char*)NULL)) {
2485 tenant = val;
2486 } else if (ceph_argparse_witharg(args, i, &val, "--access-key", (char*)NULL)) {
2487 access_key = val;
2488 } else if (ceph_argparse_witharg(args, i, &val, "--subuser", (char*)NULL)) {
2489 subuser = val;
2490 } else if (ceph_argparse_witharg(args, i, &val, "--secret", "--secret-key", (char*)NULL)) {
2491 secret_key = val;
2492 } else if (ceph_argparse_witharg(args, i, &val, "-e", "--email", (char*)NULL)) {
2493 user_email = val;
2494 user_op.user_email_specified=true;
2495 } else if (ceph_argparse_witharg(args, i, &val, "-n", "--display-name", (char*)NULL)) {
2496 display_name = val;
2497 } else if (ceph_argparse_witharg(args, i, &val, "-b", "--bucket", (char*)NULL)) {
2498 bucket_name = val;
2499 } else if (ceph_argparse_witharg(args, i, &val, "-p", "--pool", (char*)NULL)) {
2500 pool_name = val;
2501 pool = rgw_pool(pool_name);
2502 } else if (ceph_argparse_witharg(args, i, &val, "-o", "--object", (char*)NULL)) {
2503 object = val;
2504 } else if (ceph_argparse_witharg(args, i, &val, "--object-version", (char*)NULL)) {
2505 object_version = val;
2506 } else if (ceph_argparse_witharg(args, i, &val, "--client-id", (char*)NULL)) {
2507 client_id = val;
2508 } else if (ceph_argparse_witharg(args, i, &val, "--op-id", (char*)NULL)) {
2509 op_id = val;
2510 } else if (ceph_argparse_witharg(args, i, &val, "--state", (char*)NULL)) {
2511 state_str = val;
2512 } else if (ceph_argparse_witharg(args, i, &val, "--op-mask", (char*)NULL)) {
2513 op_mask_str = val;
2514 } else if (ceph_argparse_witharg(args, i, &val, "--key-type", (char*)NULL)) {
2515 key_type_str = val;
2516 if (key_type_str.compare("swift") == 0) {
2517 key_type = KEY_TYPE_SWIFT;
2518 } else if (key_type_str.compare("s3") == 0) {
2519 key_type = KEY_TYPE_S3;
2520 } else {
2521 cerr << "bad key type: " << key_type_str << std::endl;
2522 return usage();
2523 }
2524 } else if (ceph_argparse_witharg(args, i, &val, "--job-id", (char*)NULL)) {
2525 job_id = val;
2526 } else if (ceph_argparse_binary_flag(args, i, &gen_access_key, NULL, "--gen-access-key", (char*)NULL)) {
2527 // do nothing
2528 } else if (ceph_argparse_binary_flag(args, i, &gen_secret_key, NULL, "--gen-secret", (char*)NULL)) {
2529 // do nothing
2530 } else if (ceph_argparse_binary_flag(args, i, &show_log_entries, NULL, "--show_log_entries", (char*)NULL)) {
2531 // do nothing
2532 } else if (ceph_argparse_binary_flag(args, i, &show_log_sum, NULL, "--show_log_sum", (char*)NULL)) {
2533 // do nothing
2534 } else if (ceph_argparse_binary_flag(args, i, &skip_zero_entries, NULL, "--skip_zero_entries", (char*)NULL)) {
2535 // do nothing
2536 } else if (ceph_argparse_binary_flag(args, i, &admin, NULL, "--admin", (char*)NULL)) {
2537 admin_specified = true;
2538 } else if (ceph_argparse_binary_flag(args, i, &system, NULL, "--system", (char*)NULL)) {
2539 system_specified = true;
2540 } else if (ceph_argparse_binary_flag(args, i, &verbose, NULL, "--verbose", (char*)NULL)) {
2541 // do nothing
2542 } else if (ceph_argparse_binary_flag(args, i, &staging, NULL, "--staging", (char*)NULL)) {
2543 // do nothing
2544 } else if (ceph_argparse_binary_flag(args, i, &commit, NULL, "--commit", (char*)NULL)) {
2545 // do nothing
2546 } else if (ceph_argparse_witharg(args, i, &tmp, errs, "-a", "--auth-uid", (char*)NULL)) {
2547 if (!errs.str().empty()) {
2548 cerr << errs.str() << std::endl;
2549 exit(EXIT_FAILURE);
2550 }
2551 } else if (ceph_argparse_witharg(args, i, &val, "--min-rewrite-size", (char*)NULL)) {
2552 min_rewrite_size = (uint64_t)atoll(val.c_str());
2553 } else if (ceph_argparse_witharg(args, i, &val, "--max-rewrite-size", (char*)NULL)) {
2554 max_rewrite_size = (uint64_t)atoll(val.c_str());
2555 } else if (ceph_argparse_witharg(args, i, &val, "--min-rewrite-stripe-size", (char*)NULL)) {
2556 min_rewrite_stripe_size = (uint64_t)atoll(val.c_str());
2557 } else if (ceph_argparse_witharg(args, i, &val, "--max-buckets", (char*)NULL)) {
2558 max_buckets = (int)strict_strtol(val.c_str(), 10, &err);
2559 if (!err.empty()) {
2560 cerr << "ERROR: failed to parse max buckets: " << err << std::endl;
2561 return EINVAL;
2562 }
2563 max_buckets_specified = true;
2564 } else if (ceph_argparse_witharg(args, i, &val, "--max-entries", (char*)NULL)) {
2565 max_entries = (int)strict_strtol(val.c_str(), 10, &err);
2566 if (!err.empty()) {
2567 cerr << "ERROR: failed to parse max entries: " << err << std::endl;
2568 return EINVAL;
2569 }
2570 } else if (ceph_argparse_witharg(args, i, &val, "--max-size", (char*)NULL)) {
2571 max_size = strict_si_cast<int64_t>(val.c_str(), &err);
2572 if (!err.empty()) {
2573 cerr << "ERROR: failed to parse max size: " << err << std::endl;
2574 return EINVAL;
2575 }
2576 have_max_size = true;
2577 } else if (ceph_argparse_witharg(args, i, &val, "--max-objects", (char*)NULL)) {
2578 max_objects = (int64_t)strict_strtoll(val.c_str(), 10, &err);
2579 if (!err.empty()) {
2580 cerr << "ERROR: failed to parse max objects: " << err << std::endl;
2581 return EINVAL;
2582 }
2583 have_max_objects = true;
2584 } else if (ceph_argparse_witharg(args, i, &val, "--date", "--time", (char*)NULL)) {
2585 date = val;
2586 if (end_date.empty())
2587 end_date = date;
2588 } else if (ceph_argparse_witharg(args, i, &val, "--start-date", "--start-time", (char*)NULL)) {
2589 start_date = val;
2590 } else if (ceph_argparse_witharg(args, i, &val, "--end-date", "--end-time", (char*)NULL)) {
2591 end_date = val;
2592 } else if (ceph_argparse_witharg(args, i, &val, "--num-shards", (char*)NULL)) {
2593 num_shards = (int)strict_strtol(val.c_str(), 10, &err);
2594 if (!err.empty()) {
2595 cerr << "ERROR: failed to parse num shards: " << err << std::endl;
2596 return EINVAL;
2597 }
2598 num_shards_specified = true;
2599 } else if (ceph_argparse_witharg(args, i, &val, "--max-concurrent-ios", (char*)NULL)) {
2600 max_concurrent_ios = (int)strict_strtol(val.c_str(), 10, &err);
2601 if (!err.empty()) {
2602 cerr << "ERROR: failed to parse max concurrent ios: " << err << std::endl;
2603 return EINVAL;
2604 }
2605 } else if (ceph_argparse_witharg(args, i, &val, "--orphan-stale-secs", (char*)NULL)) {
2606 orphan_stale_secs = (uint64_t)strict_strtoll(val.c_str(), 10, &err);
2607 if (!err.empty()) {
2608 cerr << "ERROR: failed to parse orphan stale secs: " << err << std::endl;
2609 return EINVAL;
2610 }
2611 } else if (ceph_argparse_witharg(args, i, &val, "--shard-id", (char*)NULL)) {
2612 shard_id = (int)strict_strtol(val.c_str(), 10, &err);
2613 if (!err.empty()) {
2614 cerr << "ERROR: failed to parse shard id: " << err << std::endl;
2615 return EINVAL;
2616 }
2617 specified_shard_id = true;
2618 } else if (ceph_argparse_witharg(args, i, &val, "--daemon-id", (char*)NULL)) {
2619 daemon_id = val;
2620 specified_daemon_id = true;
2621 } else if (ceph_argparse_witharg(args, i, &val, "--access", (char*)NULL)) {
2622 access = val;
2623 perm_mask = rgw_str_to_perm(access.c_str());
2624 set_perm = true;
2625 } else if (ceph_argparse_witharg(args, i, &val, "--temp-url-key", (char*)NULL)) {
2626 temp_url_keys[0] = val;
2627 set_temp_url_key = true;
2628 } else if (ceph_argparse_witharg(args, i, &val, "--temp-url-key2", "--temp-url-key-2", (char*)NULL)) {
2629 temp_url_keys[1] = val;
2630 set_temp_url_key = true;
2631 } else if (ceph_argparse_witharg(args, i, &val, "--bucket-id", (char*)NULL)) {
2632 bucket_id = val;
2633 if (bucket_id.empty()) {
2634 cerr << "bad bucket-id" << std::endl;
2635 return usage();
2636 }
2637 } else if (ceph_argparse_witharg(args, i, &val, "--format", (char*)NULL)) {
2638 format = val;
2639 } else if (ceph_argparse_witharg(args, i, &val, "--categories", (char*)NULL)) {
2640 string cat_str = val;
2641 list<string> cat_list;
2642 list<string>::iterator iter;
2643 get_str_list(cat_str, cat_list);
2644 for (iter = cat_list.begin(); iter != cat_list.end(); ++iter) {
2645 categories[*iter] = true;
2646 }
2647 } else if (ceph_argparse_binary_flag(args, i, &delete_child_objects, NULL, "--purge-objects", (char*)NULL)) {
2648 // do nothing
2649 } else if (ceph_argparse_binary_flag(args, i, &pretty_format, NULL, "--pretty-format", (char*)NULL)) {
2650 // do nothing
2651 } else if (ceph_argparse_binary_flag(args, i, &purge_data, NULL, "--purge-data", (char*)NULL)) {
2652 delete_child_objects = purge_data;
2653 } else if (ceph_argparse_binary_flag(args, i, &purge_keys, NULL, "--purge-keys", (char*)NULL)) {
2654 // do nothing
2655 } else if (ceph_argparse_binary_flag(args, i, &yes_i_really_mean_it, NULL, "--yes-i-really-mean-it", (char*)NULL)) {
2656 // do nothing
2657 } else if (ceph_argparse_binary_flag(args, i, &fix, NULL, "--fix", (char*)NULL)) {
2658 // do nothing
2659 } else if (ceph_argparse_binary_flag(args, i, &remove_bad, NULL, "--remove-bad", (char*)NULL)) {
2660 // do nothing
2661 } else if (ceph_argparse_binary_flag(args, i, &check_head_obj_locator, NULL, "--check-head-obj-locator", (char*)NULL)) {
2662 // do nothing
2663 } else if (ceph_argparse_binary_flag(args, i, &check_objects, NULL, "--check-objects", (char*)NULL)) {
2664 // do nothing
2665 } else if (ceph_argparse_binary_flag(args, i, &sync_stats, NULL, "--sync-stats", (char*)NULL)) {
2666 // do nothing
2667 } else if (ceph_argparse_binary_flag(args, i, &include_all, NULL, "--include-all", (char*)NULL)) {
2668 // do nothing
2669 } else if (ceph_argparse_binary_flag(args, i, &extra_info, NULL, "--extra-info", (char*)NULL)) {
2670 // do nothing
2671 } else if (ceph_argparse_binary_flag(args, i, &bypass_gc, NULL, "--bypass-gc", (char*)NULL)) {
2672 // do nothing
2673 } else if (ceph_argparse_binary_flag(args, i, &warnings_only, NULL, "--warnings-only", (char*)NULL)) {
2674 // do nothing
2675 } else if (ceph_argparse_binary_flag(args, i, &inconsistent_index, NULL, "--inconsistent-index", (char*)NULL)) {
2676 // do nothing
2677 } else if (ceph_argparse_witharg(args, i, &val, "--caps", (char*)NULL)) {
2678 caps = val;
2679 } else if (ceph_argparse_witharg(args, i, &val, "-i", "--infile", (char*)NULL)) {
2680 infile = val;
2681 } else if (ceph_argparse_witharg(args, i, &val, "--metadata-key", (char*)NULL)) {
2682 metadata_key = val;
2683 } else if (ceph_argparse_witharg(args, i, &val, "--marker", (char*)NULL)) {
2684 marker = val;
2685 } else if (ceph_argparse_witharg(args, i, &val, "--start-marker", (char*)NULL)) {
2686 start_marker = val;
2687 } else if (ceph_argparse_witharg(args, i, &val, "--end-marker", (char*)NULL)) {
2688 end_marker = val;
2689 } else if (ceph_argparse_witharg(args, i, &val, "--quota-scope", (char*)NULL)) {
2690 quota_scope = val;
2691 } else if (ceph_argparse_witharg(args, i, &val, "--replica-log-type", (char*)NULL)) {
2692 replica_log_type_str = val;
2693 replica_log_type = get_replicalog_type(replica_log_type_str);
2694 if (replica_log_type == ReplicaLog_Invalid) {
2695 cerr << "ERROR: invalid replica log type" << std::endl;
2696 return EINVAL;
2697 }
2698 } else if (ceph_argparse_witharg(args, i, &val, "--index-type", (char*)NULL)) {
2699 string index_type_str = val;
2700 bi_index_type = get_bi_index_type(index_type_str);
2701 if (bi_index_type == InvalidIdx) {
2702 cerr << "ERROR: invalid bucket index entry type" << std::endl;
2703 return EINVAL;
2704 }
2705 } else if (ceph_argparse_binary_flag(args, i, &is_master_int, NULL, "--master", (char*)NULL)) {
2706 is_master = (bool)is_master_int;
2707 is_master_set = true;
2708 } else if (ceph_argparse_binary_flag(args, i, &set_default, NULL, "--default", (char*)NULL)) {
2709 /* do nothing */
2710 } else if (ceph_argparse_binary_flag(args, i, &read_only_int, NULL, "--read-only", (char*)NULL)) {
2711 read_only = (bool)read_only_int;
2712 is_read_only_set = true;
2713 } else if (ceph_argparse_witharg(args, i, &val, "--master-url", (char*)NULL)) {
2714 master_url = val;
2715 } else if (ceph_argparse_witharg(args, i, &val, "--master-zonegroup", (char*)NULL)) {
2716 master_zonegroup = val;
2717 } else if (ceph_argparse_witharg(args, i, &val, "--master-zone", (char*)NULL)) {
2718 master_zone = val;
2719 } else if (ceph_argparse_witharg(args, i, &val, "--period", (char*)NULL)) {
2720 period_id = val;
2721 } else if (ceph_argparse_witharg(args, i, &val, "--epoch", (char*)NULL)) {
2722 period_epoch = val;
2723 } else if (ceph_argparse_witharg(args, i, &val, "--remote", (char*)NULL)) {
2724 remote = val;
2725 } else if (ceph_argparse_witharg(args, i, &val, "--url", (char*)NULL)) {
2726 url = val;
2727 } else if (ceph_argparse_witharg(args, i, &val, "--realm-id", (char*)NULL)) {
2728 realm_id = val;
2729 } else if (ceph_argparse_witharg(args, i, &val, "--realm-new-name", (char*)NULL)) {
2730 realm_new_name = val;
2731 } else if (ceph_argparse_witharg(args, i, &val, "--zonegroup-id", (char*)NULL)) {
2732 zonegroup_id = val;
2733 } else if (ceph_argparse_witharg(args, i, &val, "--zonegroup-new-name", (char*)NULL)) {
2734 zonegroup_new_name = val;
2735 } else if (ceph_argparse_witharg(args, i, &val, "--placement-id", (char*)NULL)) {
2736 placement_id = val;
2737 } else if (ceph_argparse_witharg(args, i, &val, "--tags", (char*)NULL)) {
2738 get_str_list(val, tags);
2739 } else if (ceph_argparse_witharg(args, i, &val, "--tags-add", (char*)NULL)) {
2740 get_str_list(val, tags_add);
2741 } else if (ceph_argparse_witharg(args, i, &val, "--tags-rm", (char*)NULL)) {
2742 get_str_list(val, tags_rm);
2743 } else if (ceph_argparse_witharg(args, i, &val, "--api-name", (char*)NULL)) {
2744 api_name = val;
2745 } else if (ceph_argparse_witharg(args, i, &val, "--zone-id", (char*)NULL)) {
2746 zone_id = val;
2747 } else if (ceph_argparse_witharg(args, i, &val, "--zone-new-name", (char*)NULL)) {
2748 zone_new_name = val;
2749 } else if (ceph_argparse_witharg(args, i, &val, "--endpoints", (char*)NULL)) {
2750 get_str_list(val, endpoints);
2751 } else if (ceph_argparse_witharg(args, i, &val, "--sync-from", (char*)NULL)) {
2752 get_str_list(val, sync_from);
2753 } else if (ceph_argparse_witharg(args, i, &val, "--sync-from-rm", (char*)NULL)) {
2754 get_str_list(val, sync_from_rm);
2755 } else if (ceph_argparse_binary_flag(args, i, &tmp_int, NULL, "--sync-from-all", (char*)NULL)) {
2756 sync_from_all = (bool)tmp_int;
2757 sync_from_all_specified = true;
2758 } else if (ceph_argparse_witharg(args, i, &val, "--source-zone", (char*)NULL)) {
2759 source_zone_name = val;
2760 } else if (ceph_argparse_witharg(args, i, &val, "--tier-type", (char*)NULL)) {
2761 tier_type = val;
2762 tier_type_specified = true;
2763 } else if (ceph_argparse_witharg(args, i, &val, "--tier-config", (char*)NULL)) {
2764 parse_tier_config_param(val, tier_config_add);
2765 } else if (ceph_argparse_witharg(args, i, &val, "--tier-config-rm", (char*)NULL)) {
2766 parse_tier_config_param(val, tier_config_rm);
2767 } else if (ceph_argparse_witharg(args, i, &val, "--index-pool", (char*)NULL)) {
2768 index_pool = val;
2769 } else if (ceph_argparse_witharg(args, i, &val, "--data-pool", (char*)NULL)) {
2770 data_pool = val;
2771 } else if (ceph_argparse_witharg(args, i, &val, "--data-extra-pool", (char*)NULL)) {
2772 data_extra_pool = val;
2773 } else if (ceph_argparse_witharg(args, i, &val, "--placement-index-type", (char*)NULL)) {
2774 if (val == "normal") {
2775 placement_index_type = RGWBIType_Normal;
2776 } else if (val == "indexless") {
2777 placement_index_type = RGWBIType_Indexless;
2778 } else {
2779 placement_index_type = (RGWBucketIndexType)strict_strtol(val.c_str(), 10, &err);
2780 if (!err.empty()) {
2781 cerr << "ERROR: failed to parse index type index: " << err << std::endl;
2782 return EINVAL;
2783 }
2784 }
2785 index_type_specified = true;
2786 } else if (ceph_argparse_witharg(args, i, &val, "--compression", (char*)NULL)) {
2787 compression_type = val;
2788 } else if (ceph_argparse_witharg(args, i, &val, "--role-name", (char*)NULL)) {
2789 role_name = val;
2790 } else if (ceph_argparse_witharg(args, i, &val, "--path", (char*)NULL)) {
2791 path = val;
2792 } else if (ceph_argparse_witharg(args, i, &val, "--assume-role-policy-doc", (char*)NULL)) {
2793 assume_role_doc = val;
2794 } else if (ceph_argparse_witharg(args, i, &val, "--policy-name", (char*)NULL)) {
2795 policy_name = val;
2796 } else if (ceph_argparse_witharg(args, i, &val, "--policy-doc", (char*)NULL)) {
2797 perm_policy_doc = val;
2798 } else if (ceph_argparse_witharg(args, i, &val, "--path-prefix", (char*)NULL)) {
2799 path_prefix = val;
2800 } else if (strncmp(*i, "-", 1) == 0) {
2801 cerr << "ERROR: invalid flag " << *i << std::endl;
2802 return EINVAL;
2803 } else {
2804 ++i;
2805 }
2806 }
7c673cae
FG
2807
2808 if (args.empty()) {
2809 return usage();
2810 }
2811 else {
2812 const char *prev_cmd = NULL;
2813 const char *prev_prev_cmd = NULL;
2814 std::vector<const char*>::iterator i ;
2815 for (i = args.begin(); i != args.end(); ++i) {
2816 opt_cmd = get_cmd(*i, prev_cmd, prev_prev_cmd, &need_more);
2817 if (opt_cmd < 0) {
2818 cerr << "unrecognized arg " << *i << std::endl;
2819 return usage();
2820 }
2821 if (!need_more) {
2822 ++i;
2823 break;
2824 }
2825 prev_prev_cmd = prev_cmd;
2826 prev_cmd = *i;
2827 }
2828
2829 if (opt_cmd == OPT_NO_CMD)
2830 return usage();
2831
2832 /* some commands may have an optional extra param */
2833 if (i != args.end()) {
2834 switch (opt_cmd) {
2835 case OPT_METADATA_GET:
2836 case OPT_METADATA_PUT:
2837 case OPT_METADATA_RM:
2838 case OPT_METADATA_LIST:
2839 metadata_key = *i;
2840 break;
2841 default:
2842 break;
2843 }
2844 }
2845
31f18b77
FG
2846 if (tenant.empty()) {
2847 tenant = user_id.tenant;
2848 } else {
2849 if (user_id.empty() && opt_cmd != OPT_ROLE_CREATE
2850 && opt_cmd != OPT_ROLE_DELETE
2851 && opt_cmd != OPT_ROLE_GET
2852 && opt_cmd != OPT_ROLE_MODIFY
2853 && opt_cmd != OPT_ROLE_LIST
2854 && opt_cmd != OPT_ROLE_POLICY_PUT
2855 && opt_cmd != OPT_ROLE_POLICY_LIST
2856 && opt_cmd != OPT_ROLE_POLICY_GET
2857 && opt_cmd != OPT_ROLE_POLICY_DELETE) {
2858 cerr << "ERROR: --tenant is set, but there's no user ID" << std::endl;
2859 return EINVAL;
2860 }
2861 user_id.tenant = tenant;
2862 }
7c673cae
FG
2863 /* check key parameter conflict */
2864 if ((!access_key.empty()) && gen_access_key) {
2865 cerr << "ERROR: key parameter conflict, --access-key & --gen-access-key" << std::endl;
2866 return EINVAL;
2867 }
2868 if ((!secret_key.empty()) && gen_secret_key) {
2869 cerr << "ERROR: key parameter conflict, --secret & --gen-secret" << std::endl;
2870 return EINVAL;
2871 }
2872 }
2873
2874 // default to pretty json
2875 if (format.empty()) {
2876 format = "json";
2877 pretty_format = true;
2878 }
2879
2880 if (format == "xml")
2881 formatter = new XMLFormatter(pretty_format);
2882 else if (format == "json")
2883 formatter = new JSONFormatter(pretty_format);
2884 else {
2885 cerr << "unrecognized format: " << format << std::endl;
2886 return usage();
2887 }
2888
2889 realm_name = g_conf->rgw_realm;
2890 zone_name = g_conf->rgw_zone;
2891 zonegroup_name = g_conf->rgw_zonegroup;
2892
2893 RGWStreamFlusher f(formatter, cout);
2894
2895 // not a raw op if 'period update' needs to commit to master
2896 bool raw_period_update = opt_cmd == OPT_PERIOD_UPDATE && !commit;
2897 std::set<int> raw_storage_ops_list = {OPT_ZONEGROUP_ADD, OPT_ZONEGROUP_CREATE, OPT_ZONEGROUP_DELETE,
2898 OPT_ZONEGROUP_GET, OPT_ZONEGROUP_LIST,
2899 OPT_ZONEGROUP_SET, OPT_ZONEGROUP_DEFAULT,
2900 OPT_ZONEGROUP_RENAME, OPT_ZONEGROUP_MODIFY,
2901 OPT_ZONEGROUP_REMOVE,
2902 OPT_ZONEGROUP_PLACEMENT_ADD, OPT_ZONEGROUP_PLACEMENT_RM,
2903 OPT_ZONEGROUP_PLACEMENT_MODIFY, OPT_ZONEGROUP_PLACEMENT_LIST,
2904 OPT_ZONEGROUP_PLACEMENT_DEFAULT,
2905 OPT_ZONE_CREATE, OPT_ZONE_DELETE,
2906 OPT_ZONE_GET, OPT_ZONE_SET, OPT_ZONE_RENAME,
2907 OPT_ZONE_LIST, OPT_ZONE_MODIFY, OPT_ZONE_DEFAULT,
2908 OPT_ZONE_PLACEMENT_ADD, OPT_ZONE_PLACEMENT_RM,
2909 OPT_ZONE_PLACEMENT_MODIFY, OPT_ZONE_PLACEMENT_LIST,
2910 OPT_REALM_CREATE,
2911 OPT_PERIOD_DELETE, OPT_PERIOD_GET,
2912 OPT_PERIOD_PULL,
2913 OPT_PERIOD_GET_CURRENT, OPT_PERIOD_LIST,
2914 OPT_GLOBAL_QUOTA_GET, OPT_GLOBAL_QUOTA_SET,
2915 OPT_GLOBAL_QUOTA_ENABLE, OPT_GLOBAL_QUOTA_DISABLE,
2916 OPT_REALM_DELETE, OPT_REALM_GET, OPT_REALM_LIST,
2917 OPT_REALM_LIST_PERIODS,
2918 OPT_REALM_GET_DEFAULT, OPT_REALM_REMOVE,
2919 OPT_REALM_RENAME, OPT_REALM_SET,
2920 OPT_REALM_DEFAULT, OPT_REALM_PULL};
2921
2922
2923 bool raw_storage_op = (raw_storage_ops_list.find(opt_cmd) != raw_storage_ops_list.end() ||
2924 raw_period_update);
2925
2926 if (raw_storage_op) {
2927 store = RGWStoreManager::get_raw_storage(g_ceph_context);
2928 } else {
31f18b77 2929 store = RGWStoreManager::get_storage(g_ceph_context, false, false, false, false, false);
7c673cae
FG
2930 }
2931 if (!store) {
2932 cerr << "couldn't init storage provider" << std::endl;
2933 return 5; //EIO
2934 }
2935
2936 if (!source_zone_name.empty()) {
2937 if (!store->find_zone_id_by_name(source_zone_name, &source_zone)) {
2938 cerr << "WARNING: cannot find source zone id for name=" << source_zone_name << std::endl;
2939 source_zone = source_zone_name;
2940 }
2941 }
2942
2943 rgw_user_init(store);
2944 rgw_bucket_init(store->meta_mgr);
2945
2946 StoreDestructor store_destructor(store);
2947
2948 if (raw_storage_op) {
2949 switch (opt_cmd) {
2950 case OPT_PERIOD_DELETE:
2951 {
2952 if (period_id.empty()) {
2953 cerr << "missing period id" << std::endl;
2954 return EINVAL;
2955 }
2956 RGWPeriod period(period_id);
2957 int ret = period.init(g_ceph_context, store);
2958 if (ret < 0) {
2959 cerr << "period.init failed: " << cpp_strerror(-ret) << std::endl;
2960 return -ret;
2961 }
2962 ret = period.delete_obj();
2963 if (ret < 0) {
2964 cerr << "ERROR: couldn't delete period: " << cpp_strerror(-ret) << std::endl;
2965 return -ret;
2966 }
2967
2968 }
2969 break;
2970 case OPT_PERIOD_GET:
2971 {
2972 epoch_t epoch = 0;
2973 if (!period_epoch.empty()) {
2974 epoch = atoi(period_epoch.c_str());
2975 }
2976 if (staging) {
2977 RGWRealm realm(realm_id, realm_name);
2978 int ret = realm.init(g_ceph_context, store);
2979 if (ret < 0 ) {
2980 cerr << "Error initializing realm " << cpp_strerror(-ret) << std::endl;
2981 return -ret;
2982 }
2983 realm_id = realm.get_id();
2984 realm_name = realm.get_name();
2985 period_id = RGWPeriod::get_staging_id(realm_id);
2986 epoch = 1;
2987 }
2988 RGWPeriod period(period_id, epoch);
2989 int ret = period.init(g_ceph_context, store, realm_id, realm_name);
2990 if (ret < 0) {
2991 cerr << "period init failed: " << cpp_strerror(-ret) << std::endl;
2992 return -ret;
2993 }
2994 encode_json("period", period, formatter);
2995 formatter->flush(cout);
2996 cout << std::endl;
2997 }
2998 break;
2999 case OPT_PERIOD_GET_CURRENT:
3000 {
3001 int ret = read_current_period_id(store, realm_id, realm_name, &period_id);
3002 if (ret < 0) {
3003 return -ret;
3004 }
3005 formatter->open_object_section("period_get_current");
3006 encode_json("current_period", period_id, formatter);
3007 formatter->close_section();
3008 formatter->flush(cout);
3009 }
3010 break;
3011 case OPT_PERIOD_LIST:
3012 {
3013 list<string> periods;
3014 int ret = store->list_periods(periods);
3015 if (ret < 0) {
3016 cerr << "failed to list periods: " << cpp_strerror(-ret) << std::endl;
3017 return -ret;
3018 }
3019 formatter->open_object_section("periods_list");
3020 encode_json("periods", periods, formatter);
3021 formatter->close_section();
3022 formatter->flush(cout);
3023 cout << std::endl;
3024 }
3025 break;
3026 case OPT_PERIOD_UPDATE:
3027 {
3028 int ret = update_period(realm_id, realm_name, period_id, period_epoch,
3029 commit, remote, url, access_key, secret_key,
3030 formatter, yes_i_really_mean_it);
3031 if (ret < 0) {
3032 return -ret;
3033 }
3034 }
3035 break;
3036 case OPT_PERIOD_PULL:
3037 {
3038 boost::optional<RGWRESTConn> conn;
3039 RGWRESTConn *remote_conn = nullptr;
3040 if (url.empty()) {
3041 // load current period for endpoints
3042 RGWRealm realm(realm_id, realm_name);
3043 int ret = realm.init(g_ceph_context, store);
3044 if (ret < 0) {
3045 cerr << "failed to init realm: " << cpp_strerror(-ret) << std::endl;
3046 return -ret;
3047 }
3048 RGWPeriod current_period(realm.get_current_period());
3049 ret = current_period.init(g_ceph_context, store);
3050 if (ret < 0) {
3051 cerr << "failed to init current period: " << cpp_strerror(-ret) << std::endl;
3052 return -ret;
3053 }
3054 if (remote.empty()) {
3055 // use realm master zone as remote
3056 remote = current_period.get_master_zone();
3057 }
3058 conn = get_remote_conn(store, current_period.get_map(), remote);
3059 if (!conn) {
3060 cerr << "failed to find a zone or zonegroup for remote "
3061 << remote << std::endl;
3062 return -ENOENT;
3063 }
3064 remote_conn = &*conn;
3065 }
3066
3067 RGWPeriod period;
3068 int ret = do_period_pull(remote_conn, url, access_key, secret_key,
3069 realm_id, realm_name, period_id, period_epoch,
3070 &period);
3071 if (ret < 0) {
3072 cerr << "period pull failed: " << cpp_strerror(-ret) << std::endl;
3073 return -ret;
3074 }
3075
3076 encode_json("period", period, formatter);
3077 formatter->flush(cout);
3078 cout << std::endl;
3079 }
3080 break;
3081 case OPT_GLOBAL_QUOTA_GET:
3082 case OPT_GLOBAL_QUOTA_SET:
3083 case OPT_GLOBAL_QUOTA_ENABLE:
3084 case OPT_GLOBAL_QUOTA_DISABLE:
3085 {
3086 if (realm_id.empty()) {
3087 RGWRealm realm(g_ceph_context, store);
3088 if (!realm_name.empty()) {
3089 // look up realm_id for the given realm_name
3090 int ret = realm.read_id(realm_name, realm_id);
3091 if (ret < 0) {
3092 cerr << "ERROR: failed to read realm for " << realm_name
3093 << ": " << cpp_strerror(-ret) << std::endl;
3094 return -ret;
3095 }
3096 } else {
3097 // use default realm_id when none is given
3098 int ret = realm.read_default_id(realm_id);
3099 if (ret < 0 && ret != -ENOENT) { // on ENOENT, use empty realm_id
3100 cerr << "ERROR: failed to read default realm: "
3101 << cpp_strerror(-ret) << std::endl;
3102 return -ret;
3103 }
3104 }
3105 }
3106
3107 RGWPeriodConfig period_config;
3108 int ret = period_config.read(store, realm_id);
3109 if (ret < 0 && ret != -ENOENT) {
3110 cerr << "ERROR: failed to read period config: "
3111 << cpp_strerror(-ret) << std::endl;
3112 return -ret;
3113 }
3114
3115 formatter->open_object_section("period_config");
3116 if (quota_scope == "bucket") {
3117 set_quota_info(period_config.bucket_quota, opt_cmd,
3118 max_size, max_objects,
3119 have_max_size, have_max_objects);
3120 encode_json("bucket quota", period_config.bucket_quota, formatter);
3121 } else if (quota_scope == "user") {
3122 set_quota_info(period_config.user_quota, opt_cmd,
3123 max_size, max_objects,
3124 have_max_size, have_max_objects);
3125 encode_json("user quota", period_config.user_quota, formatter);
3126 } else if (quota_scope.empty() && opt_cmd == OPT_GLOBAL_QUOTA_GET) {
3127 // if no scope is given for GET, print both
3128 encode_json("bucket quota", period_config.bucket_quota, formatter);
3129 encode_json("user quota", period_config.user_quota, formatter);
3130 } else {
3131 cerr << "ERROR: invalid quota scope specification. Please specify "
3132 "either --quota-scope=bucket, or --quota-scope=user" << std::endl;
3133 return EINVAL;
3134 }
3135 formatter->close_section();
3136
3137 if (opt_cmd != OPT_GLOBAL_QUOTA_GET) {
3138 // write the modified period config
3139 ret = period_config.write(store, realm_id);
3140 if (ret < 0) {
3141 cerr << "ERROR: failed to write period config: "
3142 << cpp_strerror(-ret) << std::endl;
3143 return -ret;
3144 }
3145 if (!realm_id.empty()) {
3146 cout << "Global quota changes saved. Use 'period update' to apply "
3147 "them to the staging period, and 'period commit' to commit the "
3148 "new period." << std::endl;
3149 } else {
3150 cout << "Global quota changes saved. They will take effect as "
3151 "the gateways are restarted." << std::endl;
3152 }
3153 }
3154
3155 formatter->flush(cout);
3156 cout << std::endl;
3157 }
3158 break;
3159 case OPT_REALM_CREATE:
3160 {
3161 if (realm_name.empty()) {
3162 cerr << "missing realm name" << std::endl;
3163 return EINVAL;
3164 }
3165
3166 RGWRealm realm(realm_name, g_ceph_context, store);
3167 int ret = realm.create();
3168 if (ret < 0) {
3169 cerr << "ERROR: couldn't create realm " << realm_name << ": " << cpp_strerror(-ret) << std::endl;
3170 return -ret;
3171 }
3172
3173 if (set_default) {
3174 ret = realm.set_as_default();
3175 if (ret < 0) {
3176 cerr << "failed to set realm " << realm_name << " as default: " << cpp_strerror(-ret) << std::endl;
3177 }
3178 }
3179
3180 encode_json("realm", realm, formatter);
3181 formatter->flush(cout);
3182 cout << std::endl;
3183 }
3184 break;
3185 case OPT_REALM_DELETE:
3186 {
3187 RGWRealm realm(realm_id, realm_name);
3188 if (realm_name.empty() && realm_id.empty()) {
3189 cerr << "missing realm name or id" << std::endl;
3190 return EINVAL;
3191 }
3192 int ret = realm.init(g_ceph_context, store);
3193 if (ret < 0) {
3194 cerr << "realm.init failed: " << cpp_strerror(-ret) << std::endl;
3195 return -ret;
3196 }
3197 ret = realm.delete_obj();
3198 if (ret < 0) {
3199 cerr << "ERROR: couldn't : " << cpp_strerror(-ret) << std::endl;
3200 return -ret;
3201 }
3202
3203 }
3204 break;
3205 case OPT_REALM_GET:
3206 {
3207 RGWRealm realm(realm_id, realm_name);
3208 int ret = realm.init(g_ceph_context, store);
3209 if (ret < 0) {
3210 if (ret == -ENOENT && realm_name.empty() && realm_id.empty()) {
3211 cerr << "missing realm name or id, or default realm not found" << std::endl;
3212 } else {
3213 cerr << "realm.init failed: " << cpp_strerror(-ret) << std::endl;
3214 }
3215 return -ret;
3216 }
3217 encode_json("realm", realm, formatter);
3218 formatter->flush(cout);
3219 cout << std::endl;
3220 }
3221 break;
3222 case OPT_REALM_GET_DEFAULT:
3223 {
3224 RGWRealm realm(g_ceph_context, store);
3225 string default_id;
3226 int ret = realm.read_default_id(default_id);
3227 if (ret == -ENOENT) {
3228 cout << "No default realm is set" << std::endl;
3229 return -ret;
3230 } else if (ret < 0) {
3231 cerr << "Error reading default realm:" << cpp_strerror(-ret) << std::endl;
3232 return -ret;
3233 }
3234 cout << "default realm: " << default_id << std::endl;
3235 }
3236 break;
3237 case OPT_REALM_LIST:
3238 {
3239 RGWRealm realm(g_ceph_context, store);
3240 string default_id;
3241 int ret = realm.read_default_id(default_id);
3242 if (ret < 0 && ret != -ENOENT) {
3243 cerr << "could not determine default realm: " << cpp_strerror(-ret) << std::endl;
3244 }
3245 list<string> realms;
3246 ret = store->list_realms(realms);
3247 if (ret < 0) {
3248 cerr << "failed to list realms: " << cpp_strerror(-ret) << std::endl;
3249 return -ret;
3250 }
3251 formatter->open_object_section("realms_list");
3252 encode_json("default_info", default_id, formatter);
3253 encode_json("realms", realms, formatter);
3254 formatter->close_section();
3255 formatter->flush(cout);
3256 cout << std::endl;
3257 }
3258 break;
3259 case OPT_REALM_LIST_PERIODS:
3260 {
3261 int ret = read_current_period_id(store, realm_id, realm_name, &period_id);
3262 if (ret < 0) {
3263 return -ret;
3264 }
3265 list<string> periods;
3266 ret = store->list_periods(period_id, periods);
3267 if (ret < 0) {
3268 cerr << "list periods failed: " << cpp_strerror(-ret) << std::endl;
3269 return -ret;
3270 }
3271 formatter->open_object_section("realm_periods_list");
3272 encode_json("current_period", period_id, formatter);
3273 encode_json("periods", periods, formatter);
3274 formatter->close_section();
3275 formatter->flush(cout);
3276 cout << std::endl;
3277 }
3278 break;
3279
3280 case OPT_REALM_RENAME:
3281 {
3282 RGWRealm realm(realm_id, realm_name);
3283 if (realm_new_name.empty()) {
3284 cerr << "missing realm new name" << std::endl;
3285 return EINVAL;
3286 }
3287 if (realm_name.empty() && realm_id.empty()) {
3288 cerr << "missing realm name or id" << std::endl;
3289 return EINVAL;
3290 }
3291 int ret = realm.init(g_ceph_context, store);
3292 if (ret < 0) {
3293 cerr << "realm.init failed: " << cpp_strerror(-ret) << std::endl;
3294 return -ret;
3295 }
3296 ret = realm.rename(realm_new_name);
3297 if (ret < 0) {
3298 cerr << "realm.rename failed: " << cpp_strerror(-ret) << std::endl;
3299 return -ret;
3300 }
3301 cout << "Realm name updated. Note that this change only applies to "
3302 "the current cluster, so this command must be run separately "
3303 "on each of the realm's other clusters." << std::endl;
3304 }
3305 break;
3306 case OPT_REALM_SET:
3307 {
3308 if (realm_id.empty() && realm_name.empty()) {
3309 cerr << "no realm name or id provided" << std::endl;
3310 return EINVAL;
3311 }
3312 RGWRealm realm(realm_id, realm_name);
3313 bool new_realm = false;
3314 int ret = realm.init(g_ceph_context, store);
3315 if (ret < 0 && ret != -ENOENT) {
3316 cerr << "failed to init realm: " << cpp_strerror(-ret) << std::endl;
3317 return -ret;
3318 } else if (ret == -ENOENT) {
3319 new_realm = true;
3320 }
3321 ret = read_decode_json(infile, realm);
3322 if (ret < 0) {
3323 return 1;
3324 }
3325 if (!realm_name.empty() && realm.get_name() != realm_name) {
3326 cerr << "mismatch between --rgw-realm " << realm_name << " and json input file name " <<
3327 realm.get_name() << std::endl;
3328 return EINVAL;
3329 }
3330 /* new realm */
3331 if (new_realm) {
3332 cout << "clearing period and epoch for new realm" << std::endl;
3333 realm.clear_current_period_and_epoch();
3334 ret = realm.create();
3335 if (ret < 0) {
3336 cerr << "ERROR: couldn't create new realm: " << cpp_strerror(-ret) << std::endl;
3337 return 1;
3338 }
3339 } else {
3340 ret = realm.update();
3341 if (ret < 0) {
3342 cerr << "ERROR: couldn't store realm info: " << cpp_strerror(-ret) << std::endl;
3343 return 1;
3344 }
3345 }
3346
3347 if (set_default) {
3348 ret = realm.set_as_default();
3349 if (ret < 0) {
3350 cerr << "failed to set realm " << realm_name << " as default: " << cpp_strerror(-ret) << std::endl;
3351 }
3352 }
3353 encode_json("realm", realm, formatter);
3354 formatter->flush(cout);
3355 }
3356 break;
3357
3358 case OPT_REALM_DEFAULT:
3359 {
3360 RGWRealm realm(realm_id, realm_name);
3361 int ret = realm.init(g_ceph_context, store);
3362 if (ret < 0) {
3363 cerr << "failed to init realm: " << cpp_strerror(-ret) << std::endl;
3364 return -ret;
3365 }
3366 ret = realm.set_as_default();
3367 if (ret < 0) {
3368 cerr << "failed to set realm as default: " << cpp_strerror(-ret) << std::endl;
3369 return -ret;
3370 }
3371 }
3372 break;
3373 case OPT_REALM_PULL:
3374 {
3375 if (url.empty()) {
3376 cerr << "A --url must be provided." << std::endl;
3377 return EINVAL;
3378 }
3379 RGWEnv env;
3380 req_info info(g_ceph_context, &env);
3381 info.method = "GET";
3382 info.request_uri = "/admin/realm";
3383
3384 map<string, string> &params = info.args.get_params();
3385 if (!realm_id.empty())
3386 params["id"] = realm_id;
3387 if (!realm_name.empty())
3388 params["name"] = realm_name;
3389
3390 bufferlist bl;
3391 JSONParser p;
3392 int ret = send_to_url(url, access_key, secret_key, info, bl, p);
3393 if (ret < 0) {
3394 cerr << "request failed: " << cpp_strerror(-ret) << std::endl;
3395 if (ret == -EACCES) {
3396 cerr << "If the realm has been changed on the master zone, the "
3397 "master zone's gateway may need to be restarted to recognize "
3398 "this user." << std::endl;
3399 }
3400 return -ret;
3401 }
3402 RGWRealm realm;
3403 realm.init(g_ceph_context, store, false);
3404 try {
3405 decode_json_obj(realm, &p);
3406 } catch (JSONDecoder::err& e) {
3407 cerr << "failed to decode JSON response: " << e.message << std::endl;
3408 return EINVAL;
3409 }
3410 RGWPeriod period;
3411 auto& current_period = realm.get_current_period();
3412 if (!current_period.empty()) {
3413 // pull the latest epoch of the realm's current period
3414 ret = do_period_pull(nullptr, url, access_key, secret_key,
3415 realm_id, realm_name, current_period, "",
3416 &period);
3417 if (ret < 0) {
3418 cerr << "could not fetch period " << current_period << std::endl;
3419 return -ret;
3420 }
3421 }
3422 ret = realm.create(false);
3423 if (ret < 0 && ret != -EEXIST) {
3424 cerr << "Error storing realm " << realm.get_id() << ": "
3425 << cpp_strerror(ret) << std::endl;
3426 return -ret;
3427 } else if (ret ==-EEXIST) {
3428 ret = realm.update();
3429 if (ret < 0) {
3430 cerr << "Error storing realm " << realm.get_id() << ": "
3431 << cpp_strerror(ret) << std::endl;
3432 }
3433 }
3434
3435 if (set_default) {
3436 ret = realm.set_as_default();
3437 if (ret < 0) {
3438 cerr << "failed to set realm " << realm_name << " as default: " << cpp_strerror(-ret) << std::endl;
3439 }
3440 }
3441
3442 encode_json("realm", realm, formatter);
3443 formatter->flush(cout);
3444 cout << std::endl;
3445 }
3446 return 0;
3447
3448 case OPT_ZONEGROUP_ADD:
3449 {
3450 if (zonegroup_id.empty() && zonegroup_name.empty()) {
3451 cerr << "no zonegroup name or id provided" << std::endl;
3452 return EINVAL;
3453 }
3454
3455 RGWZoneGroup zonegroup(zonegroup_id,zonegroup_name);
3456 int ret = zonegroup.init(g_ceph_context, store);
3457 if (ret < 0) {
3458 cerr << "failed to initialize zonegroup " << zonegroup_name << " id " << zonegroup_id << " :"
3459 << cpp_strerror(-ret) << std::endl;
3460 return -ret;
3461 }
3462 RGWZoneParams zone(zone_id, zone_name);
3463 ret = zone.init(g_ceph_context, store);
3464 if (ret < 0) {
3465 cerr << "unable to initialize zone: " << cpp_strerror(-ret) << std::endl;
3466 return -ret;
3467 }
224ce89b
WB
3468 if (zone.realm_id != zonegroup.realm_id) {
3469 zone.realm_id = zonegroup.realm_id;
3470 ret = zone.update();
3471 if (ret < 0) {
3472 cerr << "failed to save zone info: " << cpp_strerror(-ret) << std::endl;
3473 return -ret;
3474 }
3475 }
3476
7c673cae
FG
3477 string *ptier_type = (tier_type_specified ? &tier_type : nullptr);
3478 zone.tier_config = tier_config_add;
3479
3480 bool *psync_from_all = (sync_from_all_specified ? &sync_from_all : nullptr);
3481
3482 ret = zonegroup.add_zone(zone,
3483 (is_master_set ? &is_master : NULL),
3484 (is_read_only_set ? &read_only : NULL),
3485 endpoints, ptier_type,
3486 psync_from_all, sync_from, sync_from_rm);
3487 if (ret < 0) {
3488 cerr << "failed to add zone " << zone_name << " to zonegroup " << zonegroup.get_name() << ": "
3489 << cpp_strerror(-ret) << std::endl;
3490 return -ret;
3491 }
3492
3493 encode_json("zonegroup", zonegroup, formatter);
3494 formatter->flush(cout);
3495 }
3496 break;
3497 case OPT_ZONEGROUP_CREATE:
3498 {
3499 if (zonegroup_name.empty()) {
3500 cerr << "Missing zonegroup name" << std::endl;
3501 return EINVAL;
3502 }
3503 RGWRealm realm(realm_id, realm_name);
3504 int ret = realm.init(g_ceph_context, store);
3505 if (ret < 0) {
3506 cerr << "failed to init realm: " << cpp_strerror(-ret) << std::endl;
3507 return -ret;
3508 }
3509
3510 RGWZoneGroup zonegroup(zonegroup_name, is_master, g_ceph_context, store, realm.get_id(), endpoints);
3511 zonegroup.api_name = (api_name.empty() ? zonegroup_name : api_name);
3512 ret = zonegroup.create();
3513 if (ret < 0) {
3514 cerr << "failed to create zonegroup " << zonegroup_name << ": " << cpp_strerror(-ret) << std::endl;
3515 return -ret;
3516 }
3517
3518 if (set_default) {
3519 ret = zonegroup.set_as_default();
3520 if (ret < 0) {
3521 cerr << "failed to set zonegroup " << zonegroup_name << " as default: " << cpp_strerror(-ret) << std::endl;
3522 }
3523 }
3524
3525 encode_json("zonegroup", zonegroup, formatter);
3526 formatter->flush(cout);
3527 cout << std::endl;
3528 }
3529 break;
3530 case OPT_ZONEGROUP_DEFAULT:
3531 {
3532 if (zonegroup_id.empty() && zonegroup_name.empty()) {
3533 cerr << "no zonegroup name or id provided" << std::endl;
3534 return EINVAL;
3535 }
3536
3537 RGWZoneGroup zonegroup(zonegroup_id, zonegroup_name);
3538 int ret = zonegroup.init(g_ceph_context, store);
3539 if (ret < 0) {
3540 cerr << "failed to init zonegroup: " << cpp_strerror(-ret) << std::endl;
3541 return -ret;
3542 }
3543
3544 ret = zonegroup.set_as_default();
3545 if (ret < 0) {
3546 cerr << "failed to set zonegroup as default: " << cpp_strerror(-ret) << std::endl;
3547 return -ret;
3548 }
3549 }
3550 break;
3551 case OPT_ZONEGROUP_DELETE:
3552 {
3553 if (zonegroup_id.empty() && zonegroup_name.empty()) {
3554 cerr << "no zonegroup name or id provided" << std::endl;
3555 return EINVAL;
3556 }
3557 RGWZoneGroup zonegroup(zonegroup_id, zonegroup_name);
3558 int ret = zonegroup.init(g_ceph_context, store);
3559 if (ret < 0) {
3560 cerr << "failed to init zonegroup: " << cpp_strerror(-ret) << std::endl;
3561 return -ret;
3562 }
3563 ret = zonegroup.delete_obj();
3564 if (ret < 0) {
3565 cerr << "ERROR: couldn't delete zonegroup: " << cpp_strerror(-ret) << std::endl;
3566 return -ret;
3567 }
3568 }
3569 break;
3570 case OPT_ZONEGROUP_GET:
3571 {
3572 RGWZoneGroup zonegroup(zonegroup_id, zonegroup_name);
3573 int ret = zonegroup.init(g_ceph_context, store);
3574 if (ret < 0) {
3575 cerr << "failed to init zonegroup: " << cpp_strerror(-ret) << std::endl;
3576 return -ret;
3577 }
3578
3579 encode_json("zonegroup", zonegroup, formatter);
3580 formatter->flush(cout);
3581 cout << std::endl;
3582 }
3583 break;
3584 case OPT_ZONEGROUP_LIST:
3585 {
3586 RGWZoneGroup zonegroup;
3587 int ret = zonegroup.init(g_ceph_context, store, false);
3588 if (ret < 0) {
3589 cerr << "failed to init zonegroup: " << cpp_strerror(-ret) << std::endl;
3590 return -ret;
3591 }
3592
3593 list<string> zonegroups;
3594 ret = store->list_zonegroups(zonegroups);
3595 if (ret < 0) {
3596 cerr << "failed to list zonegroups: " << cpp_strerror(-ret) << std::endl;
3597 return -ret;
3598 }
3599 string default_zonegroup;
3600 ret = zonegroup.read_default_id(default_zonegroup);
7c673cae
FG
3601 if (ret < 0 && ret != -ENOENT) {
3602 cerr << "could not determine default zonegroup: " << cpp_strerror(-ret) << std::endl;
3603 }
3604 formatter->open_object_section("zonegroups_list");
3605 encode_json("default_info", default_zonegroup, formatter);
3606 encode_json("zonegroups", zonegroups, formatter);
3607 formatter->close_section();
3608 formatter->flush(cout);
3609 cout << std::endl;
3610 }
3611 break;
3612 case OPT_ZONEGROUP_MODIFY:
3613 {
3614 RGWRealm realm(realm_id, realm_name);
3615 int ret = realm.init(g_ceph_context, store);
3616 if (ret < 0) {
3617 cerr << "failed to init realm: " << cpp_strerror(-ret) << std::endl;
3618 return -ret;
3619 }
3620
3621 RGWZoneGroup zonegroup(zonegroup_id, zonegroup_name);
3622 ret = zonegroup.init(g_ceph_context, store);
3623 if (ret < 0) {
3624 cerr << "failed to init zonegroup: " << cpp_strerror(-ret) << std::endl;
3625 return -ret;
3626 }
3627
3628 bool need_update = false;
3629
3630 if (!master_zone.empty()) {
3631 zonegroup.master_zone = master_zone;
3632 need_update = true;
3633 }
3634
3635 if (is_master_set) {
3636 zonegroup.update_master(is_master);
3637 need_update = true;
3638 }
3639
3640 if (!endpoints.empty()) {
3641 zonegroup.endpoints = endpoints;
3642 need_update = true;
3643 }
3644
3645 if (!api_name.empty()) {
3646 zonegroup.api_name = api_name;
3647 need_update = true;
3648 }
3649
3650 if (!realm_id.empty()) {
3651 zonegroup.realm_id = realm_id;
3652 need_update = true;
3653 } else if (!realm_name.empty()) {
3654 // get realm id from name
3655 RGWRealm realm{g_ceph_context, store};
3656 ret = realm.read_id(realm_name, zonegroup.realm_id);
3657 if (ret < 0) {
3658 cerr << "failed to find realm by name " << realm_name << std::endl;
3659 return -ret;
3660 }
3661 need_update = true;
3662 }
3663
3664 if (need_update) {
3665 ret = zonegroup.update();
3666 if (ret < 0) {
3667 cerr << "failed to update zonegroup: " << cpp_strerror(-ret) << std::endl;
3668 return -ret;
3669 }
3670 }
3671
3672 if (set_default) {
3673 ret = zonegroup.set_as_default();
3674 if (ret < 0) {
3675 cerr << "failed to set zonegroup " << zonegroup_name << " as default: " << cpp_strerror(-ret) << std::endl;
3676 }
3677 }
3678
3679 encode_json("zonegroup", zonegroup, formatter);
3680 formatter->flush(cout);
3681 }
3682 break;
3683 case OPT_ZONEGROUP_SET:
3684 {
3685 RGWRealm realm(realm_id, realm_name);
3686 int ret = realm.init(g_ceph_context, store);
3687 if (ret < 0) {
3688 cerr << "failed to init realm: " << cpp_strerror(-ret) << std::endl;
3689 return -ret;
3690 }
3691
3692 RGWZoneGroup zonegroup;
3693 ret = zonegroup.init(g_ceph_context, store, false);
3694 if (ret < 0) {
3695 cerr << "failed to init zonegroup: " << cpp_strerror(-ret) << std::endl;
3696 return -ret;
3697 }
3698 ret = read_decode_json(infile, zonegroup);
3699 if (ret < 0) {
3700 return 1;
3701 }
3702 if (zonegroup.realm_id.empty()) {
3703 zonegroup.realm_id = realm.get_id();
3704 }
3705 ret = zonegroup.create();
3706 if (ret < 0 && ret != -EEXIST) {
3707 cerr << "ERROR: couldn't create zonegroup info: " << cpp_strerror(-ret) << std::endl;
3708 return 1;
3709 } else if (ret == -EEXIST) {
3710 ret = zonegroup.update();
3711 if (ret < 0) {
3712 cerr << "ERROR: couldn't store zonegroup info: " << cpp_strerror(-ret) << std::endl;
3713 return 1;
3714 }
3715 }
3716
3717 if (set_default) {
3718 ret = zonegroup.set_as_default();
3719 if (ret < 0) {
3720 cerr << "failed to set zonegroup " << zonegroup_name << " as default: " << cpp_strerror(-ret) << std::endl;
3721 }
3722 }
3723
3724 encode_json("zonegroup", zonegroup, formatter);
3725 formatter->flush(cout);
3726 }
3727 break;
3728 case OPT_ZONEGROUP_REMOVE:
3729 {
3730 RGWZoneGroup zonegroup(zonegroup_id, zonegroup_name);
3731 int ret = zonegroup.init(g_ceph_context, store);
3732 if (ret < 0) {
3733 cerr << "failed to init zonegroup: " << cpp_strerror(-ret) << std::endl;
3734 return -ret;
3735 }
3736
3737 if (zone_id.empty()) {
3738 if (zone_name.empty()) {
3739 cerr << "no --zone-id or --rgw-zone name provided" << std::endl;
3740 return EINVAL;
3741 }
3742 // look up zone id by name
3743 for (auto& z : zonegroup.zones) {
3744 if (zone_name == z.second.name) {
3745 zone_id = z.second.id;
3746 break;
3747 }
3748 }
3749 if (zone_id.empty()) {
3750 cerr << "zone name " << zone_name << " not found in zonegroup "
3751 << zonegroup.get_name() << std::endl;
3752 return ENOENT;
3753 }
3754 }
3755
3756 ret = zonegroup.remove_zone(zone_id);
3757 if (ret < 0) {
3758 cerr << "failed to remove zone: " << cpp_strerror(-ret) << std::endl;
3759 return -ret;
3760 }
3761
3762 encode_json("zonegroup", zonegroup, formatter);
3763 formatter->flush(cout);
3764 }
3765 break;
3766 case OPT_ZONEGROUP_RENAME:
3767 {
3768 if (zonegroup_new_name.empty()) {
3769 cerr << " missing zonegroup new name" << std::endl;
3770 return EINVAL;
3771 }
3772 if (zonegroup_id.empty() && zonegroup_name.empty()) {
3773 cerr << "no zonegroup name or id provided" << std::endl;
3774 return EINVAL;
3775 }
3776 RGWZoneGroup zonegroup(zonegroup_id, zonegroup_name);
3777 int ret = zonegroup.init(g_ceph_context, store);
3778 if (ret < 0) {
3779 cerr << "failed to init zonegroup: " << cpp_strerror(-ret) << std::endl;
3780 return -ret;
3781 }
3782 ret = zonegroup.rename(zonegroup_new_name);
3783 if (ret < 0) {
3784 cerr << "failed to rename zonegroup: " << cpp_strerror(-ret) << std::endl;
3785 return -ret;
3786 }
3787 }
3788 break;
3789 case OPT_ZONEGROUP_PLACEMENT_LIST:
3790 {
3791 RGWZoneGroup zonegroup(zonegroup_id, zonegroup_name);
3792 int ret = zonegroup.init(g_ceph_context, store);
3793 if (ret < 0) {
3794 cerr << "failed to init zonegroup: " << cpp_strerror(-ret) << std::endl;
3795 return -ret;
3796 }
3797
3798 encode_json("placement_targets", zonegroup.placement_targets, formatter);
3799 formatter->flush(cout);
3800 cout << std::endl;
3801 }
3802 break;
3803 case OPT_ZONEGROUP_PLACEMENT_ADD:
3804 case OPT_ZONEGROUP_PLACEMENT_MODIFY:
3805 case OPT_ZONEGROUP_PLACEMENT_RM:
3806 case OPT_ZONEGROUP_PLACEMENT_DEFAULT:
3807 {
3808 if (placement_id.empty()) {
3809 cerr << "ERROR: --placement-id not specified" << std::endl;
3810 return EINVAL;
3811 }
3812
3813 RGWZoneGroup zonegroup(zonegroup_id, zonegroup_name);
3814 int ret = zonegroup.init(g_ceph_context, store);
3815 if (ret < 0) {
3816 cerr << "failed to init zonegroup: " << cpp_strerror(-ret) << std::endl;
3817 return -ret;
3818 }
3819
3820 if (opt_cmd == OPT_ZONEGROUP_PLACEMENT_ADD) {
3821 RGWZoneGroupPlacementTarget target;
3822 target.name = placement_id;
3823 for (auto& t : tags) {
3824 target.tags.insert(t);
3825 }
3826 zonegroup.placement_targets[placement_id] = target;
3827 } else if (opt_cmd == OPT_ZONEGROUP_PLACEMENT_MODIFY) {
3828 RGWZoneGroupPlacementTarget& target = zonegroup.placement_targets[placement_id];
3829 if (!tags.empty()) {
3830 target.tags.clear();
3831 for (auto& t : tags) {
3832 target.tags.insert(t);
3833 }
3834 }
3835 target.name = placement_id;
3836 for (auto& t : tags_rm) {
3837 target.tags.erase(t);
3838 }
3839 for (auto& t : tags_add) {
3840 target.tags.insert(t);
3841 }
3842 } else if (opt_cmd == OPT_ZONEGROUP_PLACEMENT_RM) {
3843 zonegroup.placement_targets.erase(placement_id);
3844 } else if (opt_cmd == OPT_ZONEGROUP_PLACEMENT_DEFAULT) {
3845 if (!zonegroup.placement_targets.count(placement_id)) {
3846 cerr << "failed to find a zonegroup placement target named '"
3847 << placement_id << "'" << std::endl;
3848 return -ENOENT;
3849 }
3850 zonegroup.default_placement = placement_id;
3851 }
3852
3853 zonegroup.post_process_params();
3854 ret = zonegroup.update();
3855 if (ret < 0) {
3856 cerr << "failed to update zonegroup: " << cpp_strerror(-ret) << std::endl;
3857 return -ret;
3858 }
3859
3860 encode_json("placement_targets", zonegroup.placement_targets, formatter);
3861 formatter->flush(cout);
3862 }
3863 break;
3864 case OPT_ZONE_CREATE:
3865 {
3866 if (zone_name.empty()) {
3867 cerr << "zone name not provided" << std::endl;
3868 return EINVAL;
3869 }
3870 int ret;
3871 RGWZoneGroup zonegroup(zonegroup_id, zonegroup_name);
3872 /* if the user didn't provide zonegroup info , create stand alone zone */
3873 if (!zonegroup_id.empty() || !zonegroup_name.empty()) {
3874 ret = zonegroup.init(g_ceph_context, store);
3875 if (ret < 0) {
3876 cerr << "unable to initialize zonegroup " << zonegroup_name << ": " << cpp_strerror(-ret) << std::endl;
3877 return -ret;
3878 }
3879 if (realm_id.empty() && realm_name.empty()) {
3880 realm_id = zonegroup.realm_id;
3881 }
3882 }
3883
3884 RGWZoneParams zone(zone_id, zone_name);
3885 ret = zone.init(g_ceph_context, store, false);
3886 if (ret < 0) {
3887 cerr << "unable to initialize zone: " << cpp_strerror(-ret) << std::endl;
3888 return -ret;
3889 }
3890
3891 zone.system_key.id = access_key;
3892 zone.system_key.key = secret_key;
3893 zone.realm_id = realm_id;
31f18b77 3894 zone.tier_config = tier_config_add;
7c673cae
FG
3895
3896 ret = zone.create();
3897 if (ret < 0) {
3898 cerr << "failed to create zone " << zone_name << ": " << cpp_strerror(-ret) << std::endl;
3899 return -ret;
3900 }
3901
3902 if (!zonegroup_id.empty() || !zonegroup_name.empty()) {
3903 string *ptier_type = (tier_type_specified ? &tier_type : nullptr);
3904 bool *psync_from_all = (sync_from_all_specified ? &sync_from_all : nullptr);
3905 ret = zonegroup.add_zone(zone,
3906 (is_master_set ? &is_master : NULL),
3907 (is_read_only_set ? &read_only : NULL),
3908 endpoints,
3909 ptier_type,
3910 psync_from_all,
3911 sync_from, sync_from_rm);
3912 if (ret < 0) {
3913 cerr << "failed to add zone " << zone_name << " to zonegroup " << zonegroup.get_name()
3914 << ": " << cpp_strerror(-ret) << std::endl;
3915 return -ret;
3916 }
3917 }
3918
3919 if (set_default) {
3920 ret = zone.set_as_default();
3921 if (ret < 0) {
3922 cerr << "failed to set zone " << zone_name << " as default: " << cpp_strerror(-ret) << std::endl;
3923 }
3924 }
3925
3926 encode_json("zone", zone, formatter);
3927 formatter->flush(cout);
3928 cout << std::endl;
3929 }
3930 break;
3931 case OPT_ZONE_DEFAULT:
3932 {
3933 RGWZoneGroup zonegroup(zonegroup_id,zonegroup_name);
3934 int ret = zonegroup.init(g_ceph_context, store);
3935 if (ret < 0) {
3936 cerr << "WARNING: failed to initialize zonegroup " << zonegroup_name << std::endl;
3937 }
3938 if (zone_id.empty() && zone_name.empty()) {
3939 cerr << "no zone name or id provided" << std::endl;
3940 return EINVAL;
3941 }
3942 RGWZoneParams zone(zone_id, zone_name);
3943 ret = zone.init(g_ceph_context, store);
3944 if (ret < 0) {
3945 cerr << "unable to initialize zone: " << cpp_strerror(-ret) << std::endl;
3946 return -ret;
3947 }
3948 ret = zone.set_as_default();
3949 if (ret < 0) {
3950 cerr << "failed to set zone as default: " << cpp_strerror(-ret) << std::endl;
3951 return -ret;
3952 }
3953 }
3954 break;
3955 case OPT_ZONE_DELETE:
3956 {
3957 if (zone_id.empty() && zone_name.empty()) {
3958 cerr << "no zone name or id provided" << std::endl;
3959 return EINVAL;
3960 }
3961 RGWZoneParams zone(zone_id, zone_name);
3962 int ret = zone.init(g_ceph_context, store);
3963 if (ret < 0) {
3964 cerr << "unable to initialize zone: " << cpp_strerror(-ret) << std::endl;
3965 return -ret;
3966 }
3967
3968 list<string> zonegroups;
3969 ret = store->list_zonegroups(zonegroups);
3970 if (ret < 0) {
3971 cerr << "failed to list zonegroups: " << cpp_strerror(-ret) << std::endl;
3972 return -ret;
3973 }
3974
3975 for (list<string>::iterator iter = zonegroups.begin(); iter != zonegroups.end(); ++iter) {
3976 RGWZoneGroup zonegroup(string(), *iter);
3977 int ret = zonegroup.init(g_ceph_context, store);
3978 if (ret < 0) {
3979 cerr << "WARNING: failed to initialize zonegroup " << zonegroup_name << std::endl;
3980 continue;
3981 }
3982 ret = zonegroup.remove_zone(zone.get_id());
3983 if (ret < 0 && ret != -ENOENT) {
3984 cerr << "failed to remove zone " << zone_name << " from zonegroup " << zonegroup.get_name() << ": "
3985 << cpp_strerror(-ret) << std::endl;
3986 }
3987 }
3988
3989 ret = zone.delete_obj();
3990 if (ret < 0) {
3991 cerr << "failed to delete zone " << zone_name << ": " << cpp_strerror(-ret) << std::endl;
3992 return -ret;
3993 }
3994 }
3995 break;
3996 case OPT_ZONE_GET:
3997 {
3998 RGWZoneParams zone(zone_id, zone_name);
3999 int ret = zone.init(g_ceph_context, store);
4000 if (ret < 0) {
4001 cerr << "unable to initialize zone: " << cpp_strerror(-ret) << std::endl;
4002 return -ret;
4003 }
4004 encode_json("zone", zone, formatter);
4005 formatter->flush(cout);
4006 }
4007 break;
4008 case OPT_ZONE_SET:
4009 {
4010 RGWZoneParams zone(zone_name);
4011 int ret = zone.init(g_ceph_context, store, false);
4012 if (ret < 0) {
4013 return -ret;
4014 }
4015
4016 ret = zone.read();
4017 if (ret < 0 && ret != -ENOENT) {
4018 cerr << "zone.read() returned ret=" << ret << std::endl;
4019 return -ret;
4020 }
4021
4022 string orig_id = zone.get_id();
4023
4024 ret = read_decode_json(infile, zone);
4025 if (ret < 0) {
4026 return 1;
4027 }
4028
4029 if(zone.realm_id.empty()) {
4030 RGWRealm realm(realm_id, realm_name);
4031 int ret = realm.init(g_ceph_context, store);
4032 if (ret < 0 && ret != -ENOENT) {
4033 cerr << "failed to init realm: " << cpp_strerror(-ret) << std::endl;
4034 return -ret;
4035 }
4036 zone.realm_id = realm.get_id();
4037 }
4038
4039 if( !zone_name.empty() && !zone.get_name().empty() && zone.get_name() != zone_name) {
4040 cerr << "Error: zone name" << zone_name << " is different than the zone name " << zone.get_name() << " in the provided json " << std::endl;
4041 return EINVAL;
4042 }
4043
4044 if (zone.get_name().empty()) {
4045 zone.set_name(zone_name);
4046 if (zone.get_name().empty()) {
4047 cerr << "no zone name specified" << std::endl;
4048 return EINVAL;
4049 }
4050 }
4051
4052 zone_name = zone.get_name();
4053
4054 if (zone.get_id().empty()) {
4055 zone.set_id(orig_id);
4056 }
4057
4058 if (zone.get_id().empty()) {
4059 cerr << "no zone name id the json provided, assuming old format" << std::endl;
4060 if (zone_name.empty()) {
4061 cerr << "missing zone name" << std::endl;
4062 return EINVAL;
4063 }
4064 zone.set_name(zone_name);
4065 zone.set_id(zone_name);
4066 }
4067
4068 cerr << "zone id " << zone.get_id();
4069 ret = zone.fix_pool_names();
4070 if (ret < 0) {
4071 cerr << "ERROR: couldn't fix zone: " << cpp_strerror(-ret) << std::endl;
4072 return -ret;
4073 }
4074 ret = zone.write(false);
4075 if (ret < 0) {
4076 cerr << "ERROR: couldn't create zone: " << cpp_strerror(-ret) << std::endl;
4077 return 1;
4078 }
4079
4080 if (set_default) {
4081 ret = zone.set_as_default();
4082 if (ret < 0) {
4083 cerr << "failed to set zone " << zone_name << " as default: " << cpp_strerror(-ret) << std::endl;
4084 }
4085 }
4086
4087 encode_json("zone", zone, formatter);
4088 formatter->flush(cout);
4089 }
4090 break;
4091 case OPT_ZONE_LIST:
4092 {
4093 list<string> zones;
4094 int ret = store->list_zones(zones);
4095 if (ret < 0) {
4096 cerr << "failed to list zones: " << cpp_strerror(-ret) << std::endl;
4097 return -ret;
4098 }
4099
4100 RGWZoneParams zone;
4101 ret = zone.init(g_ceph_context, store, false);
4102 if (ret < 0) {
4103 cerr << "failed to init zone: " << cpp_strerror(-ret) << std::endl;
4104 return -ret;
4105 }
4106 string default_zone;
4107 ret = zone.read_default_id(default_zone);
4108 if (ret < 0 && ret != -ENOENT) {
4109 cerr << "could not determine default zone: " << cpp_strerror(-ret) << std::endl;
4110 }
4111 formatter->open_object_section("zones_list");
4112 encode_json("default_info", default_zone, formatter);
4113 encode_json("zones", zones, formatter);
4114 formatter->close_section();
4115 formatter->flush(cout);
4116 cout << std::endl;
4117 }
4118 break;
4119 case OPT_ZONE_MODIFY:
4120 {
4121 RGWZoneParams zone(zone_id, zone_name);
4122 int ret = zone.init(g_ceph_context, store);
4123 if (ret < 0) {
4124 cerr << "failed to init zone: " << cpp_strerror(-ret) << std::endl;
4125 return -ret;
4126 }
4127
4128 bool need_zone_update = false;
4129 if (!access_key.empty()) {
4130 zone.system_key.id = access_key;
4131 need_zone_update = true;
4132 }
4133
4134 if (!secret_key.empty()) {
4135 zone.system_key.key = secret_key;
4136 need_zone_update = true;
4137 }
4138
4139 if (!realm_id.empty()) {
4140 zone.realm_id = realm_id;
4141 need_zone_update = true;
4142 } else if (!realm_name.empty()) {
4143 // get realm id from name
4144 RGWRealm realm{g_ceph_context, store};
4145 ret = realm.read_id(realm_name, zone.realm_id);
4146 if (ret < 0) {
4147 cerr << "failed to find realm by name " << realm_name << std::endl;
4148 return -ret;
4149 }
4150 need_zone_update = true;
4151 }
4152
4153 for (auto add : tier_config_add) {
4154 zone.tier_config[add.first] = add.second;
4155 need_zone_update = true;
4156 }
4157
4158 for (auto rm : tier_config_rm) {
4159 zone.tier_config.erase(rm.first);
4160 need_zone_update = true;
4161 }
4162
4163 if (need_zone_update) {
4164 ret = zone.update();
4165 if (ret < 0) {
4166 cerr << "failed to save zone info: " << cpp_strerror(-ret) << std::endl;
4167 return -ret;
4168 }
4169 }
4170
4171 RGWZoneGroup zonegroup(zonegroup_id, zonegroup_name);
4172 ret = zonegroup.init(g_ceph_context, store);
4173 if (ret < 0) {
4174 cerr << "failed to init zonegroup: " << cpp_strerror(-ret) << std::endl;
4175 return -ret;
4176 }
4177 string *ptier_type = (tier_type_specified ? &tier_type : nullptr);
4178
4179 bool *psync_from_all = (sync_from_all_specified ? &sync_from_all : nullptr);
4180
4181 ret = zonegroup.add_zone(zone,
4182 (is_master_set ? &is_master : NULL),
4183 (is_read_only_set ? &read_only : NULL),
4184 endpoints, ptier_type,
4185 psync_from_all, sync_from, sync_from_rm);
4186 if (ret < 0) {
4187 cerr << "failed to update zonegroup: " << cpp_strerror(-ret) << std::endl;
4188 return -ret;
4189 }
4190
4191 ret = zonegroup.update();
4192 if (ret < 0) {
4193 cerr << "failed to update zonegroup: " << cpp_strerror(-ret) << std::endl;
4194 return -ret;
4195 }
4196
4197 if (set_default) {
4198 ret = zone.set_as_default();
4199 if (ret < 0) {
4200 cerr << "failed to set zone " << zone_name << " as default: " << cpp_strerror(-ret) << std::endl;
4201 }
4202 }
4203
4204 encode_json("zone", zone, formatter);
4205 formatter->flush(cout);
4206 }
4207 break;
4208 case OPT_ZONE_RENAME:
4209 {
4210 if (zone_new_name.empty()) {
4211 cerr << " missing zone new name" << std::endl;
4212 return EINVAL;
4213 }
4214 if (zone_id.empty() && zone_name.empty()) {
4215 cerr << "no zonegroup name or id provided" << std::endl;
4216 return EINVAL;
4217 }
4218 RGWZoneParams zone(zone_id,zone_name);
4219 int ret = zone.init(g_ceph_context, store);
4220 if (ret < 0) {
4221 cerr << "unable to initialize zone: " << cpp_strerror(-ret) << std::endl;
4222 return -ret;
4223 }
4224 ret = zone.rename(zone_new_name);
4225 if (ret < 0) {
4226 cerr << "failed to rename zone " << zone_name << " to " << zone_new_name << ": " << cpp_strerror(-ret)
4227 << std::endl;
4228 return -ret;
4229 }
4230 RGWZoneGroup zonegroup(zonegroup_id, zonegroup_name);
4231 ret = zonegroup.init(g_ceph_context, store);
4232 if (ret < 0) {
4233 cerr << "WARNING: failed to initialize zonegroup " << zonegroup_name << std::endl;
4234 } else {
4235 ret = zonegroup.rename_zone(zone);
4236 if (ret < 0 && ret ) {
4237 cerr << "Error in zonegroup rename for " << zone_name << ": " << cpp_strerror(-ret) << std::endl;
4238 return -ret;
4239 }
4240 }
4241 }
4242 break;
4243 case OPT_ZONE_PLACEMENT_ADD:
4244 case OPT_ZONE_PLACEMENT_MODIFY:
4245 case OPT_ZONE_PLACEMENT_RM:
4246 {
4247 if (placement_id.empty()) {
4248 cerr << "ERROR: --placement-id not specified" << std::endl;
4249 return EINVAL;
4250 }
4251 RGWZoneParams zone(zone_id, zone_name);
4252 int ret = zone.init(g_ceph_context, store);
4253 if (ret < 0) {
4254 cerr << "failed to init zone: " << cpp_strerror(-ret) << std::endl;
4255 return -ret;
4256 }
4257
4258 if (opt_cmd == OPT_ZONE_PLACEMENT_ADD) {
4259 // pool names are required
4260 if (!index_pool || index_pool->empty() ||
4261 !data_pool || data_pool->empty()) {
4262 cerr << "ERROR: need to specify both --index-pool and --data-pool" << std::endl;
4263 return EINVAL;
4264 }
4265
4266 RGWZonePlacementInfo& info = zone.placement_pools[placement_id];
4267
4268 info.index_pool = *index_pool;
4269 info.data_pool = *data_pool;
4270 if (data_extra_pool) {
4271 info.data_extra_pool = *data_extra_pool;
4272 }
4273 if (index_type_specified) {
4274 info.index_type = placement_index_type;
4275 }
4276 if (compression_type) {
4277 info.compression_type = *compression_type;
4278 }
4279 } else if (opt_cmd == OPT_ZONE_PLACEMENT_MODIFY) {
4280 auto p = zone.placement_pools.find(placement_id);
4281 if (p == zone.placement_pools.end()) {
4282 cerr << "ERROR: zone placement target '" << placement_id
4283 << "' not found" << std::endl;
4284 return -ENOENT;
4285 }
4286 auto& info = p->second;
4287 if (index_pool && !index_pool->empty()) {
4288 info.index_pool = *index_pool;
4289 }
4290 if (data_pool && !data_pool->empty()) {
4291 info.data_pool = *data_pool;
4292 }
4293 if (data_extra_pool) {
4294 info.data_extra_pool = *data_extra_pool;
4295 }
4296 if (index_type_specified) {
4297 info.index_type = placement_index_type;
4298 }
4299 if (compression_type) {
4300 info.compression_type = *compression_type;
4301 }
4302 } else if (opt_cmd == OPT_ZONE_PLACEMENT_RM) {
4303 zone.placement_pools.erase(placement_id);
4304 }
4305
4306 ret = zone.update();
4307 if (ret < 0) {
4308 cerr << "failed to save zone info: " << cpp_strerror(-ret) << std::endl;
4309 return -ret;
4310 }
4311
4312 encode_json("zone", zone, formatter);
4313 formatter->flush(cout);
4314 }
4315 break;
4316 case OPT_ZONE_PLACEMENT_LIST:
4317 {
4318 RGWZoneParams zone(zone_id, zone_name);
4319 int ret = zone.init(g_ceph_context, store);
4320 if (ret < 0) {
4321 cerr << "unable to initialize zone: " << cpp_strerror(-ret) << std::endl;
4322 return -ret;
4323 }
4324 encode_json("placement_pools", zone.placement_pools, formatter);
4325 formatter->flush(cout);
4326 }
4327 break;
4328 }
4329 return 0;
4330 }
4331
4332 if (!user_id.empty()) {
4333 user_op.set_user_id(user_id);
4334 bucket_op.set_user_id(user_id);
4335 }
4336
4337 if (!display_name.empty())
4338 user_op.set_display_name(display_name);
4339
4340 if (!user_email.empty())
4341 user_op.set_user_email(user_email);
4342
4343 if (!access_key.empty())
4344 user_op.set_access_key(access_key);
4345
4346 if (!secret_key.empty())
4347 user_op.set_secret_key(secret_key);
4348
4349 if (!subuser.empty())
4350 user_op.set_subuser(subuser);
4351
4352 if (!caps.empty())
4353 user_op.set_caps(caps);
4354
4355 user_op.set_purge_data(purge_data);
4356
4357 if (purge_keys)
4358 user_op.set_purge_keys();
4359
4360 if (gen_access_key)
4361 user_op.set_generate_key();
4362
4363 if (gen_secret_key)
4364 user_op.set_gen_secret(); // assume that a key pair should be created
4365
4366 if (max_buckets_specified)
4367 user_op.set_max_buckets(max_buckets);
4368
4369 if (admin_specified)
4370 user_op.set_admin(admin);
4371
4372 if (system_specified)
4373 user_op.set_system(system);
4374
4375 if (set_perm)
4376 user_op.set_perm(perm_mask);
4377
4378 if (set_temp_url_key) {
4379 map<int, string>::iterator iter = temp_url_keys.begin();
4380 for (; iter != temp_url_keys.end(); ++iter) {
4381 user_op.set_temp_url_key(iter->second, iter->first);
4382 }
4383 }
4384
4385 if (!op_mask_str.empty()) {
4386 uint32_t op_mask;
4387 int ret = rgw_parse_op_type_list(op_mask_str, &op_mask);
4388 if (ret < 0) {
4389 cerr << "failed to parse op_mask: " << cpp_strerror(-ret) << std::endl;
4390 return -ret;
4391 }
4392
4393 user_op.set_op_mask(op_mask);
4394 }
4395
4396 if (key_type != KEY_TYPE_UNDEFINED)
4397 user_op.set_key_type(key_type);
4398
4399 // set suspension operation parameters
4400 if (opt_cmd == OPT_USER_ENABLE)
4401 user_op.set_suspension(false);
4402 else if (opt_cmd == OPT_USER_SUSPEND)
4403 user_op.set_suspension(true);
4404
4405 // RGWUser to use for user operations
4406 RGWUser user;
4407 int ret = 0;
4408 if (!user_id.empty() || !subuser.empty()) {
4409 ret = user.init(store, user_op);
4410 if (ret < 0) {
4411 cerr << "user.init failed: " << cpp_strerror(-ret) << std::endl;
4412 return -ret;
4413 }
4414 }
4415
4416 /* populate bucket operation */
4417 bucket_op.set_bucket_name(bucket_name);
4418 bucket_op.set_object(object);
4419 bucket_op.set_check_objects(check_objects);
4420 bucket_op.set_delete_children(delete_child_objects);
4421 bucket_op.set_fix_index(fix);
4422 bucket_op.set_max_aio(max_concurrent_ios);
4423
4424 // required to gather errors from operations
4425 std::string err_msg;
4426
4427 bool output_user_info = true;
4428
4429 switch (opt_cmd) {
4430 case OPT_USER_INFO:
4431 break;
4432 case OPT_USER_CREATE:
4433 if (!user_op.has_existing_user()) {
4434 user_op.set_generate_key(); // generate a new key by default
4435 }
4436 ret = user.add(user_op, &err_msg);
4437 if (ret < 0) {
4438 cerr << "could not create user: " << err_msg << std::endl;
4439 return -ret;
4440 }
4441 if (!subuser.empty()) {
4442 ret = user.subusers.add(user_op, &err_msg);
4443 if (ret < 0) {
4444 cerr << "could not create subuser: " << err_msg << std::endl;
4445 return -ret;
4446 }
4447 }
4448 break;
4449 case OPT_USER_RM:
4450 ret = user.remove(user_op, &err_msg);
31f18b77 4451 if (ret < 0) {
7c673cae
FG
4452 cerr << "could not remove user: " << err_msg << std::endl;
4453 return -ret;
4454 }
4455
4456 output_user_info = false;
4457 break;
4458 case OPT_USER_ENABLE:
4459 case OPT_USER_SUSPEND:
4460 case OPT_USER_MODIFY:
4461 ret = user.modify(user_op, &err_msg);
4462 if (ret < 0) {
4463 cerr << "could not modify user: " << err_msg << std::endl;
4464 return -ret;
4465 }
4466
4467 break;
4468 case OPT_SUBUSER_CREATE:
4469 ret = user.subusers.add(user_op, &err_msg);
4470 if (ret < 0) {
4471 cerr << "could not create subuser: " << err_msg << std::endl;
4472 return -ret;
4473 }
4474
4475 break;
4476 case OPT_SUBUSER_MODIFY:
4477 ret = user.subusers.modify(user_op, &err_msg);
4478 if (ret < 0) {
4479 cerr << "could not modify subuser: " << err_msg << std::endl;
4480 return -ret;
4481 }
4482
4483 break;
4484 case OPT_SUBUSER_RM:
4485 ret = user.subusers.remove(user_op, &err_msg);
4486 if (ret < 0) {
4487 cerr << "could not remove subuser: " << err_msg << std::endl;
4488 return -ret;
4489 }
4490
4491 break;
4492 case OPT_CAPS_ADD:
4493 ret = user.caps.add(user_op, &err_msg);
4494 if (ret < 0) {
4495 cerr << "could not add caps: " << err_msg << std::endl;
4496 return -ret;
4497 }
4498
4499 break;
4500 case OPT_CAPS_RM:
4501 ret = user.caps.remove(user_op, &err_msg);
4502 if (ret < 0) {
4503 cerr << "could not remove caps: " << err_msg << std::endl;
4504 return -ret;
4505 }
4506
4507 break;
4508 case OPT_KEY_CREATE:
4509 ret = user.keys.add(user_op, &err_msg);
4510 if (ret < 0) {
4511 cerr << "could not create key: " << err_msg << std::endl;
4512 return -ret;
4513 }
4514
4515 break;
4516 case OPT_KEY_RM:
4517 ret = user.keys.remove(user_op, &err_msg);
4518 if (ret < 0) {
4519 cerr << "could not remove key: " << err_msg << std::endl;
4520 return -ret;
4521 }
4522 break;
4523 case OPT_PERIOD_PUSH:
4524 {
4525 RGWEnv env;
4526 req_info info(g_ceph_context, &env);
4527 info.method = "POST";
4528 info.request_uri = "/admin/realm/period";
4529
4530 map<string, string> &params = info.args.get_params();
4531 if (!realm_id.empty())
4532 params["realm_id"] = realm_id;
4533 if (!realm_name.empty())
4534 params["realm_name"] = realm_name;
4535 if (!period_id.empty())
4536 params["period_id"] = period_id;
4537 if (!period_epoch.empty())
4538 params["epoch"] = period_epoch;
4539
4540 // load the period
4541 RGWPeriod period(period_id);
4542 int ret = period.init(g_ceph_context, store);
4543 if (ret < 0) {
4544 cerr << "period init failed: " << cpp_strerror(-ret) << std::endl;
4545 return -ret;
4546 }
4547 // json format into a bufferlist
4548 JSONFormatter jf(false);
4549 encode_json("period", period, &jf);
4550 bufferlist bl;
4551 jf.flush(bl);
4552
4553 JSONParser p;
4554 ret = send_to_remote_or_url(nullptr, url, access_key, secret_key,
4555 info, bl, p);
4556 if (ret < 0) {
4557 cerr << "request failed: " << cpp_strerror(-ret) << std::endl;
4558 return -ret;
4559 }
4560 }
4561 return 0;
4562 case OPT_PERIOD_UPDATE:
4563 {
4564 int ret = update_period(realm_id, realm_name, period_id, period_epoch,
4565 commit, remote, url, access_key, secret_key,
4566 formatter, yes_i_really_mean_it);
4567 if (ret < 0) {
4568 return -ret;
4569 }
4570 }
4571 return 0;
4572 case OPT_PERIOD_COMMIT:
4573 {
4574 // read realm and staging period
4575 RGWRealm realm(realm_id, realm_name);
4576 int ret = realm.init(g_ceph_context, store);
4577 if (ret < 0) {
4578 cerr << "Error initializing realm: " << cpp_strerror(-ret) << std::endl;
4579 return -ret;
4580 }
4581 RGWPeriod period(RGWPeriod::get_staging_id(realm.get_id()), 1);
4582 ret = period.init(g_ceph_context, store, realm.get_id());
4583 if (ret < 0) {
4584 cerr << "period init failed: " << cpp_strerror(-ret) << std::endl;
4585 return -ret;
4586 }
4587 ret = commit_period(realm, period, remote, url, access_key, secret_key,
4588 yes_i_really_mean_it);
4589 if (ret < 0) {
4590 cerr << "failed to commit period: " << cpp_strerror(-ret) << std::endl;
4591 return -ret;
4592 }
4593
4594 encode_json("period", period, formatter);
4595 formatter->flush(cout);
4596 cout << std::endl;
4597 }
4598 return 0;
4599 case OPT_ROLE_CREATE:
4600 {
31f18b77
FG
4601 if (role_name.empty()) {
4602 cerr << "ERROR: role name is empty" << std::endl;
4603 return -EINVAL;
4604 }
4605
4606 if (assume_role_doc.empty()) {
4607 cerr << "ERROR: assume role policy document is empty" << std::endl;
7c673cae
FG
4608 return -EINVAL;
4609 }
4610 /* The following two calls will be replaced by read_decode_json or something
4611 similar when the code for AWS Policies is in places */
4612 bufferlist bl;
4613 int ret = read_input(assume_role_doc, bl);
4614 if (ret < 0) {
4615 cerr << "ERROR: failed to read input: " << cpp_strerror(-ret) << std::endl;
4616 return ret;
4617 }
4618 JSONParser p;
4619 if (!p.parse(bl.c_str(), bl.length())) {
4620 cout << "ERROR: failed to parse JSON: " << assume_role_doc << std::endl;
4621 return -EINVAL;
4622 }
4623 string trust_policy = bl.to_str();
31f18b77 4624 RGWRole role(g_ceph_context, store, role_name, path, trust_policy, tenant);
7c673cae
FG
4625 ret = role.create(true);
4626 if (ret < 0) {
4627 return -ret;
4628 }
4629 show_role_info(role, formatter);
4630 return 0;
4631 }
4632 case OPT_ROLE_DELETE:
4633 {
4634 if (role_name.empty()) {
4635 cerr << "ERROR: empty role name" << std::endl;
4636 return -EINVAL;
4637 }
31f18b77 4638 RGWRole role(g_ceph_context, store, role_name, tenant);
7c673cae
FG
4639 ret = role.delete_obj();
4640 if (ret < 0) {
4641 return -ret;
4642 }
4643 cout << "role: " << role_name << " successfully deleted" << std::endl;
4644 return 0;
4645 }
4646 case OPT_ROLE_GET:
4647 {
4648 if (role_name.empty()) {
4649 cerr << "ERROR: empty role name" << std::endl;
4650 return -EINVAL;
4651 }
31f18b77 4652 RGWRole role(g_ceph_context, store, role_name, tenant);
7c673cae
FG
4653 ret = role.get();
4654 if (ret < 0) {
4655 return -ret;
4656 }
4657 show_role_info(role, formatter);
4658 return 0;
4659 }
4660 case OPT_ROLE_MODIFY:
4661 {
31f18b77
FG
4662 if (role_name.empty()) {
4663 cerr << "ERROR: role name is empty" << std::endl;
7c673cae
FG
4664 return -EINVAL;
4665 }
31f18b77
FG
4666
4667 if (assume_role_doc.empty()) {
4668 cerr << "ERROR: assume role policy document is empty" << std::endl;
4669 return -EINVAL;
4670 }
4671
7c673cae
FG
4672 /* The following two calls will be replaced by read_decode_json or something
4673 similar when the code for AWS Policies is in place */
4674 bufferlist bl;
4675 int ret = read_input(assume_role_doc, bl);
4676 if (ret < 0) {
4677 cerr << "ERROR: failed to read input: " << cpp_strerror(-ret) << std::endl;
4678 return ret;
4679 }
4680 JSONParser p;
4681 if (!p.parse(bl.c_str(), bl.length())) {
4682 cout << "ERROR: failed to parse JSON: " << assume_role_doc << std::endl;
4683 return -EINVAL;
4684 }
4685 string trust_policy = bl.to_str();
31f18b77 4686 RGWRole role(g_ceph_context, store, role_name, tenant);
7c673cae
FG
4687 ret = role.get();
4688 if (ret < 0) {
4689 return -ret;
4690 }
4691 role.update_trust_policy(trust_policy);
4692 ret = role.update();
4693 if (ret < 0) {
4694 return -ret;
4695 }
4696 cout << "Assume role policy document updated successfully for role: " << role_name << std::endl;
4697 return 0;
4698 }
4699 case OPT_ROLE_LIST:
4700 {
4701 vector<RGWRole> result;
31f18b77 4702 ret = RGWRole::get_roles_by_path_prefix(store, g_ceph_context, path_prefix, tenant, result);
7c673cae
FG
4703 if (ret < 0) {
4704 return -ret;
4705 }
4706 show_roles_info(result, formatter);
4707 return 0;
4708 }
4709 case OPT_ROLE_POLICY_PUT:
4710 {
31f18b77
FG
4711 if (role_name.empty()) {
4712 cerr << "role name is empty" << std::endl;
7c673cae
FG
4713 return -EINVAL;
4714 }
31f18b77
FG
4715
4716 if (policy_name.empty()) {
4717 cerr << "policy name is empty" << std::endl;
4718 return -EINVAL;
4719 }
4720
4721 if (perm_policy_doc.empty()) {
4722 cerr << "permission policy document is empty" << std::endl;
4723 return -EINVAL;
4724 }
4725
7c673cae
FG
4726 /* The following two calls will be replaced by read_decode_json or something
4727 similar, when code for AWS Policies is in place.*/
4728 bufferlist bl;
4729 int ret = read_input(perm_policy_doc, bl);
4730 if (ret < 0) {
4731 cerr << "ERROR: failed to read input: " << cpp_strerror(-ret) << std::endl;
4732 return ret;
4733 }
4734 JSONParser p;
4735 if (!p.parse(bl.c_str(), bl.length())) {
4736 cout << "ERROR: failed to parse JSON: " << std::endl;
4737 return -EINVAL;
4738 }
4739 string perm_policy;
4740 perm_policy = bl.c_str();
4741
31f18b77 4742 RGWRole role(g_ceph_context, store, role_name, tenant);
7c673cae
FG
4743 ret = role.get();
4744 if (ret < 0) {
4745 return -ret;
4746 }
4747 role.set_perm_policy(policy_name, perm_policy);
4748 ret = role.update();
4749 if (ret < 0) {
4750 return -ret;
4751 }
4752 cout << "Permission policy attached successfully" << std::endl;
4753 return 0;
4754 }
4755 case OPT_ROLE_POLICY_LIST:
4756 {
4757 if (role_name.empty()) {
4758 cerr << "ERROR: Role name is empty" << std::endl;
4759 return -EINVAL;
4760 }
31f18b77 4761 RGWRole role(g_ceph_context, store, role_name, tenant);
7c673cae
FG
4762 ret = role.get();
4763 if (ret < 0) {
4764 return -ret;
4765 }
4766 std::vector<string> policy_names = role.get_role_policy_names();
4767 show_policy_names(policy_names, formatter);
4768 return 0;
4769 }
4770 case OPT_ROLE_POLICY_GET:
4771 {
31f18b77
FG
4772 if (role_name.empty()) {
4773 cerr << "ERROR: role name is empty" << std::endl;
7c673cae
FG
4774 return -EINVAL;
4775 }
31f18b77
FG
4776
4777 if (policy_name.empty()) {
4778 cerr << "ERROR: policy name is empty" << std::endl;
4779 return -EINVAL;
4780 }
4781 RGWRole role(g_ceph_context, store, role_name, tenant);
7c673cae
FG
4782 int ret = role.get();
4783 if (ret < 0) {
4784 return -ret;
4785 }
4786 string perm_policy;
4787 ret = role.get_role_policy(policy_name, perm_policy);
4788 if (ret < 0) {
4789 return -ret;
4790 }
4791 show_perm_policy(perm_policy, formatter);
4792 return 0;
4793 }
4794 case OPT_ROLE_POLICY_DELETE:
4795 {
31f18b77
FG
4796 if (role_name.empty()) {
4797 cerr << "ERROR: role name is empty" << std::endl;
4798 return -EINVAL;
4799 }
4800
4801 if (policy_name.empty()) {
4802 cerr << "ERROR: policy name is empty" << std::endl;
7c673cae
FG
4803 return -EINVAL;
4804 }
31f18b77 4805 RGWRole role(g_ceph_context, store, role_name, tenant);
7c673cae
FG
4806 ret = role.get();
4807 if (ret < 0) {
4808 return -ret;
4809 }
4810 ret = role.delete_policy(policy_name);
4811 if (ret < 0) {
4812 return -ret;
4813 }
4814 ret = role.update();
4815 if (ret < 0) {
4816 return -ret;
4817 }
4818 cout << "Policy: " << policy_name << " successfully deleted for role: "
4819 << role_name << std::endl;
4820 return 0;
4821 }
4822 default:
4823 output_user_info = false;
4824 }
4825
4826 // output the result of a user operation
4827 if (output_user_info) {
4828 ret = user.info(info, &err_msg);
4829 if (ret < 0) {
4830 cerr << "could not fetch user info: " << err_msg << std::endl;
4831 return -ret;
4832 }
4833 show_user_info(info, formatter);
4834 }
4835
4836 if (opt_cmd == OPT_POLICY) {
4837 if (format == "xml") {
4838 int ret = RGWBucketAdminOp::dump_s3_policy(store, bucket_op, cout);
4839 if (ret < 0) {
4840 cerr << "ERROR: failed to get policy: " << cpp_strerror(-ret) << std::endl;
4841 return -ret;
4842 }
4843 } else {
4844 int ret = RGWBucketAdminOp::get_policy(store, bucket_op, f);
4845 if (ret < 0) {
4846 cerr << "ERROR: failed to get policy: " << cpp_strerror(-ret) << std::endl;
4847 return -ret;
4848 }
4849 }
4850 }
4851
31f18b77 4852 if (opt_cmd == OPT_BUCKET_LIMIT_CHECK) {
7c673cae
FG
4853 void *handle;
4854 std::list<std::string> user_ids;
4855 metadata_key = "user";
4856 int max = 1000;
4857
4858 bool truncated;
4859
4860 if (! user_id.empty()) {
4861 user_ids.push_back(user_id.id);
4862 ret =
4863 RGWBucketAdminOp::limit_check(store, bucket_op, user_ids, f,
4864 warnings_only);
4865 } else {
4866 /* list users in groups of max-keys, then perform user-bucket
4867 * limit-check on each group */
4868 ret = store->meta_mgr->list_keys_init(metadata_key, &handle);
4869 if (ret < 0) {
4870 cerr << "ERROR: buckets limit check can't get user metadata_key: "
4871 << cpp_strerror(-ret) << std::endl;
4872 return -ret;
4873 }
4874
4875 do {
4876 ret = store->meta_mgr->list_keys_next(handle, max, user_ids,
4877 &truncated);
4878 if (ret < 0 && ret != -ENOENT) {
4879 cerr << "ERROR: buckets limit check lists_keys_next(): "
4880 << cpp_strerror(-ret) << std::endl;
4881 break;
4882 } else {
4883 /* ok, do the limit checks for this group */
4884 ret =
4885 RGWBucketAdminOp::limit_check(store, bucket_op, user_ids, f,
4886 warnings_only);
4887 if (ret < 0)
4888 break;
4889 }
4890 user_ids.clear();
4891 } while (truncated);
4892 store->meta_mgr->list_keys_complete(handle);
4893 }
4894 return -ret;
31f18b77 4895 } /* OPT_BUCKET_LIMIT_CHECK */
7c673cae
FG
4896
4897 if (opt_cmd == OPT_BUCKETS_LIST) {
4898 if (bucket_name.empty()) {
4899 RGWBucketAdminOp::info(store, bucket_op, f);
4900 } else {
4901 RGWBucketInfo bucket_info;
4902 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
4903 if (ret < 0) {
4904 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
4905 return -ret;
4906 }
4907 formatter->open_array_section("entries");
4908 bool truncated;
4909 int count = 0;
4910 if (max_entries < 0)
4911 max_entries = 1000;
4912
4913 string prefix;
4914 string delim;
4915 vector<rgw_bucket_dir_entry> result;
4916 map<string, bool> common_prefixes;
4917 string ns;
4918
4919 RGWRados::Bucket target(store, bucket_info);
4920 RGWRados::Bucket::List list_op(&target);
4921
4922 list_op.params.prefix = prefix;
4923 list_op.params.delim = delim;
4924 list_op.params.marker = rgw_obj_key(marker);
4925 list_op.params.ns = ns;
4926 list_op.params.enforce_ns = false;
4927 list_op.params.list_versions = true;
4928
4929 do {
4930 ret = list_op.list_objects(max_entries - count, &result, &common_prefixes, &truncated);
4931 if (ret < 0) {
4932 cerr << "ERROR: store->list_objects(): " << cpp_strerror(-ret) << std::endl;
4933 return -ret;
4934 }
4935
4936 count += result.size();
4937
4938 for (vector<rgw_bucket_dir_entry>::iterator iter = result.begin(); iter != result.end(); ++iter) {
4939 rgw_bucket_dir_entry& entry = *iter;
4940 encode_json("entry", entry, formatter);
4941 }
4942 formatter->flush(cout);
4943 } while (truncated && count < max_entries);
4944
4945 formatter->close_section();
4946 formatter->flush(cout);
4947 } /* have bucket_name */
4948 } /* OPT_BUCKETS_LIST */
4949
4950 if (opt_cmd == OPT_BUCKET_STATS) {
4951 bucket_op.set_fetch_stats(true);
4952
31f18b77
FG
4953 int r = RGWBucketAdminOp::info(store, bucket_op, f);
4954 if (r < 0) {
4955 cerr << "failure: " << cpp_strerror(-r) << ": " << err << std::endl;
4956 return -r;
4957 }
7c673cae
FG
4958 }
4959
4960 if (opt_cmd == OPT_BUCKET_LINK) {
4961 bucket_op.set_bucket_id(bucket_id);
4962 string err;
4963 int r = RGWBucketAdminOp::link(store, bucket_op, &err);
4964 if (r < 0) {
4965 cerr << "failure: " << cpp_strerror(-r) << ": " << err << std::endl;
4966 return -r;
4967 }
4968 }
4969
4970 if (opt_cmd == OPT_BUCKET_UNLINK) {
4971 int r = RGWBucketAdminOp::unlink(store, bucket_op);
4972 if (r < 0) {
4973 cerr << "failure: " << cpp_strerror(-r) << std::endl;
4974 return -r;
4975 }
4976 }
4977
4978 if (opt_cmd == OPT_LOG_LIST) {
4979 // filter by date?
4980 if (date.size() && date.size() != 10) {
4981 cerr << "bad date format for '" << date << "', expect YYYY-MM-DD" << std::endl;
4982 return EINVAL;
4983 }
4984
4985 formatter->reset();
4986 formatter->open_array_section("logs");
4987 RGWAccessHandle h;
4988 int r = store->log_list_init(date, &h);
4989 if (r == -ENOENT) {
4990 // no logs.
4991 } else {
4992 if (r < 0) {
4993 cerr << "log list: error " << r << std::endl;
4994 return -r;
4995 }
4996 while (true) {
4997 string name;
4998 int r = store->log_list_next(h, &name);
4999 if (r == -ENOENT)
5000 break;
5001 if (r < 0) {
5002 cerr << "log list: error " << r << std::endl;
5003 return -r;
5004 }
5005 formatter->dump_string("object", name);
5006 }
5007 }
5008 formatter->close_section();
5009 formatter->flush(cout);
5010 cout << std::endl;
5011 }
5012
5013 if (opt_cmd == OPT_LOG_SHOW || opt_cmd == OPT_LOG_RM) {
5014 if (object.empty() && (date.empty() || bucket_name.empty() || bucket_id.empty())) {
5015 cerr << "specify an object or a date, bucket and bucket-id" << std::endl;
5016 return usage();
5017 }
5018
5019 string oid;
5020 if (!object.empty()) {
5021 oid = object;
5022 } else {
5023 oid = date;
5024 oid += "-";
5025 oid += bucket_id;
5026 oid += "-";
5027 oid += bucket_name;
5028 }
5029
5030 if (opt_cmd == OPT_LOG_SHOW) {
5031 RGWAccessHandle h;
5032
5033 int r = store->log_show_init(oid, &h);
5034 if (r < 0) {
5035 cerr << "error opening log " << oid << ": " << cpp_strerror(-r) << std::endl;
5036 return -r;
5037 }
5038
5039 formatter->reset();
5040 formatter->open_object_section("log");
5041
5042 struct rgw_log_entry entry;
5043
5044 // peek at first entry to get bucket metadata
5045 r = store->log_show_next(h, &entry);
5046 if (r < 0) {
5047 cerr << "error reading log " << oid << ": " << cpp_strerror(-r) << std::endl;
5048 return -r;
5049 }
5050 formatter->dump_string("bucket_id", entry.bucket_id);
5051 formatter->dump_string("bucket_owner", entry.bucket_owner.to_str());
5052 formatter->dump_string("bucket", entry.bucket);
5053
5054 uint64_t agg_time = 0;
5055 uint64_t agg_bytes_sent = 0;
5056 uint64_t agg_bytes_received = 0;
5057 uint64_t total_entries = 0;
5058
5059 if (show_log_entries)
5060 formatter->open_array_section("log_entries");
5061
5062 do {
5063 uint64_t total_time = entry.total_time.sec() * 1000000LL + entry.total_time.usec();
5064
5065 agg_time += total_time;
5066 agg_bytes_sent += entry.bytes_sent;
5067 agg_bytes_received += entry.bytes_received;
5068 total_entries++;
5069
5070 if (skip_zero_entries && entry.bytes_sent == 0 &&
5071 entry.bytes_received == 0)
5072 goto next;
5073
5074 if (show_log_entries) {
5075
5076 rgw_format_ops_log_entry(entry, formatter);
5077 formatter->flush(cout);
5078 }
5079next:
5080 r = store->log_show_next(h, &entry);
5081 } while (r > 0);
5082
5083 if (r < 0) {
5084 cerr << "error reading log " << oid << ": " << cpp_strerror(-r) << std::endl;
5085 return -r;
5086 }
5087 if (show_log_entries)
5088 formatter->close_section();
5089
5090 if (show_log_sum) {
5091 formatter->open_object_section("log_sum");
5092 formatter->dump_int("bytes_sent", agg_bytes_sent);
5093 formatter->dump_int("bytes_received", agg_bytes_received);
5094 formatter->dump_int("total_time", agg_time);
5095 formatter->dump_int("total_entries", total_entries);
5096 formatter->close_section();
5097 }
5098 formatter->close_section();
5099 formatter->flush(cout);
5100 cout << std::endl;
5101 }
5102 if (opt_cmd == OPT_LOG_RM) {
5103 int r = store->log_remove(oid);
5104 if (r < 0) {
5105 cerr << "error removing log " << oid << ": " << cpp_strerror(-r) << std::endl;
5106 return -r;
5107 }
5108 }
5109 }
5110
5111 if (opt_cmd == OPT_POOL_ADD) {
5112 if (pool_name.empty()) {
5113 cerr << "need to specify pool to add!" << std::endl;
5114 return usage();
5115 }
5116
5117 int ret = store->add_bucket_placement(pool);
5118 if (ret < 0)
5119 cerr << "failed to add bucket placement: " << cpp_strerror(-ret) << std::endl;
5120 }
5121
5122 if (opt_cmd == OPT_POOL_RM) {
5123 if (pool_name.empty()) {
5124 cerr << "need to specify pool to remove!" << std::endl;
5125 return usage();
5126 }
5127
5128 int ret = store->remove_bucket_placement(pool);
5129 if (ret < 0)
5130 cerr << "failed to remove bucket placement: " << cpp_strerror(-ret) << std::endl;
5131 }
5132
5133 if (opt_cmd == OPT_POOLS_LIST) {
5134 set<rgw_pool> pools;
5135 int ret = store->list_placement_set(pools);
5136 if (ret < 0) {
5137 cerr << "could not list placement set: " << cpp_strerror(-ret) << std::endl;
5138 return -ret;
5139 }
5140 formatter->reset();
5141 formatter->open_array_section("pools");
5142 for (auto siter = pools.begin(); siter != pools.end(); ++siter) {
5143 formatter->open_object_section("pool");
5144 formatter->dump_string("name", siter->to_str());
5145 formatter->close_section();
5146 }
5147 formatter->close_section();
5148 formatter->flush(cout);
5149 cout << std::endl;
5150 }
5151
5152 if (opt_cmd == OPT_USAGE_SHOW) {
5153 uint64_t start_epoch = 0;
5154 uint64_t end_epoch = (uint64_t)-1;
5155
5156 int ret;
5157
5158 if (!start_date.empty()) {
5159 ret = utime_t::parse_date(start_date, &start_epoch, NULL);
5160 if (ret < 0) {
5161 cerr << "ERROR: failed to parse start date" << std::endl;
5162 return 1;
5163 }
5164 }
5165 if (!end_date.empty()) {
5166 ret = utime_t::parse_date(end_date, &end_epoch, NULL);
5167 if (ret < 0) {
5168 cerr << "ERROR: failed to parse end date" << std::endl;
5169 return 1;
5170 }
5171 }
5172
5173
5174 ret = RGWUsage::show(store, user_id, start_epoch, end_epoch,
5175 show_log_entries, show_log_sum, &categories,
5176 f);
5177 if (ret < 0) {
5178 cerr << "ERROR: failed to show usage" << std::endl;
5179 return 1;
5180 }
5181 }
5182
5183 if (opt_cmd == OPT_USAGE_TRIM) {
5184 if (user_id.empty() && !yes_i_really_mean_it) {
5185 cerr << "usage trim without user specified will remove *all* users data" << std::endl;
5186 cerr << "do you really mean it? (requires --yes-i-really-mean-it)" << std::endl;
5187 return 1;
5188 }
5189 int ret;
5190 uint64_t start_epoch = 0;
5191 uint64_t end_epoch = (uint64_t)-1;
5192
5193
5194 if (!start_date.empty()) {
5195 ret = utime_t::parse_date(start_date, &start_epoch, NULL);
5196 if (ret < 0) {
5197 cerr << "ERROR: failed to parse start date" << std::endl;
5198 return 1;
5199 }
5200 }
5201
5202 if (!end_date.empty()) {
5203 ret = utime_t::parse_date(end_date, &end_epoch, NULL);
5204 if (ret < 0) {
5205 cerr << "ERROR: failed to parse end date" << std::endl;
5206 return 1;
5207 }
5208 }
5209
5210 ret = RGWUsage::trim(store, user_id, start_epoch, end_epoch);
5211 if (ret < 0) {
5212 cerr << "ERROR: read_usage() returned ret=" << ret << std::endl;
5213 return 1;
5214 }
5215 }
5216
5217 if (opt_cmd == OPT_OLH_GET || opt_cmd == OPT_OLH_READLOG) {
5218 if (bucket_name.empty()) {
5219 cerr << "ERROR: bucket not specified" << std::endl;
5220 return EINVAL;
5221 }
5222 if (object.empty()) {
5223 cerr << "ERROR: object not specified" << std::endl;
5224 return EINVAL;
5225 }
7c673cae
FG
5226 }
5227
5228 if (opt_cmd == OPT_OLH_GET) {
5229 RGWBucketInfo bucket_info;
5230 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
5231 if (ret < 0) {
5232 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
5233 return -ret;
5234 }
5235 RGWOLHInfo olh;
5236 rgw_obj obj(bucket, object);
5237 ret = store->get_olh(bucket_info, obj, &olh);
5238 if (ret < 0) {
5239 cerr << "ERROR: failed reading olh: " << cpp_strerror(-ret) << std::endl;
5240 return -ret;
5241 }
5242 encode_json("olh", olh, formatter);
5243 formatter->flush(cout);
5244 }
5245
5246 if (opt_cmd == OPT_OLH_READLOG) {
5247 RGWBucketInfo bucket_info;
5248 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
5249 if (ret < 0) {
5250 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
5251 return -ret;
5252 }
5253 map<uint64_t, vector<rgw_bucket_olh_log_entry> > log;
5254 bool is_truncated;
5255
5256 RGWObjectCtx rctx(store);
5257 rgw_obj obj(bucket, object);
5258
5259 RGWObjState *state;
5260
5261 ret = store->get_obj_state(&rctx, bucket_info, obj, &state, false); /* don't follow olh */
5262 if (ret < 0) {
5263 return -ret;
5264 }
5265
5266 ret = store->bucket_index_read_olh_log(bucket_info, *state, obj, 0, &log, &is_truncated);
5267 if (ret < 0) {
5268 cerr << "ERROR: failed reading olh: " << cpp_strerror(-ret) << std::endl;
5269 return -ret;
5270 }
5271 formatter->open_object_section("result");
5272 encode_json("is_truncated", is_truncated, formatter);
5273 encode_json("log", log, formatter);
5274 formatter->close_section();
5275 formatter->flush(cout);
5276 }
5277
5278 if (opt_cmd == OPT_BI_GET) {
5279 RGWBucketInfo bucket_info;
5280 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
5281 if (ret < 0) {
5282 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
5283 return -ret;
5284 }
5285 rgw_obj obj(bucket, object);
5286 if (!object_version.empty()) {
5287 obj.key.set_instance(object_version);
5288 }
5289
5290 rgw_cls_bi_entry entry;
5291
5292 ret = store->bi_get(bucket, obj, bi_index_type, &entry);
5293 if (ret < 0) {
5294 cerr << "ERROR: bi_get(): " << cpp_strerror(-ret) << std::endl;
5295 return -ret;
5296 }
5297
5298 encode_json("entry", entry, formatter);
5299 formatter->flush(cout);
5300 }
5301
5302 if (opt_cmd == OPT_BI_PUT) {
5303 RGWBucketInfo bucket_info;
5304 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
5305 if (ret < 0) {
5306 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
5307 return -ret;
5308 }
5309
5310 rgw_cls_bi_entry entry;
5311 cls_rgw_obj_key key;
5312 ret = read_decode_json(infile, entry, &key);
5313 if (ret < 0) {
5314 return 1;
5315 }
5316
5317 rgw_obj obj(bucket, key);
5318
5319 ret = store->bi_put(bucket, obj, entry);
5320 if (ret < 0) {
5321 cerr << "ERROR: bi_put(): " << cpp_strerror(-ret) << std::endl;
5322 return -ret;
5323 }
5324 }
5325
5326 if (opt_cmd == OPT_BI_LIST) {
5327 if (bucket_name.empty()) {
5328 cerr << "ERROR: bucket name not specified" << std::endl;
5329 return EINVAL;
5330 }
5331 RGWBucketInfo bucket_info;
5332 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
5333 if (ret < 0) {
5334 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
5335 return -ret;
5336 }
5337
5338 list<rgw_cls_bi_entry> entries;
5339 bool is_truncated;
5340 if (max_entries < 0) {
5341 max_entries = 1000;
5342 }
5343
5344 int max_shards = (bucket_info.num_shards > 0 ? bucket_info.num_shards : 1);
5345
5346 formatter->open_array_section("entries");
5347
5348 for (int i = 0; i < max_shards; i++) {
5349 RGWRados::BucketShard bs(store);
5350 int shard_id = (bucket_info.num_shards > 0 ? i : -1);
5351 int ret = bs.init(bucket, shard_id);
5352 marker.clear();
5353
5354 if (ret < 0) {
5355 cerr << "ERROR: bs.init(bucket=" << bucket << ", shard=" << shard_id << "): " << cpp_strerror(-ret) << std::endl;
5356 return -ret;
5357 }
5358
5359 do {
5360 entries.clear();
5361 ret = store->bi_list(bs, object, marker, max_entries, &entries, &is_truncated);
5362 if (ret < 0) {
5363 cerr << "ERROR: bi_list(): " << cpp_strerror(-ret) << std::endl;
5364 return -ret;
5365 }
5366
5367 list<rgw_cls_bi_entry>::iterator iter;
5368 for (iter = entries.begin(); iter != entries.end(); ++iter) {
5369 rgw_cls_bi_entry& entry = *iter;
5370 encode_json("entry", entry, formatter);
5371 marker = entry.idx;
5372 }
5373 formatter->flush(cout);
5374 } while (is_truncated);
5375 formatter->flush(cout);
5376 }
5377 formatter->close_section();
5378 formatter->flush(cout);
5379 }
5380
5381 if (opt_cmd == OPT_BI_PURGE) {
5382 if (bucket_name.empty()) {
5383 cerr << "ERROR: bucket name not specified" << std::endl;
5384 return EINVAL;
5385 }
5386 RGWBucketInfo bucket_info;
5387 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
5388 if (ret < 0) {
5389 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
5390 return -ret;
5391 }
5392
5393 RGWBucketInfo cur_bucket_info;
5394 rgw_bucket cur_bucket;
5395 ret = init_bucket(tenant, bucket_name, string(), cur_bucket_info, cur_bucket);
5396 if (ret < 0) {
5397 cerr << "ERROR: could not init current bucket info for bucket_name=" << bucket_name << ": " << cpp_strerror(-ret) << std::endl;
5398 return -ret;
5399 }
5400
5401 if (cur_bucket_info.bucket.bucket_id == bucket_info.bucket.bucket_id && !yes_i_really_mean_it) {
5402 cerr << "specified bucket instance points to a current bucket instance" << std::endl;
5403 cerr << "do you really mean it? (requires --yes-i-really-mean-it)" << std::endl;
5404 return EINVAL;
5405 }
5406
5407 int max_shards = (bucket_info.num_shards > 0 ? bucket_info.num_shards : 1);
5408
5409 for (int i = 0; i < max_shards; i++) {
5410 RGWRados::BucketShard bs(store);
5411 int shard_id = (bucket_info.num_shards > 0 ? i : -1);
5412 int ret = bs.init(bucket, shard_id);
5413 if (ret < 0) {
5414 cerr << "ERROR: bs.init(bucket=" << bucket << ", shard=" << shard_id << "): " << cpp_strerror(-ret) << std::endl;
5415 return -ret;
5416 }
5417
5418 ret = store->bi_remove(bs);
5419 if (ret < 0) {
5420 cerr << "ERROR: failed to remove bucket index object: " << cpp_strerror(-ret) << std::endl;
5421 return -ret;
5422 }
5423 }
5424 }
5425
5426 if (opt_cmd == OPT_OBJECT_RM) {
5427 RGWBucketInfo bucket_info;
5428 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
5429 if (ret < 0) {
5430 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
5431 return -ret;
5432 }
5433 rgw_obj_key key(object, object_version);
5434 ret = rgw_remove_object(store, bucket_info, bucket, key);
5435
5436 if (ret < 0) {
5437 cerr << "ERROR: object remove returned: " << cpp_strerror(-ret) << std::endl;
5438 return -ret;
5439 }
5440 }
5441
5442 if (opt_cmd == OPT_OBJECT_REWRITE) {
5443 if (bucket_name.empty()) {
5444 cerr << "ERROR: bucket not specified" << std::endl;
5445 return EINVAL;
5446 }
5447 if (object.empty()) {
5448 cerr << "ERROR: object not specified" << std::endl;
5449 return EINVAL;
5450 }
5451
5452 RGWBucketInfo bucket_info;
5453 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
5454 if (ret < 0) {
5455 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
5456 return -ret;
5457 }
5458
5459 rgw_obj obj(bucket, object);
5460 obj.key.set_instance(object_version);
5461 bool need_rewrite = true;
5462 if (min_rewrite_stripe_size > 0) {
5463 ret = check_min_obj_stripe_size(store, bucket_info, obj, min_rewrite_stripe_size, &need_rewrite);
5464 if (ret < 0) {
5465 ldout(store->ctx(), 0) << "WARNING: check_min_obj_stripe_size failed, r=" << ret << dendl;
5466 }
5467 }
5468 if (need_rewrite) {
5469 ret = store->rewrite_obj(bucket_info, obj);
5470 if (ret < 0) {
5471 cerr << "ERROR: object rewrite returned: " << cpp_strerror(-ret) << std::endl;
5472 return -ret;
5473 }
5474 } else {
5475 ldout(store->ctx(), 20) << "skipped object" << dendl;
5476 }
5477 }
5478
5479 if (opt_cmd == OPT_OBJECTS_EXPIRE) {
5480 int ret = store->process_expire_objects();
5481 if (ret < 0) {
5482 cerr << "ERROR: process_expire_objects() processing returned error: " << cpp_strerror(-ret) << std::endl;
5483 return 1;
5484 }
5485 }
5486
5487 if (opt_cmd == OPT_BUCKET_REWRITE) {
5488 if (bucket_name.empty()) {
5489 cerr << "ERROR: bucket not specified" << std::endl;
5490 return EINVAL;
5491 }
5492
5493 RGWBucketInfo bucket_info;
5494 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
5495 if (ret < 0) {
5496 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
5497 return -ret;
5498 }
5499
5500 uint64_t start_epoch = 0;
5501 uint64_t end_epoch = 0;
5502
5503 if (!end_date.empty()) {
5504 int ret = utime_t::parse_date(end_date, &end_epoch, NULL);
5505 if (ret < 0) {
5506 cerr << "ERROR: failed to parse end date" << std::endl;
5507 return EINVAL;
5508 }
5509 }
5510 if (!start_date.empty()) {
5511 int ret = utime_t::parse_date(start_date, &start_epoch, NULL);
5512 if (ret < 0) {
5513 cerr << "ERROR: failed to parse start date" << std::endl;
5514 return EINVAL;
5515 }
5516 }
5517
5518 bool is_truncated = true;
5519
5520 rgw_obj_index_key marker;
5521 string prefix;
5522
5523 formatter->open_object_section("result");
5524 formatter->dump_string("bucket", bucket_name);
5525 formatter->open_array_section("objects");
5526 while (is_truncated) {
5527 map<string, rgw_bucket_dir_entry> result;
5528 int r = store->cls_bucket_list(bucket_info, RGW_NO_SHARD, marker, prefix, 1000, true,
5529 result, &is_truncated, &marker,
5530 bucket_object_check_filter);
5531
5532 if (r < 0 && r != -ENOENT) {
5533 cerr << "ERROR: failed operation r=" << r << std::endl;
5534 }
5535
5536 if (r == -ENOENT)
5537 break;
5538
5539 map<string, rgw_bucket_dir_entry>::iterator iter;
5540 for (iter = result.begin(); iter != result.end(); ++iter) {
5541 rgw_obj_key key = iter->second.key;
5542 rgw_bucket_dir_entry& entry = iter->second;
5543
5544 formatter->open_object_section("object");
5545 formatter->dump_string("name", key.name);
5546 formatter->dump_string("instance", key.instance);
5547 formatter->dump_int("size", entry.meta.size);
5548 utime_t ut(entry.meta.mtime);
5549 ut.gmtime(formatter->dump_stream("mtime"));
5550
5551 if ((entry.meta.size < min_rewrite_size) ||
5552 (entry.meta.size > max_rewrite_size) ||
5553 (start_epoch > 0 && start_epoch > (uint64_t)ut.sec()) ||
5554 (end_epoch > 0 && end_epoch < (uint64_t)ut.sec())) {
5555 formatter->dump_string("status", "Skipped");
5556 } else {
5557 rgw_obj obj(bucket, key);
5558
5559 bool need_rewrite = true;
5560 if (min_rewrite_stripe_size > 0) {
5561 r = check_min_obj_stripe_size(store, bucket_info, obj, min_rewrite_stripe_size, &need_rewrite);
5562 if (r < 0) {
5563 ldout(store->ctx(), 0) << "WARNING: check_min_obj_stripe_size failed, r=" << r << dendl;
5564 }
5565 }
5566 if (!need_rewrite) {
5567 formatter->dump_string("status", "Skipped");
5568 } else {
5569 r = store->rewrite_obj(bucket_info, obj);
5570 if (r == 0) {
5571 formatter->dump_string("status", "Success");
5572 } else {
5573 formatter->dump_string("status", cpp_strerror(-r));
5574 }
5575 }
5576 }
5577 formatter->dump_int("flags", entry.flags);
5578
5579 formatter->close_section();
5580 formatter->flush(cout);
5581 }
5582 }
5583 formatter->close_section();
5584 formatter->close_section();
5585 formatter->flush(cout);
5586 }
5587
5588 if (opt_cmd == OPT_BUCKET_RESHARD) {
31f18b77 5589 rgw_bucket bucket;
7c673cae
FG
5590 RGWBucketInfo bucket_info;
5591 map<string, bufferlist> attrs;
31f18b77
FG
5592
5593 int ret = check_reshard_bucket_params(store,
5594 bucket_name,
5595 tenant,
5596 bucket_id,
5597 num_shards_specified,
5598 num_shards,
5599 yes_i_really_mean_it,
5600 bucket,
5601 bucket_info,
5602 attrs);
7c673cae 5603 if (ret < 0) {
31f18b77 5604 return ret;
7c673cae
FG
5605 }
5606
31f18b77 5607 RGWBucketReshard br(store, bucket_info, attrs);
7c673cae 5608
31f18b77
FG
5609#define DEFAULT_RESHARD_MAX_ENTRIES 1000
5610 if (max_entries < 1) {
5611 max_entries = DEFAULT_RESHARD_MAX_ENTRIES;
7c673cae
FG
5612 }
5613
31f18b77
FG
5614 return br.execute(num_shards, max_entries,
5615 verbose, &cout, formatter);
5616 }
7c673cae 5617
31f18b77
FG
5618 if (opt_cmd == OPT_RESHARD_ADD) {
5619 rgw_bucket bucket;
5620 RGWBucketInfo bucket_info;
5621 map<string, bufferlist> attrs;
7c673cae 5622
31f18b77
FG
5623 int ret = check_reshard_bucket_params(store,
5624 bucket_name,
5625 tenant,
5626 bucket_id,
5627 num_shards_specified,
5628 num_shards,
5629 yes_i_really_mean_it,
5630 bucket,
5631 bucket_info,
5632 attrs);
7c673cae 5633 if (ret < 0) {
31f18b77 5634 return ret;
7c673cae
FG
5635 }
5636
31f18b77 5637 int num_source_shards = (bucket_info.num_shards > 0 ? bucket_info.num_shards : 1);
7c673cae 5638
31f18b77
FG
5639 RGWReshard reshard(store);
5640 cls_rgw_reshard_entry entry;
5641 entry.time = real_clock::now();
5642 entry.tenant = tenant;
5643 entry.bucket_name = bucket_name;
5644 entry.bucket_id = bucket_info.bucket.bucket_id;
5645 entry.old_num_shards = num_source_shards;
5646 entry.new_num_shards = num_shards;
7c673cae 5647
31f18b77
FG
5648 return reshard.add(entry);
5649 }
7c673cae 5650
31f18b77
FG
5651 if (opt_cmd == OPT_RESHARD_LIST) {
5652 list<cls_rgw_reshard_entry> entries;
5653 int ret;
5654 int count = 0;
5655 if (max_entries < 0) {
5656 max_entries = 1000;
7c673cae
FG
5657 }
5658
31f18b77 5659 int num_logshards = store->ctx()->_conf->rgw_reshard_num_logs;
7c673cae 5660
31f18b77 5661 RGWReshard reshard(store);
7c673cae 5662
31f18b77
FG
5663 formatter->open_array_section("reshard");
5664 for (int i = 0; i < num_logshards; i++) {
7c673cae 5665 bool is_truncated = true;
31f18b77
FG
5666 string marker;
5667 do {
7c673cae 5668 entries.clear();
31f18b77 5669 ret = reshard.list(i, marker, max_entries, entries, &is_truncated);
7c673cae 5670 if (ret < 0) {
31f18b77
FG
5671 cerr << "Error listing resharding buckets: " << cpp_strerror(-ret) << std::endl;
5672 return ret;
7c673cae 5673 }
31f18b77
FG
5674 for (auto iter=entries.begin(); iter != entries.end(); ++iter) {
5675 cls_rgw_reshard_entry& entry = *iter;
5676 encode_json("entry", entry, formatter);
5677 entry.get_key(&marker);
5678 }
5679 count += entries.size();
5680 formatter->flush(cout);
5681 } while (is_truncated && count < max_entries);
7c673cae 5682
31f18b77
FG
5683 if (count >= max_entries) {
5684 break;
5685 }
5686 }
7c673cae 5687
31f18b77
FG
5688 formatter->close_section();
5689 formatter->flush(cout);
5690 return 0;
5691 }
7c673cae 5692
7c673cae 5693
31f18b77
FG
5694 if (opt_cmd == OPT_RESHARD_STATUS) {
5695 if (bucket_name.empty()) {
5696 cerr << "ERROR: bucket not specified" << std::endl;
5697 return EINVAL;
5698 }
7c673cae 5699
31f18b77
FG
5700 rgw_bucket bucket;
5701 RGWBucketInfo bucket_info;
5702 map<string, bufferlist> attrs;
5703 ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket, &attrs);
5704 if (ret < 0) {
5705 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
5706 return -ret;
5707 }
7c673cae 5708
31f18b77
FG
5709 RGWBucketReshard br(store, bucket_info, attrs);
5710 list<cls_rgw_bucket_instance_entry> status;
5711 int r = br.get_status(&status);
5712 if (r < 0) {
5713 cerr << "ERROR: could not get resharding status for bucket " << bucket_name << std::endl;
5714 return -r;
7c673cae 5715 }
31f18b77
FG
5716
5717 encode_json("status", status, formatter);
5718 formatter->flush(cout);
5719 }
5720
5721 if (opt_cmd == OPT_RESHARD_PROCESS) {
5722 RGWReshard reshard(store, true, &cout);
5723
5724 int ret = reshard.process_all_logshards();
5725 if (ret < 0) {
5726 cerr << "ERROR: failed to process reshard logs, error=" << cpp_strerror(-ret) << std::endl;
5727 return -ret;
7c673cae 5728 }
31f18b77 5729 }
7c673cae 5730
31f18b77
FG
5731 if (opt_cmd == OPT_RESHARD_CANCEL) {
5732 RGWReshard reshard(store);
5733
5734 if (bucket_name.empty()) {
5735 cerr << "ERROR: bucket not specified" << std::endl;
5736 return EINVAL;
5737 }
5738 cls_rgw_reshard_entry entry;
5739 //entry.tenant = tenant;
5740 entry.bucket_name = bucket_name;
5741 //entry.bucket_id = bucket_id;
5742 int ret = reshard.get(entry);
7c673cae 5743 if (ret < 0) {
31f18b77
FG
5744 cerr << "Error in getting bucket " << bucket_name << ": " << cpp_strerror(-ret) << std::endl;
5745 return ret;
7c673cae
FG
5746 }
5747
31f18b77
FG
5748 /* TBD stop running resharding */
5749
5750 ret =reshard.remove(entry);
5751 if (ret < 0) {
5752 cerr << "Error removing bucket " << bucket_name << " for resharding queue: " << cpp_strerror(-ret) <<
5753 std::endl;
5754 return ret;
7c673cae
FG
5755 }
5756 }
5757
5758 if (opt_cmd == OPT_OBJECT_UNLINK) {
5759 RGWBucketInfo bucket_info;
5760 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
5761 if (ret < 0) {
5762 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
5763 return -ret;
5764 }
5765 list<rgw_obj_index_key> oid_list;
5766 rgw_obj_key key(object, object_version);
5767 rgw_obj_index_key index_key;
5768 key.get_index_key(&index_key);
5769 oid_list.push_back(index_key);
5770 ret = store->remove_objs_from_index(bucket_info, oid_list);
5771 if (ret < 0) {
5772 cerr << "ERROR: remove_obj_from_index() returned error: " << cpp_strerror(-ret) << std::endl;
5773 return 1;
5774 }
5775 }
5776
5777 if (opt_cmd == OPT_OBJECT_STAT) {
5778 RGWBucketInfo bucket_info;
5779 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
5780 if (ret < 0) {
5781 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
5782 return -ret;
5783 }
5784 rgw_obj obj(bucket, object);
5785 obj.key.set_instance(object_version);
5786
5787 uint64_t obj_size;
5788 map<string, bufferlist> attrs;
5789 RGWObjectCtx obj_ctx(store);
5790 RGWRados::Object op_target(store, bucket_info, obj_ctx, obj);
5791 RGWRados::Object::Read read_op(&op_target);
5792
5793 read_op.params.attrs = &attrs;
5794 read_op.params.obj_size = &obj_size;
5795
5796 ret = read_op.prepare();
5797 if (ret < 0) {
5798 cerr << "ERROR: failed to stat object, returned error: " << cpp_strerror(-ret) << std::endl;
5799 return 1;
5800 }
5801 formatter->open_object_section("object_metadata");
5802 formatter->dump_string("name", object);
5803 formatter->dump_unsigned("size", obj_size);
5804
5805 map<string, bufferlist>::iterator iter;
5806 map<string, bufferlist> other_attrs;
5807 for (iter = attrs.begin(); iter != attrs.end(); ++iter) {
5808 bufferlist& bl = iter->second;
5809 bool handled = false;
5810 if (iter->first == RGW_ATTR_MANIFEST) {
5811 handled = decode_dump<RGWObjManifest>("manifest", bl, formatter);
5812 } else if (iter->first == RGW_ATTR_ACL) {
5813 handled = decode_dump<RGWAccessControlPolicy>("policy", bl, formatter);
5814 } else if (iter->first == RGW_ATTR_ID_TAG) {
5815 handled = dump_string("tag", bl, formatter);
5816 } else if (iter->first == RGW_ATTR_ETAG) {
5817 handled = dump_string("etag", bl, formatter);
5818 }
5819
5820 if (!handled)
5821 other_attrs[iter->first] = bl;
5822 }
5823
5824 formatter->open_object_section("attrs");
5825 for (iter = other_attrs.begin(); iter != other_attrs.end(); ++iter) {
5826 dump_string(iter->first.c_str(), iter->second, formatter);
5827 }
5828 formatter->close_section();
5829 formatter->close_section();
5830 formatter->flush(cout);
5831 }
5832
5833 if (opt_cmd == OPT_BUCKET_CHECK) {
5834 if (check_head_obj_locator) {
5835 if (bucket_name.empty()) {
5836 cerr << "ERROR: need to specify bucket name" << std::endl;
5837 return EINVAL;
5838 }
5839 do_check_object_locator(tenant, bucket_name, fix, remove_bad, formatter);
5840 } else {
5841 RGWBucketAdminOp::check_index(store, bucket_op, f);
5842 }
5843 }
5844
5845 if (opt_cmd == OPT_BUCKET_RM) {
5846 if (inconsistent_index == false) {
5847 RGWBucketAdminOp::remove_bucket(store, bucket_op, bypass_gc, true);
5848 } else {
5849 RGWBucketAdminOp::remove_bucket(store, bucket_op, bypass_gc, false);
5850 }
5851 }
5852
5853 if (opt_cmd == OPT_GC_LIST) {
5854 int index = 0;
5855 bool truncated;
5856 formatter->open_array_section("entries");
5857
5858 do {
5859 list<cls_rgw_gc_obj_info> result;
5860 int ret = store->list_gc_objs(&index, marker, 1000, !include_all, result, &truncated);
5861 if (ret < 0) {
5862 cerr << "ERROR: failed to list objs: " << cpp_strerror(-ret) << std::endl;
5863 return 1;
5864 }
5865
5866
5867 list<cls_rgw_gc_obj_info>::iterator iter;
5868 for (iter = result.begin(); iter != result.end(); ++iter) {
5869 cls_rgw_gc_obj_info& info = *iter;
5870 formatter->open_object_section("chain_info");
5871 formatter->dump_string("tag", info.tag);
5872 formatter->dump_stream("time") << info.time;
5873 formatter->open_array_section("objs");
5874 list<cls_rgw_obj>::iterator liter;
5875 cls_rgw_obj_chain& chain = info.chain;
5876 for (liter = chain.objs.begin(); liter != chain.objs.end(); ++liter) {
5877 cls_rgw_obj& obj = *liter;
5878 encode_json("obj", obj, formatter);
5879 }
5880 formatter->close_section(); // objs
5881 formatter->close_section(); // obj_chain
5882 formatter->flush(cout);
5883 }
5884 } while (truncated);
5885 formatter->close_section();
5886 formatter->flush(cout);
5887 }
5888
5889 if (opt_cmd == OPT_GC_PROCESS) {
5890 int ret = store->process_gc();
5891 if (ret < 0) {
5892 cerr << "ERROR: gc processing returned error: " << cpp_strerror(-ret) << std::endl;
5893 return 1;
5894 }
5895 }
5896
5897 if (opt_cmd == OPT_LC_LIST) {
5898 formatter->open_array_section("lifecycle_list");
5899 map<string, int> bucket_lc_map;
5900 string marker;
5901#define MAX_LC_LIST_ENTRIES 100
5902 if (max_entries < 0) {
5903 max_entries = MAX_LC_LIST_ENTRIES;
5904 }
5905 do {
5906 int ret = store->list_lc_progress(marker, max_entries, &bucket_lc_map);
5907 if (ret < 0) {
5908 cerr << "ERROR: failed to list objs: " << cpp_strerror(-ret) << std::endl;
5909 return 1;
5910 }
5911 map<string, int>::iterator iter;
5912 for (iter = bucket_lc_map.begin(); iter != bucket_lc_map.end(); ++iter) {
5913 formatter->open_object_section("bucket_lc_info");
5914 formatter->dump_string("bucket", iter->first);
5915 string lc_status = LC_STATUS[iter->second];
5916 formatter->dump_string("status", lc_status);
5917 formatter->close_section(); // objs
5918 formatter->flush(cout);
5919 marker = iter->first;
5920 }
5921 } while (!bucket_lc_map.empty());
5922
5923 formatter->close_section(); //lifecycle list
5924 formatter->flush(cout);
5925 }
5926
5927
5928 if (opt_cmd == OPT_LC_PROCESS) {
5929 int ret = store->process_lc();
5930 if (ret < 0) {
5931 cerr << "ERROR: lc processing returned error: " << cpp_strerror(-ret) << std::endl;
5932 return 1;
5933 }
5934 }
5935
5936 if (opt_cmd == OPT_ORPHANS_FIND) {
5937 RGWOrphanSearch search(store, max_concurrent_ios, orphan_stale_secs);
5938
5939 if (job_id.empty()) {
5940 cerr << "ERROR: --job-id not specified" << std::endl;
5941 return EINVAL;
5942 }
5943 if (pool_name.empty()) {
5944 cerr << "ERROR: --pool not specified" << std::endl;
5945 return EINVAL;
5946 }
5947
5948 RGWOrphanSearchInfo info;
5949
5950 info.pool = pool;
5951 info.job_name = job_id;
5952 info.num_shards = num_shards;
5953
5954 int ret = search.init(job_id, &info);
5955 if (ret < 0) {
5956 cerr << "could not init search, ret=" << ret << std::endl;
5957 return -ret;
5958 }
5959 ret = search.run();
5960 if (ret < 0) {
5961 return -ret;
5962 }
5963 }
5964
5965 if (opt_cmd == OPT_ORPHANS_FINISH) {
5966 RGWOrphanSearch search(store, max_concurrent_ios, orphan_stale_secs);
5967
5968 if (job_id.empty()) {
5969 cerr << "ERROR: --job-id not specified" << std::endl;
5970 return EINVAL;
5971 }
5972 int ret = search.init(job_id, NULL);
5973 if (ret < 0) {
5974 if (ret == -ENOENT) {
5975 cerr << "job not found" << std::endl;
5976 }
5977 return -ret;
5978 }
5979 ret = search.finish();
5980 if (ret < 0) {
5981 return -ret;
5982 }
5983 }
5984
5985 if (opt_cmd == OPT_ORPHANS_LIST_JOBS){
5986 RGWOrphanStore orphan_store(store);
5987 int ret = orphan_store.init();
5988 if (ret < 0){
5989 cerr << "connection to cluster failed!" << std::endl;
5990 return -ret;
5991 }
5992
5993 map <string,RGWOrphanSearchState> m;
5994 ret = orphan_store.list_jobs(m);
5995 if (ret < 0) {
5996 cerr << "job list failed" << std::endl;
5997 return -ret;
5998 }
5999 formatter->open_array_section("entries");
6000 for (const auto &it: m){
6001 if (!extra_info){
6002 formatter->dump_string("job-id",it.first);
6003 } else {
6004 encode_json("orphan_search_state", it.second, formatter);
6005 }
6006 }
6007 formatter->close_section();
6008 formatter->flush(cout);
6009 }
6010
6011 if (opt_cmd == OPT_USER_CHECK) {
6012 check_bad_user_bucket_mapping(store, user_id, fix);
6013 }
6014
6015 if (opt_cmd == OPT_USER_STATS) {
6016 if (sync_stats) {
6017 if (!bucket_name.empty()) {
6018 int ret = rgw_bucket_sync_user_stats(store, tenant, bucket_name);
6019 if (ret < 0) {
6020 cerr << "ERROR: could not sync bucket stats: " << cpp_strerror(-ret) << std::endl;
6021 return -ret;
6022 }
6023 } else {
6024 int ret = rgw_user_sync_all_stats(store, user_id);
6025 if (ret < 0) {
6026 cerr << "ERROR: failed to sync user stats: " << cpp_strerror(-ret) << std::endl;
6027 return -ret;
6028 }
6029 }
6030 }
6031
6032 if (user_id.empty()) {
6033 cerr << "ERROR: uid not specified" << std::endl;
6034 return EINVAL;
6035 }
6036 cls_user_header header;
6037 string user_str = user_id.to_str();
6038 int ret = store->cls_user_get_header(user_str, &header);
6039 if (ret < 0) {
6040 if (ret == -ENOENT) { /* in case of ENOENT */
6041 cerr << "User has not been initialized or user does not exist" << std::endl;
6042 } else {
6043 cerr << "ERROR: can't read user: " << cpp_strerror(ret) << std::endl;
6044 }
6045 return -ret;
6046 }
6047
6048 encode_json("header", header, formatter);
6049 formatter->flush(cout);
6050 }
6051
6052 if (opt_cmd == OPT_METADATA_GET) {
6053 int ret = store->meta_mgr->get(metadata_key, formatter);
6054 if (ret < 0) {
6055 cerr << "ERROR: can't get key: " << cpp_strerror(-ret) << std::endl;
6056 return -ret;
6057 }
6058
6059 formatter->flush(cout);
6060 }
6061
6062 if (opt_cmd == OPT_METADATA_PUT) {
6063 bufferlist bl;
6064 int ret = read_input(infile, bl);
6065 if (ret < 0) {
6066 cerr << "ERROR: failed to read input: " << cpp_strerror(-ret) << std::endl;
6067 return -ret;
6068 }
6069 ret = store->meta_mgr->put(metadata_key, bl, RGWMetadataHandler::APPLY_ALWAYS);
6070 if (ret < 0) {
6071 cerr << "ERROR: can't put key: " << cpp_strerror(-ret) << std::endl;
6072 return -ret;
6073 }
6074 }
6075
6076 if (opt_cmd == OPT_METADATA_RM) {
6077 int ret = store->meta_mgr->remove(metadata_key);
6078 if (ret < 0) {
6079 cerr << "ERROR: can't remove key: " << cpp_strerror(-ret) << std::endl;
6080 return -ret;
6081 }
6082 }
6083
6084 if (opt_cmd == OPT_METADATA_LIST || opt_cmd == OPT_USER_LIST) {
6085 if (opt_cmd == OPT_USER_LIST) {
6086 metadata_key = "user";
6087 }
6088 void *handle;
6089 int max = 1000;
6090 int ret = store->meta_mgr->list_keys_init(metadata_key, &handle);
6091 if (ret < 0) {
6092 cerr << "ERROR: can't get key: " << cpp_strerror(-ret) << std::endl;
6093 return -ret;
6094 }
6095
6096 bool truncated;
6097
6098 formatter->open_array_section("keys");
6099
6100 do {
6101 list<string> keys;
6102 ret = store->meta_mgr->list_keys_next(handle, max, keys, &truncated);
6103 if (ret < 0 && ret != -ENOENT) {
6104 cerr << "ERROR: lists_keys_next(): " << cpp_strerror(-ret) << std::endl;
6105 return -ret;
6106 } if (ret != -ENOENT) {
6107 for (list<string>::iterator iter = keys.begin(); iter != keys.end(); ++iter) {
6108 formatter->dump_string("key", *iter);
6109 }
6110 formatter->flush(cout);
6111 }
6112 } while (truncated);
6113
6114 formatter->close_section();
6115 formatter->flush(cout);
6116
6117 store->meta_mgr->list_keys_complete(handle);
6118 }
6119
6120 if (opt_cmd == OPT_MDLOG_LIST) {
6121 utime_t start_time, end_time;
6122
6123 int ret = parse_date_str(start_date, start_time);
6124 if (ret < 0)
6125 return -ret;
6126
6127 ret = parse_date_str(end_date, end_time);
6128 if (ret < 0)
6129 return -ret;
6130
6131 int i = (specified_shard_id ? shard_id : 0);
6132
6133 if (period_id.empty()) {
6134 int ret = read_current_period_id(store, realm_id, realm_name, &period_id);
6135 if (ret < 0) {
6136 return -ret;
6137 }
6138 std::cerr << "No --period given, using current period="
6139 << period_id << std::endl;
6140 }
6141 RGWMetadataLog *meta_log = store->meta_mgr->get_log(period_id);
6142
6143 formatter->open_array_section("entries");
6144 for (; i < g_ceph_context->_conf->rgw_md_log_max_shards; i++) {
6145 void *handle;
6146 list<cls_log_entry> entries;
6147
6148
6149 meta_log->init_list_entries(i, start_time.to_real_time(), end_time.to_real_time(), marker, &handle);
6150 bool truncated;
6151 do {
6152 int ret = meta_log->list_entries(handle, 1000, entries, NULL, &truncated);
6153 if (ret < 0) {
6154 cerr << "ERROR: meta_log->list_entries(): " << cpp_strerror(-ret) << std::endl;
6155 return -ret;
6156 }
6157
6158 for (list<cls_log_entry>::iterator iter = entries.begin(); iter != entries.end(); ++iter) {
6159 cls_log_entry& entry = *iter;
6160 store->meta_mgr->dump_log_entry(entry, formatter);
6161 }
6162 formatter->flush(cout);
6163 } while (truncated);
6164
6165 meta_log->complete_list_entries(handle);
6166
6167 if (specified_shard_id)
6168 break;
6169 }
6170
6171
6172 formatter->close_section();
6173 formatter->flush(cout);
6174 }
6175
6176 if (opt_cmd == OPT_MDLOG_STATUS) {
6177 int i = (specified_shard_id ? shard_id : 0);
6178
6179 if (period_id.empty()) {
6180 int ret = read_current_period_id(store, realm_id, realm_name, &period_id);
6181 if (ret < 0) {
6182 return -ret;
6183 }
6184 std::cerr << "No --period given, using current period="
6185 << period_id << std::endl;
6186 }
6187 RGWMetadataLog *meta_log = store->meta_mgr->get_log(period_id);
6188
6189 formatter->open_array_section("entries");
6190
6191 for (; i < g_ceph_context->_conf->rgw_md_log_max_shards; i++) {
6192 RGWMetadataLogInfo info;
6193 meta_log->get_info(i, &info);
6194
6195 ::encode_json("info", info, formatter);
6196
6197 if (specified_shard_id)
6198 break;
6199 }
6200
6201
6202 formatter->close_section();
6203 formatter->flush(cout);
6204 }
6205
6206 if (opt_cmd == OPT_MDLOG_AUTOTRIM) {
6207 // need a full history for purging old mdlog periods
6208 store->meta_mgr->init_oldest_log_period();
6209
6210 RGWCoroutinesManager crs(store->ctx(), store->get_cr_registry());
6211 RGWHTTPManager http(store->ctx(), crs.get_completion_mgr());
6212 int ret = http.set_threaded();
6213 if (ret < 0) {
6214 cerr << "failed to initialize http client with " << cpp_strerror(ret) << std::endl;
6215 return -ret;
6216 }
6217
6218 auto num_shards = g_conf->rgw_md_log_max_shards;
6219 ret = crs.run(create_admin_meta_log_trim_cr(store, &http, num_shards));
6220 if (ret < 0) {
6221 cerr << "automated mdlog trim failed with " << cpp_strerror(ret) << std::endl;
6222 return -ret;
6223 }
6224 }
6225
6226 if (opt_cmd == OPT_MDLOG_TRIM) {
6227 utime_t start_time, end_time;
6228
6229 if (!specified_shard_id) {
6230 cerr << "ERROR: shard-id must be specified for trim operation" << std::endl;
6231 return EINVAL;
6232 }
6233
6234 int ret = parse_date_str(start_date, start_time);
6235 if (ret < 0)
6236 return -ret;
6237
6238 ret = parse_date_str(end_date, end_time);
6239 if (ret < 0)
6240 return -ret;
6241
6242 if (period_id.empty()) {
6243 std::cerr << "missing --period argument" << std::endl;
6244 return EINVAL;
6245 }
6246 RGWMetadataLog *meta_log = store->meta_mgr->get_log(period_id);
6247
6248 ret = meta_log->trim(shard_id, start_time.to_real_time(), end_time.to_real_time(), start_marker, end_marker);
6249 if (ret < 0) {
6250 cerr << "ERROR: meta_log->trim(): " << cpp_strerror(-ret) << std::endl;
6251 return -ret;
6252 }
6253 }
6254
6255 if (opt_cmd == OPT_SYNC_STATUS) {
6256 sync_status(formatter);
6257 }
6258
6259 if (opt_cmd == OPT_METADATA_SYNC_STATUS) {
6260 RGWMetaSyncStatusManager sync(store, store->get_async_rados());
6261
6262 int ret = sync.init();
6263 if (ret < 0) {
6264 cerr << "ERROR: sync.init() returned ret=" << ret << std::endl;
6265 return -ret;
6266 }
6267
6268 rgw_meta_sync_status sync_status;
6269 ret = sync.read_sync_status(&sync_status);
6270 if (ret < 0) {
6271 cerr << "ERROR: sync.read_sync_status() returned ret=" << ret << std::endl;
6272 return -ret;
6273 }
6274
6275 formatter->open_object_section("summary");
6276 encode_json("sync_status", sync_status, formatter);
6277
6278 uint64_t full_total = 0;
6279 uint64_t full_complete = 0;
6280
6281 for (auto marker_iter : sync_status.sync_markers) {
6282 full_total += marker_iter.second.total_entries;
6283 if (marker_iter.second.state == rgw_meta_sync_marker::SyncState::FullSync) {
6284 full_complete += marker_iter.second.pos;
6285 } else {
6286 full_complete += marker_iter.second.total_entries;
6287 }
6288 }
6289
6290 formatter->open_object_section("full_sync");
6291 encode_json("total", full_total, formatter);
6292 encode_json("complete", full_complete, formatter);
6293 formatter->close_section();
6294 formatter->close_section();
6295
6296 formatter->flush(cout);
6297
6298 }
6299
6300 if (opt_cmd == OPT_METADATA_SYNC_INIT) {
6301 RGWMetaSyncStatusManager sync(store, store->get_async_rados());
6302
6303 int ret = sync.init();
6304 if (ret < 0) {
6305 cerr << "ERROR: sync.init() returned ret=" << ret << std::endl;
6306 return -ret;
6307 }
6308 ret = sync.init_sync_status();
6309 if (ret < 0) {
6310 cerr << "ERROR: sync.init_sync_status() returned ret=" << ret << std::endl;
6311 return -ret;
6312 }
6313 }
6314
6315
6316 if (opt_cmd == OPT_METADATA_SYNC_RUN) {
6317 RGWMetaSyncStatusManager sync(store, store->get_async_rados());
6318
6319 int ret = sync.init();
6320 if (ret < 0) {
6321 cerr << "ERROR: sync.init() returned ret=" << ret << std::endl;
6322 return -ret;
6323 }
6324
6325 ret = sync.run();
6326 if (ret < 0) {
6327 cerr << "ERROR: sync.run() returned ret=" << ret << std::endl;
6328 return -ret;
6329 }
6330 }
6331
6332 if (opt_cmd == OPT_DATA_SYNC_STATUS) {
6333 if (source_zone.empty()) {
6334 cerr << "ERROR: source zone not specified" << std::endl;
6335 return EINVAL;
6336 }
6337 RGWDataSyncStatusManager sync(store, store->get_async_rados(), source_zone);
6338
6339 int ret = sync.init();
6340 if (ret < 0) {
6341 cerr << "ERROR: sync.init() returned ret=" << ret << std::endl;
6342 return -ret;
6343 }
6344
6345 rgw_data_sync_status sync_status;
6346 ret = sync.read_sync_status(&sync_status);
6347 if (ret < 0 && ret != -ENOENT) {
6348 cerr << "ERROR: sync.read_sync_status() returned ret=" << ret << std::endl;
6349 return -ret;
6350 }
6351
6352 formatter->open_object_section("summary");
6353 encode_json("sync_status", sync_status, formatter);
6354
6355 uint64_t full_total = 0;
6356 uint64_t full_complete = 0;
6357
6358 for (auto marker_iter : sync_status.sync_markers) {
6359 full_total += marker_iter.second.total_entries;
6360 if (marker_iter.second.state == rgw_meta_sync_marker::SyncState::FullSync) {
6361 full_complete += marker_iter.second.pos;
6362 } else {
6363 full_complete += marker_iter.second.total_entries;
6364 }
6365 }
6366
6367 formatter->open_object_section("full_sync");
6368 encode_json("total", full_total, formatter);
6369 encode_json("complete", full_complete, formatter);
6370 formatter->close_section();
6371 formatter->close_section();
6372
6373 formatter->flush(cout);
6374 }
6375
6376 if (opt_cmd == OPT_DATA_SYNC_INIT) {
6377 if (source_zone.empty()) {
6378 cerr << "ERROR: source zone not specified" << std::endl;
6379 return EINVAL;
6380 }
6381 RGWDataSyncStatusManager sync(store, store->get_async_rados(), source_zone);
6382
6383 int ret = sync.init();
6384 if (ret < 0) {
6385 cerr << "ERROR: sync.init() returned ret=" << ret << std::endl;
6386 return -ret;
6387 }
6388
6389 ret = sync.init_sync_status();
6390 if (ret < 0) {
6391 cerr << "ERROR: sync.init_sync_status() returned ret=" << ret << std::endl;
6392 return -ret;
6393 }
6394 }
6395
6396 if (opt_cmd == OPT_DATA_SYNC_RUN) {
6397 if (source_zone.empty()) {
6398 cerr << "ERROR: source zone not specified" << std::endl;
6399 return EINVAL;
6400 }
6401 RGWDataSyncStatusManager sync(store, store->get_async_rados(), source_zone);
6402
6403 int ret = sync.init();
6404 if (ret < 0) {
6405 cerr << "ERROR: sync.init() returned ret=" << ret << std::endl;
6406 return -ret;
6407 }
6408
6409 ret = sync.run();
6410 if (ret < 0) {
6411 cerr << "ERROR: sync.run() returned ret=" << ret << std::endl;
6412 return -ret;
6413 }
6414 }
6415
6416 if (opt_cmd == OPT_BUCKET_SYNC_INIT) {
6417 if (source_zone.empty()) {
6418 cerr << "ERROR: source zone not specified" << std::endl;
6419 return EINVAL;
6420 }
6421 if (bucket_name.empty()) {
6422 cerr << "ERROR: bucket not specified" << std::endl;
6423 return EINVAL;
6424 }
6425 rgw_bucket bucket;
6426 int ret = init_bucket_for_sync(tenant, bucket_name, bucket_id, bucket);
6427 if (ret < 0) {
6428 return -ret;
6429 }
6430 RGWBucketSyncStatusManager sync(store, source_zone, bucket);
6431
6432 ret = sync.init();
6433 if (ret < 0) {
6434 cerr << "ERROR: sync.init() returned ret=" << ret << std::endl;
6435 return -ret;
6436 }
6437 ret = sync.init_sync_status();
6438 if (ret < 0) {
6439 cerr << "ERROR: sync.init_sync_status() returned ret=" << ret << std::endl;
6440 return -ret;
6441 }
6442 }
6443
c07f9fc5
FG
6444 if ((opt_cmd == OPT_BUCKET_SYNC_DISABLE) || (opt_cmd == OPT_BUCKET_SYNC_ENABLE)) {
6445 if (bucket_name.empty()) {
6446 cerr << "ERROR: bucket not specified" << std::endl;
6447 return EINVAL;
6448 }
6449
6450 if (ret < 0) {
6451 cerr << "could not init realm " << ": " << cpp_strerror(-ret) << std::endl;
6452 return ret;
6453 }
6454 RGWPeriod period;
6455 ret = period.init(g_ceph_context, store, realm_id, realm_name, true);
6456 if (ret < 0) {
6457 cerr << "failed to init period " << ": " << cpp_strerror(-ret) << std::endl;
6458 return ret;
6459 }
6460
6461 if (!store->is_meta_master()) {
6462 cerr << "failed to update bucket sync: only allowed on meta master zone " << std::endl;
6463 cerr << period.get_master_zone() << " | " << period.get_realm() << std::endl;
6464 return EINVAL;
6465 }
6466
6467 rgw_obj obj(bucket, object);
6468 ret = set_bucket_sync_enabled(store, opt_cmd, tenant, bucket_name);
6469 if (ret < 0)
6470 return -ret;
6471}
6472
7c673cae
FG
6473 if (opt_cmd == OPT_BUCKET_SYNC_STATUS) {
6474 if (source_zone.empty()) {
6475 cerr << "ERROR: source zone not specified" << std::endl;
6476 return EINVAL;
6477 }
6478 if (bucket_name.empty()) {
6479 cerr << "ERROR: bucket not specified" << std::endl;
6480 return EINVAL;
6481 }
6482 rgw_bucket bucket;
6483 int ret = init_bucket_for_sync(tenant, bucket_name, bucket_id, bucket);
6484 if (ret < 0) {
6485 return -ret;
6486 }
6487 RGWBucketSyncStatusManager sync(store, source_zone, bucket);
6488
6489 ret = sync.init();
6490 if (ret < 0) {
6491 cerr << "ERROR: sync.init() returned ret=" << ret << std::endl;
6492 return -ret;
6493 }
6494 ret = sync.read_sync_status();
6495 if (ret < 0) {
6496 cerr << "ERROR: sync.read_sync_status() returned ret=" << ret << std::endl;
6497 return -ret;
6498 }
6499
6500 map<int, rgw_bucket_shard_sync_info>& sync_status = sync.get_sync_status();
6501
6502 encode_json("sync_status", sync_status, formatter);
6503 formatter->flush(cout);
6504 }
6505
6506 if (opt_cmd == OPT_BUCKET_SYNC_RUN) {
6507 if (source_zone.empty()) {
6508 cerr << "ERROR: source zone not specified" << std::endl;
6509 return EINVAL;
6510 }
6511 if (bucket_name.empty()) {
6512 cerr << "ERROR: bucket not specified" << std::endl;
6513 return EINVAL;
6514 }
6515 rgw_bucket bucket;
6516 int ret = init_bucket_for_sync(tenant, bucket_name, bucket_id, bucket);
6517 if (ret < 0) {
6518 return -ret;
6519 }
6520 RGWBucketSyncStatusManager sync(store, source_zone, bucket);
6521
6522 ret = sync.init();
6523 if (ret < 0) {
6524 cerr << "ERROR: sync.init() returned ret=" << ret << std::endl;
6525 return -ret;
6526 }
6527
6528 ret = sync.run();
6529 if (ret < 0) {
6530 cerr << "ERROR: sync.run() returned ret=" << ret << std::endl;
6531 return -ret;
6532 }
6533 }
6534
6535 if (opt_cmd == OPT_BILOG_LIST) {
6536 if (bucket_name.empty()) {
6537 cerr << "ERROR: bucket not specified" << std::endl;
6538 return EINVAL;
6539 }
6540 RGWBucketInfo bucket_info;
6541 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
6542 if (ret < 0) {
6543 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
6544 return -ret;
6545 }
6546 formatter->open_array_section("entries");
6547 bool truncated;
6548 int count = 0;
6549 if (max_entries < 0)
6550 max_entries = 1000;
6551
6552 do {
6553 list<rgw_bi_log_entry> entries;
6554 ret = store->list_bi_log_entries(bucket_info, shard_id, marker, max_entries - count, entries, &truncated);
6555 if (ret < 0) {
6556 cerr << "ERROR: list_bi_log_entries(): " << cpp_strerror(-ret) << std::endl;
6557 return -ret;
6558 }
6559
6560 count += entries.size();
6561
6562 for (list<rgw_bi_log_entry>::iterator iter = entries.begin(); iter != entries.end(); ++iter) {
6563 rgw_bi_log_entry& entry = *iter;
6564 encode_json("entry", entry, formatter);
6565
6566 marker = entry.id;
6567 }
6568 formatter->flush(cout);
6569 } while (truncated && count < max_entries);
6570
6571 formatter->close_section();
6572 formatter->flush(cout);
6573 }
6574
6575 if (opt_cmd == OPT_SYNC_ERROR_LIST) {
6576 if (max_entries < 0) {
6577 max_entries = 1000;
6578 }
6579
6580 bool truncated;
6581 utime_t start_time, end_time;
6582
6583 int ret = parse_date_str(start_date, start_time);
6584 if (ret < 0)
6585 return -ret;
6586
6587 ret = parse_date_str(end_date, end_time);
6588 if (ret < 0)
6589 return -ret;
6590
6591 if (shard_id < 0) {
6592 shard_id = 0;
6593 }
6594
6595 formatter->open_array_section("entries");
6596
6597 for (; shard_id < ERROR_LOGGER_SHARDS; ++shard_id) {
6598 formatter->open_object_section("shard");
6599 encode_json("shard_id", shard_id, formatter);
6600 formatter->open_array_section("entries");
6601
6602 int count = 0;
6603 string oid = RGWSyncErrorLogger::get_shard_oid(RGW_SYNC_ERROR_LOG_SHARD_PREFIX, shard_id);
6604
6605 do {
6606 list<cls_log_entry> entries;
6607 ret = store->time_log_list(oid, start_time.to_real_time(), end_time.to_real_time(),
6608 max_entries - count, entries, marker, &marker, &truncated);
6609 if (ret == -ENOENT) {
6610 break;
6611 }
6612 if (ret < 0) {
6613 cerr << "ERROR: store->time_log_list(): " << cpp_strerror(-ret) << std::endl;
6614 return -ret;
6615 }
6616
6617 count += entries.size();
6618
6619 for (auto& cls_entry : entries) {
6620 rgw_sync_error_info log_entry;
6621
6622 auto iter = cls_entry.data.begin();
6623 try {
6624 ::decode(log_entry, iter);
6625 } catch (buffer::error& err) {
6626 cerr << "ERROR: failed to decode log entry" << std::endl;
6627 continue;
6628 }
6629 formatter->open_object_section("entry");
6630 encode_json("id", cls_entry.id, formatter);
6631 encode_json("section", cls_entry.section, formatter);
6632 encode_json("name", cls_entry.name, formatter);
6633 encode_json("timestamp", cls_entry.timestamp, formatter);
6634 encode_json("info", log_entry, formatter);
6635 formatter->close_section();
6636 formatter->flush(cout);
6637 }
6638 } while (truncated && count < max_entries);
6639
6640 formatter->close_section();
6641 formatter->close_section();
6642
6643 if (specified_shard_id) {
6644 break;
6645 }
6646 }
6647
6648 formatter->close_section();
6649 formatter->flush(cout);
6650 }
6651
6652 if (opt_cmd == OPT_BILOG_TRIM) {
6653 if (bucket_name.empty()) {
6654 cerr << "ERROR: bucket not specified" << std::endl;
6655 return EINVAL;
6656 }
6657 RGWBucketInfo bucket_info;
6658 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
6659 if (ret < 0) {
6660 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
6661 return -ret;
6662 }
6663 ret = store->trim_bi_log_entries(bucket_info, shard_id, start_marker, end_marker);
6664 if (ret < 0) {
6665 cerr << "ERROR: trim_bi_log_entries(): " << cpp_strerror(-ret) << std::endl;
6666 return -ret;
6667 }
6668 }
6669
6670 if (opt_cmd == OPT_BILOG_STATUS) {
6671 if (bucket_name.empty()) {
6672 cerr << "ERROR: bucket not specified" << std::endl;
6673 return EINVAL;
6674 }
6675 RGWBucketInfo bucket_info;
6676 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
6677 if (ret < 0) {
6678 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
6679 return -ret;
6680 }
6681 map<int, string> markers;
6682 ret = store->get_bi_log_status(bucket_info, shard_id, markers);
6683 if (ret < 0) {
31f18b77 6684 cerr << "ERROR: get_bi_log_status(): " << cpp_strerror(-ret) << std::endl;
7c673cae
FG
6685 return -ret;
6686 }
6687 formatter->open_object_section("entries");
6688 encode_json("markers", markers, formatter);
6689 formatter->close_section();
6690 formatter->flush(cout);
6691 }
6692
6693 if (opt_cmd == OPT_DATALOG_LIST) {
6694 formatter->open_array_section("entries");
6695 bool truncated;
6696 int count = 0;
6697 if (max_entries < 0)
6698 max_entries = 1000;
6699
6700 utime_t start_time, end_time;
6701
6702 int ret = parse_date_str(start_date, start_time);
6703 if (ret < 0)
6704 return -ret;
6705
6706 ret = parse_date_str(end_date, end_time);
6707 if (ret < 0)
6708 return -ret;
6709
6710 RGWDataChangesLog *log = store->data_log;
6711 RGWDataChangesLog::LogMarker marker;
6712
6713 do {
6714 list<rgw_data_change_log_entry> entries;
6715 ret = log->list_entries(start_time.to_real_time(), end_time.to_real_time(), max_entries - count, entries, marker, &truncated);
6716 if (ret < 0) {
6717 cerr << "ERROR: list_bi_log_entries(): " << cpp_strerror(-ret) << std::endl;
6718 return -ret;
6719 }
6720
6721 count += entries.size();
6722
6723 for (list<rgw_data_change_log_entry>::iterator iter = entries.begin(); iter != entries.end(); ++iter) {
6724 rgw_data_change_log_entry& entry = *iter;
6725 if (!extra_info) {
6726 encode_json("entry", entry.entry, formatter);
6727 } else {
6728 encode_json("entry", entry, formatter);
6729 }
6730 }
6731 formatter->flush(cout);
6732 } while (truncated && count < max_entries);
6733
6734 formatter->close_section();
6735 formatter->flush(cout);
6736 }
6737
6738 if (opt_cmd == OPT_DATALOG_STATUS) {
6739 RGWDataChangesLog *log = store->data_log;
6740 int i = (specified_shard_id ? shard_id : 0);
6741
6742 formatter->open_array_section("entries");
6743 for (; i < g_ceph_context->_conf->rgw_data_log_num_shards; i++) {
6744 list<cls_log_entry> entries;
6745
6746 RGWDataChangesLogInfo info;
6747 log->get_info(i, &info);
6748
6749 ::encode_json("info", info, formatter);
6750
6751 if (specified_shard_id)
6752 break;
6753 }
6754
6755 formatter->close_section();
6756 formatter->flush(cout);
6757 }
6758
6759 if (opt_cmd == OPT_DATALOG_TRIM) {
6760 utime_t start_time, end_time;
6761
6762 int ret = parse_date_str(start_date, start_time);
6763 if (ret < 0)
6764 return -ret;
6765
6766 ret = parse_date_str(end_date, end_time);
6767 if (ret < 0)
6768 return -ret;
6769
6770 RGWDataChangesLog *log = store->data_log;
6771 ret = log->trim_entries(start_time.to_real_time(), end_time.to_real_time(), start_marker, end_marker);
6772 if (ret < 0) {
6773 cerr << "ERROR: trim_entries(): " << cpp_strerror(-ret) << std::endl;
6774 return -ret;
6775 }
6776 }
6777
6778 if (opt_cmd == OPT_OPSTATE_LIST) {
6779 RGWOpState oc(store);
6780
6781 int max = 1000;
6782
6783 void *handle;
6784 oc.init_list_entries(client_id, op_id, object, &handle);
6785 list<cls_statelog_entry> entries;
6786 bool done;
6787 formatter->open_array_section("entries");
6788 do {
6789 int ret = oc.list_entries(handle, max, entries, &done);
6790 if (ret < 0) {
6791 cerr << "oc.list_entries returned " << cpp_strerror(-ret) << std::endl;
6792 oc.finish_list_entries(handle);
6793 return -ret;
6794 }
6795
6796 for (list<cls_statelog_entry>::iterator iter = entries.begin(); iter != entries.end(); ++iter) {
6797 oc.dump_entry(*iter, formatter);
6798 }
6799
6800 formatter->flush(cout);
6801 } while (!done);
6802 formatter->close_section();
6803 formatter->flush(cout);
6804 oc.finish_list_entries(handle);
6805 }
6806
6807 if (opt_cmd == OPT_OPSTATE_SET || opt_cmd == OPT_OPSTATE_RENEW) {
6808 RGWOpState oc(store);
6809
6810 RGWOpState::OpState state;
6811 if (object.empty() || client_id.empty() || op_id.empty()) {
6812 cerr << "ERROR: need to specify client_id, op_id, and object" << std::endl;
6813 return EINVAL;
6814 }
6815 if (state_str.empty()) {
6816 cerr << "ERROR: state was not specified" << std::endl;
6817 return EINVAL;
6818 }
6819 int ret = oc.state_from_str(state_str, &state);
6820 if (ret < 0) {
6821 cerr << "ERROR: invalid state: " << state_str << std::endl;
6822 return -ret;
6823 }
6824
6825 if (opt_cmd == OPT_OPSTATE_SET) {
6826 ret = oc.set_state(client_id, op_id, object, state);
6827 if (ret < 0) {
6828 cerr << "ERROR: failed to set state: " << cpp_strerror(-ret) << std::endl;
6829 return -ret;
6830 }
6831 } else {
6832 ret = oc.renew_state(client_id, op_id, object, state);
6833 if (ret < 0) {
6834 cerr << "ERROR: failed to renew state: " << cpp_strerror(-ret) << std::endl;
6835 return -ret;
6836 }
6837 }
6838 }
6839 if (opt_cmd == OPT_OPSTATE_RM) {
6840 RGWOpState oc(store);
6841
6842 if (object.empty() || client_id.empty() || op_id.empty()) {
6843 cerr << "ERROR: need to specify client_id, op_id, and object" << std::endl;
6844 return EINVAL;
6845 }
6846 ret = oc.remove_entry(client_id, op_id, object);
6847 if (ret < 0) {
6848 cerr << "ERROR: failed to set state: " << cpp_strerror(-ret) << std::endl;
6849 return -ret;
6850 }
6851 }
6852
6853 if (opt_cmd == OPT_REPLICALOG_GET || opt_cmd == OPT_REPLICALOG_UPDATE ||
6854 opt_cmd == OPT_REPLICALOG_DELETE) {
6855 if (replica_log_type_str.empty()) {
6856 cerr << "ERROR: need to specify --replica-log-type=<metadata | data | bucket>" << std::endl;
6857 return EINVAL;
6858 }
6859 }
6860
6861 if (opt_cmd == OPT_REPLICALOG_GET) {
6862 RGWReplicaBounds bounds;
6863 if (replica_log_type == ReplicaLog_Metadata) {
6864 if (!specified_shard_id) {
6865 cerr << "ERROR: shard-id must be specified for get operation" << std::endl;
6866 return EINVAL;
6867 }
6868
6869 RGWReplicaObjectLogger logger(store, pool, META_REPLICA_LOG_OBJ_PREFIX);
6870 int ret = logger.get_bounds(shard_id, bounds);
6871 if (ret < 0)
6872 return -ret;
6873 } else if (replica_log_type == ReplicaLog_Data) {
6874 if (!specified_shard_id) {
6875 cerr << "ERROR: shard-id must be specified for get operation" << std::endl;
6876 return EINVAL;
6877 }
6878 RGWReplicaObjectLogger logger(store, pool, DATA_REPLICA_LOG_OBJ_PREFIX);
6879 int ret = logger.get_bounds(shard_id, bounds);
6880 if (ret < 0)
6881 return -ret;
6882 } else if (replica_log_type == ReplicaLog_Bucket) {
6883 if (bucket_name.empty()) {
6884 cerr << "ERROR: bucket not specified" << std::endl;
6885 return EINVAL;
6886 }
6887 RGWBucketInfo bucket_info;
6888 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
6889 if (ret < 0) {
6890 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
6891 return -ret;
6892 }
6893
6894 RGWReplicaBucketLogger logger(store);
6895 ret = logger.get_bounds(bucket, shard_id, bounds);
6896 if (ret < 0)
6897 return -ret;
6898 } else { // shouldn't get here
6899 ceph_abort();
6900 }
6901 encode_json("bounds", bounds, formatter);
6902 formatter->flush(cout);
6903 cout << std::endl;
6904 }
6905
6906 if (opt_cmd == OPT_REPLICALOG_DELETE) {
6907 if (replica_log_type == ReplicaLog_Metadata) {
6908 if (!specified_shard_id) {
6909 cerr << "ERROR: shard-id must be specified for delete operation" << std::endl;
6910 return EINVAL;
6911 }
6912 if (!specified_daemon_id) {
6913 cerr << "ERROR: daemon-id must be specified for delete operation" << std::endl;
6914 return EINVAL;
6915 }
6916 RGWReplicaObjectLogger logger(store, pool, META_REPLICA_LOG_OBJ_PREFIX);
6917 int ret = logger.delete_bound(shard_id, daemon_id, false);
6918 if (ret < 0)
6919 return -ret;
6920 } else if (replica_log_type == ReplicaLog_Data) {
6921 if (!specified_shard_id) {
6922 cerr << "ERROR: shard-id must be specified for delete operation" << std::endl;
6923 return EINVAL;
6924 }
6925 if (!specified_daemon_id) {
6926 cerr << "ERROR: daemon-id must be specified for delete operation" << std::endl;
6927 return EINVAL;
6928 }
6929 RGWReplicaObjectLogger logger(store, pool, DATA_REPLICA_LOG_OBJ_PREFIX);
6930 int ret = logger.delete_bound(shard_id, daemon_id, false);
6931 if (ret < 0)
6932 return -ret;
6933 } else if (replica_log_type == ReplicaLog_Bucket) {
6934 if (bucket_name.empty()) {
6935 cerr << "ERROR: bucket not specified" << std::endl;
6936 return EINVAL;
6937 }
6938 RGWBucketInfo bucket_info;
6939 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
6940 if (ret < 0) {
6941 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
6942 return -ret;
6943 }
6944
6945 RGWReplicaBucketLogger logger(store);
6946 ret = logger.delete_bound(bucket, shard_id, daemon_id, false);
6947 if (ret < 0)
6948 return -ret;
6949 }
6950 }
6951
6952 if (opt_cmd == OPT_REPLICALOG_UPDATE) {
6953 if (marker.empty()) {
6954 cerr << "ERROR: marker was not specified" <<std::endl;
6955 return EINVAL;
6956 }
6957 utime_t time = ceph_clock_now();
6958 if (!date.empty()) {
6959 ret = parse_date_str(date, time);
6960 if (ret < 0) {
6961 cerr << "ERROR: failed to parse start date" << std::endl;
6962 return EINVAL;
6963 }
6964 }
6965 list<RGWReplicaItemMarker> entries;
6966 int ret = read_decode_json(infile, entries);
6967 if (ret < 0) {
6968 cerr << "ERROR: failed to decode entries" << std::endl;
6969 return EINVAL;
6970 }
6971 RGWReplicaBounds bounds;
6972 if (replica_log_type == ReplicaLog_Metadata) {
6973 if (!specified_shard_id) {
6974 cerr << "ERROR: shard-id must be specified for get operation" << std::endl;
6975 return EINVAL;
6976 }
6977
6978 RGWReplicaObjectLogger logger(store, pool, META_REPLICA_LOG_OBJ_PREFIX);
6979 int ret = logger.update_bound(shard_id, daemon_id, marker, time, &entries);
6980 if (ret < 0) {
6981 cerr << "ERROR: failed to update bounds: " << cpp_strerror(-ret) << std::endl;
6982 return -ret;
6983 }
6984 } else if (replica_log_type == ReplicaLog_Data) {
6985 if (!specified_shard_id) {
6986 cerr << "ERROR: shard-id must be specified for get operation" << std::endl;
6987 return EINVAL;
6988 }
6989 RGWReplicaObjectLogger logger(store, pool, DATA_REPLICA_LOG_OBJ_PREFIX);
6990 int ret = logger.update_bound(shard_id, daemon_id, marker, time, &entries);
6991 if (ret < 0) {
6992 cerr << "ERROR: failed to update bounds: " << cpp_strerror(-ret) << std::endl;
6993 return -ret;
6994 }
6995 } else if (replica_log_type == ReplicaLog_Bucket) {
6996 if (bucket_name.empty()) {
6997 cerr << "ERROR: bucket not specified" << std::endl;
6998 return EINVAL;
6999 }
7000 RGWBucketInfo bucket_info;
7001 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
7002 if (ret < 0) {
7003 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
7004 return -ret;
7005 }
7006
7007 RGWReplicaBucketLogger logger(store);
7008 ret = logger.update_bound(bucket, shard_id, daemon_id, marker, time, &entries);
7009 if (ret < 0) {
7010 cerr << "ERROR: failed to update bounds: " << cpp_strerror(-ret) << std::endl;
7011 return -ret;
7012 }
7013 }
7014 }
7015
7016 bool quota_op = (opt_cmd == OPT_QUOTA_SET || opt_cmd == OPT_QUOTA_ENABLE || opt_cmd == OPT_QUOTA_DISABLE);
7017
7018 if (quota_op) {
7019 if (bucket_name.empty() && user_id.empty()) {
7020 cerr << "ERROR: bucket name or uid is required for quota operation" << std::endl;
7021 return EINVAL;
7022 }
7023
7024 if (!bucket_name.empty()) {
7025 if (!quota_scope.empty() && quota_scope != "bucket") {
7026 cerr << "ERROR: invalid quota scope specification." << std::endl;
7027 return EINVAL;
7028 }
7029 set_bucket_quota(store, opt_cmd, tenant, bucket_name,
7030 max_size, max_objects, have_max_size, have_max_objects);
7031 } else if (!user_id.empty()) {
7032 if (quota_scope == "bucket") {
7033 set_user_bucket_quota(opt_cmd, user, user_op, max_size, max_objects, have_max_size, have_max_objects);
7034 } else if (quota_scope == "user") {
7035 set_user_quota(opt_cmd, user, user_op, max_size, max_objects, have_max_size, have_max_objects);
7036 } else {
7037 cerr << "ERROR: invalid quota scope specification. Please specify either --quota-scope=bucket, or --quota-scope=user" << std::endl;
7038 return EINVAL;
7039 }
7040 }
7041 }
7042
7043 return 0;
7044}