2017-02-11 19:23:10 +00:00
import * as plugins from './npmdocker.plugins' ;
import * as paths from './npmdocker.paths' ;
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...' )
if ( plugins . shelljs . which ( 'docker' ) ) {
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 } ` )
plugins . smartfile . memory . toFsSync ( dockerfile , paths . dockerfile )
plugins . beautylog . ok ( 'Dockerfile created!' )
done . resolve ( )
return done . promise
}
2016-07-18 22:37:13 +00:00
/ * *
* builds the Dockerimage from the built Dockerfile
* /
let buildDockerImage = ( ) = > {
2017-02-11 19:23:10 +00:00
let done = plugins . q . defer ( )
plugins . beautylog . ora . text ( 'pulling latest base image from registry...' )
plugins . shelljs . exec (
` docker pull ${ config . baseImage } ` ,
{
silent : true
} ,
( ) = > {
plugins . beautylog . ora . text ( 'building Dockerimage...' )
// are we creating a build context form project ?
if ( process . env . CI === 'true' ) {
plugins . beautylog . ora . text ( 'creating build context...' )
plugins . smartfile . fs . copySync ( paths . cwd , paths . buildContextDir )
}
plugins . shelljs . exec (
` docker build -f ${ paths . dockerfile } -t ${ dockerData . imageTag } ${ paths . assets } ` ,
{
silent : true
} ,
( ) = > {
plugins . beautylog . ok ( 'Dockerimage built!' )
done . resolve ( )
2016-07-28 22:52:30 +00:00
}
2017-02-11 19:23:10 +00:00
)
}
) // first pull latest version of baseImage
return done . promise
}
2016-07-18 22:37:13 +00:00
2016-08-04 20:25:15 +00:00
let buildDockerProjectMountString = ( ) = > {
2017-02-11 19:23:10 +00:00
let done = plugins . q . defer ( )
if ( process . env . CI !== 'true' ) {
dockerData . dockerProjectMountString = ` -v ${ paths . cwd } :/workspace `
} ;
done . resolve ( )
return done . promise
2016-08-04 20:25:15 +00:00
}
2017-02-11 19:23:10 +00:00
/ * *
* builds an environment string that docker cli understands
* /
2016-08-04 20:25:15 +00:00
let buildDockerEnvString = ( ) = > {
2017-02-11 19:23:10 +00:00
let done = plugins . q . defer ( )
for ( let keyValueObjectArg of config . keyValueObjectArray ) {
let envString = dockerData . dockerEnvString = dockerData . dockerEnvString + ` -e ${ keyValueObjectArg . key } = ${ keyValueObjectArg . value } `
} ;
done . resolve ( )
return done . promise
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
* /
2016-08-04 20:25:15 +00:00
let buildDockerSockString = ( ) = > {
2017-02-11 19:23:10 +00:00
let done = plugins . q . defer ( )
if ( config . dockerSock ) {
dockerData . dockerSockString = ` -v /var/run/docker.sock:/var/run/docker.sock `
} ;
done . resolve ( )
return done
}
2016-08-04 20:25:15 +00:00
2016-07-18 22:37:13 +00:00
/ * *
* creates a container by running the built Dockerimage
* /
let runDockerImage = ( ) = > {
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' )
config . exitCode = plugins . shelljs . exec ( ` docker run ${ dockerData . dockerProjectMountString } ${ dockerData . dockerSockString } ${ dockerData . dockerEnvString } --name ${ dockerData . containerName } ${ dockerData . imageTag } ` ) . code
done . resolve ( )
return done . promise
}
2016-07-18 22:37:13 +00:00
2017-02-11 19:23:10 +00:00
/ * *
* cleans up : deletes the test container
* /
2016-07-29 20:00:22 +00:00
let deleteDockerContainer = ( ) = > {
2017-02-11 19:23:10 +00:00
let done = plugins . q . defer ( )
plugins . shelljs . exec ( ` docker rm -f ${ dockerData . containerName } ` , {
silent : true
} )
done . resolve ( )
return done . promise
}
2016-07-18 22:37:13 +00:00
2017-02-11 19:23:10 +00:00
/ * *
* cleans up deletes the test image
* /
2016-07-18 22:37:13 +00:00
let deleteDockerImage = ( ) = > {
2017-02-11 19:23:10 +00:00
let done = plugins . q . defer ( )
plugins . shelljs . exec ( ` docker rmi ${ dockerData . imageTag } ` , {
silent : true
} )
done . resolve ( )
return done . promise
}
2016-07-18 22:37:13 +00:00
2017-02-11 19:23:10 +00:00
/ * *
* cleans up , deletes the build context
* /
2016-07-28 22:52:30 +00:00
let deleteBuildContext = ( ) = > {
2017-02-11 19:23:10 +00:00
let done = plugins . q . defer ( )
plugins . smartfile . fs . remove ( paths . buildContextDir )
. then ( ( ) = > {
done . resolve ( )
} )
return done . promise
}
2016-07-29 20:00:22 +00:00
let preClean = ( ) = > {
2017-02-11 19:23:10 +00:00
let done = plugins . q . defer ( )
deleteDockerImage ( )
. then ( deleteDockerContainer )
. then ( ( ) = > {
plugins . beautylog . ok ( 'ensured clean Docker environment!' )
done . resolve ( )
} )
}
2016-07-29 20:00:22 +00:00
let postClean = ( ) = > {
2017-02-11 19:23:10 +00:00
let done = plugins . q . defer ( )
deleteDockerContainer ( )
. then ( deleteDockerImage )
. then ( deleteBuildContext )
. then ( ( ) = > {
plugins . beautylog . ok ( 'cleaned up!' )
done . resolve ( )
} )
2016-07-28 22:52:30 +00:00
}
2016-07-18 22:37:13 +00:00
export let run = ( configArg ) = > {
2017-02-11 19:23:10 +00:00
let done = plugins . q . defer ( )
config = configArg
checkDocker ( )
. then ( preClean )
. then ( buildDockerFile )
. then ( buildDockerImage )
. then ( buildDockerProjectMountString )
. then ( buildDockerEnvString )
. then ( buildDockerSockString )
. then ( runDockerImage )
. then ( postClean )
. then ( ( ) = > {
done . resolve ( config ) ;
} ) . catch ( err = > { console . log ( err ) } )
return done . promise
2016-07-18 22:37:13 +00:00
}