Skip to content

Commit

Permalink
Merge pull request #689 from naver/feature/support-gradle-groovy
Browse files Browse the repository at this point in the history
Support Gradle groovy script

Reviewed by : geunwoo
  • Loading branch information
songeunwoo authored Nov 27, 2020
2 parents b881228 + 4161f3c commit 37c7147
Show file tree
Hide file tree
Showing 24 changed files with 656 additions and 390 deletions.
30 changes: 28 additions & 2 deletions docker/controller/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,37 @@
FROM jeanblanchard/java:serverjre-8
MAINTAINER JunHo Yoon "[email protected]"

RUN apk update; apk add curl bash
RUN apk update; apk add curl bash tar

ARG MAVEN_VERSION=3.6.3
ARG MAVEN_DOWNLOAD_BASE_URL=https://apache.osuosl.org/maven/maven-3/${MAVEN_VERSION}/binaries

ARG GRADLE_VERSION=6.7.1
ARG GRADLE_DOWNLOAD_BASE_URL=https://services.gradle.org/distributions

# Install maven
RUN mkdir -p /usr/share/maven \
&& echo "Downloading maven" \
&& curl -fsSL -o /tmp/apache-maven.tar.gz ${MAVEN_DOWNLOAD_BASE_URL}/apache-maven-${MAVEN_VERSION}-bin.tar.gz \
&& echo "Unziping maven" \
&& tar -xzf /tmp/apache-maven.tar.gz -C /usr/share/maven --strip-components=1 \
&& rm -f /tmp/apache-maven.tar.gz

# Install gradle
RUN mkdir -p /usr/share/gradle \
&& echo "Downloading gradle" \
&& curl -fsSL -o /tmp/gradle.zip ${GRADLE_DOWNLOAD_BASE_URL}/gradle-${GRADLE_VERSION}-bin.zip \
&& echo "Unziping gradle" \
&& unzip -d /usr/share/gradle /tmp/gradle.zip \
&& rm -f /tmp/gradle.zip

# Set up environment variables
ENV BASE_DIR=/opt \
NGRINDER_HOME=/opt/ngrinder-controller
NGRINDER_HOME=/opt/ngrinder-controller \
MAVEN_HOME=/usr/share/maven \
GRADLE_HOME=/usr/share/gradle/gradle-${GRADLE_VERSION}

ENV PATH=$PATH:$GRADLE_HOME/bin:$MAVEN_HOME/bin

# Expose ports
EXPOSE 80 16001 12000-12009
Expand Down
6 changes: 5 additions & 1 deletion docker/controller/scripts/run.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#!/usr/bin/env bash
echo "wait a while extracting war files... It takes time for the first run."
echo "Installed build tools."
mvn -version
gradle -version

echo "Wait a while extracting war files... It takes time for the first run."
java -jar ${BASE_DIR}/ngrinder-*.war --port 80
2 changes: 1 addition & 1 deletion docker/prepare.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
echo "copying ngrinder-controller"
mkdir -p controller/binary
rm controller/binary/ngrinder*
cp ../ngrinder-controller/target/*.war controller/binary/
cp ../ngrinder-controller/build/libs/*.war controller/binary/
8 changes: 0 additions & 8 deletions ngrinder-controller/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,6 @@ dependencies {
compile (group: "org.python", name: "jython-standalone", version: "2.5.3")
compile (group: "com.google.guava", name: "guava", version: "20.0")
compile (group: "org.springframework.security", name: "spring-security-taglibs", version: spring_security_version)

compile (group: "org.apache.maven", name: "maven-embedder", version: "3.6.3")
compile (group: "org.apache.maven", name: "maven-compat", version: "3.6.3")
compile (group: "org.eclipse.aether", name: "aether-connector-basic", version: "1.1.0")
compile (group: "org.eclipse.aether", name: "aether-transport-wagon", version: "1.1.0")
compile (group: "org.apache.maven.wagon", name: "wagon-http", version: "2.8")
compile (group: "org.apache.maven.wagon", name: "wagon-provider-api", version: "2.8")

compile (group: "org.liquibase", name: "liquibase-core", version: "3.5.3")
compile (group: "org.hibernate", name: "hibernate-jcache", version: hibernate_version)
compile (group: "com.github.ben-manes.caffeine", name: "caffeine", version: "2.6.2")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public interface CacheConstants {
String CACHE_USER_ENTITY = "org.ngrinder.model.User";

String LOCAL_CACHE_GITHUB_SCRIPTS = "github_scripts";
String LOCAL_CACHE_GITHUB_IS_MAVEN_GROOVY = "github_is_maven_groovy";
String LOCAL_CACHE_GITHUB_GROOVY_PROJECT_SCRIPT_TYPE = "github_groovy_project_script_type";

int REGION_CACHE_TIME_TO_LIVE_SECONDS = 20;
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public CacheConfigHolder cacheConfigMap() {
cm.addLocalCache(LOCAL_CACHE_RIGHT_PANEL_ENTRIES, 1 * DAY, 1);
cm.addLocalCache(LOCAL_CACHE_LEFT_PANEL_ENTRIES, 1 * DAY, 1);
cm.addLocalCache(LOCAL_CACHE_CURRENT_PERFTEST_STATISTICS, 5, 1);
cm.addLocalCache(LOCAL_CACHE_GITHUB_IS_MAVEN_GROOVY, 5 * MIN, 300);
cm.addLocalCache(LOCAL_CACHE_GITHUB_GROOVY_PROJECT_SCRIPT_TYPE, 5 * MIN, 300);
return cm;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
package org.ngrinder.infra.spring.listener;

import com.github.benmanes.caffeine.jcache.spi.CaffeineCachingProvider;
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.boot.context.event.ApplicationContextInitializedEvent;
import org.springframework.context.ApplicationListener;

import javax.cache.Caching;
import javax.cache.spi.CachingProvider;
import java.util.Iterator;

import static org.ngrinder.starter.InstallationChecker.checkAll;

/**
* @since 3.5.0
* */
public class ApplicationPreparedListener implements ApplicationListener<ApplicationPreparedEvent> {
public class ApplicationPreparedListener implements ApplicationListener<ApplicationContextInitializedEvent> {

@SuppressWarnings("NullableProblems")
@Override
public void onApplicationEvent(ApplicationPreparedEvent event) {
public void onApplicationEvent(ApplicationContextInitializedEvent event) {
removeCacheProviderExceptCaffeineCacheProvider();
checkAll();
}

/**
Expand All @@ -31,4 +34,5 @@ private static void removeCacheProviderExceptCaffeineCacheProvider() {
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ public ScriptHandler prepareDistribution(PerfTest perfTest) throws IOException {
String scriptName = perfTest.getScriptName();
gitHubFileEntryService.checkoutGitHubScript(perfTest, ghRepository, gitHubConfig);
scriptEntry = gitHubFileEntryService.getOne(ghRepository, gitHubConfig, scriptName);
gitHubFileEntryService.evictGitHubMavenGroovyCache(ghRepository, scriptName, gitHubConfig.getBranch());
gitHubFileEntryService.evictGitHubGroovyProjectScriptTypeCache(ghRepository, scriptName, gitHubConfig.getBranch());
} else {
scriptEntry = checkNotNull(
fileEntryService.getOne(user,
Expand All @@ -680,7 +680,8 @@ public ScriptHandler prepareDistribution(PerfTest perfTest) throws IOException {
} catch (IOException e) {
noOp();
}
throw processException("Error while file distribution is prepared.");
throw processException("Error while file distribution is prepared.\n" +
"If you run groovy project type script, Please check your build script and make sure Maven or Gradle is installed.");
}
return handler;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
* Copyright (c) 2012-present NAVER Corp.
*
* This file is part of The nGrinder software distribution. Refer to
* the file LICENSE which is part of The nGrinder distribution for
* licensing details. The nGrinder distribution is available on the
* Internet at https://naver.github.io/ngrinder
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.ngrinder.script.handler;

import org.apache.commons.io.IOUtils;
import org.ngrinder.common.util.PathUtils;
import org.ngrinder.infra.config.Config;
import org.ngrinder.model.User;
import org.ngrinder.script.model.FileEntry;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import static java.lang.System.getenv;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.nio.file.Paths.get;
import static org.apache.commons.io.FileUtils.deleteQuietly;
import static org.apache.commons.io.FilenameUtils.normalize;
import static org.apache.commons.lang.StringUtils.EMPTY;
import static org.apache.commons.lang.StringUtils.isNotEmpty;
import static org.ngrinder.common.util.AccessUtils.getSafe;
import static org.ngrinder.common.util.CollectionUtils.buildMap;
import static org.ngrinder.common.util.ExceptionUtils.processException;
import static org.ngrinder.common.util.UrlUtils.getHost;
import static org.ngrinder.script.model.FileCategory.SCRIPT;
import static org.ngrinder.script.model.FileType.DIR;

/**
* Groovy gradle project {@link ScriptHandler}.
*
* @since 3.5.3
*/
@Component
public class GroovyGradleProjectScriptHandler extends GroovyProjectScriptHandler {

public static final String GRADLE_HOME_ENV_NAME = "GRADLE_HOME";

private final String ngrinderHomePath;
private String gradlePath;

public GroovyGradleProjectScriptHandler(Config config) {
super("groovy_gradle", "", "Groovy Gradle Project", "groovy", "build.gradle", true);

gradlePath = getSafe(getenv(GRADLE_HOME_ENV_NAME), "");
if (isNotEmpty(gradlePath)) {
gradlePath += "/bin/";
}

ngrinderHomePath = config.getHome().getDirectory().getAbsolutePath();
}

@Override
protected String getCopyDependenciesCommand(File distDir) {
String distDirPath = distDir.getAbsolutePath();
return gradlePath + "gradle -I " + ngrinderHomePath + "/init.gradle -p" + distDirPath +
" __copyDependencies -PoutputDirectory=" + distDirPath + "/lib";
}

@Override
protected boolean isSuccess(List<String> results) {
if (results.isEmpty()) {
return false;
}
return results.stream().noneMatch(str -> str.contains("FAILED"));
}

@Override
public boolean prepareScriptEnv(User user, String path, String fileName, String name, // LF
String url, boolean createLib, String scriptContent) {
path = PathUtils.join(path, fileName);
// Create Dir entry
createBaseDirectory(user, path);
// Create each template entries
createFileEntries(user, path, name, url, scriptContent);
if (createLib) {
createLibraryDirectory(user, path);
}
return false;
}

@Override
protected void deleteUnnecessaryFilesFromDist(File distDir) {
super.deleteUnnecessaryFilesFromDist(distDir);
deleteQuietly(new File(distDir, ".gradle"));
}

private void createFileEntries(User user, String path, String name, String url, String scriptContent) {
String[] scriptTemplatePaths = { getBuildScriptName(), "src/main/resources/resource1.txt", "src/main/java/TestRunner.groovy" };
for (String scriptTemplatePath : scriptTemplatePaths) {
try (InputStream inputStream = new ClassPathResource("/script_template/groovy_gradle/" + scriptTemplatePath).getInputStream()) {
String fileContent = IOUtils.toString(inputStream, UTF_8.name());
if (scriptTemplatePath.endsWith("TestRunner.groovy")) {
fileContent = scriptContent;
} else {
fileContent = fileContent.replace("${userName}", user.getUserName());
fileContent = fileContent.replace("${name}", name);
fileContent = fileContent.replace("${url}", url);
}
FileEntry fileEntry = new FileEntry();
fileEntry.setContent(fileContent);
fileEntry.setPath(normalize(PathUtils.join(path, scriptTemplatePath), true));
fileEntry.setDescription("create groovy gradle project");
String hostName = getHost(url);
if (isNotEmpty(hostName)
&& fileEntry.getFileType().getFileCategory() == SCRIPT) {
fileEntry.getProperties().put("targetHosts", getHost(url));
} else {
fileEntry.getProperties().put("targetHosts", EMPTY);
}
getFileEntryRepository().save(user, fileEntry, UTF_8.name());
} catch (IOException e) {
throw processException("Error while saving " + get(scriptTemplatePath).getFileName(), e);
}
}
}

private void createBaseDirectory(User user, String path) {
FileEntry dirEntry = new FileEntry();
dirEntry.setPath(path);
// Make it eclipse default folder ignored.
dirEntry.setProperties(buildMap("svn:ignore", ".project\n.classpath\n.settings\ntarget"));
dirEntry.setFileType(DIR);
dirEntry.setDescription("create groovy gradle project");
getFileEntryRepository().save(user, dirEntry, null);
}
}
Loading

0 comments on commit 37c7147

Please sign in to comment.