Skip to content

Commit 537a56c

Browse files
author
Yang Guo
committed
Merge branch 'develop' into feature/api/branches_cache
2 parents 69f7a75 + a9d31b7 commit 537a56c

File tree

52 files changed

+1158
-203
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1158
-203
lines changed

docker/app-cc.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ mq.host = amqp://localhost:5672
2828
mq.management.host = http://localhost:15672
2929

3030
#### cmd queue settings ###
31+
queue.cmd.retry.enable = false
3132
queue.cmd.rabbit.enable = false
3233
queue.cmd.rabbit.name = flow-cmd-queue-default
3334
queue.cmd.idle_agent.timeout = 30
Binary file not shown.

platform-api/src/main/java/com/flow/platform/api/controller/AgentController.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public List<AgentWithFlow> index() {
8686
* }
8787
*/
8888
@PostMapping(path = "/create")
89-
public Agent create(@RequestBody AgentPath agentPath) {
89+
public AgentWithFlow create(@RequestBody AgentPath agentPath) {
9090
if (agentPath.isEmpty()) {
9191
throw new IllegalParameterException("Zone and agent name are required");
9292
}
@@ -195,4 +195,30 @@ public BooleanValue shutDown(@RequestParam String zone,
195195
public void callback(@RequestBody Agent agent) {
196196
this.dispatchEvent(new AgentStatusChangeEvent(this, agent));
197197
}
198+
199+
/**
200+
* @api {Post} /agents/delete Delete
201+
* @apiName Delete Agent
202+
* @apiGroup Agent
203+
* @apiDescription Delete agent by agentPath
204+
*
205+
* @apiParamExample {json} Request-Example:
206+
* {
207+
* zone: xxx,
208+
* name: xxx
209+
* }
210+
*
211+
* @apiSuccessExample {json} Success-Response:
212+
* HTTP/1.1 200 OK
213+
*
214+
* @apiErrorExample {json} Error-Response:
215+
* HTTP/1.1 400
216+
* {
217+
* "message": xxx
218+
* }
219+
*/
220+
@PostMapping(path = "/delete")
221+
public void delete(@RequestBody AgentPath agentPath){
222+
agentService.delete(agentPath);
223+
}
198224
}

platform-api/src/main/java/com/flow/platform/api/controller/FlowController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public Node createEmptyFlow() {
114114
}
115115

116116
/**
117-
* @api {delete} /flows/:root/delete Delete
117+
* @api {delete} /flows/:root Delete
118118
* @apiParam {String} root flow node name will be deleted
119119
* @apiDescription Delete flow node by name and return flow node object
120120
* @apiGroup Flows

platform-api/src/main/java/com/flow/platform/api/domain/envs/GitEnvs.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ public enum GitEnvs implements EnvKey {
2626
*/
2727
FLOW_GIT_SOURCE,
2828

29+
/**
30+
* For diff FLOW_GIT_SOURCE
31+
* - UNDEFINED_SSH and UNDEFINED_HTTP is repo url
32+
* - GITLAB is gitlab host url
33+
*/
2934
FLOW_GIT_URL,
3035

3136
FLOW_GIT_BRANCH,
@@ -69,5 +74,12 @@ public enum GitEnvs implements EnvKey {
6974

7075
FLOW_GIT_HTTP_USER,
7176

72-
FLOW_GIT_HTTP_PASS
77+
FLOW_GIT_HTTP_PASS,
78+
79+
/**
80+
* Env variables for GitLab
81+
*/
82+
FLOW_GITLAB_TOKEN,
83+
84+
FLOW_GITLAB_PROJECT
7385
}

platform-api/src/main/java/com/flow/platform/api/git/GitClientBuilder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.flow.platform.api.domain.envs.GitEnvs;
2020
import com.flow.platform.api.domain.node.Node;
2121
import com.flow.platform.util.git.GitClient;
22+
import com.flow.platform.util.git.GitException;
2223
import java.nio.file.Path;
2324

2425
/**
@@ -41,5 +42,5 @@ public GitClientBuilder(final Node node, final Path sourceFolder) {
4142
this.sourceFolder = sourceFolder;
4243
}
4344

44-
public abstract GitClient build();
45+
public abstract GitClient build() throws GitException;
4546
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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.git;
18+
19+
import com.flow.platform.api.domain.envs.GitEnvs;
20+
import com.flow.platform.api.domain.node.Node;
21+
import com.flow.platform.util.git.GitClient;
22+
import com.flow.platform.util.git.GitException;
23+
import com.flow.platform.util.git.GitLabClient;
24+
import java.nio.file.Path;
25+
26+
/**
27+
* @author yang
28+
*/
29+
public class GitLabClientBuilder extends GitClientBuilder {
30+
31+
private final String host;
32+
33+
private final String token;
34+
35+
private final String project;
36+
37+
public GitLabClientBuilder(Node node, Path sourceFolder) {
38+
super(node, sourceFolder);
39+
host = node.getEnv(GitEnvs.FLOW_GIT_URL);
40+
token = node.getEnv(GitEnvs.FLOW_GITLAB_TOKEN);
41+
project = node.getEnv(GitEnvs.FLOW_GITLAB_PROJECT);
42+
}
43+
44+
@Override
45+
public GitClient build() throws GitException {
46+
return new GitLabClient(host, token, project);
47+
}
48+
}

platform-api/src/main/java/com/flow/platform/api/service/AgentService.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,17 @@ public interface AgentService {
4242
/**
4343
* Create agent and return token from cc
4444
*/
45-
Agent create(AgentPath agentPath);
45+
AgentWithFlow create(AgentPath agentPath);
4646

4747
/**
4848
* Get agent setting by token from cc
4949
*/
5050
AgentSettings settings(String token);
5151

52+
/**
53+
* Delete agent by agentPath from cc
54+
*/
55+
void delete(AgentPath agentPath);
5256
/**
5357
* send sys cmd
5458
* @param agentPath required

platform-api/src/main/java/com/flow/platform/api/service/AgentServiceImpl.java

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.flow.platform.domain.AgentPath;
2929
import com.flow.platform.domain.AgentPathWithWebhook;
3030
import com.flow.platform.domain.AgentSettings;
31+
import com.flow.platform.domain.AgentStatus;
3132
import com.flow.platform.domain.CmdInfo;
3233
import com.flow.platform.domain.CmdType;
3334
import com.flow.platform.domain.Jsonable;
@@ -134,7 +135,7 @@ public Boolean shutdown(String zone, String name, String password) {
134135
}
135136

136137
@Override
137-
public Agent create(AgentPath agentPath) {
138+
public AgentWithFlow create(AgentPath agentPath) {
138139
try {
139140
AgentPathWithWebhook pathWithWebhook = new AgentPathWithWebhook(agentPath, buildAgentWebhook());
140141

@@ -148,7 +149,10 @@ public Agent create(AgentPath agentPath) {
148149
throw new HttpException("Unable to create agent via control center");
149150
}
150151

151-
return Agent.parse(response.getBody(), Agent.class);
152+
Agent agent = Agent.parse(response.getBody(), Agent.class);
153+
154+
AgentWithFlow agentWithFlow = new AgentWithFlow(agent, null);
155+
return agentWithFlow;
152156

153157
} catch (UnsupportedEncodingException | JsonSyntaxException e) {
154158
throw new IllegalStatusException("Unable to create agent", e);
@@ -170,13 +174,58 @@ public AgentSettings settings(String token) {
170174
return AgentSettings.parse(response.getBody(), AgentSettings.class);
171175
}
172176

173-
private String buildAgentWebhook() {
174-
return domain + "/agents/callback";
177+
@Override
178+
public void delete(AgentPath agentPath) {
179+
Agent agent = findAgent(agentPath);
180+
181+
try {
182+
HttpClient.build(platformURL.getAgentDeleteUrl())
183+
.post(agent.toJson())
184+
.withContentType(ContentType.APPLICATION_JSON)
185+
.retry(httpRetryTimes)
186+
.bodyAsString();
187+
188+
} catch (UnsupportedEncodingException e) {
189+
throw new IllegalStatusException(e.getMessage());
190+
}
175191
}
176192

177193
@Override
178194
public void sendSysCmd(AgentPath agentPath) {
179195
CmdInfo cmdInfo = new CmdInfo(agentPath, CmdType.SYSTEM_INFO, "");
180196
cmdService.sendCmd(agentPath, cmdInfo);
181197
}
198+
199+
private String buildAgentWebhook() {
200+
return domain + "/agents/callback";
201+
}
202+
203+
/**
204+
* find agent
205+
*/
206+
private Agent findAgent(AgentPath agentPath) {
207+
String url =
208+
platformURL.getAgentFindUrl() + "?" + "zone=" + agentPath.getZone() + "&" + "name=" + agentPath.getName();
209+
HttpResponse<String> response = HttpClient.build(url)
210+
.get()
211+
.retry(httpRetryTimes)
212+
.bodyAsString();
213+
214+
if (!response.hasSuccess()) {
215+
throw new HttpException("Unable to delete agent");
216+
}
217+
218+
Agent agent = Agent.parse(response.getBody(), Agent.class);
219+
220+
if (agent == null){
221+
throw new IllegalStatusException("agent is not exist");
222+
}
223+
224+
if (agent.getStatus() == AgentStatus.BUSY) {
225+
throw new IllegalStatusException("agent is busy, please wait");
226+
}
227+
228+
return agent;
229+
}
230+
182231
}

platform-api/src/main/java/com/flow/platform/api/service/GitService.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ interface ProgressListener {
4646
void onProgressing(String task, int total, int progress);
4747

4848
void onFinishTask(String task);
49-
50-
void onFinish();
5149
}
5250

5351
/**
@@ -58,7 +56,7 @@ interface ProgressListener {
5856
* @param progress listener for git progress
5957
* @return file content
6058
*/
61-
String clone(Node node, String filePath, ProgressListener progress) throws GitException;
59+
String fetch(Node node, String filePath, ProgressListener progress) throws GitException;
6260

6361
/**
6462
* Fetch branches from git repo

0 commit comments

Comments
 (0)