Skip to content

Commit

Permalink
refactor: config parser
Browse files Browse the repository at this point in the history
  • Loading branch information
debuggy committed Nov 19, 2018
1 parent f4b6f8c commit 079af13
Show file tree
Hide file tree
Showing 15 changed files with 167 additions and 26 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"standard.enable": true,
"javascript.validate.enable": false,
"standard.usePackageJson": true,
"files.exclude": {
Expand Down
23 changes: 23 additions & 0 deletions docs/config-parser.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Config Parser Design

- build-base

`From ${config.base_docker}\n`

- build-env

`ENV ${config.env_variables}\n`

- install
- export universal interface with a type and a function
- conda
- python os
- conda command
- pip command
- custom command
- hdfs copy

- custom
- publish to npm with a tag
- expose a certain function
- get tag list to require
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions examples/config-example.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"steps": [
{
"name": "install_python",
"type": "components",
"type": "install_python",
"config": {
"name": "python",
"version": "3.6"
Expand Down Expand Up @@ -51,7 +51,7 @@
},
{
"name": "custom_command",
"type": "custom",
"type": "custom_command",
"config": {
"command": "python hello.py"
}
Expand Down
11 changes: 11 additions & 0 deletions lib/config-parser/base_docker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

function buildBase (config) {
let dockerContent = ''
dockerContent += `From ${config.base_docker}\n`
return dockerContent
}

module.exports = {
type: 'base_docker',
build: buildBase
}
13 changes: 13 additions & 0 deletions lib/config-parser/env_variables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

function buildEnv (config) {
let dockerContent = ''
for (const envKey in config.env_variables) {
dockerContent += `ENV ${envKey}=${config.env_variables[envKey]}\n`
}
return dockerContent
}

module.exports = {
type: 'env_variables',
build: buildEnv
}
40 changes: 40 additions & 0 deletions lib/config-parser/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const version = require('./version')
const baseDocker = require('./base_docker')
const envVariables = require('./env_variables')
const steps = require('./steps')
const _ = require('lodash')

const componentsList = [version, baseDocker, envVariables, steps]

function selectComponent (type) {
const component = componentsList.find(item => item.type === type)
if (_.isNil(component)) {
throw new Error(`cannot find component: '${type}'`)
}

return component
}

function buildConfig (config) {
let dockerContent = ''

if (!_.isNil(config.version)) {
dockerContent += selectComponent('version').build(config)
}

if (!_.isNil(config.base_docker)) {
dockerContent += selectComponent('base_docker').build(config)
}

if (!_.isNil(config.env_variables)) {
dockerContent += selectComponent('env_variables').build(config)
}

if (!_.isNil(config.steps)) {
dockerContent += selectComponent('steps').build(config)
}

return dockerContent
}

module.exports = buildConfig
11 changes: 11 additions & 0 deletions lib/config-parser/steps/custom_command.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

function buildCustom (step) {
let dockerContent = ''
dockerContent += `CMD ${step.config.command}\n`
return dockerContent
}

module.exports = {
'type': 'custom_command',
'build': buildCustom
}
11 changes: 11 additions & 0 deletions lib/config-parser/steps/default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

function buildDefault (step) {
let dockerContent = ''
dockerContent += `no processing for ${step.type}\n`
return dockerContent
}

module.exports = {
'type': 'default',
'build': buildDefault
}
30 changes: 30 additions & 0 deletions lib/config-parser/steps/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const _ = require('lodash')

const customCommand = require('./custom_command')
const installPython = require('./install_python')
const defaultStep = require('./default')

const stepsList = [customCommand, installPython]

function selectStepBuilder (stepType) {
return stepsList.find(item => item.type === stepType)
}

function buildSteps (config) {
let dockerContent = ''
for (let step of config.steps) {
let stepBuilder = selectStepBuilder(step.type)
if (_.isNil(stepBuilder)) {
// throw new Error(`cannot find step builder: '${step.type}'`)
stepBuilder = defaultStep
}
dockerContent += stepBuilder.build(step)
}

return dockerContent
}

module.exports = {
type: 'steps',
build: buildSteps
}
9 changes: 9 additions & 0 deletions lib/config-parser/steps/install_python.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

function installPython (config) {
return 'install python\n'
}

module.exports = {
'type': 'install_python',
'build': installPython
};
9 changes: 9 additions & 0 deletions lib/config-parser/version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

function buildVersion (config) {
return `# version: ${config.version}\n`
}

module.exports = {
type: 'version',
build: buildVersion
}
24 changes: 3 additions & 21 deletions lib/dockerfile-generator.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,18 @@
const fs = require('fs-extra')
const _ = require('lodash')
const SchemaValidationError = require('./schema-validation-error')
const schemaValidator = require('./schema-validator')
const path = require('path')

async function configConverter (config) {
let dockerContent = ''
dockerContent += `From ${config.base_docker}\n`
if (!_.isNil(config.env_variables)) {
for (const envKey in config.env_variables) {
dockerContent += `ENV ${envKey}=${config.env_variables[envKey]}\n`
}
}
if (!_.isNil(config.steps)) {
for (const buildStep of config.steps) {
if (buildStep.type === 'custom') {
dockerContent += `CMD ${buildStep.config.command}\n`
}
}
}

return dockerContent
}
const configParser = require('./config-parser')

async function dockerfileGenerator (config, dockerfilePath) {
const configSchema = await fs.readJson(path.join(__dirname, '../schemas/config-schema.json'))
const validation = await schemaValidator(configSchema, config)
if (!validation.valid) {
throw new SchemaValidationError(`The config validation is not passed.`, validation.errors)
}
const dockerContent = await configConverter(config)
const dockerContent = configParser(config)
await fs.outputFile(dockerfilePath, dockerContent)

return dockerfilePath
}

Expand Down
7 changes: 4 additions & 3 deletions mkdocs.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
site_name: PAIFlow Docs
nav:
- Introduction: index.md
- Work Plan: work_plan.md
- Config Schema: config_schema.md
theme: readthedocs
- Work Plan: work-plan.md
- Config Schema: config-schema.md
- Config Parser: config-parser.md
theme: readthedocs

0 comments on commit 079af13

Please sign in to comment.