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<std::string, std::string>&& 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;
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<std::string, std::string>* 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<std::string, std::string> 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;
}
} 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<librbd::mirror_peer_t> &peers,
bool config_key) {
for (auto &peer : peers) {
std::map<std::string, std::string> 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;
}
}
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;
}
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) {
} else if (key == "cluster") {
r = rbd.mirror_peer_set_cluster(io_ctx, uuid.c_str(), value.c_str());
} else {
- std::vector<librbd::mirror_peer_t> 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;
}
}
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<bool>());
if (r < 0) {
return r;