841 lines
38 KiB
Java
841 lines
38 KiB
Java
package _VisualDVM.TestingSystem.Common;
|
||
import Common.CommonConstants;
|
||
import Common.Database.Objects.DBObject;
|
||
import Common.Database.Objects.riDBObject;
|
||
import Common.Database.RepositoryRefuseException;
|
||
import Common.Utils.TextLog;
|
||
import Common.Utils.Utils_;
|
||
import Common.Utils.Vector_;
|
||
import _VisualDVM.ComponentsServer.Component.Sapfor.Sapfor;
|
||
import _VisualDVM.ComponentsServer.UserAccount.AccountRole;
|
||
import _VisualDVM.ComponentsServer.UserAccount.UserAccount;
|
||
import _VisualDVM.Constants;
|
||
import _VisualDVM.Global;
|
||
import _VisualDVM.GlobalData.Tasks.TaskState;
|
||
import _VisualDVM.Passes.All.DownloadRepository;
|
||
import _VisualDVM.Passes.All.ZipFolderPass;
|
||
import _VisualDVM.Passes.PassCode;
|
||
import _VisualDVM.ProjectData.LanguageName;
|
||
import _VisualDVM.Repository.EmailMessage;
|
||
import _VisualDVM.Repository.Server.RepositoryServer;
|
||
import _VisualDVM.Repository.Server.ServerCode;
|
||
import _VisualDVM.Repository.Server.ServerExchangeUnit_2021;
|
||
import _VisualDVM.TestingSystem.Common.Group.Group;
|
||
import _VisualDVM.TestingSystem.Common.MachineProcess.MachineProcess;
|
||
import _VisualDVM.TestingSystem.Common.MachineProcess.MachineProcessSet;
|
||
import _VisualDVM.TestingSystem.Common.Test.Test;
|
||
import _VisualDVM.TestingSystem.Common.Test.TestType;
|
||
import _VisualDVM.TestingSystem.Common.TestingPackageToKill.TestingPackageToKill;
|
||
import _VisualDVM.TestingSystem.DVM.DVMConfiguration.DVMConfiguration;
|
||
import _VisualDVM.TestingSystem.DVM.DVMPackage.DVMPackage;
|
||
import _VisualDVM.TestingSystem.DVM.DVMSettings.DVMSettings;
|
||
import _VisualDVM.TestingSystem.DVM.DVMTestingChecker;
|
||
import _VisualDVM.TestingSystem.DVM.PackageCredentials.DVMPackageCredentials;
|
||
import _VisualDVM.TestingSystem.SAPFOR.Json.SapforPackage_json;
|
||
import _VisualDVM.TestingSystem.SAPFOR.Json.SapforVersion_json;
|
||
import _VisualDVM.TestingSystem.SAPFOR.SapforConfiguration.SapforConfiguration;
|
||
import _VisualDVM.TestingSystem.SAPFOR.SapforPackage.SapforPackage;
|
||
import _VisualDVM.TestingSystem.SAPFOR.SapforSettings.SapforSettings;
|
||
import _VisualDVM.TestingSystem.SAPFOR.SapforSettingsCommand.SapforSettingsCommand;
|
||
import _VisualDVM.TestingSystem.SAPFOR.SapforTask.SapforTask;
|
||
import _VisualDVM.TestingSystem.SAPFOR.SapforTestingPlanner;
|
||
import _VisualDVM.TestingSystem.SAPFOR.ServerSapfor.ServerSapfor;
|
||
import _VisualDVM.TestingSystem.SAPFOR.ServerSapfor.ServerSapforState;
|
||
import _VisualDVM.Utils;
|
||
import javafx.util.Pair;
|
||
import org.apache.commons.io.FileUtils;
|
||
|
||
import javax.swing.*;
|
||
import java.io.File;
|
||
import java.nio.file.Paths;
|
||
import java.util.Arrays;
|
||
import java.util.Comparator;
|
||
import java.util.LinkedHashMap;
|
||
import java.util.Vector;
|
||
public class TestingServer extends RepositoryServer<TestsDatabase> {
|
||
public static MachineProcessSet machinesProcesses = new MachineProcessSet();
|
||
//------>>>
|
||
public static Timer checkTimer = null;
|
||
public String name = "?";
|
||
DVMTestingChecker dvmTestingChecker = new DVMTestingChecker();
|
||
SapforTestingPlanner sapforTestingPlanner = new SapforTestingPlanner();
|
||
//-->
|
||
MachinesDatabase machines_db;
|
||
//-->
|
||
protected Thread testingThread = new Thread(() -> {
|
||
while (true) {
|
||
dvmTestingChecker.Perform();
|
||
sapforTestingPlanner.Perform();
|
||
Utils_.sleep(5000);
|
||
}
|
||
});
|
||
public TestingServer() {
|
||
super(TestsDatabase.class);
|
||
name = Utils_.getDateName("testingServer");
|
||
}
|
||
@Override
|
||
public void StartAction() throws Exception {
|
||
try {
|
||
machines_db = new MachinesDatabase();
|
||
machines_db.Activate();
|
||
} catch (Exception ex) {
|
||
ex.printStackTrace();
|
||
}
|
||
}
|
||
public static void TimerOn() {
|
||
checkTimer = new Timer(Global.normalProperties.CheckTestingIntervalSeconds * 1000, e -> {
|
||
Global.mainModule.getPass(PassCode.ActualizePackages).Do();
|
||
});
|
||
checkTimer.start();
|
||
}
|
||
public static void TimerOff() {
|
||
if (checkTimer != null)
|
||
checkTimer.stop();
|
||
}
|
||
public static void ResetTimer() {
|
||
TimerOff();
|
||
TimerOn();
|
||
}
|
||
@Override
|
||
public void afterPublishAction(DBObject object) throws Exception {
|
||
if (object instanceof Test) {
|
||
Test test = (Test) object;
|
||
if (!test.unpackProjectOnServer()) {
|
||
db.Delete(test);
|
||
throw new RepositoryRefuseException(
|
||
"Не удалось прикрепить проект к тесту с id " + test.id
|
||
+ "\nТест будет удален"
|
||
);
|
||
}
|
||
db.DetectTestMinMaxDim(db.serverSapfors.getLastDoneVersion(), db.groups.get(test.group_id), test);
|
||
} else if (object instanceof DVMPackage) {
|
||
DVMPackage dvmPackage = (DVMPackage) object;
|
||
//--
|
||
Utils_.CheckAndCleanDirectory(dvmPackage.getLocalWorkspace());
|
||
//--
|
||
dvmPackage.saveJson();
|
||
dvmPackage.package_json = null; // объект больше не нужен.
|
||
//--
|
||
//Занести учетные данные пакета в базу о машинах. это основной поток, нить не нужна. арг это пароль
|
||
DVMPackageCredentials credentials = new DVMPackageCredentials(dvmPackage, request.arg);
|
||
machines_db.Insert(credentials);
|
||
//--
|
||
} else if (object instanceof SapforPackage) {
|
||
((SapforPackage) object).init();
|
||
}
|
||
}
|
||
@Override
|
||
protected void afterCloneAction(riDBObject src, riDBObject dst) throws Exception {
|
||
if (src instanceof SapforSettings) {
|
||
Vector<PassCode> codes = new Vector<>();
|
||
for (SapforSettingsCommand command : db.sapforSettingsCommands.Data.values())
|
||
if (command.sapforsettings_id == src.id) codes.add(command.passCode);
|
||
//--
|
||
for (PassCode code : codes) {
|
||
SapforSettingsCommand command = new SapforSettingsCommand();
|
||
command.sapforsettings_id = dst.id;
|
||
command.passCode = code;
|
||
db.Insert(command);
|
||
}
|
||
}
|
||
}
|
||
@Override
|
||
protected void afterEditAction(DBObject object) throws Exception {
|
||
if (object instanceof Group) {
|
||
Group group = (Group) object;
|
||
//-
|
||
for (DVMConfiguration dvmConfiguration : db.dvmConfigurations.Data.values()) {
|
||
if (dvmConfiguration.tryUpdateGroup(group)) {
|
||
db.Update(dvmConfiguration);
|
||
}
|
||
}
|
||
for (SapforConfiguration sapforConfiguration : db.sapforConfigurations.Data.values()) {
|
||
if (sapforConfiguration.tryUpdateGroup(group)) {
|
||
db.Update(sapforConfiguration);
|
||
}
|
||
}
|
||
} else if (object instanceof DVMSettings) {
|
||
DVMSettings dvmSettings = (DVMSettings) object;
|
||
//-
|
||
for (DVMConfiguration dvmConfiguration : db.dvmConfigurations.Data.values()) {
|
||
if (dvmConfiguration.tryUpdateSettings(dvmSettings)) {
|
||
db.Update(dvmConfiguration);
|
||
}
|
||
}
|
||
} else if (object instanceof SapforSettings) {
|
||
SapforSettings sapforSettings = (SapforSettings) object;
|
||
for (SapforConfiguration sapforConfiguration : db.sapforConfigurations.Data.values()) {
|
||
if (sapforConfiguration.tryUpdateSettings(sapforSettings)) {
|
||
db.Update(sapforConfiguration);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
@Override
|
||
protected void afterDeleteAction(DBObject object) throws Exception {
|
||
if (object instanceof Test) {
|
||
Test test = (Test) object;
|
||
Utils_.forceDeleteWithCheck(test.getArchive());
|
||
Utils_.forceDeleteWithCheck(test.getServerPath());
|
||
//--
|
||
for (DVMConfiguration dvmConfiguration : db.dvmConfigurations.Data.values()) {
|
||
if (dvmConfiguration.tryDeleteTest(test)) {
|
||
db.Update(dvmConfiguration);
|
||
}
|
||
}
|
||
for (SapforConfiguration sapforConfiguration : db.sapforConfigurations.Data.values()) {
|
||
if (sapforConfiguration.tryDeleteTest(test)) {
|
||
db.Update(sapforConfiguration);
|
||
}
|
||
}
|
||
//--
|
||
} else if (object instanceof Group) {
|
||
Group group = (Group) object;
|
||
LinkedHashMap<Integer, Test> groupTests = db.getMapByFKi(group, Test.class);
|
||
//--
|
||
for (DVMConfiguration dvmConfiguration : db.dvmConfigurations.Data.values()) {
|
||
if (dvmConfiguration.tryDeleteGroup(group)) {
|
||
dvmConfiguration.tryDeleteTests(groupTests);
|
||
db.Update(dvmConfiguration);
|
||
}
|
||
}
|
||
for (SapforConfiguration sapforConfiguration : db.sapforConfigurations.Data.values()) {
|
||
if (sapforConfiguration.tryDeleteGroup(group)) {
|
||
sapforConfiguration.tryDeleteTests(groupTests);
|
||
db.Update(sapforConfiguration);
|
||
}
|
||
}
|
||
//--
|
||
for (Test group_test : groupTests.values()) {
|
||
db.Delete(group_test);
|
||
Utils_.forceDeleteWithCheck(group_test.getArchive());
|
||
Utils_.forceDeleteWithCheck(group_test.getServerPath());
|
||
}
|
||
//--
|
||
} else if (object instanceof ServerSapfor) {
|
||
Utils_.forceDeleteWithCheck(
|
||
new File(
|
||
((ServerSapfor) object).home_path
|
||
)
|
||
);
|
||
} else if (object instanceof DVMPackage) {
|
||
DVMPackage dvmPackage = (DVMPackage) object;
|
||
File workspace = dvmPackage.getLocalWorkspace();
|
||
Utils_.forceDeleteWithCheck(workspace);
|
||
//--
|
||
DVMPackageCredentials credentials = machines_db.dvmPackagesCredentials.getForPackageId(dvmPackage.id);
|
||
if (credentials != null)
|
||
machines_db.Delete(credentials);
|
||
//--
|
||
} else if (object instanceof SapforPackage) {
|
||
SapforPackage sapforPackage = (SapforPackage) object;
|
||
//--
|
||
for (SapforConfiguration sapforConfiguration : db.sapforConfigurations.Data.values()) {
|
||
if (sapforConfiguration.ethalon_id == sapforPackage.id) {
|
||
sapforConfiguration.ethalon_id = CommonConstants.Nan;
|
||
db.Update(sapforConfiguration);
|
||
}
|
||
}
|
||
//--
|
||
File workspace = sapforPackage.getLocalWorkspace();
|
||
Utils_.forceDeleteWithCheck(workspace);
|
||
} else if (object instanceof DVMSettings) {
|
||
DVMSettings dvmSettings = (DVMSettings) object;
|
||
for (DVMConfiguration dvmConfiguration : db.dvmConfigurations.Data.values()) {
|
||
if (dvmConfiguration.tryDeleteSettings(dvmSettings)) {
|
||
db.Update(dvmConfiguration);
|
||
}
|
||
}
|
||
} else if (object instanceof SapforSettings) {
|
||
//--
|
||
SapforSettings sapforSettings = (SapforSettings) object;
|
||
for (SapforConfiguration sapforConfiguration : db.sapforConfigurations.Data.values()) {
|
||
if (sapforConfiguration.tryDeleteSettings(sapforSettings)) {
|
||
db.Update(sapforConfiguration);
|
||
}
|
||
}
|
||
//--
|
||
Vector<SapforSettingsCommand> commands = new Vector<>();
|
||
for (SapforSettingsCommand command : db.sapforSettingsCommands.Data.values()) {
|
||
if (command.sapforsettings_id == sapforSettings.id)
|
||
commands.add(command);
|
||
}
|
||
for (SapforSettingsCommand command : commands) {
|
||
db.Delete(command);
|
||
}
|
||
}
|
||
//-
|
||
}
|
||
@Override
|
||
public String getServerHomeName() {
|
||
return "_testing_system";
|
||
}
|
||
//-->>>
|
||
@Override
|
||
protected void beforePublishAction(DBObject object) throws Exception {
|
||
if (object instanceof ServerSapfor) {
|
||
int current_version = getSapforActualVersion();
|
||
int max_installed_version = db.getInstalledSapforMaxVersion();
|
||
if (max_installed_version == current_version)
|
||
throw new RepositoryRefuseException("Актуальная версия SAPFOR " + max_installed_version + " уже установлена");
|
||
}
|
||
}
|
||
@Override
|
||
public int getPort() {
|
||
return Constants.TestingServerPort;
|
||
}
|
||
@Override
|
||
protected void startAdditionalThreads() {
|
||
testingThread.start();
|
||
}
|
||
|
||
void PerformAutoSapforTesting() throws Exception {
|
||
TextLog Log = new TextLog();
|
||
SapforPackage autoPackage = tryAutoSapforTesting(Log);
|
||
EmailMessage message = Log.isEmpty() ?
|
||
new EmailMessage(
|
||
"Запущено автоматическое тестирование версии " + request.arg + " системы SAPFOR",
|
||
"Пакет " + Utils_.Brackets(autoPackage.id)) :
|
||
new EmailMessage(
|
||
"Не удалось запустить автоматическое тестирование версии " + request.arg + " системы SAPFOR",
|
||
Log.toString()
|
||
);
|
||
for (String address : Constants.admins_mails) {
|
||
Email(message, address);
|
||
}
|
||
}
|
||
void DownloadTest() throws Exception {
|
||
int test_id = Integer.parseInt(request.arg);
|
||
if (db.tests.containsKey(test_id)) {
|
||
Test test = db.tests.get(test_id);
|
||
response = new ServerExchangeUnit_2021(ServerCode.OK, "", Utils_.fileToBytes(test.getArchive()));
|
||
} else
|
||
throw new RepositoryRefuseException("Теста с именем " + request.arg + " не существует");
|
||
}
|
||
void ReceiveTestsDatabase() throws Exception {
|
||
response.object = Utils_.fileToBytes(db.getFile());
|
||
}
|
||
Pair<Group, Vector<File>> ConvertDirectoryToGroup(File src, LanguageName languageName, TestType
|
||
testType, UserAccount account) throws Exception {
|
||
Group object = new Group();
|
||
Vector<File> groupFiles = null; //транспорт.
|
||
//->>
|
||
object.description = src.getName();
|
||
object.language = languageName;
|
||
object.type = testType;
|
||
object.sender_name = account.name;
|
||
object.sender_address = account.email;
|
||
//-->>
|
||
File[] files = src.listFiles(pathname ->
|
||
pathname.isFile()
|
||
&& !pathname.getName().equals("settings")
|
||
&& !pathname.getName().equals("test-analyzer.sh")
|
||
&& Utils_.getExtension(pathname).startsWith(languageName.getDVMCompile()));
|
||
if (files != null) {
|
||
groupFiles = new Vector<>(Arrays.asList(files));
|
||
groupFiles.sort(Comparator.comparing(File::getName));
|
||
}
|
||
//->>
|
||
return new Pair<>(object, groupFiles);
|
||
}
|
||
void RefreshDVMTests() throws Exception {
|
||
UserAccount account = new UserAccount("server", Constants.MailAddress);
|
||
ServerSapfor serverSapfor = db.serverSapfors.getLastDoneVersion();
|
||
DownloadRepository downloadRepository = new DownloadRepository();
|
||
if (!downloadRepository.Do())
|
||
throw new RepositoryRefuseException("Не удалось обновить репозиторий");
|
||
//-->>
|
||
Vector<Pair<Group, Vector<File>>> groups = new Vector<>();
|
||
File testsSrc = Paths.get(
|
||
Global.RepoDirectory.getAbsolutePath(),
|
||
"dvm", "tools", "tester", "trunk", "test-suite").toFile();
|
||
LanguageName[] supportedLanguages = new LanguageName[]{LanguageName.fortran, LanguageName.c};
|
||
for (LanguageName languageName : supportedLanguages) {
|
||
for (TestType testType : TestType.values()) {
|
||
File groupsSrc = null;
|
||
switch (testType) {
|
||
case Correctness:
|
||
String languageSrcName = null;
|
||
switch (languageName) {
|
||
case fortran:
|
||
languageSrcName = "Fortran";
|
||
break;
|
||
case c:
|
||
languageSrcName = "C";
|
||
break;
|
||
}
|
||
if (languageSrcName != null) {
|
||
groupsSrc = Paths.get(testsSrc.getAbsolutePath(), "Correctness", languageSrcName).toFile();
|
||
File[] groupsDirs = groupsSrc.listFiles(File::isDirectory);
|
||
if (groupsDirs != null) {
|
||
for (File groupDir : groupsDirs)
|
||
groups.add(ConvertDirectoryToGroup(groupDir, languageName, testType, account));
|
||
}
|
||
}
|
||
break;
|
||
case Performance:
|
||
File groupDir = Paths.get(testsSrc.getAbsolutePath(), "Performance").toFile();
|
||
groups.add(ConvertDirectoryToGroup(groupDir, languageName, testType, account));
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
groups.sort(Comparator.comparing(o -> o.getKey().description));
|
||
for (Pair<Group, Vector<File>> p : groups)
|
||
db.RefreshGroup(account, serverSapfor, p);
|
||
}
|
||
void GetFirstActiveSapforPackages() throws Exception {
|
||
response.object = db.getFirstActiveSapforPackagesCopies();
|
||
}
|
||
void GetSapforMaxVersion() throws Exception {
|
||
response.object = db.getInstalledSapforMaxVersion();
|
||
}
|
||
void UpdateActiveDVMPackages() throws Exception {
|
||
Vector<Pair<Integer, Long>> keys_pairs = (Vector<Pair<Integer, Long>>) request.object;
|
||
Vector<DVMPackage> res = new Vector<>();
|
||
//--
|
||
for (Pair<Integer, Long> p : keys_pairs) {
|
||
if (db.dvmPackages.containsKey(p.getKey())) {
|
||
DVMPackage actual = db.dvmPackages.get(p.getKey());
|
||
if (actual.ChangeDate != p.getValue())
|
||
res.add(new DVMPackage(actual));
|
||
}
|
||
}
|
||
response.object = res;
|
||
}
|
||
void UpdateActiveSapforPackages() {
|
||
Vector<Pair<Integer, Long>> keys_pairs = (Vector<Pair<Integer, Long>>) request.object;
|
||
Vector<SapforPackage> res = new Vector<>();
|
||
//--
|
||
for (Pair<Integer, Long> p : keys_pairs) {
|
||
if (db.sapforPackages.containsKey(p.getKey())) {
|
||
SapforPackage actual = db.sapforPackages.get(p.getKey());
|
||
if (actual.ChangeDate != p.getValue())
|
||
res.add(new SapforPackage(actual));
|
||
}
|
||
}
|
||
response.object = res;
|
||
}
|
||
void DVMPackageNeedsKill() {
|
||
response.object = CommonConstants.Nan;
|
||
int packageId = (int) request.object;
|
||
for (TestingPackageToKill packageToKill : db.testingPackagesToKill.Data.values()) {
|
||
if ((packageToKill.packageId == packageId) && (packageToKill.type == 0)) {
|
||
response.object = packageToKill.id;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
void SapforPackageNeedsKill() throws Exception {
|
||
response.object = CommonConstants.Nan;
|
||
int packageId = (int) request.object;
|
||
for (TestingPackageToKill packageToKill : db.testingPackagesToKill.Data.values()) {
|
||
if ((packageToKill.packageId == packageId) && (packageToKill.type == 1)) {
|
||
response.object = packageToKill.id;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
void DownloadDVMPackages() throws Exception {
|
||
Vector<Integer> ids = (Vector<Integer>) request.object;
|
||
Vector<Pair<Integer, Pair<byte[], byte[]>>> res = new Vector<>();
|
||
for (int dvmPackage_id : ids) {
|
||
if (!db.dvmPackages.containsKey(dvmPackage_id))
|
||
throw new RepositoryRefuseException("Не найдено пакета тестирования DVM с ключом " + dvmPackage_id);
|
||
DVMPackage dvmPackage = db.dvmPackages.get(dvmPackage_id);
|
||
File workspace = dvmPackage.getLocalWorkspace();
|
||
File results_zip = new File(workspace, "results.zip");
|
||
File package_json = dvmPackage.getJsonFile();
|
||
res.add(new Pair<>(dvmPackage_id, new Pair(Utils_.fileToBytes(results_zip), Utils_.fileToBytes(package_json))));
|
||
}
|
||
response.object = res;
|
||
}
|
||
void DownloadSapforPackage() throws Exception {
|
||
int sapforPackage_id = (int) request.object;
|
||
if (!db.sapforPackages.containsKey(sapforPackage_id))
|
||
throw new RepositoryRefuseException("Не найдено пакета тестирования SAPFOR с ключом " + sapforPackage_id);
|
||
SapforPackage sapforPackage = db.sapforPackages.get(sapforPackage_id);
|
||
File workspace = sapforPackage.getLocalWorkspace();
|
||
File results_zip = Utils.getTempFileName("results");
|
||
ZipFolderPass zipFolderPass = new ZipFolderPass();
|
||
zipFolderPass.Do(workspace.getAbsolutePath(), results_zip.getAbsolutePath());
|
||
if (results_zip.exists())
|
||
response.object = Utils_.fileToBytes(results_zip);
|
||
else
|
||
throw new RepositoryRefuseException("Не удалось заархивировать пакет тестирования SAPFOR с ключом " + sapforPackage_id);
|
||
}
|
||
void ReplaceTestCode() throws Exception {
|
||
Test test = (Test) request.object;
|
||
if (!test.unpackProjectOnServer()) {
|
||
db.Delete(test);
|
||
throw new RepositoryRefuseException(
|
||
"Не удалось прикрепить проект к тесту с id " + test.id
|
||
+ "\nТест будет удален"
|
||
);
|
||
} else {
|
||
db.Update(test);
|
||
db.DetectTestMinMaxDim(db.serverSapfors.getLastDoneVersion(), db.groups.get(test.group_id), test);
|
||
}
|
||
}
|
||
void ReplaceTestsCodes() throws Exception {
|
||
ServerSapfor serverSapfor = db.serverSapfors.getLastDoneVersion();
|
||
Vector<Test> tests = (Vector<Test>) request.object;
|
||
for (Test test : tests) {
|
||
if (!test.unpackProjectOnServer()) {
|
||
db.Delete(test);
|
||
throw new RepositoryRefuseException(
|
||
"Не удалось прикрепить проект к тесту с id " + test.id
|
||
+ "\nТест будет удален"
|
||
);
|
||
} else {
|
||
db.Update(test);
|
||
db.DetectTestMinMaxDim(serverSapfor, db.groups.get(test.group_id), test);
|
||
}
|
||
}
|
||
}
|
||
void GetServerName() throws Exception {
|
||
response.object = name;
|
||
}
|
||
//--
|
||
void GetFirstActiveDVMPackageForMachineURL() {
|
||
response.object = db.getFirstActiveDVMPackageCopyForMachineURL(request.arg);
|
||
}
|
||
void StartNecessaryMachines() {
|
||
try {
|
||
Vector<String> aborted = new Vector<>();
|
||
for (MachineProcess process : machinesProcesses.Data.values()) {
|
||
if (process.isAborted()) {
|
||
aborted.add(process.id);
|
||
}
|
||
}
|
||
//---
|
||
for (String key : aborted) {
|
||
machinesProcesses.Data.remove(key);
|
||
}
|
||
//---
|
||
LinkedHashMap<String, MachineProcess> processes_to_start = new LinkedHashMap<>();
|
||
//1. Получить список всех пакетов, которые активны, и взять из них машины.
|
||
for (DVMPackage dvmPackage : db.dvmPackages.Data.values()) {
|
||
if (dvmPackage.state.isActive()) {
|
||
//-
|
||
if (!machinesProcesses.hasProcessForPackage(dvmPackage)) {
|
||
DVMPackageCredentials credentials = machines_db.dvmPackagesCredentials.getForPackageId(dvmPackage.id);
|
||
if (credentials != null) {
|
||
MachineProcess new_process = new MachineProcess(dvmPackage, credentials);
|
||
//получить пароль.
|
||
processes_to_start.put(new_process.getUniqueKey(), new_process);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
//запуск.
|
||
for (MachineProcess process : processes_to_start.values()) {
|
||
process.Start();
|
||
if (Utils.checkFileCreation(process.getStartedFile())) {
|
||
machinesProcesses.Data.put(process.id, process);
|
||
}
|
||
}
|
||
} catch (Exception ex) {
|
||
ex.printStackTrace();
|
||
}
|
||
}
|
||
void GetSapforActualVersion() throws Exception {
|
||
File versionFile = new File(Global.TempDirectory, "version.h");
|
||
if (versionFile.exists())
|
||
FileUtils.forceDelete(versionFile);
|
||
//1. Получить версию из репозитория.
|
||
Utils.startScript(Global.TempDirectory,
|
||
Global.TempDirectory,
|
||
"get_version",
|
||
"wget --user dvmhuser --password dvmh2013 -P " +
|
||
Utils_.DQuotes(Global.TempDirectory.getAbsolutePath()) +
|
||
" http://svn.dvm-system.org/svn/dvmhrepo/sapfor/experts/Sapfor_2017/_src/Utils/version.h"
|
||
).waitFor();
|
||
if (!versionFile.exists())
|
||
throw new RepositoryRefuseException("Не удалось загрузить текущую версию SAPFOR из репозитория!");
|
||
int current_version = Sapfor.readVersionFromCode(versionFile);
|
||
int max_installed_version = db.getInstalledSapforMaxVersion();
|
||
if (max_installed_version == current_version)
|
||
throw new RepositoryRefuseException("Версия " + max_installed_version + " уже установлена");
|
||
}
|
||
//---
|
||
int getSapforActualVersion() throws Exception {
|
||
File versionFile = new File(Global.TempDirectory, "version.h");
|
||
if (versionFile.exists())
|
||
FileUtils.forceDelete(versionFile);
|
||
//1. Получить версию из репозитория.
|
||
Utils.startScript(Global.TempDirectory,
|
||
Global.TempDirectory,
|
||
"get_version",
|
||
"wget --user dvmhuser --password dvmh2013 -P " +
|
||
Utils_.DQuotes(Global.TempDirectory.getAbsolutePath()) +
|
||
" http://svn.dvm-system.org/svn/dvmhrepo/sapfor/experts/Sapfor_2017/_src/Utils/version.h"
|
||
).waitFor();
|
||
if (!versionFile.exists())
|
||
throw new RepositoryRefuseException("Не удалось загрузить текущую версию SAPFOR из репозитория!");
|
||
return Sapfor.readVersionFromCode(versionFile);
|
||
}
|
||
void GetSapforForCompilation() throws Exception {
|
||
//1. Проверить наличие заказов от пользователя
|
||
ServerSapfor serverSapfor = db.getSapforCopyForCompilation();
|
||
if (serverSapfor == null) {
|
||
//2 если нет. проверить есть ли свежие версии.
|
||
int max_version = db.getInstalledSapforMaxVersion();
|
||
int current_version = getSapforActualVersion();
|
||
if (current_version > max_version) {
|
||
serverSapfor = new ServerSapfor();
|
||
serverSapfor.sender_name = "server";
|
||
serverSapfor.sender_address = Constants.MailAddress;
|
||
serverSapfor.state = ServerSapforState.Queued;
|
||
db.Insert(serverSapfor);
|
||
}
|
||
}
|
||
response.object = serverSapfor;
|
||
}
|
||
SapforPackage tryAutoSapforTesting(TextLog Log) throws Exception {
|
||
//--
|
||
UserAccount account = new UserAccount();
|
||
account.name = "server";
|
||
account.email = Constants.MailAddress;
|
||
account.role = AccountRole.Admin;
|
||
//-
|
||
int sapforId = Integer.parseInt(request.arg);
|
||
if (!db.serverSapfors.containsKey(sapforId)) {
|
||
Log.Writeln_("Версия SAPFOR " + sapforId + " не существует.");
|
||
return null;
|
||
}
|
||
ServerSapfor sapfor = db.serverSapfors.get(sapforId);
|
||
if (!sapfor.state.equals(ServerSapforState.Done)) {
|
||
Log.Writeln_("Выбранная версия SAPFOR " + sapforId + " не собрана!");
|
||
return null;
|
||
}
|
||
Vector<SapforConfiguration> configurations = db.sapforConfigurations.getAutoConfigurations();
|
||
if (configurations.isEmpty()) {
|
||
Log.Writeln_("Не найдено конфигураций для автоматического тестирования!");
|
||
return null;
|
||
}
|
||
SapforPackage target = new SapforPackage(account,
|
||
sapfor,
|
||
configurations,
|
||
1,
|
||
Log);
|
||
//-
|
||
if (target.tasksCount == 0) {
|
||
Log.Writeln_("Не сформировано ни одной новой задачи.");
|
||
return null;
|
||
}
|
||
beforePublishAction(target);
|
||
db.InsertS(target);
|
||
afterPublishAction(target);
|
||
//--
|
||
return target;
|
||
}
|
||
Vector<String> getSapforTaskResults(SapforTask task) throws Exception {
|
||
Vector<String> res = new Vector<>();
|
||
//--
|
||
if (!task.variants.isEmpty()) {
|
||
for (SapforVersion_json version_json : task.variants) {
|
||
res.add(version_json.version);
|
||
}
|
||
} else {
|
||
if (!task.versions.isEmpty()) {
|
||
res.add(task.versions.lastElement().version);
|
||
}
|
||
}
|
||
return res;
|
||
}
|
||
void JoinSapforPackageToGroup() throws Exception {
|
||
String[] packed = request.arg.split("\n");
|
||
int groupId = Integer.parseInt(packed[0]);
|
||
int packageId = Integer.parseInt(packed[1]);
|
||
if (!db.sapforPackages.containsKey(packageId))
|
||
throw new RepositoryRefuseException("Пакета с ключом " + packageId + " не существует!");
|
||
if (!db.groups.containsKey(groupId))
|
||
throw new RepositoryRefuseException("Группы с ключом " + groupId + " не существует!");
|
||
//--
|
||
SapforPackage sapforPackage = db.sapforPackages.get(packageId);
|
||
Group group = db.groups.get(groupId);
|
||
//--
|
||
SapforPackage_json json = Utils_.jsonFromFile(sapforPackage.getJsonFile(), SapforPackage_json.class);
|
||
LinkedHashMap<String, File> versions = new LinkedHashMap<>();
|
||
LinkedHashMap<String, String> descriptions = new LinkedHashMap<>();
|
||
for (SapforTask task : json.tasks) {
|
||
if (task.state.equals(TaskState.Done)) {
|
||
File task_root = Paths.get(
|
||
sapforPackage.getLocalWorkspace().getAbsolutePath(),
|
||
String.valueOf(task.set_id),
|
||
String.valueOf(task.sapfor_configuration_id)
|
||
).toFile();
|
||
for (String path : getSapforTaskResults(task)) {
|
||
File versionProject = Paths.get(task_root.getAbsolutePath(), path).toFile();
|
||
Vector<String> name_parts = new Vector_<>(
|
||
String.valueOf(task.id),
|
||
task.test_description,
|
||
versionProject.getName()
|
||
);
|
||
String name = String.join("_", name_parts);
|
||
versions.put(name, versionProject);
|
||
//--
|
||
String[] data = task.codes.split(" ");
|
||
Vector<String> codes_ = new Vector<>();
|
||
for (String datum : data) {
|
||
PassCode code = PassCode.valueOf(datum);
|
||
codes_.add(code.getDescription());
|
||
//--
|
||
descriptions.put(name, String.join("\n", codes_));
|
||
}
|
||
}
|
||
}
|
||
}
|
||
ZipFolderPass zip = new ZipFolderPass();
|
||
//создание тестов.
|
||
ServerSapfor serverSapfor = db.serverSapfors.getLastDoneVersion();
|
||
Vector<Test> tests = new Vector<>();
|
||
for (String name : versions.keySet()) {
|
||
File src = versions.get(name);
|
||
File[] files = src.listFiles(pathname -> !pathname.isDirectory());
|
||
if (files != null && files.length > 0) {
|
||
//--
|
||
Vector<String> filesNames = new Vector<>();
|
||
for (File file : files)
|
||
filesNames.add(file.getName());
|
||
//--
|
||
Test test = new Test();
|
||
test.group_id = group.id;
|
||
test.description = name;
|
||
test.files = String.join("\n", filesNames);
|
||
test.sender_address = group.sender_address;
|
||
test.sender_name = group.sender_name;
|
||
test.extended_description = descriptions.get(name);
|
||
db.Insert(test);
|
||
//---
|
||
File testProject = new File(Global.TestsDirectory, String.valueOf(test.id));
|
||
File testArchive = new File(Global.TestsDirectory, test.id + ".zip");
|
||
//---
|
||
if (testProject.exists())
|
||
FileUtils.forceDelete(testProject);
|
||
if (testArchive.exists())
|
||
FileUtils.forceDelete(testArchive);
|
||
//---
|
||
FileUtils.copyDirectory(src, testProject);
|
||
//---
|
||
if (!zip.Do(testProject.getAbsolutePath(), testArchive.getAbsolutePath())) {
|
||
db.Delete(test);
|
||
throw new RepositoryRefuseException("Не удалось переписать архив проекта");
|
||
}
|
||
tests.add(test);
|
||
db.DetectTestMinMaxDim(serverSapfor, group, test);
|
||
}
|
||
}
|
||
}
|
||
void ReplaceDVMPackageJson() throws Exception {
|
||
Pair<DVMPackage, byte[]> p = (Pair<DVMPackage, byte[]>) request.object;
|
||
DVMPackage src = p.getKey();
|
||
byte[] packed_file = p.getValue();
|
||
//todo написать стандартное исключение на несуществование объекта с ключом
|
||
if (!db.dvmPackages.containsKey(src.id))
|
||
throw new RepositoryRefuseException("не существует пакета с ключом " + src.id);
|
||
DVMPackage dst = db.dvmPackages.get(src.id);
|
||
dst.SynchronizeFields(src);
|
||
db.Update(dst);
|
||
Utils_.bytesToFile(packed_file, dst.getJsonFile());
|
||
}
|
||
void DetectTestsMinMaxDim() throws Exception {
|
||
Vector<Object> keys = (Vector<Object>) request.object;
|
||
for (Object key : keys) {
|
||
if (!db.tests.containsKey(key))
|
||
throw new RepositoryRefuseException("не существует пакета с ключом " + key);
|
||
}
|
||
ServerSapfor serverSapfor = db.serverSapfors.getLastDoneVersion();
|
||
for (Object key : keys) {
|
||
Test test = db.tests.get(key);
|
||
Group group = db.groups.get(test.group_id);
|
||
db.DetectTestMinMaxDim(serverSapfor, group, test);
|
||
}
|
||
}
|
||
void GetDVMPackageCredentials() throws Exception {
|
||
int package_id = (int) request.object;
|
||
if (!db.dvmPackages.containsKey(package_id))
|
||
throw new RepositoryRefuseException("Не найден пакет с ключом " + Utils_.Brackets(package_id));
|
||
DVMPackageCredentials res = machines_db.dvmPackagesCredentials.getForPackageId(package_id);
|
||
if (res == null)
|
||
throw new RepositoryRefuseException("Не найдено учетных данных для пакета " + Utils_.Brackets(package_id));
|
||
response.object = res;
|
||
}
|
||
//--
|
||
@Override
|
||
protected void Session() throws Exception {
|
||
switch (code) {
|
||
case GetDVMPackageCredentials:
|
||
GetDVMPackageCredentials();
|
||
break;
|
||
case PerformAutoSapforTesting:
|
||
PerformAutoSapforTesting();
|
||
break;
|
||
case DownloadTest:
|
||
DownloadTest();
|
||
break;
|
||
case ReceiveTestsDatabase:
|
||
ReceiveTestsDatabase();
|
||
break;
|
||
case RefreshDVMTests:
|
||
RefreshDVMTests();
|
||
break;
|
||
case DVMPackageNeedsKill:
|
||
DVMPackageNeedsKill();
|
||
break;
|
||
case UpdateActiveDVMPackages:
|
||
UpdateActiveDVMPackages();
|
||
break;
|
||
case GetFirstActiveSapforPackages:
|
||
GetFirstActiveSapforPackages();
|
||
break;
|
||
case SapforPackageNeedsKill:
|
||
SapforPackageNeedsKill();
|
||
break;
|
||
case UpdateActiveSapforPackages:
|
||
UpdateActiveSapforPackages();
|
||
break;
|
||
case DownloadDVMPackages:
|
||
DownloadDVMPackages();
|
||
break;
|
||
case DownloadSapforPackage:
|
||
DownloadSapforPackage();
|
||
break;
|
||
case ReplaceTestCode:
|
||
ReplaceTestCode();
|
||
break;
|
||
case ReplaceTestsCodes:
|
||
ReplaceTestsCodes();
|
||
break;
|
||
case GetFirstActiveDVMPackageForMachineURL:
|
||
GetFirstActiveDVMPackageForMachineURL();
|
||
break;
|
||
case GetServerName:
|
||
GetServerName();
|
||
break;
|
||
case StartNecessaryMachines:
|
||
StartNecessaryMachines();
|
||
break;
|
||
case GetSapforForCompilation:
|
||
GetSapforForCompilation();
|
||
break;
|
||
case GetMaxSapforVersion:
|
||
GetSapforMaxVersion();
|
||
break;
|
||
case JoinSapforVersionsToGroup:
|
||
JoinSapforPackageToGroup();
|
||
break;
|
||
case ReplaceDVMPackageResults:
|
||
ReplaceDVMPackageJson();
|
||
break;
|
||
case DetectTestsMinMaxDim:
|
||
DetectTestsMinMaxDim();
|
||
break;
|
||
default:
|
||
throw new RepositoryRefuseException("Неподдерживаемый код: " + code);
|
||
}
|
||
}
|
||
}
|
||
|