Skip to content

Commit

Permalink
extracted common classes and methods
Browse files Browse the repository at this point in the history
  • Loading branch information
souvik-karfa committed Aug 7, 2024
1 parent fd62653 commit c4a0b88
Show file tree
Hide file tree
Showing 11 changed files with 240 additions and 233 deletions.
Original file line number Diff line number Diff line change
@@ -1,33 +1,22 @@
package io.jenkins.plugins.traceable.ast;

import com.google.common.base.Charsets;
import com.google.common.io.CharStreams;
import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.FilePath.FileCallable;
import hudson.Launcher;
import hudson.model.AbstractProject;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.remoting.VirtualChannel;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Builder;
import hudson.util.Secret;
import io.jenkins.plugins.traceable.ast.scan.helper.Assets;
import io.jenkins.plugins.traceable.ast.scan.helper.TrafficType;
import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;
import java.util.UUID;
import io.jenkins.plugins.traceable.ast.scan.utils.DownloadTraceableCliBinary;
import io.jenkins.plugins.traceable.ast.scan.utils.RunScript;
import java.io.IOException;
import jenkins.tasks.SimpleBuildStep;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.remoting.RoleChecker;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

Expand Down Expand Up @@ -478,233 +467,17 @@ private void abortScan(FilePath workspace, TaskListener listener) {

private void runScript(FilePath workspace, TaskListener listener, String scriptPath, String[] args, String caller) {
try {
String tempFilePath = workspace.act(new CopyScript(scriptPath));

if (caller.equals("initAndRunScan")) {
TraceableASTInitAndRunStepBuilder.scanId =
workspace.act(new RunScript(listener, tempFilePath, args, caller));
workspace.act(new RunScript(listener, scriptPath, args, caller));
} else if (caller.equals("abortScan")) {
workspace.act(new RunScript(listener, tempFilePath, args, caller));
workspace.act(new RunScript(listener, scriptPath, args, caller));
}

deleteScript(workspace, listener, tempFilePath);

} catch (Exception e) {
log.error("Exception in running {} script : {}", scriptPath, e);
e.printStackTrace();
}
}

private void deleteScript(FilePath workspace, TaskListener listener, String scriptPath)
throws IOException, InterruptedException {
String[] command = {"rm", scriptPath};
Launcher nodeLauncher = workspace.createLauncher(listener);

nodeLauncher
.launch()
.cmds(command)
.stdout(listener.getLogger())
.stderr(listener.getLogger())
.join();
}

// Download the binary at the workspace node if the location is remote
private static final class DownloadTraceableCliBinary implements FileCallable<Void> {

private String workspacePath;
private String version;
private String osName;
private String arch;
private String filename;

DownloadTraceableCliBinary(String version) {
this.version = version;
}

@Override
public Void invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {

this.workspacePath = f.getAbsolutePath();
this.osName = getKernelName();
this.arch = getArchName();

URL url = new URL(getDownloadUrl());
String filepath = this.workspacePath + "/" + this.filename;

try (InputStream inp = url.openStream();
BufferedInputStream bis = new BufferedInputStream(inp);
FileOutputStream fops = new FileOutputStream(filepath)) {

byte[] d = new byte[1024];
int i;
while ((i = bis.read(d, 0, 1024)) != -1) {
fops.write(d, 0, i);
}
}

unTar(filepath);

return null;
}

@Override
public void checkRoles(RoleChecker checker) throws SecurityException {
return;
}

private String getKernelName() throws IOException, InterruptedException {
String[] command = {"uname", "-s"};
ProcessBuilder pb = new ProcessBuilder(command);
Process p = pb.start();

// Catch the output
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuilder output = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
output.append(line).append(System.lineSeparator());
}

p.waitFor();
return output.toString();
}

private String getArchName() {
if (this.osName.equals("Darwin")) {
return "macosx-x86_64.tar.gz";
}

return "linux-x86_64.tar.gz";
}

private String getDownloadUrl() {
String url = "";
this.version = this.version.replace(" ", "");
if (this.version == null || this.version.isEmpty()) {
this.version = "''";
}

if (this.version.contains("-rc.")) {
url = "https://downloads.traceable.ai/cli/rc/" + version + "/traceable-cli-" + version + "-" + arch;
filename = "traceable-cli-" + version + "-" + arch;
} else if (this.version.equals("latest") || this.version.equals("''")) {
url = "https://downloads.traceable.ai/cli/release/latest/traceable-cli-latest-" + arch;
filename = "traceable-cli-latest-" + arch;
} else {
url = "https://downloads.traceable.ai/cli/release/" + version + "/traceable-cli-" + version + "-"
+ arch;
filename = "traceable-cli-" + version + "-" + arch;
}

return url;
}

private void unTar(String filepath) throws IOException, InterruptedException {
String[] command = {"tar", "-xvf", filepath, "--directory", this.workspacePath};
ProcessBuilder pb = new ProcessBuilder(command);
Process p = pb.start();
p.waitFor();
}
}

private static final class CopyScript implements FileCallable<String> {

private final String scriptPath;

public CopyScript(String scriptPath) {
this.scriptPath = scriptPath;
}

@Override
public String invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {

String bundledScript = CharStreams.toString(new InputStreamReader(
Objects.requireNonNull(getClass().getResourceAsStream(this.scriptPath)), Charsets.UTF_8));

File tempFile = File.createTempFile(
"script_" + this.scriptPath.replaceAll(".sh", "") + "_"
+ UUID.randomUUID().toString(),
".sh");

BufferedWriter x = com.google.common.io.Files.newWriter(tempFile, Charsets.UTF_8);
x.write(bundledScript);
x.close();

return tempFile.getAbsolutePath();
}

@Override
public void checkRoles(RoleChecker checker) throws SecurityException {
return;
}
}

private static final class RunScript implements FileCallable<String> {

private final TaskListener listener;
private final String scriptPath;
private static String scanId = null;
private String[] args;
private final String caller;

RunScript(TaskListener listener, String scriptPath, String[] args, String caller) {
this.listener = listener;
this.scriptPath = scriptPath;
this.args = args;
this.caller = caller;
}

@Override
public String invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {

List<String> command = new ArrayList<>();
command.add("/bin/bash");
command.add(scriptPath);

for (int i = 0; i < args.length; i++) {
if (!StringUtils.isEmpty(args[i])) {
args[i] = args[i].replace(" ", "");
}

if (args[i] != null && !args[i].isEmpty()) command.add(args[i]);
else command.add("''");
}

ProcessBuilder pb = new ProcessBuilder(command);
pb.redirectErrorStream(true);
Process p = pb.start();
logOutput(p.getInputStream(), "");
logOutput(p.getErrorStream(), "Error: ");
p.waitFor();

return RunScript.scanId;
}

@Override
public void checkRoles(RoleChecker checker) throws SecurityException {
return;
}

private void logOutput(InputStream inputStream, String prefix) {
new Thread(() -> {
Scanner scanner = new Scanner(inputStream, StandardCharsets.UTF_8);
while (scanner.hasNextLine()) {
synchronized (this) {
String line = scanner.nextLine();
// Extract the scan ID from the cli output of scan init command.
if (prefix.isEmpty() && line.contains("Running scan with ID")) {
String[] tokens = line.split(" ");
RunScript.scanId = tokens[tokens.length - 1].substring(0, 36);
}

if (!caller.equals("abortScan")) {
listener.getLogger().println(prefix + line);
}
}
}
scanner.close();
})
.start();
e.printStackTrace(listener.getLogger());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package io.jenkins.plugins.traceable.ast.scan.utils;

import hudson.FilePath.FileCallable;
import hudson.remoting.VirtualChannel;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import org.jenkinsci.remoting.RoleChecker;

public class DownloadTraceableCliBinary implements FileCallable<Void> {

private String workspacePath;
private String version;
private String osName;
private String arch;
private String filename;

public DownloadTraceableCliBinary(String version) {
this.version = version;
}

@Override
public Void invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {

this.workspacePath = f.getAbsolutePath();
this.osName = getKernelName();
this.arch = getArchName();

URL url = new URL(getDownloadUrl());
String filepath = this.workspacePath + "/" + this.filename;

try (InputStream inp = url.openStream();
BufferedInputStream bis = new BufferedInputStream(inp);
FileOutputStream fops = new FileOutputStream(filepath)) {

byte[] d = new byte[1024];
int i;
while ((i = bis.read(d, 0, 1024)) != -1) {
fops.write(d, 0, i);
}
}

unTar(filepath);

return null;
}

@Override
public void checkRoles(RoleChecker checker) throws SecurityException {
// We want this to be able to run on any type of machine (controller or agent)
return;
}

private String getKernelName() throws IOException, InterruptedException {
String[] command = {"uname", "-s"};
ProcessBuilder pb = new ProcessBuilder(command);
Process p = pb.start();

// Catch the output
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuilder output = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
output.append(line).append(System.lineSeparator());
}

p.waitFor();
return output.toString();
}

private String getArchName() {
if (this.osName.equals("Darwin")) {
return "macosx-x86_64.tar.gz";
}

return "linux-x86_64.tar.gz";
}

private String getDownloadUrl() {
String url = "";
this.version = this.version.replace(" ", "");
if (this.version == null || this.version.isEmpty()) {
this.version = "''";
}

if (this.version.contains("-rc.")) {
url = "https://downloads.traceable.ai/cli/rc/" + version + "/traceable-cli-" + version + "-" + arch;
filename = "traceable-cli-" + version + "-" + arch;
} else if (this.version.equals("latest") || this.version.equals("''")) {
url = "https://downloads.traceable.ai/cli/release/latest/traceable-cli-latest-" + arch;
filename = "traceable-cli-latest-" + arch;
} else {
url = "https://downloads.traceable.ai/cli/release/" + version + "/traceable-cli-" + version + "-" + arch;
filename = "traceable-cli-" + version + "-" + arch;
}

return url;
}

private void unTar(String filepath) throws IOException, InterruptedException {
String[] command = {"tar", "-xvf", filepath, "--directory", this.workspacePath};
ProcessBuilder pb = new ProcessBuilder(command);
Process p = pb.start();
p.waitFor();
}
}
Loading

0 comments on commit c4a0b88

Please sign in to comment.