Skip to content

Commit 2ff5668

Browse files
author
Yang Guo
committed
enable to create agent workspace by FLOW_AGNET_WORKSAPCE varable
1 parent dced192 commit 2ff5668

File tree

3 files changed

+46
-10
lines changed

3 files changed

+46
-10
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2017 flow.ci
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+
17+
package com.flow.platform.api.domain.envs;
18+
19+
/**
20+
* @author yang
21+
*/
22+
public enum AgentEnvs implements EnvKey {
23+
24+
FLOW_AGENT_WORKSPACE,
25+
26+
FLOW_AGENT_OUTPUT_ENV_PREFIX
27+
}

platform-api/src/main/java/com/flow/platform/api/service/job/CmdServiceImpl.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.flow.platform.api.service.job;
1818

19+
import com.flow.platform.api.domain.envs.AgentEnvs;
1920
import com.flow.platform.api.domain.job.Job;
2021
import com.flow.platform.api.domain.node.Node;
2122
import com.flow.platform.api.util.PlatformURL;
@@ -101,10 +102,11 @@ public CmdInfo runShell(Job job, Node node, String cmdId) {
101102
CmdInfo cmdInfo = new CmdInfo(zone, null, CmdType.RUN_SHELL, node.getScript());
102103
cmdInfo.setInputs(node.getEnvs());
103104
cmdInfo.setWebhook(buildCmdWebhook(job));
104-
cmdInfo.setOutputEnvFilter("FLOW_OUTPUT");
105+
cmdInfo.setOutputEnvFilter(job.getEnv(AgentEnvs.FLOW_AGENT_OUTPUT_ENV_PREFIX, "FLOW_OUTPUT"));
105106
cmdInfo.setSessionId(job.getSessionId());
106107
cmdInfo.setExtra(node.getPath()); // use cmd.extra to keep node path info
107108
cmdInfo.setCustomizedId(cmdId);
109+
cmdInfo.setWorkingDir(job.getEnv(AgentEnvs.FLOW_AGENT_WORKSPACE, null));
108110

109111
try {
110112
LOGGER.traceMarker("RunShell", "step name - %s, node path - %s", node.getName(), node.getPath());

platform-cmd-runner/src/main/java/com/flow/platform/cmd/CmdExecutor.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import java.io.*;
2525
import java.lang.reflect.Field;
26+
import java.nio.file.Files;
2627
import java.time.ZonedDateTime;
2728
import java.util.*;
2829
import java.util.concurrent.ConcurrentLinkedQueue;
@@ -128,12 +129,12 @@ public Thread newThread(Runnable r) {
128129
* @param cmd exec cmd
129130
*/
130131
public CmdExecutor(final ProcListener procListener,
131-
final LogListener logListener,
132-
final Map<String, String> inputs,
133-
final String workingDir,
134-
final String outputEnvFilter,
135-
final Integer timeout,
136-
final List<String> cmds) {
132+
final LogListener logListener,
133+
final Map<String, String> inputs,
134+
final String workingDir,
135+
final String outputEnvFilter,
136+
final Integer timeout,
137+
final List<String> cmds) {
137138

138139
if (procListener != null) {
139140
this.procListener = procListener;
@@ -153,9 +154,15 @@ public CmdExecutor(final ProcListener procListener,
153154
// check and init working dir
154155
if (workingDir != null) {
155156
File dir = new File(workingDir);
157+
156158
if (!dir.exists()) {
157-
throw new IllegalArgumentException(String.format("Cmd defined working dir '%s' not found", workingDir));
159+
try {
160+
Files.createDirectories(dir.toPath());
161+
} catch (IOException e) {
162+
throw new IllegalArgumentException("Unable to create working dir: " + dir);
163+
}
158164
}
165+
159166
this.pBuilder.directory(dir);
160167
}
161168

@@ -341,8 +348,8 @@ private Runnable createStdStreamReader(final Log.Type type, final InputStream is
341348
* put env item which match 'start with filter' to CmdResult.output map
342349
*/
343350
private void readEnv(final BufferedReader reader,
344-
final Map<String, String> output,
345-
final String filter) throws IOException {
351+
final Map<String, String> output,
352+
final String filter) throws IOException {
346353
String line;
347354
while ((line = reader.readLine()) != null) {
348355
if (line.startsWith(filter)) {

0 commit comments

Comments
 (0)