Skip to content
This repository has been archived by the owner on Jul 19, 2021. It is now read-only.

Commit

Permalink
make sure project docker network exists when starting
Browse files Browse the repository at this point in the history
  • Loading branch information
greyarch committed Jul 11, 2017
1 parent 84c46b8 commit 5bef674
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 27 deletions.
15 changes: 8 additions & 7 deletions index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ ENABLE_NETWORK_HEALTHCHECK = env.get 'ENABLE_NETWORK_HEALTHCHECK', false
NETWORK_HEALTHCHECK_TEST_INTERFACE = env.get 'NETWORK_HEALTHCHECK_TEST_INTERFACE', 'eth0'
NETWORK_HEALTHCHECK_TEST_IP_PREFIX = env.get 'NETWORK_HEALTHCHECK_TEST_IP_PREFIX', '10.25'

scanCmd = env.get 'NETWORK_SCAN_CMD', "nmap -sP -n 10.25.55.51-240"

datastoreScanEnabled = env.get 'DATASTORE_SCAN_ENABLED', true
if datastoreScanEnabled is 'false' then datastoreScanEnabled = false

Expand All @@ -30,9 +28,9 @@ config =
scriptBaseDir: env.assert 'SCRIPT_BASE_DIR'
network:
name: env.assert 'NETWORK_NAME'
scanCmd: scanCmd
scanInterval: parseInt(env.get 'NETWORK_SCAN_INTERVAL', '60000')
parentNic: env.assert 'NETWORK_PARENT_NIC'
scanEnabled: env.get 'NETWORK_SCAN_ENABLED', 'true'
scanInterval: parseInt(env.get 'NETWORK_SCAN_INTERVAL', '60000')
dhcp:
scanInterval: parseInt(env.get 'DHCP_SCAN_INTERVAL', '5000')
scanEnabled: env.get 'DHCP_SCAN_ENABLED', 'true'
Expand All @@ -43,7 +41,6 @@ config =
datastore:
scanEnabled: datastoreScanEnabled


if ENABLE_NETWORK_HEALTHCHECK and ENABLE_NETWORK_HEALTHCHECK isnt 'false'
config.net_container.healthcheck =
test: env.get 'NETWORK_HEALTHCHECK_TEST', "ifconfig #{NETWORK_HEALTHCHECK_TEST_INTERFACE} | grep inet | grep #{NETWORK_HEALTHCHECK_TEST_IP_PREFIX}"
Expand All @@ -67,9 +64,13 @@ mqtt = Mqtt.connect config.mqtt
publishState = (instance, state) ->
mqtt.publish '/instance/state', {instance: instance, state: state}

publishNetworkInfo = (data) -> mqtt.publish '/network/info', data
unless config.network.scanEnabled is 'false'
publishNetworkInfo = (data) -> mqtt.publish '/network/info', data
(require './src/js/network') config, publishNetworkInfo
config.network.scanCmd = env.assert 'NETWORK_SCAN_CMD'
network = require('./src/js/network')(config, publishNetworkInfo)
network.createProjectNet()
network.scan() unless config.network.scanEnabled is 'false'

unless config.dhcp.scanEnabled is 'false'
publishDhcpInfo = (data) -> mqtt.publish '/dhcp/info', data
(require './src/js/dhcp') config, publishDhcpInfo
Expand Down
11 changes: 6 additions & 5 deletions run-dev.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
#!/bin/bash

DOCKER_HOST=tcp://10.19.88.248:2375 \
DOCKER_HOST=unix:///var/run/docker.sock \
AUTH_TOKEN=innovation \
HTTP_PORT=8080 \
PIPEWORKS_CMD="eth1 -i eth0 @CONTAINER_NAME@ 0/0 @3080" \
MQTT_URL=mqtt://localhost \
MQTT_USER=user \
MQTT_PASS=pass \
DOMAIN=infra \
DOMAIN=acc \
TLD=ictu \
SCRIPT_BASE_DIR=/local/data/scripts \
DATA_DIR=/local/data \
DATA_DIR=/tmp \
REMOTEFS_URL=http://10.19.88.248:8000 \
NETWORK_NAME=bigboat-apps-3080 \
NETWORK_PARENT_NIC=wlp3s0 \
NETWORK_SCAN_INTERVAL=60000 \
NETWORK_SCAN_ENABLED=true \
NETWORK_SCAN_ENABLED=false \
NETWORK_HEALTHCHECK_INTERVAL=2s \
NETWORK_HEALTHCHECK_TIMEOUT=35s \
NETWORK_HEALTHCHECK_RETRIES=10 \
Expand Down
50 changes: 36 additions & 14 deletions src/js/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,42 @@ shell.config.verbose = false;
shell.config.silent = true;

module.exports = (config, dataCallback, _setInterval = setInterval) => {
const getNetStats = () => {
console.log(`Perfom: ${config.network.scanCmd}`);
res = shell.exec(config.network.scanCmd);
if (res.code == 0) {
totalIps = res.stdout.match(/Nmap done: ?(\d+)/);
usedIps = res.stdout.match(/(\d+) hosts up/);
dataCallback({
totalIps: parseInt(totalIps[1]),
usedIps: parseInt(usedIps[1])
});
} else {
console.error("nmap probe failed", res.stderr);
return {
createProjectNet: () => {
// check if network already exists
if (
shell.exec(`docker network inspect ${config.network.name}`).code != 0
) {
// if network does not exist - create it
const netCreateCmd =
"docker network create -d macvlan --subnet=192.168.10.0/24 --gateway=192.168.10.1 -o parent=" +
config.network.parentNic +
" " +
config.network.name;
const res = shell.exec(netCreateCmd);
if (res.code != 0) {
console.error("Could not create macvlan network.", res.stderr);
process.exit(1);
}
}
},
scan: () => {
const getNetStats = () => {
console.log(`Perfom: ${config.network.scanCmd}`);
res = shell.exec(config.network.scanCmd);
if (res.code == 0) {
totalIps = res.stdout.match(/Nmap done: ?(\d+)/);
usedIps = res.stdout.match(/(\d+) hosts up/);
dataCallback({
totalIps: parseInt(totalIps[1]),
usedIps: parseInt(usedIps[1])
});
} else {
console.error("nmap probe failed", res.stderr);
}
};
getNetStats();
_setInterval(getNetStats, config.network.scanInterval);
}
};
getNetStats();
_setInterval(getNetStats, config.network.scanInterval);
};
2 changes: 1 addition & 1 deletion tests/js/network.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe("network", () => {
td
.when(shell.exec("nmap -sP -n 10.25.181.51-240"))
.thenReturn({ code: 0, stdout: stdout });
network(conf, cb, _setInterval);
network(conf, cb, _setInterval).scan();
td.verify(
cb({
totalIps: 200,
Expand Down

0 comments on commit 5bef674

Please sign in to comment.