Skip to content

Commit 0fdfd7d

Browse files
committed
2 parents 6b5c495 + 50d0207 commit 0fdfd7d

3 files changed

Lines changed: 130 additions & 11 deletions

File tree

lib/networks.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@ module.exports = async function (docker, projectName, recipe, output) {
33
var networkNames = Object.keys(recipe.networks || []);
44
for (var networkName of networkNames) {
55
var network = recipe.networks[networkName];
6+
if (network === null) {
7+
try {
8+
networks.push({ "name": projectName + '_' + networkName, "network": await docker.createNetwork({ 'Name': projectName + '_' + networkName, 'CheckDuplicate': true }) });
9+
} catch (err) {
10+
if (err.statusCode == 409 && err.json.message.includes("already exists")) {
11+
let returnedNetwork = await docker.listNetworks({ "filters": { "name": [projectName + '_' + networkName] } })
12+
networks.push({ "name": projectName + '_' + networkName, "network": await docker.getNetwork(returnedNetwork[0].Id) })
13+
} else {
14+
throw err;
15+
}
16+
}
17+
continue
18+
}
619
if (network.external === true) continue;
720
var opts = {
821
'Name': projectName + '_' + networkName,
@@ -32,17 +45,23 @@ module.exports = async function (docker, projectName, recipe, output) {
3245
}
3346
}
3447
try {
35-
networks.push(await docker.createNetwork(opts));
48+
networks.push({ "name": projectName + '_' + networkName, "network": await docker.createNetwork(opts) });
3649
} catch (err) {
50+
//if exists we have to compare with the existing network
3751
throw err;
3852
}
3953
}
4054

4155
if (networks.length === 0) {
4256
try {
43-
await docker.createNetwork({ 'Name': projectName + '_default', 'CheckDuplicate': true });
57+
networks.push({ "name": projectName + '_' + networkName, "network": await docker.createNetwork({ 'Name': projectName + '_default', 'CheckDuplicate': true }) });
4458
} catch (err) {
45-
throw err;
59+
if (err.statusCode == 409 && err.json.message.includes("already exists")) {
60+
let returnedNetwork = await docker.listNetworks({ "filters": { "name": [projectName + '_default'] } })
61+
networks.push({ "name": projectName + '_' + networkName, "network": await docker.getNetwork(returnedNetwork[0].Id) })
62+
} else {
63+
throw err;
64+
}
4665
}
4766
}
4867

lib/services.js

Lines changed: 83 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module.exports = async function (docker, projectName, recipe, output) {
44
var services = [];
55
var serviceNames = tools.sortServices(recipe);
66
for (var serviceName of serviceNames) {
7+
var networksToAttach = [];
78
var service = recipe.services[serviceName];
89

910
var opts = {
@@ -17,13 +18,67 @@ module.exports = async function (docker, projectName, recipe, output) {
1718
}
1819
};
1920

20-
opts.NetworkingConfig.EndpointsConfig[projectName + '_default'] = {
21-
'IPAMConfig': {},
22-
'Links': [],
23-
'Aliases': [
24-
serviceName,
25-
]
26-
};
21+
if (service.networks !== undefined) {
22+
if (Array.isArray(service.networks)) {
23+
for (let index = 0; index < service.networks.length; index++) {
24+
let networkName = projectName + '_' + service.networks[index]
25+
let networkTemplate = {
26+
'NetworkingConfig': {
27+
'EndpointsConfig': {
28+
}
29+
}
30+
}
31+
networkTemplate.NetworkingConfig.EndpointsConfig[networkName] = {};
32+
networkTemplate.NetworkingConfig.EndpointsConfig[networkName]['Aliases'] = [serviceName]
33+
if (index === 0)
34+
opts.NetworkingConfig.EndpointsConfig = networkTemplate.NetworkingConfig.EndpointsConfig
35+
36+
networksToAttach.push(networkTemplate.NetworkingConfig.EndpointsConfig)
37+
}
38+
} else {
39+
let networkNames = Object.keys(service.networks);
40+
for (let index = 0; index < networkNames.length; index++) {
41+
let network = service.networks[networkNames[index]] || {};
42+
let networkName = projectName + '_' + networkNames[index]
43+
let networkTemplate = {
44+
'NetworkingConfig': {
45+
'EndpointsConfig': {
46+
}
47+
}
48+
}
49+
networkTemplate.NetworkingConfig.EndpointsConfig[networkName] = {}
50+
networkTemplate.NetworkingConfig.EndpointsConfig[networkName]['IPAMConfig'] = {}
51+
if (network.aliases !== undefined) {
52+
networkTemplate.NetworkingConfig.EndpointsConfig[networkName]['Aliases'] = network.aliases
53+
}
54+
if (network.ipv4_address !== undefined) {
55+
networkTemplate.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig['IPv4Address'] = network.ipv4_address
56+
}
57+
if (network.ipv6_address !== undefined) {
58+
networkTemplate.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig['IPv6Address'] = network.ipv6_address
59+
}
60+
if (network.link_local_ips !== undefined) {
61+
networkTemplate.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig['LinkLocalIPs'] = network.link_local_ips
62+
}
63+
if (network.priority !== undefined) {
64+
networkTemplate.NetworkingConfig.EndpointsConfig[networkName].priority = network.priority
65+
} else {
66+
networkTemplate.NetworkingConfig.EndpointsConfig[networkName].priority = 0
67+
}
68+
if (index === 0)
69+
opts.NetworkingConfig.EndpointsConfig = networkTemplate.NetworkingConfig.EndpointsConfig
70+
networksToAttach.push(networkTemplate.NetworkingConfig.EndpointsConfig)
71+
}
72+
}
73+
} else {
74+
opts.NetworkingConfig.EndpointsConfig[projectName + '_default'] = {
75+
'IPAMConfig': {},
76+
'Links': [],
77+
'Aliases': [
78+
serviceName,
79+
]
80+
};
81+
}
2782

2883
if (service.volumes) {
2984
opts['Volumes'] = {};
@@ -44,6 +99,19 @@ module.exports = async function (docker, projectName, recipe, output) {
4499
}
45100
try {
46101
var container = await docker.createContainer(opts);
102+
103+
if (networksToAttach.length > 1) {
104+
let networkNames = Object.keys(networksToAttach[0]);
105+
let network = findNetwork(output, networkNames[0])
106+
await network.disconnect({ 'Container': container.id })
107+
let networksToAttachSorted = tools.sortNetworksToAttach(networksToAttach)
108+
for (var networkToAttach of networksToAttachSorted) {
109+
let networkName = Object.keys(networkToAttach);
110+
let network = findNetwork(output, networkName)
111+
await network.connect({ 'Container': container.id, 'EndpointConfig': networkToAttach[networkName] })
112+
}
113+
114+
}
47115
await container.start();
48116
services.push(container);
49117
} catch (err) {
@@ -105,4 +173,12 @@ var buildEnvVars = function (service) {
105173
output.push(envName + '=' + service.environment[envName])
106174
}
107175
return output;
176+
}
177+
178+
179+
var findNetwork = function (output, name) {
180+
for (var network of output.networks) {
181+
if (network.name == name)
182+
return network.network
183+
}
108184
}

lib/tools.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,36 @@ module.exports = {
55

66
while (order.length !== serviceNames.length) {
77
for (var serviceName of serviceNames) {
8-
if(order.indexOf(serviceName) === -1) {
8+
if (order.indexOf(serviceName) === -1) {
99
insertService(serviceName, recipe.services[serviceName].depends_on || [], order)
1010
}
1111
}
1212
}
1313
return order;
14+
},
15+
'sortNetworksToAttach': function (networksToAttach) {
16+
var networksToAttachSorted = [];
17+
for (let i = 0; i < networksToAttach.length; i++) {
18+
let networkName = Object.keys(networksToAttach[i]);
19+
if (i === 0) {
20+
networksToAttachSorted.push(networksToAttach[i])
21+
} else {
22+
let aux = 0;
23+
for (let j = 0; j < networksToAttachSorted.length; j++) {
24+
let networkNameSorted = Object.keys(networksToAttachSorted[j]);
25+
if (networksToAttachSorted[j][networkNameSorted].priority > networksToAttach[i][networkName].priority) {
26+
aux += j + 1;
27+
} else if (networksToAttachSorted[j][networkNameSorted].priority < networksToAttach[i][networkName].priority) {
28+
aux += j - 1;
29+
} else {
30+
aux += j + 1;
31+
}
32+
}
33+
if (aux < 0) aux = 0;
34+
networksToAttachSorted.splice(aux, 0, networksToAttach[i]);
35+
}
36+
}
37+
return networksToAttachSorted
1438
}
1539
}
1640

0 commit comments

Comments
 (0)