Skip to content

Commit

Permalink
fix ci failed
Browse files Browse the repository at this point in the history
  • Loading branch information
JervyShi committed Feb 10, 2019
1 parent 93d9d5d commit ff90bd7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 59 deletions.
24 changes: 1 addition & 23 deletions src/main/java/name/jervyshi/nacos/NacosProcess.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@

import static org.slf4j.LoggerFactory.getLogger;

import java.io.File;
import java.lang.ProcessBuilder.Redirect;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;

import name.jervyshi.nacos.infra.NacosWaiter;
Expand All @@ -38,10 +32,6 @@ public class NacosProcess implements AutoCloseable {
/** logger */
private static final Logger logger = getLogger(NacosProcess.class);

private Path downloadPath;

private File shutdownScript;

private String host;

private int port;
Expand All @@ -51,15 +41,10 @@ public class NacosProcess implements AutoCloseable {
/**
* Instantiates a new Nacos process.
*
* @param downloadPath the download path
* @param shutdownScript the shutdown script
* @param host the host
* @param port the port
*/
public NacosProcess(Path downloadPath, File shutdownScript, String host, int port,
Process process) {
this.downloadPath = downloadPath;
this.shutdownScript = shutdownScript;
public NacosProcess(String host, int port, Process process) {
this.host = host;
this.port = port;
this.process = process;
Expand All @@ -69,13 +54,6 @@ public NacosProcess(Path downloadPath, File shutdownScript, String host, int por
public void close() throws Exception {
logger.info("Stopping nacos server");
process.destroy();
List<String> command = new ArrayList<>();
command.add(shutdownScript.getAbsolutePath());
Process innerProcess = new ProcessBuilder().directory(downloadPath.toFile())
.command(command).inheritIO().redirectOutput(Redirect.PIPE).start();

// TODO log process result
innerProcess.getInputStream();
if (new NacosWaiter(host, port).avoidUntilNacosServerStopped()) {
logger.info("Stopped nacos server");
} else {
Expand Down
63 changes: 27 additions & 36 deletions src/main/java/name/jervyshi/nacos/NacosStarter.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,10 @@
import java.io.File;
import java.io.IOException;
import java.lang.ProcessBuilder.Redirect;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFilePermission;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;

import org.slf4j.Logger;
Expand Down Expand Up @@ -90,22 +86,32 @@ public NacosProcess start() {
downloadAndUnpackBinary();
}

File scriptFile = getNacosStartUpScript();

List<String> command = new ArrayList<>();
command.add(scriptFile.getAbsolutePath());
if (!isWindows()) {
command.add("-m");
command.add("standalone");
if (isWindows()) {
command.add(Paths.get(System.getenv("JAVA_HOME"), "bin", "java.exe")
.toAbsolutePath().toString());
} else {
command.add(Paths.get(System.getenv("JAVA_HOME"), "bin", "java")
.toAbsolutePath().toString());
}
Process innerProcess = new ProcessBuilder().directory(downloadPath.toFile())
.command(command).inheritIO().redirectOutput(Redirect.PIPE).start();
command.add("-Dnacos.standalone=true");
command.add("-Dnacos.home=" + getAbsolutePath("nacos"));
command.add("-jar");
command.add(getAbsolutePath("nacos", "target", "nacos-server.jar"));
command.add(
"--spring.config.location=classpath:/,classpath:/config/,file:./,file:./config/,file:"
+ getAbsolutePath("nacos", "conf") + File.separator);
command.add(
"--logging.config=" + getAbsolutePath("nacos", "conf", "nacos-logback.xml"));

Process innerProcess = new ProcessBuilder()
.directory(Paths.get(getAbsolutePath("nacos")).toFile()).command(command)
.inheritIO().redirectOutput(Redirect.PIPE).start();

// TODO log process result
innerProcess.getInputStream();

nacosProcess = new NacosProcess(downloadPath, getNacosShutdownScript(), host, port,
innerProcess);
nacosProcess = new NacosProcess(host, port, innerProcess);

logger.info("Starting nacos server on port: {}", port);
new NacosWaiter(host, port).avoidUntilNacosServerStarted();
Expand All @@ -128,37 +134,19 @@ private void downloadAndUnpackBinary() throws IOException {

logger.info("Unzip nacos archive files into: {}", downloadPath);
ZipUtil.unzip(archive.getAbsolutePath(), downloadPath.toAbsolutePath().toString());

if (!isWindows()) {
Set<PosixFilePermission> permissions = new HashSet<>();
permissions.add(PosixFilePermission.OWNER_READ);
permissions.add(PosixFilePermission.OWNER_WRITE);
permissions.add(PosixFilePermission.OWNER_EXECUTE);
permissions.add(PosixFilePermission.GROUP_READ);
permissions.add(PosixFilePermission.OTHERS_READ);

Files.setPosixFilePermissions(getNacosStartUpScript().toPath(), permissions);
Files.setPosixFilePermissions(getNacosShutdownScript().toPath(), permissions);
}
}

private boolean isWindows() {
return OsResolver.resolve().contains("windows");
}

private boolean isBinaryDownloaded() {
return getNacosStartUpScript().exists();
return getNacosServerJar().exists();
}

private File getNacosStartUpScript() {
String scriptName = isWindows() ? "startup.cmd" : "startup.sh";
Path path = Paths.get(downloadPath.toAbsolutePath().toString(), "nacos", "bin", scriptName);
return path.toFile();
}

private File getNacosShutdownScript() {
String scriptName = isWindows() ? "shutdown.cmd" : "shutdown.sh";
Path path = Paths.get(downloadPath.toAbsolutePath().toString(), "nacos", "bin", scriptName);
private File getNacosServerJar() {
Path path = Paths.get(downloadPath.toAbsolutePath().toString(), "nacos", "target",
"nacos-server.jar");
return path.toFile();
}

Expand All @@ -169,4 +157,7 @@ private void checkInitialState() {
}
}

private String getAbsolutePath(String... item) {
return Paths.get(downloadPath.toAbsolutePath().toString(), item).toString();
}
}
9 changes: 9 additions & 0 deletions src/test/java/name/jervyshi/nacos/NacosStarterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@
*/
package name.jervyshi.nacos;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

import name.jervyshi.nacos.infra.NacosClient;

/**
* The type Nacos starter test.
* @author JervyShi
Expand All @@ -34,6 +39,10 @@ public void start() throws Exception {
.build();
NacosProcess start = nacosStarter.start();

NacosClient client = new NacosClient("127.0.0.1", 8848);
assertTrue(client.isHealthy());

start.close();
assertFalse(client.isHealthy());
}
}

0 comments on commit ff90bd7

Please sign in to comment.