Skip to content

Commit

Permalink
Merge pull request #53 from ErnestOrt/28-ms-from-git
Browse files Browse the repository at this point in the history
28 ms from git
  • Loading branch information
ErnestOrt authored Mar 12, 2018
2 parents 6ace568 + 6cca5b4 commit 97d7eb4
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.ernest.applications.trampoline.exceptions.ReadingEcosystemException;
import org.ernest.applications.trampoline.exceptions.SavingEcosystemException;
import org.ernest.applications.trampoline.services.EcosystemManager;
import org.ernest.applications.trampoline.services.FileManager;
import org.ernest.applications.trampoline.services.GitManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
Expand All @@ -29,6 +30,9 @@ public class SettingsController {
@Autowired
EcosystemManager ecosystemManager;

@Autowired
FileManager fileManager;

@Autowired
GitManager gitManager;

Expand All @@ -42,6 +46,7 @@ public String getSettingsView(Model model) {
model.addAttribute("mavenBinaryLocationMessage", ecosystem.getMavenBinaryLocation() == null ? "Set Maven Binary Location if necessary. Otherwise it will automatically be searched in a bin folder inside your Maven Home Location" : ecosystem.getMavenBinaryLocation());
model.addAttribute("mavenHomeLocationMessage", ecosystem.getMavenHomeLocation() == null ? "Please set maven Home Location. Ex: /Users/ernest/Documents/workspace/tools/apache-maven-3.2.1" : ecosystem.getMavenHomeLocation());
model.addAttribute("gitUsername", gitManager.getRegisteredUsername(ecosystem));
model.addAttribute("settingsFolder", fileManager.getSettingsFolder());

return SETTINGS_VIEW;
}
Expand All @@ -61,6 +66,16 @@ public void setNewMicroservice(@RequestParam(value="name") String name, @Request
ecosystemManager.setNewMicroservice(name, pomLocation, defaultPort, actuatorPrefix, vmArguments, buildTool, gitLocation);
}

@RequestMapping(value= "/setnewmicroservice/git", method = RequestMethod.POST)
@ResponseBody
public void setNewMicroserviceFromGit(@RequestParam(value="gitRepo") String gitRepo, @RequestParam(value="destinationFolder") String destinationFolder,
@RequestParam(value="name") String name, @RequestParam(value="pomLocation") String pomLocation,
@RequestParam(value="defaultPort") String defaultPort, @RequestParam(value="actuatorPrefix") String actuatorPrefix,
@RequestParam(value="vmArguments") String vmArguments, @RequestParam(value="buildTool") String buildTool, @RequestParam(value="gitLocation") String gitLocation) throws CreatingSettingsFolderException, ReadingEcosystemException, CreatingMicroserviceScriptException, SavingEcosystemException, GitAPIException {
gitManager.cloneRepository(gitRepo, destinationFolder);
ecosystemManager.setNewMicroservice(name, pomLocation, defaultPort, actuatorPrefix, vmArguments, buildTool, gitLocation);
}

@RequestMapping(value= "/updatemicroservice", method = RequestMethod.POST)
@ResponseBody
public void updateMicroservice(@RequestParam(value="id") String id, @RequestParam(value="pomLocation") String pomLocation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ public void updateMicroservice(String id, String pomLocation, String defaultPort
microservice.setVmArguments(vmArguments);
microservice.setGitLocation(gitLocation);

fileManager.createScript(microservice);

fileManager.saveEcosystem(ecosystem);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,16 @@ public void runScript(Microservice microservice, String mavenBinaryLocation, Str
public void createScript(Microservice microservice) throws CreatingMicroserviceScriptException {
try {
if(System.getProperties().getProperty("os.name").contains("Windows")){
try{FileUtils.forceDelete(new File(getSettingsFolder() + "/" + microservice.getId() + ".txt"));}catch (Exception e){}

if(microservice.getBuildTool().equals(BuildTools.MAVEN)) {
FileUtils.writeStringToFile(new File(getSettingsFolder() + "/" + microservice.getId() + ".txt"), ScriptContentsProvider.getMavenWindows(microservice.getPomLocation()));
}else{
FileUtils.writeStringToFile(new File(getSettingsFolder() + "/" + microservice.getId() + ".txt"), ScriptContentsProvider.getGradleWindows(microservice.getPomLocation()));
}
}else{
try{FileUtils.forceDelete(new File(getSettingsFolder() + "/" + microservice.getId() + ".sh"));}catch (Exception e){}

if(microservice.getBuildTool().equals(BuildTools.MAVEN)) {
FileUtils.writeStringToFile(new File(getSettingsFolder() + "/" + microservice.getId() + ".sh"),ScriptContentsProvider.getMavenUnix(microservice.getPomLocation()));
}else{
Expand All @@ -145,7 +149,7 @@ public void createScript(Microservice microservice) throws CreatingMicroserviceS
}
}

private String getSettingsFolder() {
public String getSettingsFolder() {

if(System.getProperties().getProperty("os.name").contains("Mac")){
return settingsFolderPathMac.replaceAll("#userName", System.getProperties().getProperty("user.name"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.ernest.applications.trampoline.services;

import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.CreateBranchCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.ListBranchCommand;
Expand All @@ -14,6 +15,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

Expand Down Expand Up @@ -78,6 +80,16 @@ public void cleanCred() {
ecosystemManager.cleanGitCred();
}

public void cloneRepository(String gitUrl, String destinationFolder) throws GitAPIException {
Ecosystem ecosystem = ecosystemManager.getEcosystem();
CloneCommand cloneCommand = Git.cloneRepository();

if(ecosystem.getGitCredentials()!=null){
cloneCommand.setCredentialsProvider(buildCredentialsProvider(ecosystem.getGitCredentials())).call();
}
cloneCommand.setURI(gitUrl).setDirectory(new File(destinationFolder)).call();
}

private UsernamePasswordCredentialsProvider buildCredentialsProvider(GitCredentials gitCredentials) {
return new UsernamePasswordCredentialsProvider(encryptService.decrypt(gitCredentials.getUsername()), encryptService.decrypt(gitCredentials.getPass()));
}
Expand Down
127 changes: 87 additions & 40 deletions trampoline/src/main/resources/static/v2/js/app/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,13 @@ function setNewMicroservice(){
if($("#input-hidden-mavenhomelocation").val() == '' && $("#input-newmicroservice-build-tool").val() == 'maven'){
$("#form-mavenhomelocation").addClass("has-error");
}else{
cleaningNewMicroserviceFrom();
if($("#input-newmicroservice-name").val() == '' || $("#input-newmicroservice-pomlocation").val() == '' || $("#input-newmicroservice-defaultport").val() == '' || $("#input-newmicroservice-build-tool").val() == '-1'){
checkEachNewMicroserviceFromField();
}else{
var fieldsToCheck = [];
fieldsToCheck.push("newmicroservice-name");
fieldsToCheck.push("newmicroservice-pomlocation");
fieldsToCheck.push("newmicroservice-defaultport");
fieldsToCheck.push("newmicroservice-build-tool");

if(!cheackEmptyValuesForm(fieldsToCheck)){
$('.front-loading').show();
$.ajax({
url : "/settings/setnewmicroservice",
Expand All @@ -81,41 +84,6 @@ function setNewMicroservice(){
}
}

function cleaningNewMicroserviceFrom(){
$("#form-newmicroservice-name").removeClass("has-error");
$("#form-newmicroservice-pomlocation").removeClass("has-error");
$("#form-newmicroservice-defaultport").removeClass("has-error");
$("#form-newmicroservice-build-tool").removeClass("has-error");

$("#form-newmicroservice-name").removeClass("has-success");
$("#form-newmicroservice-pomlocation").removeClass("has-success");
$("#form-newmicroservice-defaultport").removeClass("has-success");
$("#form-newmicroservice-build-tool").removeClass("has-success");
}

function checkEachNewMicroserviceFromField(){
if($("#input-newmicroservice-name").val() == ''){
$("#form-newmicroservice-name").addClass("has-error");
}else{
$("#form-newmicroservice-name").addClass("has-success");
}
if($("#input-newmicroservice-pomlocation").val() == ''){
$("#form-newmicroservice-pomlocation").addClass("has-error");
}else{
$("#form-newmicroservice-pomlocation").addClass("has-success");
}
if($("#input-newmicroservice-defaultport").val() == ''){
$("#form-newmicroservice-defaultport").addClass("has-error");
}else{
$("#form-newmicroservice-defaultport").addClass("has-success");
}
if($("#input-newmicroservice-build-tool").val() == '-1'){
$("#form-newmicroservice-build-tool").addClass("has-error");
}else{
$("#form-newmicroservice-build-tool").addClass("has-success");
}
}

function removeMicroservice(microserviceId){
$('.front-loading').show();
$.ajax({
Expand Down Expand Up @@ -350,4 +318,83 @@ function showNotification(notificationType, notificationMessage){

$(document).ready(function(){
$('[data-toggle="git-popover"]').popover();
});
$("#tab-newmicroservice-git-repo").addClass("active");
$('#content-file-system').hide();
});

function showNewMsForm(component){
$('#content-file-system').hide();
$('#content-newmicroservice-git-repo').hide();
$('#content-'+component).show();

$("#tab-newmicroservice-git-repo").removeClass("active");
$("#tab-file-system").removeClass("active");
$("#tab-"+component).addClass("active");
}

function fillFormGitNewMs(){
var girUrl =$('#input-git-newmicroservice-repo').val();
var repoName = girUrl.split("/")[girUrl.split("/").length-1].replace('.git','');

$('#input-git-newmicroservice-name').val(repoName);
$('#input-git-newmicroservice-destination').val(settingsFolder+"/"+repoName);
$('#input-git-newmicroservice-pomlocation').val(settingsFolder+"/"+repoName);
$('#input-git-newmicroservice-gitLocation').val(settingsFolder+"/"+repoName);
}

function setNewMicroserviceFromGit(){
var fieldsToCheck = [];
fieldsToCheck.push("git-newmicroservice-repo");
fieldsToCheck.push("git-newmicroservice-destination");
fieldsToCheck.push("git-newmicroservice-name");
fieldsToCheck.push("git-newmicroservice-pomlocation");
fieldsToCheck.push("git-newmicroservice-defaultport");
fieldsToCheck.push("git-newmicroservice-build-tool");
fieldsToCheck.push("git-newmicroservice-gitLocation");

if($("#input-hidden-mavenhomelocation").val() == '' && $("#input-newmicroservice-build-tool").val() == 'maven'){
$("#form-mavenhomelocation").addClass("has-error");
}else{
if(!cheackEmptyValuesForm(fieldsToCheck)){
$('.front-loading').show();
$.ajax({
url : "/settings/setnewmicroservice/git",
type: "POST",
data : {gitRepo: $('#input-git-newmicroservice-repo').val(),
destinationFolder: $('#input-git-newmicroservice-destination').val(),
name: $("#input-git-newmicroservice-name").val(),
pomLocation: $("#input-git-newmicroservice-pomlocation").val(),
defaultPort: $("#input-git-newmicroservice-defaultport").val(),
actuatorPrefix: $("#input-git-newmicroservice-actuatorprefix").val(),
vmArguments: $("#input-git-newmicroservice-vmarguments").val(),
buildTool: $("#input-git-newmicroservice-build-tool").val(),
gitLocation: $("#input-git-newmicroservice-gitLocation").val()},
success: function(data, textStatus, jqXHR) { location.reload(); },
error: function (request, status, error) {
$('.front-loading').hide();
showNotification('danger', "Error occurred when trying to register a microservice. Check Logs for more info");

}
});
}
}
}

function cheackEmptyValuesForm(fieldsToCheck){
var errors = false;
fieldsToCheck.forEach(function(field) {
$("#form-" + field).removeClass("has-error");
$("#form-" + field).removeClass("has-success");

if($("#input-" + field).val() == '' || $("#input-" + field).val() == '-1'){
errors = true
$("#form-" + field).addClass("has-error");
}else{
$("#form-" + field).addClass("has-success");
}
});

return errors;
}


Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<li>
<a href="https://github.com/ErnestOrt/Trampoline">
<i class="ti-github"></i>
<p>Project Repo</p>
<p>Trampoline Repo</p>
</a>
</li>
<li>
Expand Down
85 changes: 83 additions & 2 deletions trampoline/src/main/resources/templates/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,84 @@ <h4 class="title">GIT Settings <i class="fa fa-info-circle" data-toggle="git-pop
<div class="card">
<div class="header">
<h4 class="title">Register Microservice</h4>
<br/>
</div>
<div class="content">
<ul class="nav nav-tabs">
<li role="presentation" id="tab-newmicroservice-git-repo" onclick="showNewMsForm('newmicroservice-git-repo')"><a>GIT Repo</a></li>
<li role="presentation" id="tab-file-system" onclick="showNewMsForm('file-system')"><a>File System</a></li>
</ul>
<div id="content-newmicroservice-git-repo" class="content">
<div class="row">
<div class="col-md-6">
<div class="form-group" id="form-git-newmicroservice-repo">
<label>GIT Repo</label>
<input id="input-git-newmicroservice-repo" onchange="fillFormGitNewMs()" type="text" class="form-control border-input" placeholder="Ex: https://github.com/ErnestOrt/Trampoline.git"/>
</div>
</div>
<div class="col-md-6">
<div class="form-group" id="form-git-newmicroservice-destination">
<label>Destination Folder</label>
<input id="input-git-newmicroservice-destination" type="text" class="form-control border-input" placeholder="Ex: C:\Users\Trampoline\Documents\workarea"/>
</div>
</div>
<div class="col-md-5">
<div class="form-group" id="form-git-newmicroservice-name">
<label>Name *</label>
<input id="input-git-newmicroservice-name" type="text" class="form-control border-input" placeholder="Ex: microservice-1"/>
</div>
</div>
<div class="col-md-2">
<div class="form-group" id="form-git-newmicroservice-build-tool">
<label>Build Tool *</label>
<select id="input-git-newmicroservice-build-tool" class="form-control border-input">
<option value="-1">Select...</option>
<option value="maven">Maven</option>
<option value="gradle">Gradle</option>
</select>
</div>
</div>
<div class="col-md-2">
<div class="form-group" id="form-git-newmicroservice-defaultport">
<label>Default Port *</label>
<input id="input-git-newmicroservice-defaultport" type="text" class="form-control border-input" placeholder="Ex: 9988"/>
</div>
</div>
<div class="col-md-3">
<div class="form-group" id="form-git-newmicroservice-actuatorprefix">
<label>Actuator Prefix</label>
<input id="input-git-newmicroservice-actuatorprefix" type="email" class="form-control border-input" placeholder="Ex: /service-one"/>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group" id="form-git-newmicroservice-pomlocation">
<label>Pom or Build File Location *</label>
<input id="input-git-newmicroservice-pomlocation" type="text" class="form-control border-input" placeholder="Ex: /Users/ernest/Documents/workarea/microservice-1" />
</div>
</div>
<div class="col-md-6">
<div class="form-group" id="form-git-newmicroservice-gitLocation">
<label>Git Repo Root Location</label>
<input id="input-git-newmicroservice-gitLocation" type="text" class="form-control border-input" placeholder="Ex: /Users/ernest/Documents/workarea/microservice-1"/>
</div>
</div>
</div>
<div class="row">

<div class="col-md-12">
<div class="form-group" id="form-git-newmicroservice-vmarguments">
<label>VM Arguments</label>
<input id="input-git-newmicroservice-vmarguments" type="text" class="form-control border-input" placeholder="Ex: -Dmy.arg.one=false -Dmy.arg.two=true"/>
</div>
</div>
</div>
<div class="text-center">
<button onclick="setNewMicroserviceFromGit()" class="btn btn-success btn-fill btn-wd">Clone and Register Microservice</button>
</div>
<div class="clearfix"></div>
</div>
<div id="content-file-system" class="content">
<div class="row">
<div class="col-md-5">
<div class="form-group" id="form-newmicroservice-name">
Expand Down Expand Up @@ -343,9 +418,15 @@ <h4 class="modal-title">Microservices Group Information</h4>
<script type="text/javascript" th:src="@{/v2/js/bootstrap-notify.js}"></script>
<script type="text/javascript" th:src="@{/v2/js/paper-dashboard.js}"></script>
<script type="text/javascript" th:src="@{/v2/js/demo.js}"></script>
<script type="text/javascript" th:src="@{/v2/js/app/settings.js?v3.10}"></script>
<script type="text/javascript" th:src="@{/v2/js/app/settings.js?v3.11}"></script>
<script type="text/javascript" th:src="@{/v2/js/app/loading.js?v3.7}"></script>

<script th:inline="javascript">
/*<![CDATA[*/
var settingsFolder = [[${settingsFolder}]]
/*]]>*/
</script>



</html>

0 comments on commit 97d7eb4

Please sign in to comment.