hubot-deployment/deployment.coffee

194 lines
6.1 KiB
CoffeeScript
Raw Permalink Normal View History

2016-01-21 13:52:05 +01:00
# Description:
# Deployment script
#
# Commands:
2016-01-21 16:07:27 +01:00
# hubot deploy <project> [with <program>] [from branch <branch>] to <target> [after <command>[, <command>, ...]]
2016-01-21 13:52:05 +01:00
#
# URLS:
#
# Notes:
#
2016-01-21 17:00:53 +01:00
fs = require 'fs'
2016-01-21 13:52:05 +01:00
2016-01-23 10:49:44 +01:00
deploymentDirectory = null
2016-01-21 13:52:05 +01:00
module.exports = (robot) ->
2016-01-23 10:49:44 +01:00
robot.hear /deploy set ([^\s]+) +(.+)$/i, (res) ->
if res.match[1] == "directory"
deploymentDirectory = res.match[2]
2016-01-21 16:07:27 +01:00
robot.hear /deploy +([^\s]+)( +with +([^\s]+))?( +from +branch +([^\s]+))? +to +([^\s]+)( +after (.*))?$/i, (res) ->
2016-01-23 10:49:44 +01:00
if ! process.env.hasOwnProperty('HUBO_DEPLOYMENT_PROJECTS_DIRECTORY')
if deploymentDirectory == null
res.reply "You must set the env var HUBO_DEPLOYMENT_PROJECTS_DIRECTORY with the directory of the projects."
res.reply "Temporaly, you can use the command: deploy set directory <directory>"
return
2016-01-21 16:07:27 +01:00
project = res.match[1]
target = res.match[6]
program = if res.match[3] then res.match[3] else null
branch = if res.match[5] then res.match[5] else null
commands = if res.match[8] then cleanCommands res.match[8].split "," else []
2016-01-21 15:15:45 +01:00
2016-01-23 10:49:44 +01:00
if deploymentDirectory != null
directory = deploymentDirectory
else
directory = process.env.HUBO_DEPLOYMENT_PROJECTS_DIRECTORY
directory += "/" + project
2016-01-21 15:15:45 +01:00
try
fs.stat directory, (err, stats) ->
if err != null or !stats.isDirectory
return res.reply "Project not found [" + directory + "]"
2016-01-21 16:38:31 +01:00
try
fs.statSync directory + '/.svn'
pv = new SvnProjectVersionning branch
catch e
2016-01-21 16:07:27 +01:00
2016-01-21 16:38:31 +01:00
try
fs.statSync directory + '/.git'
pv = new GitProjectVersionning branch
catch e
if pv
2016-01-23 11:24:19 +01:00
d = deploy directory, pv, commands, target, program
2016-01-23 11:54:59 +01:00
res.send d
2016-01-21 16:38:31 +01:00
else
res.reply "Not a SVN or GIT repository"
2016-01-21 15:15:45 +01:00
catch e
res.reply "Exception: " + e
2016-01-21 13:52:05 +01:00
2016-01-21 16:07:27 +01:00
class ProjectVersionning
constructor: (@branch) ->
getUpdateCommands: () ->
class SvnProjectVersionning extends ProjectVersionning
getUpdateCommands: () ->
['svn up']
class GitProjectVersionning extends ProjectVersionning
getUpdateCommands: () ->
commands = []
if @branch == null
commands.push 'git pull origin $(git rev-parse --abbrev-ref HEAD)'
else
2016-01-22 11:08:25 +01:00
br = @branch.split "/"
2016-01-21 16:07:27 +01:00
if br.length == 1
commands.push 'git checkout ' + br[0]
commands.push 'git pull origin ' + br[0]
else if br.length == 2
commands.push 'git checkout ' + br[1]
commands.push 'git pull ' + br.join(' ')
commands
2016-01-21 17:42:54 +01:00
logExec = (command, options) ->
2016-01-22 11:08:25 +01:00
exec = require "sync-exec"
2016-01-21 17:42:54 +01:00
exec command, options
deploy = (directory, projectVersionning, commands, target, program) ->
2016-01-21 16:38:31 +01:00
commands = projectVersionning.getUpdateCommands().concat(commands)
2016-01-23 11:24:19 +01:00
canDeploy = true
results = []
for command in commands
if !canDeploy
continue
stmt = logExec command , {cwd: directory}
results.push {
command: command,
status: stmt.status,
stdout: stmt.stdout,
stderr: stmt.stderr,
}
if stmt.status != 0
canDeploy = false
if canDeploy
command = buildDeployCommand(directory, program, target)
stmt = logExec command, {cwd: directory}
results.push {
command: command,
status: stmt.status,
stdout: stmt.stdout,
stderr: stmt.stderr,
}
output = ""
2016-01-21 16:38:31 +01:00
2016-01-23 11:24:19 +01:00
for stmt in results
2016-01-23 11:54:59 +01:00
output += generateOutput stmt
2016-01-23 11:24:19 +01:00
output
2016-01-23 11:54:59 +01:00
generateOutput = (stmt) ->
prefix = "deploy-" + Date.now()
status =
if stmt.status == 0
"<span color='green'>OK</span>"
else
"<span color='red'>Failed</span>"
"""
<p><pre>#{stmt.command}</pre> #{status}</p>
<div>
Outputs: <a href="#" onclick="var e = document.getElementById('#{prefix}-stdout'); if (e.getAttribute('data-show') == 0) {e.style.display = 'block'; e.setAttribute('data-show', '1');} else {e.display = 'none'; e.setAttribute('data-show', '0');}">STDOUT</a> <a href="#" onclick="var e = document.getElementById('#{prefix}-stderr'); if (e.getAttribute('data-show') == 0) {e.style.display = 'block'; e.setAttribute('data-show', '1');} else {e.display = 'none'; e.setAttribute('data-show', '0');}">STDERR</a>
<pre id="#{prefix}-stdout" data-show="0" style="display: none">#{stmt.stdout}</pre>
<pre id="#{prefix}-stderr" data-show="0" style="display: none">#{stmt.stderr}</pre>
</div>
"""
2016-01-21 17:00:53 +01:00
buildDeployCommand = (directory, program, target) ->
if program == 'mage'
2016-01-21 17:42:54 +01:00
command = 'vendor/bin/mage deploy to:' + target
2016-01-21 17:00:53 +01:00
else
interpreter = null
try
fs.statSync directory + '/bin/console'
interpreter = 'php bin/console'
catch e
try
fs.statSync directory + '/app/console'
interpreter = 'php app/console'
catch e
if interpreter != null
command = interpreter + ' project:deploy ' + target + ' --go'
command
2016-01-21 13:52:05 +01:00
cleanCommands = (commands) ->
results = []
2016-01-21 15:15:45 +01:00
commands = commands.map Function.prototype.call, String.prototype.trim
interpreters = {
2016-01-21 16:41:30 +01:00
"sf" : "php symfony",
"sf2" : "php app/console",
"sf3" : "php bin/console",
2016-01-21 17:00:53 +01:00
"composer": "composer"
2016-01-21 15:15:45 +01:00
}
2016-01-21 13:52:05 +01:00
for command in commands
2016-01-21 15:15:45 +01:00
if command != ""
2016-01-22 11:08:25 +01:00
if command.match /(sf[2-3]?|composer):.+/
elements = command.split ":"
2016-01-21 15:15:45 +01:00
interpreter = elements[0]
elements = elements.splice 1
args = elements.join(":")
results.push interpreters[interpreter] + " " + args
2016-01-21 17:42:54 +01:00
else
results.push command
2016-01-21 13:52:05 +01:00
results