diff --git a/client/projects.controller.js b/client/projects.controller.js index ab7cbcf..86c6b7c 100644 --- a/client/projects.controller.js +++ b/client/projects.controller.js @@ -6,30 +6,54 @@ Template.projects.helpers({ Template.projectForm.onRendered(function() { new Clipboard('.btn.clipboard'); + Session.set('vars', []); + $('.variables input').val(''); }); Template.projectForm.events({ 'submit .new-project': function (event) { event.preventDefault(); - var form = event.target; + var form = event.target, + variables = Session.get('vars'), + callback = function(errors, result) { + + if( errors ) { + + } else { + Session.set('projectToEdit', undefined); + Session.set('vars', []); + $('.new-project input').val(''); + $('.new-project textarea').val(''); + } + }; if( form.id.value ) { - Meteor.call('editProject',form.id.value, form.label.value, form.git_url.value, form.public_url.value, form.commands.value); - form.id.value = ''; + Meteor.call('editProject', + form.id.value, + form.label.value, + form.git_url.value, + form.public_url.value, + form.commands.value, + form.run.value, + variables, + callback); } else { - Meteor.call('addProject', form.label.value, form.git_url.value, form.public_url.value, form.commands.value); + Meteor.call('addProject', + form.label.value, + form.git_url.value, + form.public_url.value, + form.commands.value, + form.run.value, + variables, + callback); } - - Session.set('projectToEdit', undefined); - form.label.value = ''; - form.git_url.value = ''; - form.public_url.value = ''; - form.commands.value = ''; }, 'click .cancel': function(event) { event.preventDefault(); Session.set('projectToEdit', undefined); + Session.set('vars', []); + $('.variables input').val(''); }, 'click .trash': function(event) { @@ -37,7 +61,9 @@ Template.projectForm.events({ Meteor.call('deleteProject', Session.get('projectToEdit')._id); Session.set('projectToEdit', undefined); + Session.set('vars', []); } + }); Template.projectForm.helpers({ @@ -51,6 +77,69 @@ Template.projectForm.helpers({ deployLink: function() { return Meteor.absoluteUrl('deploy?token=XXXX&project_id=' + Session.get('projectToEdit')._id); + }, + + vars: function() { + return Session.get('vars'); + } +}); + +Template.variables.events({ + 'click .remove': function(event) { + event.preventDefault(); + var variable = Template.currentData().name + vars = Session.get('vars'); + + vars = _.filter(vars, function(object) { + return object.name !== variable; + }); + + Session.set('vars', vars); + }, + + 'click .add': function(event) { + event.preventDefault(); + var instance = Template.instance(), + name = instance.$('.name').val(), + value = instance.$('.value').val(); + + var variables = Session.get('vars'); + variables.push({name: name, value: value}); + Session.set('vars', variables); + Session.set('variables.active', undefined); + instance.$('.name').val(undefined); + instance.$('.value').val(undefined); + } +}); + +Template.variables.helpers({ + action: function() { + return Template.currentData() ? 'remove' : 'add'; + }, + + logo: function() { + return Template.currentData() ? 'minus' : 'plus'; + }, + + active: function() { + var data = Template.currentData(), + active = data && data.name && data.value; + + return active || Session.get('variables.active') ? '' : 'disabled'; + }, + + readonly: function() { + return Template.currentData() ? 'readonly' : ''; + } +}); + +Template.variables.events({ + 'keyup .name, keyup .value': function(event) { + var instance = Template.instance(), + name = instance.$('.name').val(), + value = instance.$('.value').val(); + + Session.set('variables.active', name && value); } }); @@ -59,6 +148,7 @@ Template.project.events({ event.preventDefault(); return Meteor.call('getProject', this._id, function(error, result) { Session.set('projectToEdit', result); + Session.set('vars', result.variables || []); }); }, }); \ No newline at end of file diff --git a/client/projects.view.html b/client/projects.view.html index d0d667f..239a662 100644 --- a/client/projects.view.html +++ b/client/projects.view.html @@ -47,17 +47,36 @@ +
+ +
+ {{#each vars}} + {{> variables}} + {{/each}} + {{> variables }} +
+
- + +
+
+
+ +
+
- @@ -107,3 +126,18 @@ + \ No newline at end of file diff --git a/server/constants.js b/server/constants.js index 8c0a7de..b55c227 100644 --- a/server/constants.js +++ b/server/constants.js @@ -1,5 +1,11 @@ DEPLOYMENT_FOLDER = '/home/ubuntu/deployment'; +STEPS = [ + 'CREATION', + 'COMMANDS', + 'RUN' +]; + SCRIPTS = { CREATE : [ { diff --git a/server/lib/command_runner.js b/server/lib/command_runner.js index 903507d..4ba4bb4 100644 --- a/server/lib/command_runner.js +++ b/server/lib/command_runner.js @@ -33,7 +33,7 @@ var exec = Npm.require('child_process').exec, CommandRunner = { run: function( data, callback = undefined) { - var bundle = _.extend({deployment: {}, project:{}, stdout: console.log, stderr: console.error, counter: 0, deploy_script: true}, data), + var bundle = _.extend({deployment: {}, project:{}, stdout: console.log, stderr: console.error, counter: 0, step: 0}, data), customs = {'%CWD%': bundle.project._id, '%GIT%': bundle.project.git_url}; var line = bundle.script[bundle.counter], @@ -50,14 +50,18 @@ CommandRunner = { return; } } - + bundle.counter++; if( bundle.counter >= bundle.script.length ) { - if( bundle.deploy_script && bundle.project.commands ) { - bundle.deploy_script = false; - bundle.script = bundle.project.commands.split('\n'); + if( bundle.step < 1 ) { + bundle.step++; bundle.counter = 0; - CommandRunner.commands(bundle, callback); + if( bundle.project.commands ) { + bundle.script = bundle.project.commands.split('\n'); + CommandRunner.commands(bundle, callback); + } else if( callback ) { + callback(); + } } else if( callback ) { callback(); } @@ -85,10 +89,38 @@ CommandRunner = { bundle.counter++; if( bundle.counter >= bundle.script.length ) { - callback(); + if( bundle.project.run ) { + bundle.script = [bundle.project.run]; + bundle.counter = 0; + CommandRunner.launch(bundle, callback); + } else { + callback(); + } } else { CommandRunner.commands(bundle, callback); } }); + }, + + launch: function(bundle, callback = undefined) { + var command = bundle.script[bundle.counter], + customs = {'%CWD%': bundle.project._id}, + options = { + cwd: replace('%ROOT_CWD%/%CWD%', customs), + env: {} + }, + variables = bundle.project.variables; + + for( var index in variables ) { + options.env[variables[index].name] = variables[index].value; + } + + execSync(command, options, bundle.stdout, bundle.stderr, function(errors) { + if( callback ) { + return callback(); + } else { + return; + } + }); } } \ No newline at end of file diff --git a/server/lib/projects.methods.js b/server/lib/projects.methods.js index 036eab5..ed69cff 100644 --- a/server/lib/projects.methods.js +++ b/server/lib/projects.methods.js @@ -7,16 +7,16 @@ Meteor.methods({ return ProjectService.get(id); }, - addProject: function(label, git_url, public_url ,commands) { - return ProjectService.insert(label, git_url, public_url ,commands, function(errors, id) { + addProject: function(label, git_url, public_url ,commands, run, variables) { + return ProjectService.insert(label, git_url, public_url ,commands, run, variables, function(errors, id) { if( id ) { DeploymentService.create(ProjectService.get(id)); } }); }, - editProject: function(id, label, git_url, public_url ,commands) { - ProjectService.update(id, label, git_url, public_url ,commands, function(errors, updated_count) { + editProject: function(id, label, git_url, public_url ,commands, run, variables) { + ProjectService.update(id, label, git_url, public_url ,commands, run, variables, function(errors, updated_count) { if( updated_count ) { DeploymentService.update(id); } diff --git a/server/lib/projects.service.js b/server/lib/projects.service.js index 1ba47a5..b217db7 100644 --- a/server/lib/projects.service.js +++ b/server/lib/projects.service.js @@ -1,23 +1,27 @@ Projects = new Mongo.Collection('projects'); ProjectService = { - insert: function(label, git_url, public_url, commands, callback) { + insert: function(label, git_url, public_url, commands, run, variables, callback) { Projects.insert({ label: label, git_url: git_url, public_url: public_url, - commands: commands + run: run, + commands: commands, + variables: variables }, callback); }, - update: function(id, label, git_url, public_url ,commands, callback) { + update: function(id, label, git_url, public_url ,commands, run, variables, callback) { Projects.update( id, { $set: { label: label, git_url: git_url, public_url: public_url, - commands: commands + commands: commands, + run: run, + variables: variables } }, callback