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 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("UserKnownHostsFile=/dev/null");
54         cmd.add("-o");
55         cmd.add("StrictHostKeyChecking=no");
56         cmd.add("-o");
57         cmd.add("ServerAliveInterval=10");
58         cmd.add("-i");
59         cmd.add(sshKey.getAbsolutePath());
60         if (extraOptions != null) {
61             for (String op : extraOptions) {
62                 cmd.add(op);
63             }
64         }
65         cmd.add(user + "@" + hostName);
66         for (String cmdOption : command) {
67             cmd.add(cmdOption);
68         }
69         return cmd;
70     }
71 
72     /**
73      * Utility to create a scp command to fetch a file from a remote gce device.
74      *
75      * @param sshKey the ssh key {@link File}.
76      * @param extraOptions a List of {@link String} that can be added for extra ssh options. can be
77      *     null.
78      * @param hostName the hostname where to connect to the gce device.
79      * @param remoteFile the file to be fetched on the remote gce device.
80      * @param localFile the local file where to put the remote file.
81      * @param mode whether we are pushing the local file to the remote or pulling the remote
82      * @return a list representing the scp command for a gce device.
83      */
getScpCommand( File sshKey, List<String> extraOptions, String user, String hostName, String remoteFile, String localFile, ScpMode mode)84     public static List<String> getScpCommand(
85             File sshKey,
86             List<String> extraOptions,
87             String user,
88             String hostName,
89             String remoteFile,
90             String localFile,
91             ScpMode mode) {
92         List<String> cmd = new ArrayList<>();
93         cmd.add("scp");
94         cmd.add("-o");
95         cmd.add("UserKnownHostsFile=/dev/null");
96         cmd.add("-o");
97         cmd.add("StrictHostKeyChecking=no");
98         cmd.add("-o");
99         cmd.add("ServerAliveInterval=10");
100         cmd.add("-i");
101         cmd.add(sshKey.getAbsolutePath());
102         if (extraOptions != null) {
103             for (String op : extraOptions) {
104                 cmd.add(op);
105             }
106         }
107         if (ScpMode.PULL.equals(mode)) {
108             cmd.add(String.format("%s@%s:%s", user, hostName, remoteFile));
109             cmd.add(localFile);
110         } else {
111             cmd.add(localFile);
112             cmd.add(String.format("%s@%s:%s", user, hostName, remoteFile));
113         }
114         return cmd;
115     }
116 }
117