Skip to content

Commit

Permalink
Restore luci downloading to fetch_cvd
Browse files Browse the repository at this point in the history
This was originally added in PR #458 but clobbered in an import at PR #478.

Also updates the changelog to mention this feature.

Bug: b/322559348
Test: b/322559348#comment4
  • Loading branch information
Databean committed Apr 8, 2024
1 parent dbe1aa7 commit edd6cd8
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 4 deletions.
69 changes: 65 additions & 4 deletions base/cvd/cuttlefish/host/commands/cvd/fetch/fetch_cvd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "common/libs/fs/shared_buf.h"
#include "common/libs/fs/shared_fd.h"
#include "common/libs/utils/archive.h"
#include "common/libs/utils/contains.h"
#include "common/libs/utils/environment.h"
#include "common/libs/utils/files.h"
#include "common/libs/utils/result.h"
Expand All @@ -44,8 +45,10 @@
#include "host/libs/image_aggregator/sparse_image_utils.h"
#include "host/libs/web/android_build_api.h"
#include "host/libs/web/android_build_string.h"
#include "host/libs/web/chrome_os_build_string.h"
#include "host/libs/web/credential_source.h"
#include "host/libs/web/http_client/http_client.h"
#include "host/libs/web/luci_build_api.h"

namespace cuttlefish {
namespace {
Expand All @@ -62,6 +65,7 @@ struct BuildStrings {
std::optional<BuildString> android_efi_loader_build;
std::optional<BuildString> otatools_build;
std::optional<BuildString> host_package_build;
std::optional<ChromeOsBuildString> chrome_os_build;
};

struct DownloadFlags {
Expand All @@ -74,6 +78,7 @@ struct TargetDirectories {
std::string otatools;
std::string default_target_files;
std::string system_target_files;
std::string chrome_os;
};

struct Builds {
Expand All @@ -84,6 +89,7 @@ struct Builds {
std::optional<Build> bootloader;
std::optional<Build> android_efi_loader;
std::optional<Build> otatools;
std::optional<ChromeOsBuildString> chrome_os;
};

struct Target {
Expand Down Expand Up @@ -128,6 +134,8 @@ BuildStrings GetBuildStrings(const VectorFlags& flags, const int index) {
flags.android_efi_loader_build, index, std::nullopt),
.otatools_build = AccessOrDefault<std::optional<BuildString>>(
flags.otatools_build, index, std::nullopt),
.chrome_os_build = AccessOrDefault<std::optional<ChromeOsBuildString>>(
flags.chrome_os_build, index, std::nullopt),
};
auto possible_boot_artifact =
AccessOrDefault<std::string>(flags.boot_artifact, index, "");
Expand Down Expand Up @@ -160,7 +168,8 @@ TargetDirectories GetTargetDirectories(
return TargetDirectories{.root = base_directory,
.otatools = base_directory + "/otatools/",
.default_target_files = base_directory + "/default",
.system_target_files = base_directory + "/system"};
.system_target_files = base_directory + "/system",
.chrome_os = base_directory + "/chromeos"};
}

#ifndef O_BINARY
Expand Down Expand Up @@ -276,6 +285,7 @@ Result<void> EnsureDirectoriesExist(const std::string& target_directory,
kRwxAllMode));
CF_EXPECT(EnsureDirectoryExists(target.directories.system_target_files,
kRwxAllMode));
CF_EXPECT(EnsureDirectoryExists(target.directories.chrome_os, kRwxAllMode));
}
return {};
}
Expand Down Expand Up @@ -313,6 +323,37 @@ Result<BuildApi> GetBuildApi(const BuildApiFlags& flags) {
flags.wait_retry_period, flags.api_base_url);
}

Result<LuciBuildApi> GetLuciBuildApi(const BuildApiFlags& flags) {
auto resolver =
flags.external_dns_resolver ? GetEntDnsResolve : NameResolver();
const bool use_logging_debug_function = true;
std::unique_ptr<HttpClient> curl =
HttpClient::CurlClient(resolver, use_logging_debug_function);
std::unique_ptr<HttpClient> retrying_http_client =
HttpClient::ServerErrorRetryClient(*curl, 10,
std::chrono::milliseconds(5000));
std::string luci_oauth_filepath =
StringFromEnv("HOME", ".") + "/.config/chrome_infra/auth/tokens.json";
std::unique_ptr<CredentialSource> luci_credential_source =
CF_EXPECT(GetCredentialSource(
*retrying_http_client, flags.credential_source, luci_oauth_filepath,
flags.credential_flags.use_gce_metadata,
flags.credential_flags.credential_filepath,
flags.credential_flags.service_account_filepath));

std::string gsutil_oauth_filepath = StringFromEnv("HOME", ".") + "/.boto";
std::unique_ptr<CredentialSource> gsutil_credential_source =
CF_EXPECT(GetCredentialSource(
*retrying_http_client, flags.credential_source, gsutil_oauth_filepath,
flags.credential_flags.use_gce_metadata,
flags.credential_flags.credential_filepath,
flags.credential_flags.service_account_filepath));

return LuciBuildApi(std::move(retrying_http_client), std::move(curl),
std::move(luci_credential_source),
std::move(gsutil_credential_source));
}

Result<std::optional<Build>> GetBuildHelper(
BuildApi& build_api, const std::optional<BuildString>& build_source,
const std::string& fallback_target) {
Expand Down Expand Up @@ -343,6 +384,7 @@ Result<Builds> GetBuilds(BuildApi& build_api,
"gbl_efi_dist_and_test")),
.otatools = CF_EXPECT(GetBuildHelper(
build_api, build_sources.otatools_build, kDefaultBuildTarget)),
.chrome_os = build_sources.chrome_os_build,
};
if (!result.otatools) {
if (result.system) {
Expand Down Expand Up @@ -390,7 +432,8 @@ Result<void> SaveConfig(FetcherConfig& config,
return {};
}

Result<void> FetchTarget(BuildApi& build_api, const Builds& builds,
Result<void> FetchTarget(BuildApi& build_api, LuciBuildApi& luci_build_api,
const Builds& builds,
const TargetDirectories& target_directories,
const DownloadFlags& flags,
const bool keep_downloaded_archives,
Expand Down Expand Up @@ -624,6 +667,22 @@ Result<void> FetchTarget(BuildApi& build_api, const Builds& builds,
ota_tools_files, target_directories.root));
DeAndroidSparse(ota_tools_files);
}

if (builds.chrome_os) {
auto artifacts_opt =
CF_EXPECT(luci_build_api.GetBuildArtifacts(*builds.chrome_os));
auto artifacts = CF_EXPECT(std::move(artifacts_opt));
std::string archive_name = "chromiumos_test_image.tar.xz";
CF_EXPECT(Contains(artifacts.artifact_files, archive_name));
auto archive_path = target_directories.root + "/" + archive_name;
CF_EXPECT(luci_build_api.DownloadArtifact(artifacts.artifact_link,
archive_name, archive_path));
auto archive_files = CF_EXPECT(ExtractArchiveContents(
archive_path, target_directories.chrome_os, keep_downloaded_archives));
CF_EXPECT(config.AddFilesToConfig(FileSource::CHROME_OS_BUILD, "", "",
archive_files, target_directories.root));
}

return {};
}

Expand All @@ -638,6 +697,8 @@ Result<void> Fetch(const FetchFlags& flags, HostToolsTarget& host_target,
curl_global_init(CURL_GLOBAL_DEFAULT);
{
BuildApi build_api = CF_EXPECT(GetBuildApi(flags.build_api_flags));
LuciBuildApi luci_build_api =
CF_EXPECT(GetLuciBuildApi(flags.build_api_flags));
CF_EXPECT(UpdateTargetsWithBuilds(build_api, targets));
std::optional<Build> fallback_host_build = std::nullopt;
if (!targets.empty()) {
Expand All @@ -654,8 +715,8 @@ Result<void> Fetch(const FetchFlags& flags, HostToolsTarget& host_target,
for (const auto& target : targets) {
LOG(INFO) << "Starting fetch to \"" << target.directories.root << "\"";
FetcherConfig config;
CF_EXPECT(FetchTarget(build_api, target.builds, target.directories,
target.download_flags,
CF_EXPECT(FetchTarget(build_api, luci_build_api, target.builds,
target.directories, target.download_flags,
flags.keep_downloaded_archives, config));
CF_EXPECT(SaveConfig(config, target.directories.root));
LOG(INFO) << "Completed fetch to \"" << target.directories.root << "\"";
Expand Down
6 changes: 6 additions & 0 deletions base/debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
cuttlefish-common (0.9.29) UNRELEASED; urgency=medium

* Add capability to `cvd fetch` to download ChromeOS builds

-- A. Cody Schuffelen <schuffelen@google.com> Fri, 05 Apr 2024 13:22:03 -0700

cuttlefish-common (0.9.28) unstable; urgency=medium

* Other bug fixes
Expand Down

0 comments on commit edd6cd8

Please sign in to comment.