1 /* 2 * Copyright (C) 2017 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.sandbox; 17 18 import com.android.tradefed.config.ConfigurationException; 19 import com.android.tradefed.config.GlobalConfiguration; 20 import com.android.tradefed.config.IConfiguration; 21 import com.android.tradefed.log.LogUtil.CLog; 22 import com.android.tradefed.sandbox.SandboxConfigDump.DumpCmd; 23 import com.android.tradefed.util.CommandResult; 24 import com.android.tradefed.util.CommandStatus; 25 import com.android.tradefed.util.FileUtil; 26 import com.android.tradefed.util.IRunUtil; 27 import com.android.tradefed.util.IRunUtil.EnvPriority; 28 import com.android.tradefed.util.TimeUtil; 29 30 import com.google.common.base.Joiner; 31 import com.google.common.base.Strings; 32 33 import java.io.File; 34 import java.io.IOException; 35 import java.util.ArrayList; 36 import java.util.List; 37 import java.util.Set; 38 39 /** A utility class for managing {@link IConfiguration} when doing sandboxing. */ 40 public class SandboxConfigUtil { 41 42 private static final long DUMP_TIMEOUT = 5 * 60 * 1000; // 5 minutes 43 44 /** 45 * Create a subprocess based on the Tf jars from any version, and dump the xml {@link 46 * IConfiguration} based on the command line args. 47 * 48 * @param classpath the classpath to use to run the sandbox. 49 * @param runUtil the {@link IRunUtil} to use to run the command. 50 * @param args the command line args. 51 * @param dump the {@link DumpCmd} driving some of the outputs. 52 * @param globalConfig the file describing the global configuration to be used. 53 * @return A {@link File} containing the xml dump from the command line. 54 * @throws SandboxConfigurationException if the dump is not successful. 55 */ dumpConfigForVersion( String classpath, IRunUtil runUtil, String[] args, DumpCmd dump, File globalConfig)56 public static File dumpConfigForVersion( 57 String classpath, IRunUtil runUtil, String[] args, DumpCmd dump, File globalConfig) 58 throws SandboxConfigurationException, IOException { 59 if (Strings.isNullOrEmpty(classpath)) { 60 throw new SandboxConfigurationException( 61 "Something went wrong with the sandbox setup, classpath was empty."); 62 } 63 runUtil.unsetEnvVariable(GlobalConfiguration.GLOBAL_CONFIG_VARIABLE); 64 runUtil.unsetEnvVariable(GlobalConfiguration.GLOBAL_CONFIG_SERVER_CONFIG_VARIABLE); 65 File destination = null; 66 try { 67 destination = FileUtil.createTempFile("config-container", ".xml"); 68 if (globalConfig != null) { 69 runUtil.setEnvVariable( 70 GlobalConfiguration.GLOBAL_CONFIG_VARIABLE, globalConfig.getAbsolutePath()); 71 runUtil.setEnvVariablePriority(EnvPriority.SET); 72 } 73 } catch (IOException e) { 74 FileUtil.deleteFile(globalConfig); 75 FileUtil.deleteFile(destination); 76 throw e; 77 } 78 List<String> mCmdArgs = new ArrayList<>(); 79 mCmdArgs.add("java"); 80 mCmdArgs.add("-cp"); 81 mCmdArgs.add(classpath); 82 mCmdArgs.add(SandboxConfigDump.class.getCanonicalName()); 83 mCmdArgs.add(dump.toString()); 84 mCmdArgs.add(destination.getAbsolutePath()); 85 for (String arg : args) { 86 mCmdArgs.add(arg); 87 } 88 CommandResult result = runUtil.runTimedCmd(DUMP_TIMEOUT, mCmdArgs.toArray(new String[0])); 89 CLog.d("stdout: %s", result.getStdout()); 90 if (result.getStderr() != null && !result.getStderr().isEmpty()) { 91 CLog.d("stderr: %s", result.getStderr()); 92 } 93 if (CommandStatus.SUCCESS.equals(result.getStatus())) { 94 return destination; 95 } 96 FileUtil.deleteFile(destination); 97 // Do not delete the global configuration file here in this case, it might still be used. 98 String errorMessage = "Error when dumping the config."; 99 if (CommandStatus.TIMED_OUT.equals(result.getStatus())) { 100 errorMessage += 101 String.format(" Timed out after %s.", TimeUtil.formatElapsedTime(DUMP_TIMEOUT)); 102 } 103 errorMessage += String.format(" stderr: %s", result.getStderr()); 104 throw new SandboxConfigurationException(errorMessage); 105 } 106 107 /** 108 * Create a subprocess based on the Tf jars from any version, and dump the xml {@link 109 * IConfiguration} based on the command line args. 110 * 111 * @param rootDir the directory containing all the jars from TF. 112 * @param runUtil the {@link IRunUtil} to use to run the command. 113 * @param args the command line args. 114 * @param dump the {@link DumpCmd} driving some of the outputs. 115 * @param globalConfig the file describing the global configuration to be used. 116 * @return A {@link File} containing the xml dump from the command line. 117 * @throws ConfigurationException if the dump is not successful. 118 */ dumpConfigForVersion( File rootDir, IRunUtil runUtil, String[] args, DumpCmd dump, File globalConfig)119 public static File dumpConfigForVersion( 120 File rootDir, IRunUtil runUtil, String[] args, DumpCmd dump, File globalConfig) 121 throws ConfigurationException, IOException { 122 // include all jars on the classpath 123 String classpath = ""; 124 Set<String> jarFiles = FileUtil.findFiles(rootDir, ".*.jar"); 125 classpath = Joiner.on(":").join(jarFiles); 126 return dumpConfigForVersion(classpath, runUtil, args, dump, globalConfig); 127 } 128 129 /** Create a global config with only the keystore to make it available in subprocess. */ dumpFilteredGlobalConfig(Set<String> exclusionPatterns)130 public static File dumpFilteredGlobalConfig(Set<String> exclusionPatterns) throws IOException { 131 return GlobalConfiguration.getInstance().cloneConfigWithFilter(exclusionPatterns); 132 } 133 } 134