Skip to content

Commit 80cf84a

Browse files
authored
Merge pull request #345 from FlowCI/feature/1433
load default template from url
2 parents 5558b72 + 86ab451 commit 80cf84a

File tree

10 files changed

+56
-72
lines changed

10 files changed

+56
-72
lines changed

core/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,6 @@
129129
<artifactId>springfox-swagger-ui</artifactId>
130130
</dependency>
131131

132-
<dependency>
133-
<groupId>org.apache.velocity</groupId>
134-
<artifactId>velocity-engine-core</artifactId>
135-
</dependency>
136-
137132
<dependency>
138133
<groupId>com.cronutils</groupId>
139134
<artifactId>cron-utils</artifactId>

core/src/main/java/com/flowci/core/common/config/AppConfig.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
import com.flowci.core.common.helper.JacksonHelper;
2222
import com.flowci.util.FileHelper;
2323
import lombok.extern.log4j.Log4j2;
24+
import org.apache.http.client.HttpClient;
25+
import org.apache.http.client.config.RequestConfig;
2426
import org.apache.http.client.utils.URIBuilder;
27+
import org.apache.http.impl.client.HttpClientBuilder;
2528
import org.springframework.beans.factory.annotation.Autowired;
2629
import org.springframework.boot.autoconfigure.web.servlet.MultipartProperties;
2730
import org.springframework.cache.annotation.EnableCaching;
@@ -91,6 +94,18 @@ public ObjectMapper objectMapper() {
9194
return JacksonHelper.create();
9295
}
9396

97+
@Bean("httpClient")
98+
public HttpClient httpClient() {
99+
int timeout = 10 * 1000;
100+
RequestConfig config = RequestConfig.custom()
101+
.setConnectTimeout(timeout)
102+
.setConnectionRequestTimeout(timeout)
103+
.setSocketTimeout(timeout)
104+
.build();
105+
106+
return HttpClientBuilder.create().setDefaultRequestConfig(config).build();
107+
}
108+
94109
@Bean(name = "applicationEventMulticaster")
95110
public ApplicationEventMulticaster simpleApplicationEventMulticaster() {
96111
SimpleApplicationEventMulticaster multicaster = new SimpleApplicationEventMulticaster() {

core/src/main/java/com/flowci/core/common/config/AppProperties.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ public Zookeeper zk() {
7272
return new Zookeeper();
7373
}
7474

75+
@Bean("flowProperties")
76+
@ConfigurationProperties(prefix = "app.flow")
77+
public Flow flow() {
78+
return new Flow();
79+
}
80+
7581
@Bean("jobProperties")
7682
@ConfigurationProperties(prefix = "app.job")
7783
public Job job() {
@@ -114,6 +120,12 @@ public static class Admin {
114120
private String defaultPassword;
115121
}
116122

123+
@Data
124+
public static class Flow {
125+
126+
private String defaultTemplateUrl;
127+
}
128+
117129
@Data
118130
public static class Job {
119131

core/src/main/java/com/flowci/core/flow/config/FlowConfig.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,22 @@
1616

1717
package com.flowci.core.flow.config;
1818

19+
import com.flowci.core.common.config.AppProperties;
1920
import com.flowci.core.common.helper.CacheHelper;
2021
import com.flowci.core.common.helper.ThreadHelper;
2122
import com.github.benmanes.caffeine.cache.Cache;
2223
import lombok.extern.log4j.Log4j2;
23-
import org.apache.velocity.Template;
24-
import org.apache.velocity.app.Velocity;
24+
import org.apache.http.HttpResponse;
25+
import org.apache.http.client.HttpClient;
26+
import org.apache.http.client.methods.HttpGet;
27+
import org.apache.http.util.EntityUtils;
28+
import org.springframework.beans.factory.annotation.Autowired;
2529
import org.springframework.context.annotation.Bean;
2630
import org.springframework.context.annotation.Configuration;
2731
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
2832

33+
import java.io.IOException;
2934
import java.util.List;
30-
import java.util.Properties;
3135

3236
/**
3337
* @author yang
@@ -36,28 +40,28 @@
3640
@Configuration
3741
public class FlowConfig {
3842

39-
private static final Properties templateProperties = new Properties();
43+
@Autowired
44+
private AppProperties.Flow flowProperties;
4045

41-
static {
42-
templateProperties.setProperty("resource.loader", "class");
43-
templateProperties.setProperty("class.resource.loader.class",
44-
"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
45-
46-
Velocity.init(templateProperties);
47-
}
46+
@Autowired
47+
private HttpClient httpClient;
4848

4949
@Bean("gitTestExecutor")
5050
public ThreadPoolTaskExecutor gitTestExecutor() {
5151
return ThreadHelper.createTaskExecutor(20, 20, 100, "git-test-");
5252
}
5353

54-
@Bean
55-
public Template defaultYmlTemplate() {
56-
return Velocity.getTemplate("templates/example.yml.vm");
57-
}
58-
5954
@Bean("gitBranchCache")
6055
public Cache<String, List<String>> gitBranchCache() {
6156
return CacheHelper.createLocalCache(50, 300);
6257
}
58+
59+
@Bean("defaultTemplateYml")
60+
public String defaultTemplateYml() throws IOException {
61+
String url = flowProperties.getDefaultTemplateUrl();
62+
HttpResponse response = httpClient.execute(new HttpGet(url));
63+
String yml = EntityUtils.toString(response.getEntity());
64+
log.info("Default template yml is loaded from {}", url);
65+
return yml;
66+
}
6367
}

core/src/main/java/com/flowci/core/flow/service/YmlServiceImpl.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232
import com.flowci.tree.YmlParser;
3333
import com.flowci.util.StringHelper;
3434
import com.google.common.base.Strings;
35-
import org.apache.velocity.Template;
36-
import org.apache.velocity.VelocityContext;
3735
import org.springframework.beans.factory.annotation.Autowired;
3836
import org.springframework.context.event.EventListener;
3937
import org.springframework.stereotype.Service;
@@ -51,7 +49,7 @@
5149
public class YmlServiceImpl implements YmlService {
5250

5351
@Autowired
54-
private Template defaultYmlTemplate;
52+
private String defaultTemplateYml;
5553

5654
@Autowired
5755
private YmlDao ymlDao;
@@ -115,7 +113,7 @@ public Yml saveYml(Flow flow, String yml) {
115113

116114
@Override
117115
public Yml saveDefaultTemplate(Flow flow) {
118-
return saveYml(flow, getTemplateYml());
116+
return saveYml(flow, defaultTemplateYml);
119117
}
120118

121119
//====================================================================
@@ -159,15 +157,4 @@ private Optional<RuntimeException> verifyPlugins(FlowNode flowNode) {
159157

160158
return Optional.empty();
161159
}
162-
163-
private String getTemplateYml() {
164-
VelocityContext context = new VelocityContext();
165-
166-
try (StringWriter sw = new StringWriter()) {
167-
defaultYmlTemplate.merge(context, sw);
168-
return sw.toString();
169-
} catch (IOException e) {
170-
return StringHelper.EMPTY;
171-
}
172-
}
173160
}

core/src/main/resources/flow.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ app.auth.enabled=true
1414
app.auth.expire-seconds=7200
1515
app.auth.refresh-expired-seconds=14400
1616

17+
app.flow.default-template-url=${FLOWCI_DEFAULT_TEMPLATE:https://raw.githubusercontent.com/FlowCI/templates/master/helloworld.yaml}
18+
1719
app.job.retry-waiting-seconds=10
1820

1921
app.plugin.default-repo=${FLOWCI_PLUGIN_URL:https://raw.githubusercontent.com/FlowCI/plugins/master/repository.json}

core/src/main/resources/templates/example.yml.vm

Lines changed: 0 additions & 26 deletions
This file was deleted.

core/src/test/java/com/flowci/core/test/flow/FlowServiceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public void should_create_and_confirm_flow_with_git_template() {
134134
Assert.assertNotNull(yml);
135135

136136
Node root = YmlParser.load("test", yml.getRaw());
137-
Assert.assertEquals(1, root.getChildren().size());
137+
Assert.assertTrue(root.getChildren().size() > 0);
138138

139139
// then:
140140
List<Flow> flows = flowService.list(Status.CONFIRMED);

core/src/test/resources/flow.properties

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ app.auth.enabled=false
1414
app.auth.expire-seconds=5
1515
app.auth.refresh-expired-seconds=7200
1616

17+
app.flow.default-template-url=${FLOWCI_DEFAULT_TEMPLATE:https://raw.githubusercontent.com/FlowCI/templates/master/helloworld.yaml}
18+
1719
app.job.retry-waiting-seconds=5
1820

1921
app.plugin.default-repo=https://raw.githubusercontent.com/yang-guo-2016/flowci-plugins/master/plugin-repos.json
@@ -27,8 +29,8 @@ app.minio.secret = minio123
2729

2830
app.rabbitmq.uri=amqp://guest:[email protected]:5672
2931
app.rabbitmq.callback-queue = flow.q.callback-test
30-
app.rabbitmq.shell-log-exchange = flow.ex.shelllog-test
31-
app.rabbitmq.tty-log-exchange = flow.ex.ttylog-test
32+
app.rabbitmq.shell-log-ex = flow.ex.shelllog-test
33+
app.rabbitmq.tty-log-ex = flow.ex.ttylog-test
3234
app.rabbitmq.job-dl-queue = flow.q.job-dl-test
3335
app.rabbitmq.job-dl-exchange = flow.ex.job-dl-test
3436

pom.xml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
<groovy-all.version>2.4.12</groovy-all.version>
3939
<docker-java.version>3.2.1</docker-java.version>
4040
<cron-utils.version>7.0.5</cron-utils.version>
41-
<velocity-engine-core.version>2.1</velocity-engine-core.version>
4241
<amqp-client.version>5.9.0</amqp-client.version>
4342
<java-jwt.version>3.8.1</java-jwt.version>
4443
<minio.version>6.0.11</minio.version>
@@ -204,12 +203,6 @@
204203
<version>${jackson.version}</version>
205204
</dependency>
206205

207-
<dependency>
208-
<groupId>org.apache.velocity</groupId>
209-
<artifactId>velocity-engine-core</artifactId>
210-
<version>${velocity-engine-core.version}</version>
211-
</dependency>
212-
213206
<dependency>
214207
<groupId>org.springframework.boot</groupId>
215208
<artifactId>spring-boot-dependencies</artifactId>

0 commit comments

Comments
 (0)