1 /*
2  * Copyright (C) 2019 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.cluster;
17 
18 import com.android.loganalysis.util.ArrayUtil;
19 import com.android.tradefed.log.LogUtil.CLog;
20 import com.android.tradefed.util.CommandResult;
21 import com.android.tradefed.util.CommandStatus;
22 import com.android.tradefed.util.FileUtil;
23 import com.android.tradefed.util.IRunUtil;
24 import com.android.tradefed.util.RunUtil;
25 import java.io.File;
26 import java.io.IOException;
27 import java.net.URL;
28 import java.util.List;
29 
30 /** A class to download test resource files from file system/GCS/HTTP. */
31 public class TestResourceDownloader {
32 
33     private static final long DOWNLOAD_TIMEOUT_MS = 60 * 60 * 1000;
34     private static final long RETRY_INTERVAL_MS = 10 * 1000;
35     private static final int MAX_RETRY_COUNT = 2;
36 
37     private IRunUtil mRunUtil = null;
38 
download(String urlString, File dest)39     public void download(String urlString, File dest) throws IOException {
40         final URL url = new URL(urlString);
41         final String protocol = url.getProtocol();
42         final File parent = dest.getParentFile();
43         if (!parent.exists()) {
44             parent.mkdirs();
45         }
46         if ("file".equals(protocol)) {
47             final File src = new File(url.getPath());
48             FileUtil.hardlinkFile(src, dest);
49             return;
50         }
51 
52         final List<String> cmdArgs = buildDownloadCommandArgs(url, dest);
53         final CommandResult result =
54                 getRunUtil()
55                         .runTimedCmdRetry(
56                                 DOWNLOAD_TIMEOUT_MS,
57                                 RETRY_INTERVAL_MS,
58                                 MAX_RETRY_COUNT + 1,
59                                 cmdArgs.toArray(new String[0]));
60         if (!result.getStatus().equals(CommandStatus.SUCCESS)) {
61             final String msg =
62                     String.format(
63                             "Failed to download %s: command status=%s", url, result.getStatus());
64             CLog.e(msg);
65             CLog.e("stdout:\n'''\n%s'''\n", result.getStdout());
66             CLog.e("stderr:\n'''\n%s'''\n", result.getStderr());
67             throw new RuntimeException(msg);
68         }
69         return;
70     }
71 
72     /** Build a list of command line arguments to download a file. */
buildDownloadCommandArgs(URL url, File file)73     private List<String> buildDownloadCommandArgs(URL url, File file) {
74         final String protocol = url.getProtocol();
75         if ("gs".equals(protocol)) {
76             // FIXME: Check whether gsutil is available on a host.
77             return ArrayUtil.list("gsutil", "cp", url.toString(), file.getAbsolutePath());
78         }
79         if ("http".equals(protocol) || "https".equals(protocol)) {
80             // FIXME: Check whether curl is available on a host.
81             // Add -L option to handle redirect.
82             return ArrayUtil.list("curl", "-o", file.getAbsolutePath(), "-fL", url.toString());
83         }
84         throw new UnsupportedOperationException("protocol " + protocol + " is not supported");
85     }
86 
getRunUtil()87     IRunUtil getRunUtil() {
88         if (mRunUtil == null) {
89             mRunUtil = new RunUtil();
90         }
91         return mRunUtil;
92     }
93 }
94