| 562 | |
| 563 | |
| 564 | Future<Nothing> LocalResourceProviderDaemonProcess::cleanupContainers( |
| 565 | const ResourceProviderInfo& info, |
| 566 | const Option<string>& authToken) |
| 567 | { |
| 568 | const Principal principal = LocalResourceProvider::principal(info); |
| 569 | CHECK(principal.claims.contains("cid_prefix")); |
| 570 | |
| 571 | const string& cidPrefix = principal.claims.at("cid_prefix"); |
| 572 | |
| 573 | LOG(INFO) << "Cleaning up containers prefixed by '" << cidPrefix << "'"; |
| 574 | |
| 575 | // TODO(chhsiao): Consider using a more reliable way to get the v1 endpoint. |
| 576 | http::URL agentUrl = url; |
| 577 | agentUrl.path = Path(url.path).dirname(); |
| 578 | |
| 579 | http::Headers headers{{"Accept", stringify(ContentType::PROTOBUF)}}; |
| 580 | if (authToken.isSome()) { |
| 581 | headers["Authorization"] = "Bearer " + authToken.get(); |
| 582 | } |
| 583 | |
| 584 | agent::Call call; |
| 585 | call.set_type(agent::Call::GET_CONTAINERS); |
| 586 | call.mutable_get_containers()->set_show_nested(false); |
| 587 | call.mutable_get_containers()->set_show_standalone(true); |
| 588 | |
| 589 | return http::post( |
| 590 | agentUrl, |
| 591 | headers, |
| 592 | serialize(ContentType::PROTOBUF, evolve(call)), |
| 593 | stringify(ContentType::PROTOBUF)) |
| 594 | .then(defer(self(), [=]( |
| 595 | const http::Response& httpResponse) -> Future<Nothing> { |
| 596 | if (httpResponse.status != http::OK().status) { |
| 597 | return Failure( |
| 598 | "Failed to get containers: Unexpected response '" + |
| 599 | httpResponse.status + "' (" + httpResponse.body + ")"); |
| 600 | } |
| 601 | |
| 602 | Try<v1::agent::Response> v1Response = deserialize<v1::agent::Response>( |
| 603 | ContentType::PROTOBUF, httpResponse.body); |
| 604 | if (v1Response.isError()) { |
| 605 | return Failure("Failed to get containers: " + v1Response.error()); |
| 606 | } |
| 607 | |
| 608 | vector<Future<Nothing>> futures; |
| 609 | |
| 610 | agent::Response response = devolve(v1Response.get()); |
| 611 | foreach (const agent::Response::GetContainers::Container& container, |
| 612 | response.get_containers().containers()) { |
| 613 | const ContainerID& containerId = container.container_id(); |
| 614 | |
| 615 | if (!strings::startsWith(containerId.value(), cidPrefix)) { |
| 616 | continue; |
| 617 | } |
| 618 | |
| 619 | // NOTE: We skip containers that are not actually running by checking |
| 620 | // their `executor_pid`s to avoid `ESRCH` errors or killing an arbitrary |
| 621 | // process. But we might skip the ones that are being launched as well. |
nothing calls this directly
no test coverage detected