Android-cuttlefish cvd tool
cas_downloader.h
Go to the documentation of this file.
1//
2// Copyright (C) 2024 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#pragma once
17
18#include <memory>
19#include <optional>
20#include <string>
21#include <vector>
22
23#include "json/value.h"
24
31
32namespace cuttlefish {
33
34inline constexpr char kKeyDownloaderPath[] = "downloader-path";
35inline constexpr char kKeyFlags[] = "flags";
36
37inline constexpr char kFlagDigest[] = "digest";
38inline constexpr char kFlagDir[] = "dir";
39inline constexpr char kFlagDumpJson[] = "dump-json";
40inline constexpr char kFlagDisableCache[] = "disable-cache";
41inline constexpr char kFlagCasInstance[] = "cas-instance";
42inline constexpr char kFlagCasAddr[] = "cas-addr";
43inline constexpr char kFlagServiceAccountJson[] = "service-account-json";
44inline constexpr char kFlagUseAdc[] = "use-adc";
45
46// Identifies an artifact in CAS.
47// The digest of an artifact is unique in a CAS instance. To identify the CAS
48// instance, the cas_instance and cas_addr are required. An artifact can only be
49// downloaded from the CAS instance it is uploaded to. This info is available in
50// cas_digests.json from AB.
52 std::string cas_instance;
53 std::string cas_addr;
54 std::string digest;
55 // The actual filename in CAS, can be different from the the artifact_name.
56 std::string filename;
57};
58
59// The name of the file, published with each Android Build build, that lists
60// the CAS digests of the build's artifacts.
61inline constexpr char kCasDigestsFilename[] = "cas_digests.json";
62
63// Downloads the artifacts of Android Build builds from CAS, as a C++ wrapper
64// for the CAS downloader binary.
65//
66// Only the "-img-" artifacts of a build are downloaded through CAS, and only
67// as whole artifacts: every other request fails. It is meant to go ahead of an
68// API that serves the rest, such as the Android Build API. The digests of a
69// build's artifacts are read from its cas_digests.json, which `digests_source`
70// downloads.
71//
72// Example:
73// std::unique_ptr<CasDownloader> cas_downloader =
74// CF_EXPECT(CasDownloader::Create(cas_downloader_flags,
75// service_account_filepath,
76// android_build_api));
77// FallbackBuildApi<DeviceBuildString, DeviceBuild> build_api({
78// cas_downloader.get(),
79// &android_build_api,
80// });
81// CF_EXPECT(build_api.DownloadFile(device_build, target_dir, artifact_name));
82//
83class CasDownloader : public BuildApi<DeviceBuildString, DeviceBuild> {
84 public:
85 // `digests_source` must outlive this object.
87 std::string downloader_path, std::vector<std::string> flags,
88 bool prefer_uncompressed = false);
90 const CasDownloaderFlags& cas_downloader_flags,
91 const std::string& service_account_filepath,
93 ~CasDownloader() override = default;
94
95 // CAS cannot resolve build strings; always fails.
96 Result<DeviceBuild> GetBuild(const DeviceBuildString& build_string) override;
97
99 const std::string& target_directory,
100 const std::string& artifact_name) override;
101 // As above, and also has the CAS downloader write its download statistics
102 // to `stats_filepath` when set.
104 const DeviceBuild& build, const std::string& target_directory,
105 const std::string& artifact_name,
106 const std::optional<std::string>& stats_filepath);
107
108 // CAS cannot serve parts of an artifact; always fails.
110 const DeviceBuild& build, const std::string& artifact_name) override;
111
112 private:
114 const CasDownloaderFlags& cas_downloader_flags,
115 const std::string& service_account_filepath,
117
119 const std::string& target_directory,
120 const std::string& artifact_name);
121
123 std::string downloader_path_;
124 std::vector<std::string> flags_;
126 std::string build_desc_; // e.g. "build_id:build_target"
127 Json::Value cas_digests_;
128};
129
130} // namespace cuttlefish
Definition: build_api.h:27
Definition: cas_downloader.h:83
static Result< std::unique_ptr< CasDownloader > > Create(const CasDownloaderFlags &cas_downloader_flags, const std::string &service_account_filepath, BuildApi< DeviceBuildString, DeviceBuild > &digests_source)
Definition: cas_downloader.cpp:298
Json::Value cas_digests_
Definition: cas_downloader.h:127
Result< DeviceBuild > GetBuild(const DeviceBuildString &build_string) override
Definition: cas_downloader.cpp:462
Result< std::unique_ptr< ReaderSeeker > > FileReader(const DeviceBuild &build, const std::string &artifact_name) override
Definition: cas_downloader.cpp:507
Result< std::string > DownloadFile(const DeviceBuild &build, const std::string &target_directory, const std::string &artifact_name) override
Definition: cas_downloader.cpp:466
bool prefer_uncompressed_
Definition: cas_downloader.h:125
std::string downloader_path_
Definition: cas_downloader.h:123
Result< CasIdentifier > GetCasIdentifier(const DeviceBuild &build, const std::string &target_directory, const std::string &artifact_name)
Definition: cas_downloader.cpp:519
~CasDownloader() override=default
std::string build_desc_
Definition: cas_downloader.h:126
CasDownloader(BuildApi< DeviceBuildString, DeviceBuild > &digests_source, std::string downloader_path, std::vector< std::string > flags, bool prefer_uncompressed=false)
Definition: cas_downloader.cpp:453
static Result< std::unique_ptr< CasDownloader > > CreateImpl(const CasDownloaderFlags &cas_downloader_flags, const std::string &service_account_filepath, BuildApi< DeviceBuildString, DeviceBuild > &digests_source)
Definition: cas_downloader.cpp:315
BuildApi< DeviceBuildString, DeviceBuild > & digests_source_
Definition: cas_downloader.h:122
std::vector< std::string > flags_
Definition: cas_downloader.h:124
Definition: alloc_driver.h:21
constexpr char kFlagDumpJson[]
Definition: cas_downloader.h:39
constexpr char kKeyDownloaderPath[]
Definition: cas_downloader.h:34
constexpr char kFlagDigest[]
Definition: cas_downloader.h:37
constexpr char kFlagCasInstance[]
Definition: cas_downloader.h:41
constexpr char kFlagDisableCache[]
Definition: cas_downloader.h:40
constexpr char kFlagUseAdc[]
Definition: cas_downloader.h:44
constexpr char kFlagCasAddr[]
Definition: cas_downloader.h:42
constexpr char kFlagDir[]
Definition: cas_downloader.h:38
constexpr char kKeyFlags[]
Definition: cas_downloader.h:35
constexpr char kFlagServiceAccountJson[]
Definition: cas_downloader.h:43
constexpr char kCasDigestsFilename[]
Definition: cas_downloader.h:61
tl::expected< T, StackTraceError< E > > Result
Definition: result_type.h:30
Definition: cas_flags.h:71
Definition: cas_downloader.h:51
std::string filename
Definition: cas_downloader.h:56
std::string cas_instance
Definition: cas_downloader.h:52
std::string digest
Definition: cas_downloader.h:54
std::string cas_addr
Definition: cas_downloader.h:53
Definition: device_build_string.h:25
Definition: android_build.h:36