Files
VisualSapfor/src/_VisualDVM/TestingSystem/Common/TestingServer.java

846 lines
38 KiB
Java
Raw Normal View History

2024-10-09 22:21:57 +03:00
package _VisualDVM.TestingSystem.Common;
2024-10-07 14:22:52 +03:00
import Common.CommonConstants;
2024-10-14 15:19:13 +03:00
import Common.Database.Objects.DBObject;
2025-02-05 01:14:41 +03:00
import Common.Database.Objects.riDBObject;
import Common.Database.RepositoryRefuseException;
2024-10-14 15:19:13 +03:00
import Common.Utils.TextLog;
2024-10-11 00:00:30 +03:00
import Common.Utils.Utils_;
import Common.Utils.Vector_;
2025-02-18 16:21:20 +03:00
import _VisualDVM.ComponentsServer.Component.Sapfor.Sapfor;
import _VisualDVM.ComponentsServer.UserAccount.AccountRole;
import _VisualDVM.ComponentsServer.UserAccount.UserAccount;
2024-10-09 22:01:19 +03:00
import _VisualDVM.Constants;
import _VisualDVM.Global;
import _VisualDVM.GlobalData.Tasks.TaskState;
import _VisualDVM.Passes.All.DownloadDvmTestsRepository;
2024-10-14 15:19:13 +03:00
import _VisualDVM.Passes.All.ZipFolderPass;
import _VisualDVM.Passes.PassCode;
import _VisualDVM.ProjectData.Files.ProjectFile;
2024-10-09 22:21:57 +03:00
import _VisualDVM.ProjectData.LanguageName;
import _VisualDVM.Repository.EmailMessage;
2025-02-18 16:21:20 +03:00
import _VisualDVM.Repository.Server.RepositoryServer;
2024-10-09 22:21:57 +03:00
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.Json.TestFileJson;
import _VisualDVM.TestingSystem.Common.Test.Json.TestFilesJson;
2024-10-09 22:21:57 +03:00
import _VisualDVM.TestingSystem.Common.Test.Test;
import _VisualDVM.TestingSystem.Common.Test.TestType;
import _VisualDVM.TestingSystem.Common.TestingPackageToKill.TestingPackageToKill;
import _VisualDVM.TestingSystem.DVM.DVMConfiguration.DVMConfiguration;
2024-10-09 22:21:57 +03:00
import _VisualDVM.TestingSystem.DVM.DVMPackage.DVMPackage;
import _VisualDVM.TestingSystem.DVM.DVMSettings.DVMSettings;
2024-10-09 22:21:57 +03:00
import _VisualDVM.TestingSystem.DVM.DVMTestingChecker;
import _VisualDVM.TestingSystem.DVM.PackageCredentials.DVMPackageCredentials;
2024-10-09 22:21:57 +03:00
import _VisualDVM.TestingSystem.SAPFOR.Json.SapforPackage_json;
import _VisualDVM.TestingSystem.SAPFOR.Json.SapforVersion_json;
2024-10-09 22:21:57 +03:00
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;
2024-10-09 22:21:57 +03:00
import _VisualDVM.TestingSystem.SAPFOR.SapforTestingPlanner;
import _VisualDVM.TestingSystem.SAPFOR.ServerSapfor.ServerSapfor;
import _VisualDVM.TestingSystem.SAPFOR.ServerSapfor.ServerSapforState;
2024-10-14 15:19:13 +03:00
import _VisualDVM.Utils;
2023-09-17 22:13:42 +03:00
import javafx.util.Pair;
import org.apache.commons.io.FileUtils;
2023-09-17 22:13:42 +03:00
import javax.swing.*;
2023-09-17 22:13:42 +03:00
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;
2023-09-17 22:13:42 +03:00
public class TestingServer extends RepositoryServer<TestsDatabase> {
public static MachineProcessSet machinesProcesses = new MachineProcessSet();
2024-10-14 15:19:13 +03:00
//------>>>
public static Timer checkTimer = null;
public String name = "?";
DVMTestingChecker dvmTestingChecker = null;
SapforTestingPlanner sapforTestingPlanner = null;
//-->
MachinesDatabase machines_db;
//-->
2024-10-14 15:19:13 +03:00
public TestingServer() {
super(TestsDatabase.class);
name = Utils_.getDateName("testingServer");
}
public static void TimerOn() {
2025-01-18 01:36:02 +03:00
checkTimer = new Timer(Global.normalProperties.CheckTestingIntervalSeconds * 1000, e -> {
2024-10-14 15:19:13 +03:00
Global.mainModule.getPass(PassCode.ActualizePackages).Do();
});
checkTimer.start();
}
public static void TimerOff() {
if (checkTimer != null)
checkTimer.stop();
}
public static void ResetTimer() {
TimerOff();
TimerOn();
}
@Override
2025-03-13 00:32:20 +03:00
public void StartAction() throws Exception {
try {
machines_db = new MachinesDatabase();
machines_db.Activate();
} catch (Exception ex) {
ex.printStackTrace();
}
}
@Override
protected void extraBackup(File todayBackUp) {
zip.Do("Tests", new File(todayBackUp, "Tests.zip").getAbsolutePath());
}
@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;
//--
2024-10-14 12:54:52 +03:00
Utils_.CheckAndCleanDirectory(dvmPackage.getLocalWorkspace());
//--
dvmPackage.saveJson();
dvmPackage.package_json = null; // объект больше не нужен.
2024-04-26 20:28:50 +03:00
//--
//Занести учетные данные пакета в базу о машинах. это основной поток, нить не нужна. арг это пароль
DVMPackageCredentials credentials = new DVMPackageCredentials(dvmPackage, request.arg);
machines_db.Insert(credentials);
//--
} else if (object instanceof SapforPackage) {
((SapforPackage) object).init();
}
}
2024-09-14 00:18:27 +03:00
@Override
protected void afterCloneAction(riDBObject src, riDBObject dst) throws Exception {
if (src instanceof SapforSettings) {
2025-02-05 01:14:41 +03:00
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;
2024-10-14 12:54:52 +03:00
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.getServerPath());
}
//--
} else if (object instanceof ServerSapfor) {
2024-10-14 12:54:52 +03:00
Utils_.forceDeleteWithCheck(
new File(
((ServerSapfor) object).home_path
)
);
2024-10-14 15:19:13 +03:00
} else if (object instanceof DVMPackage) {
DVMPackage dvmPackage = (DVMPackage) object;
File workspace = dvmPackage.getLocalWorkspace();
2024-10-14 12:54:52 +03:00
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();
2024-10-14 12:54:52 +03:00
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()) {
2024-10-14 15:19:13 +03:00
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 + " уже установлена");
}
}
2023-09-17 22:13:42 +03:00
@Override
public int getPort() {
return Constants.TestingServerPort;
2023-09-17 22:13:42 +03:00
}
@Override
protected void startAdditionalThreads() {
dvmTestingChecker = new DVMTestingChecker();
sapforTestingPlanner = new SapforTestingPlanner();
new Thread(() -> {
while (true) {
dvmTestingChecker.Perform();
sapforTestingPlanner.Perform();
Utils_.sleep(5000);
}
}).start();
}
void PerformAutoSapforTesting() throws Exception {
int sapforId = Integer.parseInt(request.arg);
if (!db.serverSapfors.containsKey(sapforId)) {
throw new RepositoryRefuseException("Версия SAPFOR " + sapforId + " не существует.");
}
ServerSapfor sapfor = db.serverSapfors.get(sapforId);
TextLog Log = new TextLog();
SapforPackage autoPackage = tryAutoSapforTesting(sapfor,Log);
EmailMessage message = Log.isEmpty() ?
new EmailMessage(
"Запущено автоматическое тестирование версии " + sapfor.version + " системы SAPFOR",
"Пакет " + Utils_.Brackets(autoPackage.id)) :
new EmailMessage(
"Не удалось запустить автоматическое тестирование версии " + sapfor.version + " системы SAPFOR",
Log.toString()
);
for (String address : Constants.admins_mails)
2025-03-13 00:32:20 +03:00
EmailMessagesQueue.add(new Pair<>(address, message));
2023-09-17 22:13:42 +03:00
}
void DownloadTest() throws Exception {
int test_id = Integer.parseInt(request.arg);
if (db.tests.containsKey(test_id)) {
Test test = db.tests.get(test_id);
File archive = new File(Global.TempDirectory, Utils_.getDateName(String.valueOf(test.id)));
ZipFolderPass zipFolderPass = new ZipFolderPass();
if (!zipFolderPass.Do(test.getServerPath().getAbsolutePath(), archive.getAbsolutePath()))
throw new RepositoryRefuseException("Не удалось заархивировать тест");
response = new ServerExchangeUnit_2021(ServerCode.OK, "", Utils_.fileToBytes(archive));
FileUtils.forceDelete(archive);
} 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")
2024-10-11 00:00:30 +03:00
&& 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);
2025-02-15 23:30:48 +03:00
ServerSapfor serverSapfor = db.serverSapfors.getLastDoneVersion();
DownloadDvmTestsRepository downloadRepository = new DownloadDvmTestsRepository();
if (!downloadRepository.Do())
throw new RepositoryRefuseException("Не удалось обновить репозиторий");
//-->>
Vector<Pair<Group, Vector<File>>> groups = new Vector<>();
File testsSrc = Paths.get(
Global.RepoDirectory.getAbsolutePath(),
"dvm-auto-test", "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)
2025-02-15 23:30:48 +03:00
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() {
2024-10-07 14:22:52 +03:00
response.object = CommonConstants.Nan;
2023-12-15 02:34:30 +03:00
int packageId = (int) request.object;
for (TestingPackageToKill packageToKill : db.testingPackagesToKill.Data.values()) {
if ((packageToKill.packageId == packageId) && (packageToKill.type == 0)) {
response.object = packageToKill.id;
2023-12-15 02:34:30 +03:00
break;
}
}
}
void SapforPackageNeedsKill() throws Exception {
2024-10-07 14:22:52 +03:00
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();
2024-10-11 00:00:30 +03:00
res.add(new Pair<>(dvmPackage_id, new Pair(Utils_.fileToBytes(results_zip), Utils_.fileToBytes(package_json))));
}
response.object = res;
}
void DownloadSapforPackage() throws Exception {
2023-12-18 00:04:44 +03:00
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())
2024-10-11 00:00:30 +03:00
response.object = Utils_.fileToBytes(results_zip);
else
throw new RepositoryRefuseException("Не удалось заархивировать пакет тестирования SAPFOR с ключом " + sapforPackage_id);
2023-12-18 00:04:44 +03:00
}
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 " +
2024-10-11 00:00:30 +03:00
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 -P " +
2024-10-11 00:00:30 +03:00
Utils_.DQuotes(Global.TempDirectory.getAbsolutePath()) +
" " + Utils_.DQuotes(Constants.SAPFOR_VERSION_URL)
).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 versionSapfor = db.getSapforForVersion(String.valueOf(current_version));
if (versionSapfor == null || !versionSapfor.state.equals(ServerSapforState.DoneWithErrors)) {
serverSapfor = new ServerSapfor();
serverSapfor.sender_name = "server";
serverSapfor.sender_address = Constants.MailAddress;
serverSapfor.version = String.valueOf(current_version);
serverSapfor.state = ServerSapforState.Queued;
db.Insert(serverSapfor);
}
}
}
response.object = serverSapfor;
}
SapforPackage tryAutoSapforTesting(ServerSapfor sapfor, TextLog Log) throws Exception {
2024-09-24 20:22:41 +03:00
//--
UserAccount account = new UserAccount();
account.name = "server";
account.email = Constants.MailAddress;
account.role = AccountRole.Admin;
2024-09-24 20:22:41 +03:00
//-
if (!sapfor.state.equals(ServerSapforState.Done)) {
Log.Writeln_("Выбранная версия SAPFOR " + sapfor.version + " не собрана!");
return null;
2024-09-24 20:22:41 +03:00
}
Vector<SapforConfiguration> configurations = db.sapforConfigurations.getAutoConfigurations();
if (configurations.isEmpty()) {
Log.Writeln_("Не найдено конфигураций для автоматического тестирования!");
return null;
2024-09-24 20:22:41 +03:00
}
SapforPackage target = new SapforPackage(account,
sapfor,
configurations,
1,
Log);
//-
if (target.tasksCount == 0) {
Log.Writeln_("Не сформировано ни одной новой задачи.");
return null;
}
2024-09-24 20:22:41 +03:00
beforePublishAction(target);
db.InsertS(target);
afterPublishAction(target);
//--
return target;
2024-09-24 20:22:41 +03:00
}
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_));
}
}
}
}
//создание тестов.
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) {
//--
TestFilesJson filesJson = new TestFilesJson();
for (File file : files) {
filesJson.values.add(new TestFileJson(new ProjectFile(file)));
}
//--
Test test = new Test();
test.group_id = group.id;
test.description = name;
test.packedFilesJson = Utils_.gson.toJson(filesJson);
2025-02-12 14:22:11 +03:00
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));
//---
if (testProject.exists())
FileUtils.forceDelete(testProject);
2025-02-11 23:08:27 +03:00
FileUtils.copyDirectory(src, testProject);
//---
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());
2025-02-12 14:22:11 +03:00
}
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);
}
}
2023-09-17 22:13:42 +03:00
}