• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 #include "host/commands/cvd/selector/instance_selector.h"
18 
19 #include <android-base/parseint.h>
20 
21 #include "host/commands/cvd/selector/device_selector_utils.h"
22 #include "host/commands/cvd/selector/instance_database_types.h"
23 #include "host/commands/cvd/selector/selector_constants.h"
24 #include "host/libs/config/cuttlefish_config.h"
25 
26 namespace cuttlefish {
27 namespace selector {
28 
GetSelector(const cvd_common::Args & selector_args,const Queries & extra_queries,const cvd_common::Envs & envs,const uid_t uid)29 Result<InstanceSelector> InstanceSelector::GetSelector(
30     const cvd_common::Args& selector_args, const Queries& extra_queries,
31     const cvd_common::Envs& envs, const uid_t uid) {
32   cvd_common::Args selector_args_copied{selector_args};
33   SelectorCommonParser common_parser =
34       CF_EXPECT(SelectorCommonParser::Parse(uid, selector_args_copied, envs));
35   std::stringstream unused_args;
36   unused_args << "{";
37   for (const auto& arg : selector_args_copied) {
38     unused_args << arg << ", ";
39   }
40   std::string unused_arg_list = unused_args.str();
41   if (!selector_args_copied.empty()) {
42     unused_arg_list.pop_back();
43     unused_arg_list.pop_back();
44   }
45   unused_arg_list.append("}");
46   if (!selector_args_copied.empty()) {
47     LOG(ERROR) << "Warning: there are unused selector options. "
48                << unused_arg_list;
49   }
50 
51   // search by instance and instances
52   // search by HOME if overridden
53   Queries queries;
54   if (IsHomeOverridden(common_parser)) {
55     CF_EXPECT(common_parser.Home());
56     queries.emplace_back(kHomeField, common_parser.Home().value());
57   }
58   if (common_parser.GroupName()) {
59     queries.emplace_back(kGroupNameField, common_parser.GroupName().value());
60   }
61   if (common_parser.PerInstanceNames()) {
62     const auto per_instance_names = common_parser.PerInstanceNames().value();
63     CF_EXPECT_LE(per_instance_names.size(), 1,
64                  "Instance Selector only picks up to 1 instance and thus "
65                  "only take up to 1 instance_name");
66     if (!per_instance_names.empty()) {
67       queries.emplace_back(kInstanceNameField, per_instance_names.front());
68     }
69   }
70   if (Contains(envs, kCuttlefishInstanceEnvVarName)) {
71     int id;
72     const std::string instance_id_str = envs.at(kCuttlefishInstanceEnvVarName);
73     if (android::base::ParseInt(instance_id_str, std::addressof(id))) {
74       queries.emplace_back(kInstanceIdField, std::to_string(id));
75     } else {
76       LOG(ERROR) << kCuttlefishInstanceEnvVarName << "=" << id
77                  << " was given but it must have one valid instance ID.";
78     }
79   }
80 
81   for (const auto& extra_query : extra_queries) {
82     queries.push_back(extra_query);
83   }
84 
85   InstanceSelector instance_selector(uid, queries);
86   return instance_selector;
87 }
88 
IsHomeOverridden(const SelectorCommonParser & common_parser)89 bool InstanceSelector::IsHomeOverridden(
90     const SelectorCommonParser& common_parser) {
91   auto home_overridden_result = common_parser.HomeOverridden();
92   if (!home_overridden_result.ok()) {
93     return false;
94   }
95   return *home_overridden_result;
96 }
97 
FindInstance(const InstanceDatabase & instance_database)98 Result<LocalInstance::Copy> InstanceSelector::FindInstance(
99     const InstanceDatabase& instance_database) {
100   if (queries_.empty()) {
101     auto default_instance = CF_EXPECT(FindDefaultInstance(instance_database));
102     return default_instance;
103   }
104 
105   auto instances = CF_EXPECT(instance_database.FindInstances(queries_));
106   CF_EXPECT(instances.size() == 1, "instances.size() = " << instances.size());
107   auto& instance = *(instances.cbegin());
108   return instance.Get().GetCopy();
109 }
110 
FindDefaultInstance(const InstanceDatabase & instance_database)111 Result<LocalInstance::Copy> InstanceSelector::FindDefaultInstance(
112     const InstanceDatabase& instance_database) {
113   auto group = CF_EXPECT(GetDefaultGroup(instance_database, client_uid_));
114   const auto instances = CF_EXPECT(group.FindAllInstances());
115   CF_EXPECT_EQ(instances.size(), 1,
116                "Default instance is the single instance in the default group.");
117   return instances.cbegin()->Get().GetCopy();
118 }
119 
120 }  // namespace selector
121 }  // namespace cuttlefish
122