This repository has been archived by the owner on Jul 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
app.js
executable file
·174 lines (148 loc) · 4.02 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
"use strict";
var DockerWatcher = require('./lib/docker-watcher')
, DockerContainer = require('./lib/docker-container')
, ContainersHelper = require('./lib/containers-helper')
, url = require('url')
, Network = require('./lib/network')
, HTTPProxy = require('./lib/http-proxy')
, Logger = require('./lib/logger')
, DNSServer = require('./lib/dns-server').DNSServer
, exec = require('child_process').exec
require('colors')
/**
* App
*
* ProxyDriver, domain, dns_ip, dns_port
*
* @constructor
*/
var App = function (ProxyDriver, HTTPDriver, DNSDriver, DockerDriver, domain, api_port, proxy_ip, dns_ip, dns_port, dns_ttl) {
this.dockerInfos = url.parse(process.env.DOCKER_HOST)
this.proxyDriver = ProxyDriver
this.httpDriver = HTTPDriver
this.dnsDriver = DNSDriver
this.dockerDriver = DockerDriver
this.domain = domain
this.apiPort = api_port
this.proxyIp = proxy_ip
this.dnsIp = dns_ip
this.dnsPort = dns_port
this.dnsTtl = dns_ttl
this.version = require('./package.json').version
// DNS stuff
this.dnsServer = new DNSServer(this, this.dnsDriver, domain);
// proxy
this.proxy = new HTTPProxy(this, this.proxyDriver, this.httpDriver, this.dnsServer)
}
/**
* Return docker infos
* @returns {Object}
*/
App.prototype.getDockerInfos = function () {
return this.dockerInfos
}
/**
* Run the app
*/
App.prototype.run = function () {
Logger.info("Starting Muguet App".green)
Network.setupResolver(this.domain, this.dnsPort, this.dnsIp)
this.dnsServer.listen(this.dnsPort)
var watcher = new DockerWatcher(this).run()
watcher
.on('setup', this._onContainerSetup.bind(this))
.on('change', this._onContainerChange.bind(this))
.on('error', this._onError.bind(this))
}
/**
* Get proxy IP address
*
* @returns {String}
*/
App.prototype.getProxyIp = function () {
return this.proxyIp
}
/**
* Get domain
*
* @returns {String}
*/
App.prototype.getDomain = function () {
return this.domain
}
/**
* Callback on the first containers list received
*
* @param {Array} containers
* @private
*/
App.prototype._onContainerSetup = function (containers) {
Logger.info('Containers list received from Docker-watcher: %s containers found', String(containers.length).yellow)
this.containers = containers
this.dnsServer.setEntries(this.getDnsEntries())
this.proxy.setRoutes(this.getProxyRoutes())
this.proxy.listen(this.apiPort)
}
/**
* Callback on containers list changes
*
* @param {Array} containers
* @private
*/
App.prototype._onContainerChange = function (containers) {
Logger.info("Containers have changed: %s running", String(containers.length).yellow)
this.containers = containers
this.dnsServer.setEntries(this.getDnsEntries())
this.proxy.updateRoutes(this.getProxyRoutes())
}
/**
* Error callback
*
* @param {String} err
* @private
*/
App.prototype._onError = function (err) {
exec('boot2docker status', function (error, stdout) {
if (stdout.trim() === 'poweroff') {
return Logger.error("Your boot2docker VM does not seem to be started. Please run `boot2docker up`.");
}
Logger.error("Error while watching containers. (" + err + ")")
})
}
/**
* Build proxy routes for all container
*
* @private
*/
App.prototype.getProxyRoutes = function () {
var flags = DockerContainer.IS_RUNNING | DockerContainer.HAS_PROXY_ENABLED | DockerContainer.HAS_EXPOSED_PORTS;
var routes = ContainersHelper.filterContainers(this.containers, flags).map(function (cnt) {
return cnt.getProxyRoutes()
})
if (routes.length) {
routes = routes.reduce(function (prev, next) {
return prev.concat(next)
})
}
return routes
}
/**
* Build dns entries
*
* @private
*/
App.prototype.getDnsEntries = function () {
var finalEntries = {}
, entries = this.containers.map(function (cnt) {
return cnt.getDnsEntries()
})
entries.forEach(function (entry) {
for (var n in entry) {
if (entry.hasOwnProperty(n)) {
finalEntries[n] = (finalEntries[n] || []).concat(entry[n])
}
}
})
return finalEntries
}
module.exports = App