This repository has been archived by the owner on Apr 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from juniorbird/create-dockers
Create dockers
- Loading branch information
Showing
4 changed files
with
92 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
coverage/ | ||
node_modules | ||
Dockerfile | ||
workflow.* | ||
docker/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters