1 /*
2  * Copyright (C) 2018 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 package com.android.tradefed.device.cloud;
17 
18 import com.android.tradefed.device.TestDeviceOptions;
19 import com.android.tradefed.util.CommandResult;
20 import com.android.tradefed.util.IRunUtil;
21 
22 import java.io.OutputStream;
23 import java.util.List;
24 
25 /** Utility to execute ssh commands on remote instances. */
26 public class RemoteSshUtil {
27 
28     /**
29      * Execute a command on the remote instance using ssh.
30      *
31      * @param remoteInstance The {@link GceAvdInfo} that describe the device.
32      * @param options a {@link TestDeviceOptions} describing the device options to be used for the
33      *     GCE device.
34      * @param runUtil a {@link IRunUtil} to execute commands.
35      * @param timeoutMs in millisecond for the fetch to complete
36      * @param stdout An {@link OutputStream} where the stdout will be logged.
37      * @param stderr An {@link OutputStream} where the stderr will be logged.
38      * @param command The command to be executed.
39      * @return A {@link CommandResult} containing the status and logs.
40      */
remoteSshCommandExec( GceAvdInfo remoteInstance, TestDeviceOptions options, IRunUtil runUtil, long timeoutMs, OutputStream stdout, OutputStream stderr, String... command)41     public static CommandResult remoteSshCommandExec(
42             GceAvdInfo remoteInstance,
43             TestDeviceOptions options,
44             IRunUtil runUtil,
45             long timeoutMs,
46             OutputStream stdout,
47             OutputStream stderr,
48             String... command) {
49         List<String> sshCmd =
50                 GceRemoteCmdFormatter.getSshCommand(
51                         options.getSshPrivateKeyPath(),
52                         null,
53                         options.getInstanceUser(),
54                         remoteInstance.hostAndPort().getHost(),
55                         command);
56         return runUtil.runTimedCmd(timeoutMs, stdout, stderr, sshCmd.toArray(new String[0]));
57     }
58 
59     /**
60      * Execute a command on the remote instance using ssh.
61      *
62      * @param remoteInstance The {@link GceAvdInfo} that describe the device.
63      * @param options a {@link TestDeviceOptions} describing the device options to be used for the
64      *     GCE device.
65      * @param runUtil a {@link IRunUtil} to execute commands.
66      * @param timeoutMs in millisecond for the fetch to complete
67      * @param command The command to be executed.
68      * @return A {@link CommandResult} containing the status and logs.
69      */
remoteSshCommandExec( GceAvdInfo remoteInstance, TestDeviceOptions options, IRunUtil runUtil, long timeoutMs, String... command)70     public static CommandResult remoteSshCommandExec(
71             GceAvdInfo remoteInstance,
72             TestDeviceOptions options,
73             IRunUtil runUtil,
74             long timeoutMs,
75             String... command) {
76         return remoteSshCommandExec(
77                 remoteInstance, options, runUtil, timeoutMs, null, null, command);
78     }
79 }
80