| 199 | |
| 200 | |
| 201 | Future<bool> LocalResourceProviderDaemonProcess::add( |
| 202 | const ResourceProviderInfo& info) |
| 203 | { |
| 204 | CHECK(!info.has_id()); // Should have already been validated by the agent. |
| 205 | |
| 206 | if (configDir.isNone()) { |
| 207 | return Failure("Missing required flag --resource_provider_config_dir"); |
| 208 | } |
| 209 | |
| 210 | // Return true if the info has been added for idempotency. |
| 211 | if (providers[info.type()].contains(info.name())) { |
| 212 | const ProviderData& data = providers[info.type()].at(info.name()); |
| 213 | |
| 214 | if (data.removing.isSome()) { |
| 215 | return Failure( |
| 216 | "Failed to add resource provider with type '" + info.type() + |
| 217 | "' and name '" + info.name() + "' as a removal is still in progress"); |
| 218 | } |
| 219 | |
| 220 | return data.info == info; |
| 221 | } |
| 222 | |
| 223 | // Generate a filename for the config. |
| 224 | // NOTE: We use the following template `<type>.<name>.<uuid>.json` |
| 225 | // with a random UUID to generate a new filename to avoid any conflict |
| 226 | // with existing ad-hoc config files. |
| 227 | const string path = path::join( |
| 228 | configDir.get(), |
| 229 | strings::join(".", info.type(), info.name(), id::UUID::random(), "json")); |
| 230 | |
| 231 | LOG(INFO) << "Creating new config file '" << path << "'"; |
| 232 | |
| 233 | Try<Nothing> _save = save(path, info); |
| 234 | if (_save.isError()) { |
| 235 | return Failure( |
| 236 | "Failed to write config file '" + path + "': " + _save.error()); |
| 237 | } |
| 238 | |
| 239 | providers[info.type()].put(info.name(), {path, info}); |
| 240 | |
| 241 | // Launch the resource provider if the daemon is already started. |
| 242 | if (slaveId.isSome()) { |
| 243 | auto err = [](const ResourceProviderInfo& info, const string& message) { |
| 244 | LOG(ERROR) |
| 245 | << "Failed to launch resource provider with type '" << info.type() |
| 246 | << "' and name '" << info.name() << "': " << message; |
| 247 | }; |
| 248 | |
| 249 | launch(info.type(), info.name()) |
| 250 | .onFailed(std::bind(err, info, lambda::_1)) |
| 251 | .onDiscarded(std::bind(err, info, "future discarded")); |
| 252 | } |
| 253 | |
| 254 | return true; |
| 255 | } |
| 256 | |
| 257 | |
| 258 | Future<bool> LocalResourceProviderDaemonProcess::update( |
no test coverage detected