временно отложенные наработки
This commit is contained in:
@@ -16,6 +16,25 @@
|
|||||||
<version>${asys.version}</version>
|
<version>${asys.version}</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>1.16.10</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>commons-io</groupId>
|
||||||
|
<artifactId>commons-io</artifactId>
|
||||||
|
<version>2.5</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>asys</groupId>
|
||||||
|
<artifactId>core</artifactId>
|
||||||
|
<version>0.3</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<finalName>${project.groupId}.${project.artifactId}-${project.version}</finalName>
|
<finalName>${project.groupId}.${project.artifactId}-${project.version}</finalName>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
|||||||
@@ -4,11 +4,47 @@
|
|||||||
*/
|
*/
|
||||||
package asys.brain;
|
package asys.brain;
|
||||||
|
|
||||||
|
import asys.core.buildscript.BuildScript;
|
||||||
|
import asys.core.buildscript.CommandException;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
class Brain {
|
class Brain {
|
||||||
private Logger logger = LoggerFactory.getLogger(Brain.class);
|
private Logger logger = LoggerFactory.getLogger(Brain.class);
|
||||||
|
@Getter @Setter
|
||||||
|
private File buildscriptDir;
|
||||||
|
@Getter @Setter
|
||||||
|
private Map<String, String> environment;
|
||||||
|
|
||||||
|
public void deployServers(Map<String, Integer> data) {
|
||||||
|
File[] buildscriptFiles = buildscriptDir.listFiles(file -> file.isFile() && file.getName().endsWith(".bs"));
|
||||||
|
if (buildscriptFiles == null) return;
|
||||||
|
|
||||||
|
for (File buildFile : buildscriptFiles) {
|
||||||
|
String serverName = buildFile.getName().substring(0,buildFile.getName().lastIndexOf('.'));
|
||||||
|
Integer count = data.get(serverName);
|
||||||
|
if (count == null) continue;
|
||||||
|
|
||||||
|
try {
|
||||||
|
BuildScript buildScript = BuildScript.loadFromFile(buildFile);
|
||||||
|
buildScript.setVariables(environment);
|
||||||
|
|
||||||
|
for (int id = 1; id <= count; id++) {
|
||||||
|
logger.debug("Deploy \"{}{}\" server", serverName, id);
|
||||||
|
buildScript.setVariable("id", String.valueOf(id));
|
||||||
|
buildScript.execute();
|
||||||
|
}
|
||||||
|
} catch (IOException | CommandException e) {
|
||||||
|
logger.error("Error deploy server", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void start() {
|
void start() {
|
||||||
logger.trace("Brain module started");
|
logger.trace("Brain module started");
|
||||||
|
|||||||
@@ -7,6 +7,9 @@ package asys.brain;
|
|||||||
import org.osgi.framework.BundleActivator;
|
import org.osgi.framework.BundleActivator;
|
||||||
import org.osgi.framework.BundleContext;
|
import org.osgi.framework.BundleContext;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
public class BrainActivator implements BundleActivator {
|
public class BrainActivator implements BundleActivator {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
51
brain/src/test/java/asys/brain/TestBrain.java
Normal file
51
brain/src/test/java/asys/brain/TestBrain.java
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
/*
|
||||||
|
* DmitriyMX <mail@dmitriymx.ru>
|
||||||
|
* 2016-07-17
|
||||||
|
*/
|
||||||
|
package asys.brain;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Логика работы модуля
|
||||||
|
*
|
||||||
|
* Вначале происходит загрузка конфига, где прописаны папка со скриптами
|
||||||
|
* и папка с серверами. Затем он проверяет кол-во уже развёрнутых серверов с кол-вом
|
||||||
|
* указанным в конфиге. Проверка простая: если папка есть и в ней есть необходимые файлы
|
||||||
|
* для запуска - значит система полноценно развернута. В ином случае, папка затирается
|
||||||
|
* и разворачиватеся по новому. Помимо разворачивания серверов, "мозг" запускает минимальное кол-во серверов, которые
|
||||||
|
* должы быть активными.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class TestBrain {
|
||||||
|
private static Brain brain;
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void beforeClass() {
|
||||||
|
File tmpDir = new File("D:/tmp");
|
||||||
|
brain = new Brain();
|
||||||
|
brain.setBuildscriptDir(new File(tmpDir, "scripts"));
|
||||||
|
|
||||||
|
Map<String, String> env = new HashMap<>();
|
||||||
|
env.put("servers", new File(tmpDir, "servers").toString());
|
||||||
|
env.put("distrib", new File(tmpDir, "distrib").toString());
|
||||||
|
|
||||||
|
brain.setEnvironment(env);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test() {
|
||||||
|
Map<String, Integer> data = new HashMap<>();
|
||||||
|
data.put("bedwars", 19);
|
||||||
|
|
||||||
|
brain.deployServers(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
10
brain/src/test/resources/brain.conf
Normal file
10
brain/src/test/resources/brain.conf
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
# Папка в которой находятся скрипты разворачивания серверов
|
||||||
|
buildscript.folder = templates
|
||||||
|
#
|
||||||
|
# Папка в которую разворачиваются сервера
|
||||||
|
servers.folder = servers
|
||||||
|
#
|
||||||
|
# Число серверов, которое необходимо сразу развернуть.
|
||||||
|
# Имя параметра формируется по шаблону: server.название_шаблона
|
||||||
|
server.skywars = 9
|
||||||
|
server.bedwars = 7
|
||||||
@@ -5,6 +5,7 @@
|
|||||||
package asys.core;
|
package asys.core;
|
||||||
|
|
||||||
import asys.core.api.ICore;
|
import asys.core.api.ICore;
|
||||||
|
import org.apache.commons.io.FileUtils;
|
||||||
import org.osgi.framework.Bundle;
|
import org.osgi.framework.Bundle;
|
||||||
import org.osgi.framework.BundleContext;
|
import org.osgi.framework.BundleContext;
|
||||||
import org.osgi.framework.BundleException;
|
import org.osgi.framework.BundleException;
|
||||||
|
|||||||
@@ -9,13 +9,11 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.StringTokenizer;
|
|
||||||
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipEntry;
|
||||||
import java.util.zip.ZipInputStream;
|
import java.util.zip.ZipInputStream;
|
||||||
|
|
||||||
@@ -32,7 +30,11 @@ import java.util.zip.ZipInputStream;
|
|||||||
public class BuildScript {
|
public class BuildScript {
|
||||||
private Logger logger = LoggerFactory.getLogger(BuildScript.class);
|
private Logger logger = LoggerFactory.getLogger(BuildScript.class);
|
||||||
List<String[]> scriptLines = new ArrayList<>();
|
List<String[]> scriptLines = new ArrayList<>();
|
||||||
private Map<String, String> variables;
|
private Map<String, String> variables = new HashMap<>();
|
||||||
|
|
||||||
|
public static BuildScript loadFromFile(File file) throws IOException, UnknowCommandException {
|
||||||
|
return new BuildScript(FileUtils.readFileToString(file, Charset.forName("UTF-8")));
|
||||||
|
}
|
||||||
|
|
||||||
public BuildScript(String script) throws UnknowCommandException {
|
public BuildScript(String script) throws UnknowCommandException {
|
||||||
if (script == null || script.trim().isEmpty()) {
|
if (script == null || script.trim().isEmpty()) {
|
||||||
@@ -54,6 +56,10 @@ public class BuildScript {
|
|||||||
this.variables = variables;
|
this.variables = variables;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setVariable(String name, String value) {
|
||||||
|
this.variables.put(name, value);
|
||||||
|
}
|
||||||
|
|
||||||
private String[] parseCommandLine(String line) throws UnknowCommandException {
|
private String[] parseCommandLine(String line) throws UnknowCommandException {
|
||||||
final StringTokenizer strTok = new StringTokenizer(line, " \"\'", true);
|
final StringTokenizer strTok = new StringTokenizer(line, " \"\'", true);
|
||||||
final List<String> preArr = new ArrayList<>();
|
final List<String> preArr = new ArrayList<>();
|
||||||
@@ -243,6 +249,7 @@ public class BuildScript {
|
|||||||
|
|
||||||
ZipEntry ze;
|
ZipEntry ze;
|
||||||
while ((ze = zis.getNextEntry()) != null) {
|
while ((ze = zis.getNextEntry()) != null) {
|
||||||
|
if (ze.isDirectory()) continue;
|
||||||
String fileName = ze.getName();
|
String fileName = ze.getName();
|
||||||
File newFile = new File(targetPath.toFile(), fileName);
|
File newFile = new File(targetPath.toFile(), fileName);
|
||||||
Files.createDirectories(Paths.get(newFile.getParent()));
|
Files.createDirectories(Paths.get(newFile.getParent()));
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
*/
|
*/
|
||||||
package asys.core.buildscript;
|
package asys.core.buildscript;
|
||||||
|
|
||||||
class CommandException extends Exception {
|
public class CommandException extends Exception {
|
||||||
CommandException(String message) {
|
CommandException(String message) {
|
||||||
super(message);
|
super(message);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
*/
|
*/
|
||||||
package asys.core.buildscript;
|
package asys.core.buildscript;
|
||||||
|
|
||||||
class UnknowCommandException extends CommandException {
|
public class UnknowCommandException extends CommandException {
|
||||||
UnknowCommandException(String message) {
|
UnknowCommandException(String message) {
|
||||||
super(message);
|
super(message);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user