Skip to content
This repository has been archived by the owner on Apr 7, 2018. It is now read-only.

Commit

Permalink
Merge pull request #23 from juniorbird/create-dockers
Browse files Browse the repository at this point in the history
Create dockers
  • Loading branch information
juniorbird committed Jun 2, 2016
2 parents 3279adc + 72db3a6 commit 7d05b39
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 27 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
coverage/
node_modules
Dockerfile
workflow.*
docker/
11 changes: 5 additions & 6 deletions lib/package-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,20 +136,19 @@ packageParser.matchDependencies = function matchDependencies(path_to_pkg) {
* @param {string} The path, from root of application (_not_ from this module) to the package.json to load
* @returns {Object} An object describing all of the needed docker modules
**/

let packageJSON = this.dependencies(path_to_pkg);
const packageJSON = this.dependencies(path_to_pkg);

let packageURLs = packageJSON.map((pkg) => {
return this.depURL(pkg);
});

let depPromises = [];
let depKeywords = [];
// Drop our output in an object so that we can later see
// Drop our output in an array so that we can later see
// if an individual module is in that list
let depList = {};
let depList = [];

let dockerPromises = this.fetchDockers();
const dockerPromises = this.fetchDockers();

// We now have an array of NPM URLs
// We need to iterate over the array
Expand All @@ -176,7 +175,7 @@ packageParser.matchDependencies = function matchDependencies(path_to_pkg) {
// If there's not a key in depList with that name
// And if there's a matching Docker module
// Add a key
depList[kw] = true;
depList.push(kw);
}
});
});
Expand Down
103 changes: 83 additions & 20 deletions lib/writeDockerfile.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,93 @@
'use strict';

/** very basic description of what's going into the file */

const fs = require('fs');
const Promise = require('bluebird');
const parser = require('./package-parser.js');
// just some basic official images
const arrOfImages = ['mongo', 'redis', 'postgres', 'mysql'];

const makeDockerFiles = {};
// For the moment, stub out the package.json
const PKG_PATH = './test/fixtures/package-test.json';

// TODO: make this work from node_modules
const PATH_TO_DOCKER_FILES = 'docker/';

/** Make dockerfile for node server */
makeDockerFiles.makeNodeServer = function buildFile() {
// get dependencies from package.json (for now test package)
let dependencies = parser.dependencies(`${__dirname}/../test/fixtures/package-test.json`);
// Uncomment this when we're calling writeDockerfile from writeComposefile
const whatImages = parser.matchDependencies(PKG_PATH);

// filter out ones that arent in official images
dependencies = dependencies.filter(dep => arrOfImages.indexOf(dep) !== -1);
const makeDockerFiles = {};
/**
* makeDockerFiles is the parent for our internal Dockerfile construction
* processes. Pretty much everything is private to the function itself.
* To call it, use something like:
* makeDockerFiles.makeDockers(whatImages).then(p => console.log(p));
* @private
**/

makeDockerFiles.formulateDocker = function formulateDocker(dependency) {
/**
* Makes the text string for a dockerfile
* @param {string} The name of the dependency
* @returns {Object} What should be created, form of {dependency: the contents for the Dockerfile}
* @private
**/
return `FROM ${dependency}: latestt
EXPOSE $${dependency}_PORT`;
}

// if there are no dependencies, just setup basic node container
if (!dependencies.length) {
const dockerfile = `FROM node:6.2-onbuild \n EXPOSE ${process.env.PORT}`;
return fs.writeFileSync('Dockerfile', dockerfile);
makeDockerFiles.saveDocker = function saveDocker(dependency, formulaObj) {
/**
* Saves a dockerfile
* All dockerfiles will be saved in the /docker directory
* @param {Object} Format: {dependency: the contents for the Dockerfile}
* @returns {Function} Promise of filepath to saved dockerfile
* @private
**/
if (formulaObj) {
return new Promise((resolve, reject) => {
// Check for a /docker/ dir
fs.stat(PATH_TO_DOCKER_FILES, (err, stats) => {
if (err) {
// If not, returns an error value
// So, make a directory
fs.mkdir(PATH_TO_DOCKER_FILES, (err) => {
if (err) {
// Can't make a directory!
// Might as well just plotz
reject(err);
}
});
}
// Now we know we have a directory. Save the file.
fs.writeFile(PATH_TO_DOCKER_FILES + dependency, formulaObj, err => {
if (err) {
// Can't write a file!
// Always the shlmiel. Might as well make the app my shlimazel.
reject(err);
} else {
// alert! [Below] is ES6 only
resolve({ [dependency] : PATH_TO_DOCKER_FILES + dependency});
}
});
});
});
} else {
return null;
}
// this part is clearly not what's actually going in (just to get a basis for it);
const dockerfile = `FROM node:latest \nEXPOSE ${process.env.PORT || 3000}`;
return fs.writeFileSync('Dockerfile', dockerfile);
};
}

makeDockerFiles.makeDockers = function makeDockers(imagesNeeded) {
/**
* Bring the other methods together, which we have to do because
* There will be a lot of Promises here.
* All dockerfiles will be saved in the /docker directory
* @param {Function} a Promise of the images needed to be made
* @returns {Function} a Promise of the dockerfiles and their paths. [{ dependency: path }]
* @private
**/
let obj;

module.exports = makeDockerFiles;
// Map the promises of images needed to promises of images created
return Promise.map(imagesNeeded, image => {
// Formulate a Docker, save it, and store the Promise resulting
return this.saveDocker(image, this.formulateDocker(image));
});
}
2 changes: 1 addition & 1 deletion test/package-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ describe('docker fetcher', () => {
});

describe('npm/docker matcher', () => {

//TODO - test this
});

// Check that we grab all of the official repos from Docker (#1)
Expand Down

0 comments on commit 7d05b39

Please sign in to comment.