2017-10-13 14:44:25 +00:00
import * as plugins from './npmdocker.plugins'
import * as paths from './npmdocker.paths'
2017-02-11 19:23:10 +00:00
import * as snippets from './npmdocker.snippets'
2016-07-19 22:40:37 +00:00
2016-08-04 20:25:15 +00:00
// interfaces
2017-02-11 19:23:10 +00:00
import { IConfig } from './npmdocker.config'
let config : IConfig
2016-08-04 20:25:15 +00:00
2017-02-11 19:23:10 +00:00
/ * *
* the docker data used to build the internal testing container
* /
2016-07-19 17:21:06 +00:00
let dockerData = {
2017-02-11 19:23:10 +00:00
imageTag : 'npmdocker-temp-image:latest' ,
containerName : 'npmdocker-temp-container' ,
dockerProjectMountString : '' ,
dockerSockString : '' ,
dockerEnvString : ''
}
2016-07-19 22:40:37 +00:00
2016-07-18 18:48:34 +00:00
/ * *
* check if docker is available
* /
2016-07-18 22:59:57 +00:00
let checkDocker = ( ) = > {
2017-02-11 19:23:10 +00:00
let done = plugins . q . defer ( )
plugins . beautylog . ora . text ( 'checking docker...' )
2017-03-28 23:01:37 +00:00
if ( plugins . smartshell . which ( 'docker' ) ) {
2017-02-11 19:23:10 +00:00
plugins . beautylog . ok ( 'Docker found!' )
done . resolve ( )
} else {
done . reject ( new Error ( 'docker not found on this machine' ) )
}
return done . promise
}
2016-07-18 18:48:34 +00:00
2016-07-18 22:37:13 +00:00
/ * *
* builds the Dockerfile according to the config in the project
* /
let buildDockerFile = ( ) = > {
2017-02-11 19:23:10 +00:00
let done = plugins . q . defer ( )
plugins . beautylog . ora . text ( 'building Dockerfile...' )
let dockerfile : string = snippets . dockerfileSnippet ( {
baseImage : config.baseImage ,
command : config.command
} )
plugins . beautylog . info ( ` Base image is: ${ config . baseImage } ` )
plugins . beautylog . info ( ` Command is: ${ config . command } ` )
2017-10-13 14:44:25 +00:00
plugins . smartfile . memory . toFsSync ( dockerfile , plugins . path . join ( paths . cwd , 'npmdocker' ) )
2017-02-11 19:23:10 +00:00
plugins . beautylog . ok ( 'Dockerfile created!' )
2017-10-13 14:44:25 +00:00
plugins . beautylog . ora . stop ( )
2017-02-11 19:23:10 +00:00
done . resolve ( )
return done . promise
}
2016-07-18 22:37:13 +00:00
/ * *
* builds the Dockerimage from the built Dockerfile
* /
2017-03-28 23:01:37 +00:00
let buildDockerImage = async ( ) = > {
2017-10-13 14:44:25 +00:00
plugins . beautylog . info ( 'pulling latest base image from registry...' )
await plugins . smartshell . exec (
2017-03-28 23:01:37 +00:00
` docker pull ${ config . baseImage } `
2017-10-13 14:44:25 +00:00
)
plugins . beautylog . ora . text ( 'building Dockerimage...' )
let execResult = await plugins . smartshell . execSilent (
` docker build -f npmdocker -t ${ dockerData . imageTag } ${ paths . cwd } `
)
if ( execResult . exitCode !== 0 ) {
console . log ( execResult . stdout )
process . exit ( 1 )
}
plugins . beautylog . ok ( 'Dockerimage built!' )
2017-02-11 19:23:10 +00:00
}
2016-07-18 22:37:13 +00:00
2017-03-28 23:01:37 +00:00
let buildDockerProjectMountString = async ( ) = > {
2017-02-11 19:23:10 +00:00
if ( process . env . CI !== 'true' ) {
dockerData . dockerProjectMountString = ` -v ${ paths . cwd } :/workspace `
} ;
2016-08-04 20:25:15 +00:00
}
2017-02-11 19:23:10 +00:00
/ * *
* builds an environment string that docker cli understands
* /
2017-03-28 23:01:37 +00:00
let buildDockerEnvString = async ( ) = > {
2017-02-11 19:23:10 +00:00
for ( let keyValueObjectArg of config . keyValueObjectArray ) {
let envString = dockerData . dockerEnvString = dockerData . dockerEnvString + ` -e ${ keyValueObjectArg . key } = ${ keyValueObjectArg . value } `
2017-10-13 14:44:25 +00:00
}
2016-08-04 20:25:15 +00:00
}
2017-02-11 19:23:10 +00:00
/ * *
* creates string to mount the docker . sock inside the testcontainer
* /
2017-03-28 23:01:37 +00:00
let buildDockerSockString = async ( ) = > {
2017-02-11 19:23:10 +00:00
if ( config . dockerSock ) {
dockerData . dockerSockString = ` -v /var/run/docker.sock:/var/run/docker.sock `
} ;
}
2016-08-04 20:25:15 +00:00
2016-07-18 22:37:13 +00:00
/ * *
* creates a container by running the built Dockerimage
* /
2017-03-28 23:01:37 +00:00
let runDockerImage = async ( ) = > {
2017-02-11 19:23:10 +00:00
let done = plugins . q . defer ( )
plugins . beautylog . ora . text ( 'starting Container...' )
plugins . beautylog . ora . end ( )
plugins . beautylog . log ( 'now running Dockerimage' )
2017-03-28 23:01:37 +00:00
config . exitCode = ( await plugins . smartshell . exec ( ` docker run ${ dockerData . dockerProjectMountString } ${ dockerData . dockerSockString } ${ dockerData . dockerEnvString } --name ${ dockerData . containerName } ${ dockerData . imageTag } ` ) ) . exitCode
2017-02-11 19:23:10 +00:00
}
2016-07-18 22:37:13 +00:00
2017-02-11 19:23:10 +00:00
/ * *
* cleans up : deletes the test container
* /
2017-03-28 23:01:37 +00:00
let deleteDockerContainer = async ( ) = > {
await plugins . smartshell . execSilent ( ` docker rm -f ${ dockerData . containerName } ` )
2017-02-11 19:23:10 +00:00
}
2016-07-18 22:37:13 +00:00
2017-02-11 19:23:10 +00:00
/ * *
* cleans up deletes the test image
* /
2017-03-28 23:01:37 +00:00
let deleteDockerImage = async ( ) = > {
2017-04-02 12:48:23 +00:00
await plugins . smartshell . execSilent ( ` docker rmi ${ dockerData . imageTag } ` ) . then ( async ( response ) = > {
if ( response . exitCode !== 0 ) {
console . log ( response . stdout )
}
} )
2017-02-11 19:23:10 +00:00
}
2016-07-18 22:37:13 +00:00
2017-03-28 23:01:37 +00:00
let preClean = async ( ) = > {
await deleteDockerImage ( )
2017-02-11 19:23:10 +00:00
. then ( deleteDockerContainer )
2017-03-28 23:01:37 +00:00
. then ( async ( ) = > {
2017-02-11 19:23:10 +00:00
plugins . beautylog . ok ( 'ensured clean Docker environment!' )
} )
}
2016-07-29 20:00:22 +00:00
2017-03-28 23:01:37 +00:00
let postClean = async ( ) = > {
await deleteDockerContainer ( )
2017-02-11 19:23:10 +00:00
. then ( deleteDockerImage )
2017-03-28 23:01:37 +00:00
. then ( async ( ) = > {
2017-02-11 19:23:10 +00:00
plugins . beautylog . ok ( 'cleaned up!' )
} )
2017-10-13 16:06:12 +00:00
plugins . smartfile . fs . removeSync ( paths . npmdockerFile )
2016-07-28 22:52:30 +00:00
}
2017-03-28 23:01:37 +00:00
export let run = async ( configArg : IConfig ) : Promise < IConfig > = > {
plugins . beautylog . ora . start ( )
2017-02-11 19:23:10 +00:00
config = configArg
2017-03-28 23:01:37 +00:00
let resultConfig = await checkDocker ( )
2017-02-11 19:23:10 +00:00
. then ( preClean )
. then ( buildDockerFile )
. then ( buildDockerImage )
. then ( buildDockerProjectMountString )
. then ( buildDockerEnvString )
. then ( buildDockerSockString )
. then ( runDockerImage )
. then ( postClean )
2017-03-28 23:01:37 +00:00
. catch ( err = > { console . log ( err ) } )
return config
2017-04-02 12:48:23 +00:00
}