Skip to content

Commit

Permalink
Use config file to connect to docker
Browse files Browse the repository at this point in the history
  • Loading branch information
khornberg committed Nov 13, 2015
1 parent 9b656f1 commit 436e215
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 43 deletions.
10 changes: 5 additions & 5 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

"use strict";

var menubar = require('menubar');
var Docker = require('dockerode');
var fs = require('fs');
var docker = new Docker();
var menubar = require('menubar');
var Docker = require('dockerode');
var docker = require('./src/connect');

var mb = menubar({
dir: __dirname + "/src",
Expand Down Expand Up @@ -44,7 +43,8 @@ mb.on('ready', function ready () {
mb.on('show', function () {
//send events to the page
if (lastConnectionStatus == 'OK') {
mb.window.webContents.send('ping', 'connected');
mb.window.webContents.send('send', 'connected');
mb.window.webContents.send('send', docker);
}
});

Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
"dependencies": {
"docker-events": "0.0.2",
"dockerode": "^2.2.3",
"menubar": "*"
"menubar": "*",
"read-yaml": "^1.0.0",
"user-home": "^2.0.0"
},
"devDependencies": {
"electron-packager": "^5.1.1",
"electron-prebuilt": "0.34.2"
},
"main": "main.js",
"scripts": {
"build": "node_modules/.bin/electron-packager . docker-indicator --out=dist --platform=darwin --arch=x64 --version=0.34.2 --ignore=node_modules/electron-packager --icon=Icon.icns --build-version=0.0.1",
"build": "node_modules/.bin/electron-packager . docker-indicator --out=dist --platform=darwin --arch=x64 --version=0.34.2 --ignore=\"node_modules/(electron-packager|electron-prebuilt)\" --icon=Icon.icns --build-version=0.0.2",
"start": "electron ."
},
"author": "Kyle Hornberg <[email protected]>",
Expand Down
18 changes: 16 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,30 @@ Started container list (shows on top)
This is very early in development.
Start with `npm start` or `electron .` from the repo directory.

## Connection
Put a .docker-indicator.yaml file in your home directory.

```
docker:
protocol: https
ip: 192.168.99.100
port: 2376
ca: /Users/user/.docker/machine/machines/default/ca.pem
cert: /Users/user/.docker/machine/machines/default/cert.pem
key: /Users/user/.docker/machine/machines/default/key.pem
```

## Changelog

0.0.2
* Try to connect to docker smartly

0.0.1
* UI reacts to Docker events
* Start/Stop buttons work on containers

## TODO

* use docker machine settings if available
* ui errors, better state management, etc.
* fix build command
* build (icons, size, packaging, etc.)
* configuration via ui?
35 changes: 1 addition & 34 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@

"use strict()";

var os = require('os');
var execSync = require('child_process').execSync;
var Docker = require('dockerode');
var fs = require('fs');
var DockerEvents = require('docker-events');
var docker = connect();
var docker = require('./connect');

var emitter = new DockerEvents({
docker: docker,
Expand All @@ -32,36 +29,6 @@ emitter.on("stop", (message) => {

emitter.on("disconnect", (message) => console.log('disconnect', message));

// connect without relying on envvars
// when starting from app doesn't get envvars because docker machine has not added them
// nor when starting from a shell with out the docker command run already
function connect() {
if (os.platform != 'Linux') {
var output = execSync('docker-machine env default').toString();
var lines = output.split('\n');
var docker_machine = {};
lines.forEach((line) => {
x = line.split('=');
if (x[0].indexOf('export') >= 0) {
docker_machine[x[0].split(' ')[1]] = x[1].replace(/"/g, '');
}
});

var ip = docker_machine['DOCKER_HOST'].match(/\w*:\/\/(.*):/)[1];
var port = docker_machine['DOCKER_HOST'].match(/\w*:\/\/.*:(.*)/)[1];

return new Docker({
host: ip,
port: port,
ca: fs.readFileSync(`${docker_machine['DOCKER_CERT_PATH']}/ca.pem`),
cert: fs.readFileSync(`${docker_machine['DOCKER_CERT_PATH']}/cert.pem`),
key: fs.readFileSync(`${docker_machine['DOCKER_CERT_PATH']}/key.pem`),
});
}
// assumes a local docker connection
return new Docker();
}

function render_container(container) {
var exited = container.Status.substr(0, 6) === 'Exited' ? true : false;
var button_state = exited ? "negative" : "positive";
Expand Down
17 changes: 17 additions & 0 deletions src/connect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';
var fs = require('fs');
var userHome = require('user-home');
var read = require('read-yaml');
var Docker = require('dockerode');

module.exports = (function () {
var config = read.sync(`${userHome}/.docker-indicator.yaml`);
return new Docker({
protocol: config.docker.protocol,
host: config.docker.ip,
port: config.docker.port,
ca: fs.readFileSync(config.docker.ca),
cert: fs.readFileSync(config.docker.cert),
key: fs.readFileSync(config.docker.key),
});
})()

0 comments on commit 436e215

Please sign in to comment.