• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.util;
17 
18 import java.io.File;
19 import java.util.ArrayList;
20 import java.util.List;
21 
22 /** Utility class to format commands to reach a remote gce device. */
23 public class GceRemoteCmdFormatter {
24 
25     /**
26      * SCP can be used to push or pull file depending of the structure of the args. Ensure we can
27      * support both.
28      */
29     public enum ScpMode {
30         PUSH,
31         PULL;
32     }
33 
34     /**
35      * Utility to create a ssh command for a gce device based on some parameters.
36      *
37      * @param sshKey the ssh key {@link File}.
38      * @param extraOptions a List of {@link String} that can be added for extra ssh options. can be
39      *     null.
40      * @param hostName the hostname where to connect to the gce device.
41      * @param command the actual command to run on the gce device.
42      * @return a list representing the ssh command for a gce device.
43      */
getSshCommand( File sshKey, List<String> extraOptions, String user, String hostName, String... command)44     public static List<String> getSshCommand(
45             File sshKey,
46             List<String> extraOptions,
47             String user,
48             String hostName,
49             String... command) {
50         List<String> cmd = new ArrayList<>();
51         cmd.add("ssh");
52         cmd.add("-o");
53         cmd.add("LogLevel=ERROR");
54         cmd.add("-o");
55         cmd.add("UserKnownHostsFile=/dev/null");
56         cmd.add("-o");
57         cmd.add("StrictHostKeyChecking=no");
58         cmd.add("-o");
59         cmd.add("ServerAliveInterval=10");
60         cmd.add("-i");
61         cmd.add(sshKey.getAbsolutePath());
62         if (extraOptions != null) {
63             for (String op : extraOptions) {
64                 cmd.add(op);
65             }
66         }
67         cmd.add(user + "@" + hostName);
68         for (String cmdOption : command) {
69             cmd.add(cmdOption);
70         }
71         return cmd;
72     }
73 
74     /**
75      * Utility to create a scp command to fetch a file from a remote gce device.
76      *
77      * @param sshKey the ssh key {@link File}.
78      * @param extraOptions a List of {@link String} that can be added for extra ssh options. can be
79      *     null.
80      * @param hostName the hostname where to connect to the gce device.
81      * @param remoteFile the file to be fetched on the remote gce device.
82      * @param localFile the local file where to put the remote file.
83      * @param mode whether we are pushing the local file to the remote or pulling the remote
84      * @return a list representing the scp command for a gce device.
85      */
getScpCommand( File sshKey, List<String> extraOptions, String user, String hostName, String remoteFile, String localFile, ScpMode mode)86     public static List<String> getScpCommand(
87             File sshKey,
88             List<String> extraOptions,
89             String user,
90             String hostName,
91             String remoteFile,
92             String localFile,
93             ScpMode mode) {
94         List<String> cmd = new ArrayList<>();
95         cmd.add("scp");
96         cmd.add("-o");
97         cmd.add("LogLevel=ERROR");
98         cmd.add("-o");
99         cmd.add("UserKnownHostsFile=/dev/null");
100         cmd.add("-o");
101         cmd.add("StrictHostKeyChecking=no");
102         cmd.add("-o");
103         cmd.add("ServerAliveInterval=10");
104         cmd.add("-i");
105         cmd.add(sshKey.getAbsolutePath());
106         if (extraOptions != null) {
107             for (String op : extraOptions) {
108                 cmd.add(op);
109             }
110         }
111         if (ScpMode.PULL.equals(mode)) {
112             cmd.add(String.format("%s@%s:%s", user, hostName, remoteFile));
113             cmd.add(localFile);
114         } else {
115             cmd.add(localFile);
116             cmd.add(String.format("%s@%s:%s", user, hostName, remoteFile));
117         }
118         return cmd;
119     }
120 }
121