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