Android-cuttlefish cvd tool
local_instance_group.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022 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
17#pragma once
18
19#include <json/json.h>
20
21#include <memory>
22#include <string>
23#include <vector>
24
25#include "cuttlefish/host/commands/cvd/instances/cvd_persistent_data.pb.h"
29
30namespace cuttlefish {
31
33 public:
34 class Builder {
35 public:
36 Builder(std::string group_name);
37
38 Builder& AddInstance(unsigned id) &;
39 Builder& AddInstance(unsigned id, std::string name) &;
40
41 Builder&& AddInstance(unsigned id) &&;
42 Builder&& AddInstance(unsigned id, std::string name) &&;
43
45
46 private:
47 std::string base_dir_;
48 cvd::InstanceGroup group_proto_;
49 };
50
54
55 const std::string& GroupName() const { return group_proto_->name(); }
56 const std::string& HomeDir() const { return group_proto_->home_directory(); }
57 const std::string& HostArtifactsPath() const {
58 return group_proto_->host_artifacts_path();
59 }
60 const std::string& ProductOutPath() const {
61 return group_proto_->product_out_path();
62 }
63 TimeStamp StartTime() const;
64 void SetStartTime(TimeStamp time);
65 const std::vector<LocalInstance>& Instances() const { return instances_; }
66 std::vector<LocalInstance>& Instances() { return instances_; }
67 bool HasActiveInstances() const;
68 const cvd::InstanceGroup& Proto() const { return *group_proto_; }
69 void SetAllStates(cvd::InstanceState state);
70
71 std::string BaseDir() const;
72 std::string AssemblyDir() const;
73 std::string MetricsDir() const;
74 std::string ArtifactsDir() const;
75 std::string ProductDir(int instance_index) const;
76
77 // Return list of filenames of group-level log files.
78 std::vector<std::string> LogsFilenames() const;
79
80 static std::vector<std::string> GroupLogBasenames();
81
82 Result<LocalInstance> FindInstanceById(unsigned id) const;
90 std::vector<LocalInstance> FindByInstanceName(
91 const std::string& instance_name) const;
92 // Fetches status from all instances in the group. Waits for run_cvd to
93 // respond for at most timeout seconds for each instance.
95 std::chrono::seconds timeout = std::chrono::seconds(5));
96
97 private:
98 friend class InstanceDatabase;
99
101 const cvd::InstanceGroup& group_proto);
102
103 LocalInstanceGroup(const cvd::InstanceGroup& group_proto);
104
105 // Ownership of the proto is shared between the LocalInstanceGroup and
106 // LocalInstance classes to ensure the references the latter maintains remain
107 // valid if the LocalInstanceGroup is destroyed before it.
108 std::shared_ptr<cvd::InstanceGroup> group_proto_;
109 std::vector<LocalInstance> instances_;
110};
111
112} // namespace cuttlefish
Definition: instance_database.h:34
Definition: local_instance_group.h:34
Builder & AddInstance(unsigned id) &
Definition: local_instance_group.cpp:104
std::string base_dir_
Definition: local_instance_group.h:47
cvd::InstanceGroup group_proto_
Definition: local_instance_group.h:48
Builder(std::string group_name)
Definition: local_instance_group.cpp:97
Result< LocalInstanceGroup > Build()
Definition: local_instance_group.cpp:130
Definition: local_instance_group.h:32
const std::vector< LocalInstance > & Instances() const
Definition: local_instance_group.h:65
void SetStartTime(TimeStamp time)
Definition: local_instance_group.cpp:159
Result< LocalInstance > FindInstanceById(unsigned id) const
Definition: local_instance_group.cpp:170
std::vector< LocalInstance > FindByInstanceName(const std::string &instance_name) const
Definition: local_instance_group.cpp:180
void SetAllStates(cvd::InstanceState state)
Definition: local_instance_group.cpp:149
std::string ProductDir(int instance_index) const
Definition: local_instance_group.cpp:199
const std::string & ProductOutPath() const
Definition: local_instance_group.h:60
const cvd::InstanceGroup & Proto() const
Definition: local_instance_group.h:68
bool HasActiveInstances() const
Definition: local_instance_group.cpp:140
std::vector< LocalInstance > instances_
Definition: local_instance_group.h:109
static std::vector< std::string > GroupLogBasenames()
Definition: local_instance_group.cpp:217
LocalInstanceGroup & operator=(const LocalInstanceGroup &)=default
Result< Json::Value > FetchStatus(std::chrono::seconds timeout=std::chrono::seconds(5))
Definition: local_instance_group.cpp:226
LocalInstanceGroup(const LocalInstanceGroup &)=default
std::string ArtifactsDir() const
Definition: local_instance_group.cpp:195
const std::string & HostArtifactsPath() const
Definition: local_instance_group.h:57
std::vector< LocalInstance > & Instances()
Definition: local_instance_group.h:66
std::vector< std::string > LogsFilenames() const
Definition: local_instance_group.cpp:208
const std::string & HomeDir() const
Definition: local_instance_group.h:56
const std::string & GroupName() const
Definition: local_instance_group.h:55
static Result< LocalInstanceGroup > Create(const cvd::InstanceGroup &group_proto)
Definition: local_instance_group.cpp:76
std::string MetricsDir() const
Definition: local_instance_group.cpp:191
std::string BaseDir() const
Definition: local_instance_group.cpp:203
std::string AssemblyDir() const
Definition: local_instance_group.cpp:187
std::shared_ptr< cvd::InstanceGroup > group_proto_
Definition: local_instance_group.h:108
LocalInstanceGroup(LocalInstanceGroup &&)=default
TimeStamp StartTime() const
Definition: local_instance_group.cpp:155
char * name
Definition: libf2fs.c:1403
Definition: alloc_driver.h:20
std::chrono::time_point< CvdServerClock > TimeStamp
Definition: instance_database_types.h:29
tl::expected< T, StackTraceError > Result
Definition: result_type.h:30