Skip to content

Commit

Permalink
Merge pull request #70 from jenkinsci/feature/project-env-cli-from-path
Browse files Browse the repository at this point in the history
Add Project-Env CLI PATH handling
  • Loading branch information
repolevedavaj authored Dec 14, 2022
2 parents 4e31564 + 6c6f723 commit 00058ca
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import hudson.EnvVars;
import hudson.FilePath;
import hudson.Launcher.ProcStarter;
import hudson.Util;
import io.jenkins.plugins.projectenv.agent.AgentInfo;
import io.jenkins.plugins.projectenv.agent.AgentInfoCallable;
Expand Down Expand Up @@ -84,22 +83,35 @@ private void execute() throws Exception {
AgentInfo agentInfo = getAgentInfo();

FilePath temporaryDirectory = createTemporaryDirectory();
EnvVars projectEnvVars = new EnvVars();

FilePath projectEnvCliArchive = downloadProjectEnvCliArchive(agentInfo, temporaryDirectory);
extractProjectEnvCliArchive(projectEnvCliArchive, temporaryDirectory);

FilePath executable = resolveProjectEnvCliExecutable(agentInfo, temporaryDirectory);
String executable = resolveProjectEnvCliExecutableFromPath();
if (executable == null) {
FilePath projectEnvCliArchive = downloadProjectEnvCliArchive(agentInfo, temporaryDirectory);
extractProjectEnvCliArchive(projectEnvCliArchive, temporaryDirectory);

executable = resolveProjectEnvCliExecutable(agentInfo, temporaryDirectory);
projectEnvVars.put(PATH_VAR_PREFIX + "PROJECT_ENV_CLI", temporaryDirectory.getRemote());
}
Map<String, List<ToolInfo>> allToolInfos = executeProjectEnvCli(executable);

EnvVars projectEnvVars = processToolInfos(allToolInfos);
processToolInfos(projectEnvVars, allToolInfos);
projectEnvVars.put(PATH_VAR_PREFIX + "PROJECT_ENV_CLI", temporaryDirectory.getRemote());
projectEnvVars.put(PATH_VAR_PREFIX + "PROJECT_ENV_CLI", temporaryDirectory.getRemote());

BodyExecutionCallback callback = createTempDirectoryCleanupCallback(temporaryDirectory);

invokeBodyWithEnvVarsAndCallback(projectEnvVars, callback);
}

private String resolveProjectEnvCliExecutableFromPath() throws Exception {
String executable = getProjectEnvCliExecutableName(getAgentInfo());

OperatingSystem operatingSystem = getAgentInfo().getOperatingSystem();
if (operatingSystem == OperatingSystem.WINDOWS) {
return ProcHelper.executeAndGetStdOut(getContext(), "where", executable);
} else {
return ProcHelper.executeAndGetStdOut(getContext(), "/bin/sh", "-c", "which " + executable);
}
}

private AgentInfo getAgentInfo() throws Exception {
return StepContextHelper.getComputer(getContext()).getChannel().call(new AgentInfoCallable());
}
Expand Down Expand Up @@ -208,52 +220,47 @@ private void extractProjectEnvCliArchive(FilePath archive, FilePath target) thro
}
}

private FilePath resolveProjectEnvCliExecutable(AgentInfo agentInfo, FilePath sourceDirectory) throws Exception {
String executableFilename = CLI_EXECUTABLE_FILE_NAME + getExecutableExtension(agentInfo);
private String resolveProjectEnvCliExecutable(AgentInfo agentInfo, FilePath sourceDirectory) throws Exception {
String executableFilename = getProjectEnvCliExecutableName(agentInfo);
FilePath executable = sourceDirectory.child(executableFilename);
if (!executable.exists()) {
throw new IllegalStateException("could not find Project-Env CLI at " + executable);
}

return executable;
return executable.getRemote();
}

private String getProjectEnvCliExecutableName(AgentInfo agentInfo) {
return CLI_EXECUTABLE_FILE_NAME + getExecutableExtension(agentInfo);
}

private String getExecutableExtension(AgentInfo agentInfo) {
return agentInfo.getOperatingSystem() == OperatingSystem.WINDOWS ?
CLI_EXECUTABLE_FILE_EXTENSION_WINDOWS : CLI_EXECUTABLE_FILE_EXTENSION_OTHERS;
}

private Map<String, List<ToolInfo>> executeProjectEnvCli(FilePath executable) throws Exception {
List<String> commands = createProjectEnvCliCommand(executable);
FilePath workspace = StepContextHelper.getWorkspace(getContext());

ProcStarter procStarter = StepContextHelper.getLauncher(getContext())
.launch()
.cmds(commands)
.pwd(workspace);

ProcResult procResult = ProcHelper.executeAndReturnStdOut(procStarter, getContext());
private Map<String, List<ToolInfo>> executeProjectEnvCli(String executable) throws Exception {
String[] commands = createProjectEnvCliCommand(executable);
ProcResult procResult = ProcHelper.execute(getContext(), commands);
if (procResult.getExitCode() != 0) {
throw new IllegalStateException("received non-zero exit code " + procResult.getExitCode() + " from Project-Env CLI");
}

return ToolInfoParser.fromJson(procResult.getStdOutput());
}

private List<String> createProjectEnvCliCommand(FilePath executable) {
private String[] createProjectEnvCliCommand(String executable) {
List<String> command = new ArrayList<>();
command.add(executable.getRemote());
command.add(executable);
command.add("--config-file=" + configFile);
if (cliDebug) {
command.add("--debug");
}

return command;
return command.toArray(new String[0]);
}

private EnvVars processToolInfos(Map<String, List<ToolInfo>> allToolInfos) {
EnvVars envVars = new EnvVars();

private void processToolInfos(EnvVars envVars, Map<String, List<ToolInfo>> allToolInfos) {
for (Map.Entry<String, List<ToolInfo>> entry : allToolInfos.entrySet()) {
for (ToolInfo toolInfo : entry.getValue()) {
List<String> pathElements = toolInfo.getPathElements();
Expand All @@ -266,8 +273,6 @@ private EnvVars processToolInfos(Map<String, List<ToolInfo>> allToolInfos) {
envVars.putAll(toolInfo.getEnvironmentVariables());
}
}

return envVars;
}

private BodyExecutionCallback createTempDirectoryCleanupCallback(FilePath tempDirectory) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.jenkins.plugins.projectenv.context;

import hudson.EnvVars;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.Computer;
Expand All @@ -19,6 +20,10 @@ public static Launcher getLauncher(StepContext stepContext) throws Exception {
return getOrThrow(stepContext, Launcher.class);
}

public static EnvVars getEnvVars(StepContext stepContext) throws Exception {
return getOrThrow(stepContext, EnvVars.class);
}

public static TaskListener getTaskListener(StepContext stepContext) throws Exception {
return getOrThrow(stepContext, TaskListener.class);
}
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/io/jenkins/plugins/projectenv/proc/ProcHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,24 @@ private ProcHelper() {
// noop
}

public static ProcResult executeAndReturnStdOut(ProcStarter procStarter, StepContext context) throws Exception {
public static String executeAndGetStdOut(StepContext context, String... commands) throws Exception {
ProcResult procResult = execute(context, commands);
if (procResult.getExitCode() == 0) {
return procResult.getStdOutput();
} else {
return null;
}
}

public static ProcResult execute(StepContext context, String... commands) throws Exception {
try (ByteArrayOutputStream stdOutInputStream = new ByteArrayOutputStream()) {

ProcStarter procStarter = StepContextHelper.getLauncher(context)
.launch()
.cmds(commands)
.envs(StepContextHelper.getEnvVars(context))
.pwd(StepContextHelper.getWorkspace(context));

Thread stdErrThread;
int exitCode;
try (PipedInputStream stdErrInputStream = new PipedInputStream();
Expand Down

0 comments on commit 00058ca

Please sign in to comment.