From 6c3640c4773bb9864b8341b68b3ba2a73715f9f0 Mon Sep 17 00:00:00 2001 From: Jason Dillaman Date: Wed, 14 Nov 2018 21:56:51 +0530 Subject: [PATCH] rbd: switch to use new mirror peer attribute API methods Signed-off-by: Jason Dillaman --- src/tools/rbd/action/MirrorPool.cc | 128 ++++++----------------------- 1 file changed, 23 insertions(+), 105 deletions(-) diff --git a/src/tools/rbd/action/MirrorPool.cc b/src/tools/rbd/action/MirrorPool.cc index 8b2efaded97..e477d959423 100644 --- a/src/tools/rbd/action/MirrorPool.cc +++ b/src/tools/rbd/action/MirrorPool.cc @@ -146,54 +146,10 @@ int get_remote_cluster_spec(const po::variables_map &vm, return 0; } -std::string get_peer_config_key_name(int64_t pool_id, - const std::string& peer_uuid) { - return RBD_MIRROR_PEER_CONFIG_KEY_PREFIX + stringify(pool_id) + "/" + - peer_uuid; -} - -int remove_peer_config_key(librados::Rados& rados, int64_t pool_id, - const std::string& peer_uuid) { - std::string cmd = - "{" - "\"prefix\": \"config-key rm\", " - "\"key\": \"" + get_peer_config_key_name(pool_id, peer_uuid) + "\"" - "}"; - bufferlist in_bl; - bufferlist out_bl; - int r = rados.mon_command(cmd, in_bl, &out_bl, nullptr); - if (r == -EPERM) { - } else if (r < 0 && r != -ENOENT) { - std::cerr << "rbd: failed to remove mirroring peer config: " - << cpp_strerror(r) << std::endl; - return r; - } - return 0; -} - -int set_peer_config_key(librados::Rados& rados, int64_t pool_id, - const std::string& peer_uuid, +int set_peer_config_key(librados::IoCtx& io_ctx, const std::string& peer_uuid, std::map&& attributes) { - std::stringstream ss; - ss << "{"; - for (auto& pair : attributes) { - ss << "\\\"" << pair.first << "\\\": " - << "\\\"" << pair.second << "\\\""; - if (&pair != &(*attributes.rbegin())) { - ss << ", "; - } - } - ss << "}"; - - std::string cmd = - "{" - "\"prefix\": \"config-key set\", " - "\"key\": \"" + get_peer_config_key_name(pool_id, peer_uuid) + "\", " - "\"val\": \"" + ss.str() + "\"" - "}"; - bufferlist in_bl; - bufferlist out_bl; - int r = rados.mon_command(cmd, in_bl, &out_bl, nullptr); + librbd::RBD rbd; + int r = rbd.mirror_peer_set_attributes(io_ctx, peer_uuid, attributes); if (r == -EPERM) { std::cerr << "rbd: permission denied attempting to set peer " << "config-key secrets in the monitor" << std::endl; @@ -206,58 +162,36 @@ int set_peer_config_key(librados::Rados& rados, int64_t pool_id, return 0; } -int get_peer_config_key(librados::Rados& rados, int64_t pool_id, - const std::string& peer_uuid, +int get_peer_config_key(librados::IoCtx& io_ctx, const std::string& peer_uuid, std::map* attributes) { - std::string cmd = - "{" - "\"prefix\": \"config-key get\", " - "\"key\": \"" + get_peer_config_key_name(pool_id, peer_uuid) + "\"" - "}"; - - bufferlist in_bl; - bufferlist out_bl; - int r = rados.mon_command(cmd, in_bl, &out_bl, nullptr); - if (r == -EPERM) { + librbd::RBD rbd; + int r = rbd.mirror_peer_get_attributes(io_ctx, peer_uuid, attributes); + if (r == -ENOENT) { + return r; + } else if (r == -EPERM) { std::cerr << "rbd: permission denied attempting to access peer " << "config-key secrets from the monitor" << std::endl; return r; - } else if (r == -ENOENT || out_bl.length() == 0) { - return -ENOENT; + } else if (r == -EINVAL) { + std::cerr << "rbd: corrupt mirroring peer config" << std::endl; + return r; } else if (r < 0) { std::cerr << "rbd: error reading mirroring peer config: " << cpp_strerror(r) << std::endl; return r; } - bool json_valid = false; - json_spirit::mValue json_root; - if(json_spirit::read(out_bl.to_str(), json_root)) { - try { - auto& json_obj = json_root.get_obj(); - for (auto& pairs : json_obj) { - (*attributes)[pairs.first] = pairs.second.get_str(); - } - json_valid = true; - } catch (std::runtime_error&) { - } - } - - if (!json_valid) { - std::cerr << "rbd: corrupt mirroring peer config" << std::endl; - return -EINVAL; - } return 0; } -int update_peer_config_key(librados::Rados& rados, int64_t pool_id, +int update_peer_config_key(librados::IoCtx& io_ctx, const std::string& peer_uuid, const std::string& key, const std::string& value) { std::map attributes; - int r = get_peer_config_key(rados, pool_id, peer_uuid, &attributes); + int r = get_peer_config_key(io_ctx, peer_uuid, &attributes); if (r == -ENOENT) { - return set_peer_config_key(rados, pool_id, peer_uuid, {{key, value}}); + return set_peer_config_key(io_ctx, peer_uuid, {{key, value}}); } else if (r < 0) { return r; } @@ -267,10 +201,10 @@ int update_peer_config_key(librados::Rados& rados, int64_t pool_id, } else { attributes[key] = value; } - return set_peer_config_key(rados, pool_id, peer_uuid, std::move(attributes)); + return set_peer_config_key(io_ctx, peer_uuid, std::move(attributes)); } -int format_mirror_peers(librados::Rados& rados, int64_t pool_id, +int format_mirror_peers(librados::IoCtx& io_ctx, at::Format::Formatter formatter, const std::vector &peers, bool config_key) { @@ -296,7 +230,7 @@ int format_mirror_peers(librados::Rados& rados, int64_t pool_id, for (auto &peer : peers) { std::map attributes; if (config_key) { - int r = get_peer_config_key(rados, pool_id, peer.uuid, &attributes); + int r = get_peer_config_key(io_ctx, peer.uuid, &attributes); if (r < 0 && r != -ENOENT) { return r; } @@ -782,8 +716,7 @@ int execute_peer_add(const po::variables_map &vm, } if (!attributes.empty()) { - r = set_peer_config_key(rados, io_ctx.get_id(), uuid, - std::move(attributes)); + r = set_peer_config_key(io_ctx, uuid, std::move(attributes)); if (r < 0) { return r; } @@ -823,11 +756,6 @@ int execute_peer_remove(const po::variables_map &vm, return r; } - r = remove_peer_config_key(rados, io_ctx.get_id(), uuid); - if (r < 0) { - return r; - } - librbd::RBD rbd; r = rbd.mirror_peer_remove(io_ctx, uuid); if (r < 0) { @@ -904,23 +832,13 @@ int execute_peer_set(const po::variables_map &vm, } else if (key == "cluster") { r = rbd.mirror_peer_set_cluster(io_ctx, uuid.c_str(), value.c_str()); } else { - std::vector mirror_peers; - r = rbd.mirror_peer_list(io_ctx, &mirror_peers); - if (r < 0) { - std::cerr << "rbd: failed to list mirror peers" << std::endl; - return r; - } - - if (std::find_if(mirror_peers.begin(), mirror_peers.end(), - [&uuid](const librbd::mirror_peer_t& peer) { - return uuid == peer.uuid; - }) == mirror_peers.end()) { + r = update_peer_config_key(io_ctx, uuid, key, value); + if (r == -ENOENT) { std::cerr << "rbd: mirror peer " << uuid << " does not exist" << std::endl; - return -ENOENT; } - r = update_peer_config_key(rados, io_ctx.get_id(), uuid, key, value); } + if (r < 0) { return r; } @@ -1077,7 +995,7 @@ int execute_info(const po::variables_map &vm, } if (mirror_mode != RBD_MIRROR_MODE_DISABLED) { - r = format_mirror_peers(rados, io_ctx.get_id(), formatter, mirror_peers, + r = format_mirror_peers(io_ctx, formatter, mirror_peers, vm[ALL_NAME].as()); if (r < 0) { return r; -- 2.39.5