1 |
|
2 | path = require "path"
|
3 | {EventEmitter} = require "events"
|
4 | Service = require "./Service"
|
5 | Server = require "./Server"
|
6 | async = require "async"
|
7 | {curry} = require "fjs"
|
8 |
|
9 |
|
10 |
|
11 | class Layer extends EventEmitter
|
12 |
|
13 | constructor: (@name, layer, @repoName, mainConfig, isLocal) ->
|
14 | @runPlugins layer, mainConfig, (err) =>
|
15 | return @emit "error", err if err?
|
16 |
|
17 | @services = []
|
18 |
|
19 |
|
20 |
|
21 | if isLocal
|
22 | console.log "DEPLOYING LOCALLY"
|
23 | layer.hosts = [layer.hosts[0]]
|
24 | else
|
25 | console.log "WORKING WITH #{layer.hosts.length} SERVERS: #{layer.hosts.join(',')}"
|
26 |
|
27 | if layer.hosts.length > 1
|
28 | @groupDeploy = true
|
29 |
|
30 | for server in layer.hosts
|
31 | serverConfig = new Server @name, server, layer, mainConfig
|
32 | @services.push new Service(@name, @repoName, serverConfig, this, isLocal)
|
33 |
|
34 | @emit "ready"
|
35 |
|
36 |
|
37 | runPlugins: (layer, mainConfig, cb) ->
|
38 | plugins = layer.plugins || mainConfig.getPlugins()
|
39 |
|
40 | return process.nextTick cb if not plugins
|
41 | toRun = []
|
42 | for name, plugin of plugins
|
43 | plugin.name = name
|
44 | toRun.push plugin
|
45 |
|
46 | withLayer = @runPlugin layer
|
47 | async.forEach toRun, withLayer, cb
|
48 |
|
49 | runPlugin: curry (layer, plugin, cb) ->
|
50 | if not plugin.overrides
|
51 | return cb new Error("invalid plugin definition, you need an overrides directive")
|
52 |
|
53 | pluginPath = ''
|
54 | if plugin.name.match /^\.\//
|
55 | pluginPath = path.join process.cwd(), plugin.name
|
56 | else
|
57 | pluginPath = path.join __dirname, '../plugins/', plugin.name
|
58 |
|
59 | pluginModule = require pluginPath
|
60 | console.log "running plugin at #{pluginPath}"
|
61 | pluginModule plugin.opts, (err, res) ->
|
62 | return cb err if err?
|
63 | layer[plugin.overrides] = res
|
64 | cb()
|
65 |
|
66 | deployOne: curry (branch, service, cb) ->
|
67 | service.deploy branch, cb
|
68 |
|
69 | deploy: (branch, cb) ->
|
70 | withBranch = @deployOne branch
|
71 | async.forEach @services, withBranch, cb
|
72 |
|
73 | logOne: curry (lines, service, cb) ->
|
74 | service.serverLogs lines, cb
|
75 |
|
76 | historyOne: curry (revisions, service, cb) ->
|
77 | service.getHistory revisions, cb
|
78 |
|
79 | serverLogs: (lines, cb) ->
|
80 | withLines = @logOne lines
|
81 | async.forEach @services, withLines, cb
|
82 |
|
83 | commitHistory: (revisions, cb) ->
|
84 | withRevisions = @historyOne revisions
|
85 | async.forEach @services, withRevisions, cb
|
86 |
|
87 |
|
88 | restart: (cb) ->
|
89 | @serviceAction "restart", cb
|
90 |
|
91 | start: (cb) ->
|
92 | @serviceAction "start", cb
|
93 |
|
94 | stop: (cb) ->
|
95 | @serviceAction "stop", cb
|
96 |
|
97 | serviceAction: (action, cb) =>
|
98 | actionCurry = @actionOne action
|
99 | async.forEach @services, actionCurry, cb
|
100 |
|
101 | actionOne: curry (action, service, cb) ->
|
102 | service[action] cb
|
103 |
|
104 | log: (parentAddress, msg) ->
|
105 | if @groupDeploy
|
106 | console.log "#{parentAddress}: #{msg}"
|
107 | else
|
108 | console.log msg
|
109 |
|
110 | module.exports = Layer
|