Skip to content

Commit

Permalink
More experimental movements of code and consolidation
Browse files Browse the repository at this point in the history
  • Loading branch information
chadlwilson committed Apr 12, 2023
1 parent 092f993 commit 4a8958c
Show file tree
Hide file tree
Showing 57 changed files with 169 additions and 269 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
public class AgentPluginsInitializer implements ApplicationListener<ContextRefreshedEvent> {
private static final Logger LOG = LoggerFactory.getLogger(AgentPluginsInitializer.class);
private final DefaultPluginJarLocationMonitor defaultPluginJarLocationMonitor;
private PluginManager pluginManager;
private ZipUtil zipUtil;
private SystemEnvironment systemEnvironment;
private final PluginManager pluginManager;
private final ZipUtil zipUtil;
private final SystemEnvironment systemEnvironment;

@Autowired
public AgentPluginsInitializer(PluginManager pluginManager, DefaultPluginJarLocationMonitor defaultPluginJarLocationMonitor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import com.thoughtworks.go.util.SslVerificationMode;
import com.thoughtworks.go.util.SystemEnvironment;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
Expand All @@ -32,6 +31,7 @@
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.security.*;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
Expand Down Expand Up @@ -129,7 +129,7 @@ private List<X509Certificate> agentCertificate() throws IOException, Certificate

private char[] passphrase() throws IOException {
if (sslPrivateKeyPassphraseFile != null && sslPrivateKeyPassphraseFile.exists()) {
String passphrase = FileUtils.readFileToString(sslPrivateKeyPassphraseFile, StandardCharsets.UTF_8);
String passphrase = Files.readString(sslPrivateKeyPassphraseFile.toPath(), StandardCharsets.UTF_8);
return StringUtils.trimToEmpty(passphrase).toCharArray();
}
throw new RuntimeException("SSL private key passphrase not specified!");
Expand Down
26 changes: 2 additions & 24 deletions base/src/main/java/com/thoughtworks/go/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,18 @@
*/
package com.thoughtworks.go.util;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.util.UUID;

public class FileUtil {
public static final String TMP_PARENT_DIR = "data";
private static final String CRUISE_TMP_FOLDER = "cruise" + "-" + UUID.randomUUID();
private static final Logger LOGGER = LoggerFactory.getLogger(FileUtil.class);

private FileUtil() {}

Expand Down Expand Up @@ -66,7 +63,7 @@ public static void validateAndCreateDirectory(File directory) {
return;
}
try {
FileUtils.forceMkdir(directory);
Files.createDirectories(directory.toPath());
} catch (IOException e) {
throw new RuntimeException("Failed to create folder: " + directory.getAbsolutePath());
}
Expand Down Expand Up @@ -138,25 +135,6 @@ public static String getCanonicalPath(File workDir) {
}
}

public static void deleteDirectoryNoisily(File defaultDirectory) {
if (!defaultDirectory.exists()) {
return;
}

try {
FileUtils.deleteDirectory(defaultDirectory);
} catch (IOException e) {
throw new RuntimeException("Failed to delete directory: " + defaultDirectory.getAbsolutePath(), e);
}
}

public static String join(File defaultWorkingDir, String actualFileToUse) {
if (actualFileToUse == null) {
LOGGER.trace("Using the default Directory->{}", defaultWorkingDir);
return FilenameUtils.separatorsToUnix(defaultWorkingDir.getPath());
}
return applyBaseDirIfRelativeAndNormalize(defaultWorkingDir, new File(actualFileToUse));
}
}


24 changes: 5 additions & 19 deletions base/src/main/java/com/thoughtworks/go/util/ZipBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,12 @@
import java.util.Map;
import java.util.zip.ZipOutputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ZipBuilder {
private static final Logger LOGGER = LoggerFactory.getLogger(ZipBuilder.class);
private final ZipUtil zipUtil;
private final OutputStream destinationStream;
private final boolean excludeRootDir;
private final int level;
private ZipUtil zipUtil;
private OutputStream destinationStream;
private boolean excludeRootDir;
private Map<String, File> toAdd = new HashMap<>();
private final Map<String, File> toAdd = new HashMap<>();

public ZipBuilder(ZipUtil zipUtil, int level, OutputStream destinationStream, boolean excludeRootDir) {
this.zipUtil = zipUtil;
Expand All @@ -47,24 +43,14 @@ public ZipBuilder add(String directoryNameInsideZip, File sourceToZip) {
}

public void done() throws IOException {
ZipOutputStream zip = null;
try {
zip = new ZipOutputStream(new BufferedOutputStream(destinationStream));
try (ZipOutputStream zip = new ZipOutputStream(new BufferedOutputStream(destinationStream))) {
zip.setLevel(level);
for (Map.Entry<String, File> zipDirToSourceFileEntry : toAdd.entrySet()) {
File sourceFileToZip = zipDirToSourceFileEntry.getValue();
String destinationFolder = zipDirToSourceFileEntry.getKey();
zipUtil.addToZip(new ZipPath(destinationFolder), sourceFileToZip, zip, excludeRootDir);
}
zip.flush();
} finally {
if (zip != null) {
try {
zip.close();
} catch (IOException e) {
LOGGER.error("Failed to close the stream", e);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@
*/
package com.thoughtworks.go.util;

import com.thoughtworks.go.util.command.*;
import org.apache.commons.io.IOUtils;
import com.thoughtworks.go.util.command.ConsoleOutputStreamConsumer;
import com.thoughtworks.go.util.command.ErrorConsumer;
import com.thoughtworks.go.util.command.OutputConsumer;
import com.thoughtworks.go.util.command.StreamPumper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.*;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

public class ProcessWrapper {

Expand All @@ -51,14 +52,17 @@ public class ProcessWrapper {

public int waitForExit() {
int returnValue = -1;
try {
try (InputStream ignored = process.getInputStream();
InputStream ignored1 = process.getErrorStream();
OutputStream ignored2 = process.getOutputStream()) {

returnValue = process.waitFor();
processOutputStream.readToEnd();
processErrorStream.readToEnd();
} catch (InterruptedException ignored) {
LOGGER.warn(ignored.getMessage(), ignored);
} catch (InterruptedException | IOException ignored) {
} finally {
close();
process.destroy();
ProcessManager.getInstance().processKilled(process);
}
return returnValue;
}
Expand All @@ -71,7 +75,6 @@ public void typeInputToConsole(List<String> inputs) {
processInputStream.close();
}


private long lastHeardTime() {
if (processErrorStream == null & processOutputStream == null) {
return System.currentTimeMillis();
Expand All @@ -85,16 +88,10 @@ private long lastHeardTime() {
return Math.min(processOutputStream.getLastHeard(), processErrorStream.getLastHeard());
}


public void closeOutputStream() throws IOException {
process.getOutputStream().close();
}

public void close() {
close(process);
ProcessManager.getInstance().processKilled(process);
}

public boolean isRunning() {
try {
process.exitValue();
Expand Down Expand Up @@ -131,28 +128,12 @@ public boolean equals(Object o) {

ProcessWrapper that = (ProcessWrapper) o;

if (process != null ? !process.equals(that.process) : that.process != null) {
return false;
}

return true;
return Objects.equals(process, that.process);
}

@Override
public int hashCode() {
return process != null ? process.hashCode() : 0;
}

private void close(Process p) {
try {
IOUtils.closeQuietly(p.getInputStream());
IOUtils.closeQuietly(p.getOutputStream());
IOUtils.closeQuietly(p.getErrorStream());
} finally {
if (p != null) {
p.destroy();
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@

public class StreamPumper implements Runnable {

private Reader in;

private boolean completed;
private final Reader in;
private final StreamConsumer streamConsumer;
private final String prefix;
private boolean completed;
private long lastHeard;
private final Clock clock;

Expand Down
1 change: 1 addition & 0 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ dependencies {
api project.deps.commonsCollections4
api project.deps.commonsText
api project.deps.springTx
implementation project.deps.commonsIO
implementation project.deps.jodaTime
annotationProcessor project.deps.lombok
compileOnly project.deps.jetBrainsAnnotations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
import com.thoughtworks.go.remote.work.BuildWork;
import com.thoughtworks.go.remote.work.Work;
import com.thoughtworks.go.util.SystemEnvironment;
import com.thoughtworks.go.util.TestFileUtil;
import com.thoughtworks.go.util.command.EnvironmentVariableContext;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -51,7 +51,7 @@ public class DefaultWorkCreator implements WorkCreator {

private static File tempFile() {
try {
File tempFile = TestFileUtil.createTempFile("artifact.tmp");
File tempFile = Files.createTempFile("artifact", "artifact.tmp").toFile();
tempFile.deleteOnExit();
return tempFile;
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,14 @@

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

public class FakeBuildRepositoryRemote implements BuildRepositoryRemote {
public final static List<AgentRuntimeStatus> AGENT_STATUS = new ArrayList<>();

private static final Logger LOGGER = LoggerFactory.getLogger(FakeBuildRepositoryRemote.class);

public static final String PIPELINE_NAME = "studios";
public static final String PIPELINE_LABEL = "100";
public static final String STAGE_NAME = "pipeline";
public static final String JOB_PLAN_NAME = "cruise-test-data";

private static final BlockingQueue<Boolean> buildResult = new LinkedBlockingQueue<>();

@Override
public AgentInstruction ping(AgentRuntimeInfo info) {
Expand All @@ -54,22 +47,17 @@ public AgentInstruction ping(AgentRuntimeInfo info) {
@Override
public Work getWork(AgentRuntimeInfo runtimeInfo) {
String className = SystemEnvironment.getProperty("WORKCREATOR", DefaultWorkCreator.class.getCanonicalName());
Class<? extends WorkCreator> aClass = null;
try {
aClass = (Class<? extends WorkCreator>) Class.forName(className);
Class<? extends WorkCreator> aClass = (Class<? extends WorkCreator>) Class.forName(className);
return aClass.getDeclaredConstructor().newInstance().work(runtimeInfo.getIdentifier());
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}

@Override
public void reportCurrentStatus(AgentRuntimeInfo agentRuntimeInfo, JobIdentifier jobIdentifier, JobState jobState) {
LOGGER.info("Current status of build instance with id {} is {}", jobIdentifier, jobState);
if (jobState.isCompleted()) {
buildResult.offer(Boolean.TRUE);
}
}

@Override
Expand All @@ -91,17 +79,4 @@ public boolean isIgnored(AgentRuntimeInfo agentRuntimeInfo, JobIdentifier jobIde
public String getCookie(AgentRuntimeInfo agentRuntimeInfo) {
throw new UnsupportedOperationException("Not implemented");
}

public static void waitUntilBuildCompleted() throws InterruptedException {
while (!isBuildCompleted()) {
Thread.sleep(1000);
}
}

private static boolean isBuildCompleted() throws InterruptedException {
Boolean aBoolean = buildResult.poll(1, TimeUnit.SECONDS);
return aBoolean != null && aBoolean;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import com.thoughtworks.go.util.TempDirUtils;
import com.thoughtworks.go.util.command.*;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
Expand All @@ -41,7 +40,7 @@

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Date;
Expand Down Expand Up @@ -697,10 +696,7 @@ void shouldThrowExceptionIfRepoCanNotConnectWhenModificationCheck() {

@Test
void shouldParseGitOutputCorrectly() throws IOException {
List<String> stringList;
try (InputStream resourceAsStream = getClass().getResourceAsStream("git_sample_output.text")) {
stringList = IOUtils.readLines(resourceAsStream, UTF_8);
}
List<String> stringList = Files.readAllLines(Path.of(getClass().getResource("git_sample_output.text").getPath()));

GitModificationParser parser = new GitModificationParser();
List<Modification> mods = parser.parse(stringList);
Expand Down
1 change: 1 addition & 0 deletions config/config-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ dependencies {

implementation project.deps.commonsCodec
implementation project.deps.commonsCollections4
implementation project.deps.commonsIO
implementation project.deps.commonsText
api(project.deps.quartz) {
transitive = false
Expand Down

0 comments on commit 4a8958c

Please sign in to comment.