Skip to content

Commit

Permalink
Configuration files copying to the server
Browse files Browse the repository at this point in the history
  • Loading branch information
Shathe committed Apr 18, 2016
1 parent 2463ddb commit ff7097e
Show file tree
Hide file tree
Showing 12 changed files with 592 additions and 290 deletions.
1 change: 0 additions & 1 deletion butler/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ compileJava {
dependencies {
compile 'org.springframework.boot:spring-boot-starter'
compile 'org.springframework.shell:spring-shell:1.1.0.RELEASE'
compile 'org.springframework.boot:spring-boot-starter-remote-shell'
compile 'org.apache.lucene:lucene-analyzers-common:4.10.4'
compile 'org.apache.lucene:lucene-queryparser:4.10.4'
compile 'org.apache.lucene:lucene-core:4.10.4'
Expand Down
1 change: 1 addition & 0 deletions site/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("org.springframework:spring-jdbc")
compile("commons-io:commons-io:2.4")
compile("com.h2database:h2")
testCompile("junit:junit")
}
Expand Down
5 changes: 5 additions & 0 deletions site/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
Expand Down
10 changes: 2 additions & 8 deletions site/src/main/java/crawlers/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,15 @@ public void run(String... strings) throws Exception {
jdbcTemplate
.execute("CREATE TABLE projectCrawlers(" + "id SERIAL, idUser long, name VARCHAR(100), date DATETIME)");
jdbcTemplate.execute(
"CREATE TABLE configurationCrawlers(id SERIAL,idProject long, dslPath VARCHAR(200),pluginsPath VARCHAR(200))");
"CREATE TABLE configurationCrawlers(id SERIAL,idProject long)");

jdbcTemplate.execute(
"CREATE TABLE imageCrawlers(id SERIAL, idProject long, idConfiguration long, name VARCHAR(100), date DATETIME)");

jdbcTemplate.execute(
"CREATE TABLE containerCrawlers(" + "id SERIAL, idProject long,idImage long, name VARCHAR(100), date DATETIME)");

jdbcTemplate.execute("CREATE TABLE pluginsCrawlers(id SERIAL, idProject long, idConfiguration long)");

jdbcTemplate.execute("CREATE TABLE jarsCrawlers(id SERIAL, idPlugin long,path VARCHAR(200))");
jdbcTemplate.execute("CREATE TABLE xmlCrawlers(id SERIAL, idPlugin long,path VARCHAR(200))");

CommonOps ops= new CommonOps();
// Inserts
Expand All @@ -66,10 +63,7 @@ public void run(String... strings) throws Exception {
jdbcTemplate.update("insert into projectCrawlers (idUser,name,date) values (?,?,?)", "1", "First project",
new Date(System.currentTimeMillis()));

jdbcTemplate.update("insert into configurationCrawlers (dslPath,pluginsPath,idProject) values (?,?,?)", "Vieja",
"C://RutaLarga/lolo/jeje/plugins", 1);
jdbcTemplate.update("insert into configurationCrawlers (dslPath,pluginsPath,idProject) values (?,?,?)", "Nueva",
"C://RutaLarga/lolo/jeje/plugins", 1);
jdbcTemplate.update("insert into configurationCrawlers (idProject) values (?)", 1);
jdbcTemplate.update("insert into imageCrawlers (idProject,idConfiguration,name,date) values (?,?,?,?)", 1, 1,
"Image :D First", new Date(System.currentTimeMillis()));

Expand Down
166 changes: 158 additions & 8 deletions site/src/main/java/crawlers/ConfigurationController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,28 @@

package crawlers;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.nio.file.Files;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import dataBase.ConfigurationDatabase;
import errors.BadRequestError;
import errors.InternalError;
import models.Configuration;

Expand Down Expand Up @@ -44,34 +54,174 @@ ResponseEntity<Configuration> configuration(@RequestParam(value = "idProject") S
try {
configuration = configDB.GetConfigurationFromProject(idProject);
} catch (Exception a) {
throw new InternalError("Error getting configuration: "+a.getMessage());
throw new InternalError("Error getting configuration: " + a.getMessage());
}

return new ResponseEntity<Configuration>(configuration, HttpStatus.OK);
}



/**
* Returns the created project if it has been created, if not, returns an
* error message
*/
@RequestMapping(value = "/createConfiguration", method = RequestMethod.POST)
ResponseEntity<Configuration> createProject(@RequestParam(value = "idProject") String idProject, @RequestParam(value = "pluginsPath") String pluginsPath,
@RequestParam(value = "dslPath") String dslPath) {
ResponseEntity<Configuration> createProject(@RequestParam(value = "idProject") String idProject) {

Configuration configuration = new Configuration(0, pluginsPath, dslPath, idProject);
Configuration configuration = new Configuration(0, idProject);
ConfigurationDatabase configurationDB = new ConfigurationDatabase(jdbcTemplate);
try {
configurationDB.createConfiguration(configuration);
log.info("created configuration " + configuration.getId());
configuration=configurationDB.GetConfigurationFromProject(idProject);
configuration = configurationDB.GetConfigurationFromProject(idProject);
// Creates the project files (Not implemented)
// CCreate docker image?
} catch (Exception a) {
throw new InternalError("Error creating: "+a.getMessage());
throw new InternalError("Error creating: " + a.getMessage());
}
return new ResponseEntity<Configuration>(configuration, HttpStatus.OK);
}

/**
* Uploads the dsl file of a user to an auxiliar folder, if it's not upload
* returns an error
*
* @param file
* @param idUser
*/
@RequestMapping(method = RequestMethod.POST, value = "/uploadDSL")
public ResponseEntity<String> handleFileUploadDSL(@RequestParam("dsl") MultipartFile file,
@RequestParam("idUser") String idUser) {

// target DSL name
String name = "dsl.yml";
if (!file.isEmpty()) {
try {
// User folder
File userFolder = new File(idUser);
if (!userFolder.exists())
Files.createDirectory(userFolder.toPath());
// Auxiliar folder where the temporal configuration files are
// copy
String folderAux = idUser + "/aux";
File folder = new File(folderAux);
if (!folder.exists())
Files.createDirectory(folder.toPath());
// Copy DSL file
BufferedOutputStream stream = new BufferedOutputStream(
new FileOutputStream(new File(folderAux + "/" + name)));
FileCopyUtils.copy(file.getInputStream(), stream);
stream.close();
log.info("DSL copied: " + idUser);
} catch (Exception e) {
log.warn("DSL not copied:" + e.getMessage());
throw new InternalError("Error copying: " + e.getMessage());

}
} else {
log.warn("DSL not copied, empty file: " + idUser);
throw new BadRequestError("Empty file");
}
return new ResponseEntity<>(new String("copied"), HttpStatus.OK);
}

/**
* Uploads a list of files of a user, the plugin files to an auxiliar
* folder, if it's not upload returns an error
*
* @param file
* @param idUser
*/

@RequestMapping(method = RequestMethod.POST, value = "/pluginForm")
public ResponseEntity<String> PluginForm(@RequestParam("name") String name, @RequestParam("idUser") String idUser,
@RequestParam("plugin") List<MultipartFile> files) {
if (!files.isEmpty()) {
try {
// If there's no user folder, creates it
File userFolder = new File(idUser);
if (!userFolder.exists())
Files.createDirectory(userFolder.toPath());
// Auxiliar folder where the temporal configuration files are
// copy
String folderAux = idUser + "/aux";
File folder = new File(folderAux);
if (!folder.exists())
Files.createDirectory(folder.toPath());
// Plugins folder
String pluginsfolder = folderAux + "/plugins";
File folderPluginAll = new File(pluginsfolder);
if (!folderPluginAll.exists())
Files.createDirectory(folderPluginAll.toPath());
// Plugin folder (in the plugins folder)
File folderPlugin = new File(pluginsfolder + "/" + name);
if (!folderPlugin.exists())
Files.createDirectory(folderPlugin.toPath());
// Copy every plugin file (plugin.xml and jars)
for (MultipartFile file : files) {
BufferedOutputStream stream = new BufferedOutputStream(
new FileOutputStream(new File(folderPlugin + "/" + file.getOriginalFilename())));
FileCopyUtils.copy(file.getInputStream(), stream);
stream.close();
}
log.info("Plugin copied: " + idUser);
} catch (Exception e) {
log.warn("Plugin not copied:" + e.getMessage());
throw new InternalError("Error copying: " + e.getMessage());
}
} else {
log.warn("There are no plugins to upload: " + idUser);
throw new BadRequestError("There are no plugins to upload");
}
return new ResponseEntity<>(new String("copied"), HttpStatus.OK);
}

/**
* Deletes every file uploaded in the auxiliar folder of a user
*
* @param idUser
*/
@RequestMapping(method = RequestMethod.POST, value = "/resetUploadConfig")
public ResponseEntity<String> resetUploadConfig(@RequestParam("idUser") String idUser) {

try {
// Deletes the auxiliar folder
String folderAux = idUser + "/aux";
File folder = new File(folderAux);
FileUtils.deleteDirectory(folder);

log.info("Auxuliar folder deleted: " + idUser);
} catch (Exception e) {
log.warn("Auxuliar folder deleted:" + e.getMessage());
throw new InternalError("Error deleting auxiliar folder: " + e.getMessage());
}
return new ResponseEntity<>(new String("deleted"), HttpStatus.OK);
}

/**
* Saves the configuration files of the auxiliar folder to an static one
*
* @param idUser
* @param idProject
* @param idConfig
* @return
*/
@RequestMapping(method = RequestMethod.POST, value = "/saveConfigurationFiles")
public ResponseEntity<String> saveConfigurationFiles(@RequestParam("idUser") String idUser,
@RequestParam("idProject") String idProject, @RequestParam("idConfig") String idConfig) {

try {
// Rename auxiliar folder to IdProject_idConfiguration_configFiles (1_2_configFiles)
// This files will be used by Butler builder
String folderAux = idUser + "/aux";
File folder = new File(folderAux);
File folderNew = new File(idUser + "/" + idProject + "_" + idConfig + "_configFiles");
folder.renameTo(folderNew);
log.info("Configuration saved: " + idUser);
} catch (Exception e) {
log.warn("Configuration not saved:" + e.getMessage());
throw new InternalError("Error saving: " + e.getMessage());
}
return new ResponseEntity<>(new String("saved"), HttpStatus.OK);
}

}
40 changes: 32 additions & 8 deletions site/src/main/java/crawlers/ProjectController.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Date;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -23,9 +24,29 @@
import dataBase.ContainerDockerDatabase;
import dataBase.ImageDockerDatabase;
import dataBase.ProjectDatabase;
import errors.BadRequestError;
import errors.InternalError;
import models.Project;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
* Controller for projects. Manage every operation which deals with the
* projects.
Expand All @@ -37,6 +58,8 @@ public class ProjectController {
private static final Logger log = LoggerFactory.getLogger(ProjectController.class);
@Autowired
JdbcTemplate jdbcTemplate;
// ops.executeCommand("java -jar ../butler.jar do config --file
// ../conf_tutorial.yml --idProject nuevo", true);

/**
* Returns the projects of a specified user
Expand All @@ -51,7 +74,7 @@ ResponseEntity<List<Project>> listProjects(@RequestParam(value = "idUser") Strin
try {
projects = projectDB.getProjects(idUser);
} catch (Exception a) {
throw new InternalError("Error listing projects: "+a.getMessage());
throw new InternalError("Error listing projects: " + a.getMessage());
}

return new ResponseEntity<>(projects, HttpStatus.OK);
Expand All @@ -76,15 +99,16 @@ ResponseEntity<Project> deleteProject(@RequestBody Project project) {
imageDB.deleteImagesOfAProject(String.valueOf(project.getId()));
ContainerDockerDatabase containerDB = new ContainerDockerDatabase(jdbcTemplate);
containerDB.deleteContainersOfAProject(String.valueOf(project.getId()));

log.info("deleted project " + project.getId());

// FALTA ESTO
// Deletes also the dockerImages, Containers.. (also in Docker stop+delete)
// Deletes also the dockerImages, Containers.. (also in Docker
// stop+delete)
// Delete the project files (dsl,plugins..) (Not implemented)

} catch (Exception a) {
throw new InternalError("Error deleting: "+a.getMessage());
throw new InternalError("Error deleting: " + a.getMessage());
}

return new ResponseEntity<>(project, HttpStatus.OK);
Expand All @@ -106,7 +130,7 @@ ResponseEntity<Project> editProject(@RequestBody Project project) {
// Change the project files (Not implemented)

} catch (Exception a) {
throw new InternalError("Error updating: "+a.getMessage());
throw new InternalError("Error updating: " + a.getMessage());
}

return new ResponseEntity<>(project, HttpStatus.OK);
Expand All @@ -125,11 +149,11 @@ ResponseEntity<Project> createProject(@RequestParam(value = "idUser") String idU
try {
projectDB.createProject(project);
log.info("created project " + project.getId());
project=projectDB.getImageJustCreated(idUser);
project = projectDB.getImageJustCreated(idUser);
// Creates the project files (Not implemented)
// CCreate docker image?
} catch (Exception a) {
throw new InternalError("Error creating: "+a.getMessage());
throw new InternalError("Error creating: " + a.getMessage());
}

return new ResponseEntity<>(project, HttpStatus.OK);
Expand Down
Loading

0 comments on commit ff7097e

Please sign in to comment.