Skip to content

Commit cff2fb5

Browse files
OmniLab Teamcopybara-github
authored andcommitted
Internal change
PiperOrigin-RevId: 915269919
1 parent 072fd3c commit cff2fb5

2 files changed

Lines changed: 162 additions & 0 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
16+
load("@rules_java//java:java_binary.bzl", "java_binary")
17+
load("@rules_java//java:java_library.bzl", "java_library")
18+
19+
package(
20+
default_applicable_licenses = ["//:license"],
21+
default_visibility = [
22+
"//:deviceinfra_all_pkg",
23+
"//javatests/com/google/devtools/mobileharness/service/deviceconfig/tool:__pkg__",
24+
],
25+
)
26+
27+
java_binary(
28+
name = "device_config_importer",
29+
main_class = "com.google.devtools.mobileharness.service.deviceconfig.tool.DeviceConfigImporter",
30+
runtime_deps = [":device_config_importer_lib"],
31+
)
32+
33+
java_library(
34+
name = "device_config_importer_lib",
35+
srcs = ["DeviceConfigImporter.java"],
36+
deps = [
37+
"//src/devtools/mobileharness/api/deviceconfig/proto:device_config_service_java_proto",
38+
"//src/devtools/mobileharness/api/deviceconfig/proto:device_java_proto",
39+
"//src/devtools/mobileharness/api/deviceconfig/proto:lab_device_java_proto",
40+
"//src/java/com/google/devtools/mobileharness/service/deviceconfig/rpc/stub:annotation",
41+
"//src/java/com/google/devtools/mobileharness/service/deviceconfig/rpc/stub:device_config_stub",
42+
"//src/java/com/google/devtools/mobileharness/service/deviceconfig/rpc/stub/grpc:device_config_grpc_stub_module",
43+
"//src/java/com/google/devtools/mobileharness/shared/util/base:proto_text_format",
44+
"//src/java/com/google/devtools/mobileharness/shared/util/flags",
45+
"//src/java/com/google/devtools/mobileharness/shared/util/flags/core:flags_manager",
46+
"//src/java/com/google/devtools/mobileharness/shared/util/logging:google_logger",
47+
"@maven//:com_google_guava_guava",
48+
"@maven//:com_google_inject_guice",
49+
],
50+
)
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Copyright 2022 Google LLC
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+
* https://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+
package com.google.devtools.mobileharness.service.deviceconfig.tool;
18+
19+
import static com.google.common.collect.ImmutableList.toImmutableList;
20+
21+
import com.google.common.annotations.VisibleForTesting;
22+
import com.google.common.collect.ImmutableList;
23+
import com.google.common.flogger.FluentLogger;
24+
import com.google.devtools.mobileharness.api.deviceconfig.proto.Device.DeviceLocator;
25+
import com.google.devtools.mobileharness.api.deviceconfig.proto.Device.DeviceLocatorConfigPair;
26+
import com.google.devtools.mobileharness.api.deviceconfig.proto.DeviceConfigServiceProto.UpdateDeviceConfigsRequest;
27+
import com.google.devtools.mobileharness.api.deviceconfig.proto.DeviceConfigServiceProto.UpdateLabConfigRequest;
28+
import com.google.devtools.mobileharness.api.deviceconfig.proto.LabDevice.LabDeviceConfig;
29+
import com.google.devtools.mobileharness.service.deviceconfig.rpc.stub.Annotation.DeviceConfigGrpcStub;
30+
import com.google.devtools.mobileharness.service.deviceconfig.rpc.stub.DeviceConfigStub;
31+
import com.google.devtools.mobileharness.service.deviceconfig.rpc.stub.grpc.DeviceConfigGrpcStubModule;
32+
import com.google.devtools.mobileharness.shared.util.base.ProtoTextFormat;
33+
import com.google.devtools.mobileharness.shared.util.flags.Flags;
34+
import com.google.devtools.mobileharness.shared.util.flags.core.FlagsManager;
35+
import com.google.inject.Guice;
36+
import com.google.inject.Injector;
37+
import com.google.inject.Key;
38+
import java.nio.file.Files;
39+
import java.nio.file.Path;
40+
41+
/** Tool to import device configs from a textproto file to the config service. */
42+
public final class DeviceConfigImporter {
43+
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
44+
45+
public static void main(String[] args) throws Exception {
46+
FlagsManager.parse(args);
47+
48+
String filePath = Flags.labDeviceConfigFile.getNonNull();
49+
if (filePath.isEmpty()) {
50+
System.err.println("Error: --lab_device_config is required.");
51+
System.exit(1);
52+
}
53+
54+
String target = Flags.configServiceGrpcTarget.getNonNull();
55+
56+
logger.atInfo().log("Reading config file: %s", filePath);
57+
String content = Files.readString(Path.of(filePath));
58+
59+
logger.atInfo().log("Parsing config file...");
60+
LabDeviceConfig labDeviceConfig = ProtoTextFormat.parse(content, LabDeviceConfig.class);
61+
62+
logger.atInfo().log("Connecting to Config Service at: %s", target);
63+
Injector injector = Guice.createInjector(new DeviceConfigGrpcStubModule(target));
64+
DeviceConfigStub stub =
65+
injector.getInstance(Key.get(DeviceConfigStub.class, DeviceConfigGrpcStub.class));
66+
67+
new DeviceConfigImporter().importConfig(stub, labDeviceConfig);
68+
}
69+
70+
@VisibleForTesting
71+
void importConfig(DeviceConfigStub stub, LabDeviceConfig labDeviceConfig) throws Exception {
72+
if (labDeviceConfig.hasLabConfig()) {
73+
var labConfig = labDeviceConfig.getLabConfig();
74+
logger.atInfo().log("Found LabConfig for host: %s", labConfig.getHostName());
75+
logger.atInfo().log("Updating LabConfig...");
76+
stub.updateLabConfig(
77+
UpdateLabConfigRequest.newBuilder()
78+
.setLabConfig(labConfig)
79+
.setClient("importer")
80+
.build());
81+
logger.atInfo().log("LabConfig updated.");
82+
}
83+
84+
var deviceConfigs = labDeviceConfig.getDeviceConfigList();
85+
if (!deviceConfigs.isEmpty()) {
86+
logger.atInfo().log("Found %d DeviceConfigs.", deviceConfigs.size());
87+
ImmutableList<DeviceLocatorConfigPair> pairs =
88+
deviceConfigs.stream()
89+
.map(
90+
c ->
91+
DeviceLocatorConfigPair.newBuilder()
92+
.setDeviceLocator(
93+
DeviceLocator.newBuilder().setDeviceUuid(c.getUuid()).build())
94+
.setDeviceConfig(c)
95+
.build())
96+
.collect(toImmutableList());
97+
98+
logger.atInfo().log("Updating DeviceConfigs...");
99+
stub.updateDeviceConfigs(
100+
UpdateDeviceConfigsRequest.newBuilder()
101+
.setClient("importer")
102+
.addAllDeviceLocatorConfig(pairs)
103+
.build());
104+
logger.atInfo().log("DeviceConfigs updated.");
105+
}
106+
107+
logger.atInfo().log("Done.");
108+
}
109+
110+
@VisibleForTesting
111+
DeviceConfigImporter() {}
112+
}

0 commit comments

Comments
 (0)