Skip to content

Commit

Permalink
done deploymanager and desktop run task
Browse files Browse the repository at this point in the history
  • Loading branch information
attiasas committed Aug 20, 2023
1 parent 0578625 commit fde3d58
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 83 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ repositories {
}

dependencies {
implementation group: 'org.attias.open.interactive.simulation', name: 'open-interactive-simulation-core', version: '1.0-SNAPSHOT'
implementation group: 'org.attias.open.interactive.simulation', name: 'open-interactive-simulation-core', version: '0.1'

implementation 'org.eclipse.jgit:org.eclipse.jgit:6.6.0.202305301015-r'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import org.attias.open.interactive.simulation.core.backend.engine.AppConfiguration;
import org.attias.open.interactive.simulation.core.backend.config.ProjectConfiguration;
import org.attias.open.interactive.simulation.core.utils.IOUtils;
import org.attias.open.interactive.simulation.deployer.Constant;
import org.attias.open.interactive.simulation.deployer.utils.ExtensionUtils;
import org.attias.open.interactive.simulation.deployer.utils.GitUtils;
import org.attias.open.interactive.simulation.deployer.utils.LogUtils;
import org.attias.open.interactive.simulation.deployer.utils.PluginUtils;
import org.eclipse.jgit.api.errors.GitAPIException;
Expand All @@ -16,61 +19,65 @@

import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;

public class DeployerManager {
private static final Logger log = LoggerFactory.getLogger(DeployerManager.class);
// version -> runner path
private static Map<String, Path> runnersWorkingDir = new Hashtable<>();

public static boolean fetchRunner(String version) throws GitAPIException {
Path runnerWorkingDir = getRunnerWorkingDirPath(version);

boolean fetch = IOUtils.createDirIfNotExists(runnerWorkingDir,true);
runnersWorkingDir.put(version, runnerWorkingDir);
if (fetch) {
// log.info("Fetching OIS runners version {}", this.deployerConfig.getVersion());
// GitUtils.cloneRepoByTag(GitUtils.OIS_RUNNER_GIT_REPO,this.deployerConfig.getVersion(),deployerDirectory.toString());
runnersWorkingDir.put(version, runnerWorkingDir);
throw new GradleException("Can't find validated runner in directory " + runnerWorkingDir);
if (getValidatedRunnerWorkingDir(version) != null) {
return false;
}
Path workingDir = getRunnerWorkingDirPath(version);
if (IOUtils.createDirIfNotExists(workingDir, true)) {
log.debug("Created OIS runner directory at {}", workingDir);
}
return fetch;
log.info("Fetching OIS runners version {}", version);
GitUtils.cloneRepoByTag(Constant.OIS_RUNNER_GIT_REPO, version, workingDir.toString());
return true;
}

public static void executeRunOnPlatform(Project project, Set<File> projectJars, AppConfiguration.AppType appType) throws IOException {
ProjectConfiguration configuration = PluginUtils.getProjectConfiguration(PluginUtils.getProjectConfigurationPath(project));
if (configuration == null) {
throw new GradleException("Can't fetch project configurations " + project.getPath());
}
String[] gradleCommands = null;
if (appType.equals(AppConfiguration.AppType.Desktop)) {
gradleCommands = new String[]{"clean", "run"};
}
if (gradleCommands == null) {
throw new GradleException("Unknown application type " + appType);
}
executeGradleCommand(configuration.runner.version, getRunnerEnvVariables(project, projectJars), gradleCommands);
executeGradleCommand(
getActualRunnerWorkingDir(project),
getRunnerEnvVariables(project, projectJars),
gradleCommands
);
}

public static Path getValidatedRunnerWorkingDir(String version) {
Path workingDir = getRunnerWorkingDirPath(version);
return validateRunnerWorkingDir(workingDir) ? workingDir : null;
}

public static Map<String, String> getRunnerEnvVariables(Project project, Set<File> projectJars) {
Map<String, String> env = new HashMap<>();
// Add Runners expected environment variables
env.put(AppConfiguration.ENV_PROJECT_JAR, String.valueOf(new ArrayList<>(projectJars).get(0)));

Path assetsPath = ExtensionUtils.getOverrideAssetsPath(project);
if (assetsPath != null) {
env.put(AppConfiguration.ENV_PROJECT_ASSETS_DIR, assetsPath.toString());
}
assetsPath = PluginUtils.getProjectDefaultResourcesDirPath(project);
if (assetsPath != null) {
env.put(AppConfiguration.ENV_PROJECT_ASSETS_DIR, assetsPath.toString());
}
env.put(ProjectConfiguration.ENV_PROJECT_CONFIG_PATH, PluginUtils.getProjectConfigurationPath(project).toString());
log.info("Runner Env: {}", env);
// Add other existing
env.putAll(System.getenv());
return env;
}

private static void executeGradleCommand(String runnerVersion, Map<String, String> environmentVariables, String... gradleCommands) {
Path workingDir = getValidatedRunnerWorkingDir(runnerVersion);
workingDir = Paths.get("C:\\Users\\Assaf Attias\\code\\OpenInteractiveSimulation\\open-interactive-simulation-runner");
if (workingDir == null) {
throw new RuntimeException("Before executing gradle task on runner " + runnerVersion + " fetch it");
}
private static void executeGradleCommand(Path workingDir, Map<String, String> environmentVariables, String... gradleCommands) {
try (ProjectConnection connection = GradleConnector.newConnector().forProjectDirectory(workingDir.toFile()).connect()){
BuildLauncher launcher = connection.newBuild()
.forTasks(gradleCommands);
Expand All @@ -84,21 +91,28 @@ private static void executeGradleCommand(String runnerVersion, Map<String, Strin
}
}

private static boolean validateRunnerWorkingDir(Path workingDir) {
return workingDir.toFile().exists();
private static Path getActualRunnerWorkingDir(Project project) throws IOException {
Path workingDir = ExtensionUtils.getOverrideRunnerPath(project);
if (workingDir != null) {
return workingDir;
}
ProjectConfiguration configuration = PluginUtils.getProjectConfiguration(project);
if (configuration == null) {
throw new GradleException("Can't fetch project configurations " + project.getPath());
}
workingDir = getValidatedRunnerWorkingDir(configuration.runner.version);
if (workingDir == null) {
throw new RuntimeException("Can't find valid runner working directory to run gradle task.");
}
return workingDir;
}

public static Path getRunnerWorkingDirPath(String version) {
return PluginUtils.RUNNERS_PATH.resolve(version);
private static boolean validateRunnerWorkingDir(Path workingDirPath) {
File dir = workingDirPath.toFile();
return dir.exists() && dir.isDirectory() && dir.list().length > 0;
}

public static Path getValidatedRunnerWorkingDir(String version) {
Path workingDir = getRunnerWorkingDirPath(version);
if (!validateRunnerWorkingDir(workingDir)) {
runnersWorkingDir.remove(version);
return null;
}
runnersWorkingDir.put(version, workingDir);
return workingDir;
private static Path getRunnerWorkingDirPath(String version) {
return Constant.RUNNERS_PATH.resolve(version);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.attias.open.interactive.simulation.deployer.tasks;

import org.attias.open.interactive.simulation.core.backend.config.ProjectConfiguration;
import org.attias.open.interactive.simulation.core.utils.IOUtils;
import org.attias.open.interactive.simulation.deployer.SimulationDeployerExtension;
import org.attias.open.interactive.simulation.deployer.backend.DeployerManager;
import org.attias.open.interactive.simulation.deployer.utils.ExtensionUtils;
import org.attias.open.interactive.simulation.deployer.utils.PluginUtils;
Expand All @@ -13,18 +11,13 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class InitializeDeployerTask extends DefaultTask {

private static final Logger log = LoggerFactory.getLogger(InitializeDeployerTask.class);

public static final String NAME = "initializeDeployer";

@TaskAction
public void initialize() throws IOException, GitAPIException {
Project project = getProject();
Expand All @@ -41,15 +34,20 @@ public void initialize() throws IOException, GitAPIException {
log.info("{}: Getting project configurations", projectPath);
ProjectConfiguration configuration = PluginUtils.getProjectConfiguration(PluginUtils.getProjectConfigurationPath(project));
// Validate runner is ready to be used
log.debug("{}: Using runner {}", projectPath, configuration.runner.version);
if (DeployerManager.fetchRunner(configuration.runner.version)) {
log.info("{}: Runner {} created from git repository", projectPath, configuration.runner.version);
Path overrideRunner = ExtensionUtils.getOverrideRunnerPath(project);
if (overrideRunner != null) {
log.info("{}: Using custom runner from {}", projectPath, overrideRunner);
} else {
log.info("{}: Using runner version {}", projectPath, configuration.runner.version);
if (DeployerManager.fetchRunner(configuration.runner.version)) {
log.info("{}: Runner {} created from git repository", projectPath, configuration.runner.version);
}
}
Path tempAssetsDirectory = Files.createTempDirectory("ois");
log.info("{}: Preparing assets at {}", projectPath, tempAssetsDirectory);
for (String assetDirectory : configuration.runner.assetsDirectories) {
log.info("{} Copying assets from {}", projectPath, assetDirectory);
IOUtils.copyDirectoryContent(Paths.get(assetDirectory), tempAssetsDirectory);
Path assetsDirectory = ExtensionUtils.getOverrideAssetsPath(project);
if (assetsDirectory != null) {
log.info("{}: Preparing assets from custom path {}", projectPath, assetsDirectory);
} else {
log.info("{}: Preparing assets from resources directory {}", projectPath, PluginUtils.getProjectDefaultResourcesDirPath(project));
}
log.info("{}: Run environment is ready", projectPath);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.attias.open.interactive.simulation.core.backend.engine.AppConfiguration;
import org.attias.open.interactive.simulation.deployer.backend.DeployerManager;
import org.attias.open.interactive.simulation.deployer.utils.ExtensionUtils;
import org.attias.open.interactive.simulation.deployer.utils.PluginUtils;
import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
Expand All @@ -12,13 +11,10 @@

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Set;
import java.util.stream.Collectors;

public class RunDesktopSimulationTask extends DefaultTask {
private static final Logger log = LoggerFactory.getLogger(RunDesktopSimulationTask.class);
public static final String NAME = "runDesktop";

@TaskAction
public void runSimulation() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,33 @@

import org.attias.open.interactive.simulation.core.backend.config.ProjectConfiguration;
import org.attias.open.interactive.simulation.core.utils.IOUtils;
import org.attias.open.interactive.simulation.deployer.SimulationDeployerExtension;
import org.attias.open.interactive.simulation.deployer.Constant;
import org.gradle.api.Project;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Set;

public class PluginUtils {

private static final Logger log = LoggerFactory.getLogger(PluginUtils.class);

public static final Path HOME_PATH = Paths.get(System.getProperty("user.home"), ".ois");
public static final Path RUNNERS_PATH = HOME_PATH.resolve("runners");
public static final String PROJECT_CONFIG_FILE_NAME = "simulation.ois";

public static boolean createPluginDirectoryIfNotExists() {
return IOUtils.createDirIfNotExists(HOME_PATH,true);
return IOUtils.createDirIfNotExists(Constant.HOME_PATH,true);
}

public static boolean createRunnersDirectoryIfNotExists() {
return IOUtils.createDirIfNotExists(RUNNERS_PATH,true);
return IOUtils.createDirIfNotExists(Constant.RUNNERS_PATH,true);
}

public static Path getProjectConfigurationPath(Project project) {
Path configFile = project.getProjectDir().toPath().resolve(PROJECT_CONFIG_FILE_NAME);
Path configFile = project.getProjectDir().toPath().resolve(Constant.PROJECT_CONFIG_FILE_NAME);
// override config file location with extension value if exist
SimulationDeployerExtension extension = ExtensionUtils.getExtensionWithDeployer(project);
if (extension != null) {
configFile = Paths.get(extension.getConfigPath());
Path override = ExtensionUtils.getOverrideConfigPath(project);
if (override != null) {
configFile = override;
}
return configFile;
}
Expand All @@ -45,11 +39,18 @@ public static boolean createProjectConfigurationIfNotExists(Project project) thr
return false;
}
log.info("Creating default project configurations at {}", configPath);
IOUtils.writeAsJsonFile(createProjectConfiguration(), configPath);
return true;
}

private static ProjectConfiguration createProjectConfiguration() {
ProjectConfiguration configuration = new ProjectConfiguration();
configuration.name = Constant.DEFAULT_PROJECT_TITLE;
configuration.initialState = "StateKey";
configuration.states.put("StateKey", "com.example.IStateClassName");
IOUtils.writeAsJsonFile(configuration, configPath);
return true;
configuration.runner.version = Constant.DEFAULT_RUNNER_VERSION;
configuration.runner.types = Constant.APP_TYPES;
return configuration;
}

public static ProjectConfiguration getProjectConfiguration(Project project) throws IOException {
Expand All @@ -59,7 +60,7 @@ public static ProjectConfiguration getProjectConfiguration(Project project) thro
public static ProjectConfiguration getProjectConfiguration(Path path) throws IOException {
File file = path.toFile();
if (file.isDirectory()) {
return IOUtils.getObjFromJsonFile(path.resolve(PROJECT_CONFIG_FILE_NAME).toFile(), ProjectConfiguration.class);
return IOUtils.getObjFromJsonFile(path.resolve(Constant.PROJECT_CONFIG_FILE_NAME).toFile(), ProjectConfiguration.class);
} else {
return IOUtils.getObjFromJsonFile(file, ProjectConfiguration.class);
}
Expand All @@ -68,4 +69,12 @@ public static ProjectConfiguration getProjectConfiguration(Path path) throws IOE
public static Set<File> getProjectJars(Project project) {
return project.getTasks().getByName("jar").getOutputs().getFiles().getFiles();
}

public static Path getProjectDefaultResourcesDirPath(Project project) {
File file = project.file("src/main/resources");
if (file.exists() && file.isDirectory() && file.list().length > 0) {
return file.toPath();
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.attias.open.interactive.simulation.deployer.utils;

import org.attias.open.interactive.simulation.deployer.Constant;
import org.attias.open.interactive.simulation.deployer.tasks.InitializeDeployerTask;
import org.attias.open.interactive.simulation.deployer.tasks.RunDesktopSimulationTask;
import org.gradle.api.Project;
Expand All @@ -11,26 +12,25 @@

public class TaskUtils {
private static final Logger log = LoggerFactory.getLogger(TaskUtils.class);
private static final String GROUP_NAME = "ois";

public static <T extends Task> TaskProvider<T> registerTaskInProject(String taskName, Class<T> taskClass, String taskDescription, Project project) {
log.debug("Configure {} task for project", taskName, project.getPath());
return project.getTasks().register(taskName, taskClass, task -> {
task.setDescription(taskDescription);
task.setGroup(GROUP_NAME);
task.setGroup(Constant.GROUP_NAME);
});
}

public static TaskProvider<InitializeDeployerTask> addInitializeDeployerTask(Project project) {
try {
return project.getTasks().named(RunDesktopSimulationTask.NAME, InitializeDeployerTask.class);
return project.getTasks().named(Constant.INIT_TASK_NAME, InitializeDeployerTask.class);
} catch (UnknownTaskException e) {
log.debug("Registering '{}' task to the project {}", RunDesktopSimulationTask.NAME, project.getPath());
log.debug("Registering '{}' task to the project {}", Constant.INIT_TASK_NAME, project.getPath());
}
TaskProvider<InitializeDeployerTask> task = registerTaskInProject(
InitializeDeployerTask.NAME,
Constant.INIT_TASK_NAME,
InitializeDeployerTask.class,
"Initialize the simulation deployer resources before running related tasks",
Constant.INIT_TASK_DESCRIPTION,
project
);
task.configure(initializeDeployerTask -> initializeDeployerTask.dependsOn(project.getTasks().named("build")));
Expand All @@ -39,15 +39,15 @@ public static TaskProvider<InitializeDeployerTask> addInitializeDeployerTask(Pro

public static void addRunDesktopTask(Project project, TaskProvider<InitializeDeployerTask> initTask) {
try {
project.getTasks().named(RunDesktopSimulationTask.NAME);
project.getTasks().named(Constant.RUN_DESKTOP_TASK_NAME);
return;
} catch (UnknownTaskException e) {
log.debug("Registering '{}' task to the project {}", RunDesktopSimulationTask.NAME, project.getPath());
log.debug("Registering '{}' task to the project {}", Constant.RUN_DESKTOP_TASK_NAME, project.getPath());
}
registerTaskInProject(
RunDesktopSimulationTask.NAME,
Constant.RUN_DESKTOP_TASK_NAME,
RunDesktopSimulationTask.class,
"Runs the simulation using desktop",
Constant.RUN_DESKTOP_TASK_DESCRIPTION,
project
).configure(runDesktopSimulationTask -> runDesktopSimulationTask.dependsOn(initTask));
}
Expand Down

0 comments on commit fde3d58

Please sign in to comment.