|
| 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