@@ -172,7 +172,7 @@ export default class Environment {
172172 * @param options Optional attributes for the environment e.g. `packages`, `meta`
173173 * @param force If the environment already exists should it be overitten?
174174 */
175- static async create ( name : string , options : { [ key : string ] : any } = { } , force : boolean = false ) : Promise < Environment > {
175+ static async create ( name : string , options : { [ key : string ] : any } = { } , force : boolean = false ) : Promise < Environment > {
176176 const env = new Environment ( name , false )
177177
178178 if ( ! force ) {
@@ -380,7 +380,7 @@ export default class Environment {
380380 *
381381 * @param pure Should the shell that this command is executed in be 'pure'?
382382 */
383- async vars ( pure : boolean = false ) : Promise < { [ key : string ] : string } > {
383+ async vars ( pure : boolean = false ) : Promise < { [ key : string ] : string } > {
384384 const location = nix . location ( this . name )
385385
386386 let PATH = `${ location } /bin:${ location } /sbin`
@@ -422,25 +422,25 @@ export default class Environment {
422422 const { command, cpuShares, memoryLimit } = sessionParameters
423423 const nixLocation = nix . location ( this . name )
424424 const shellArgs = [
425- dockerCommand , '--interactive' , '--tty' , '--rm' ,
426- // Prepend the environment path to the PATH variable
427- '--env' , `PATH=${ nixLocation } /bin:${ nixLocation } /sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin` ,
428- // We also need to tell R where to find libraries
429- '--env' , `R_LIBS_SITE=${ nixLocation } /library` ,
430- // Read-only bind mount of the Nix store
431- '--volume' , '/nix/store:/nix/store:ro' ,
432- // Apply CPU shares
433- `--cpu-shares=${ cpuShares } ` ,
434- // Apply memory limit
435- `--memory=${ memoryLimit } ` ,
436- // We use Alpine Linux as a base image because it is very small but has some basic
437- // shell utilities (lkike ls and uname) that are good for debugging but also sometimes
438- // required for things like R
439- 'alpine'
440- ] . concat (
441- // Command to execute in the container
442- command ? command . split ( ' ' ) : DOCKER_DEFAULT_COMMAND
443- )
425+ dockerCommand , '--interactive' , '--tty' , '--rm' ,
426+ // Prepend the environment path to the PATH variable
427+ '--env' , `PATH=${ nixLocation } /bin:${ nixLocation } /sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin` ,
428+ // We also need to tell R where to find libraries
429+ '--env' , `R_LIBS_SITE=${ nixLocation } /library` ,
430+ // Read-only bind mount of the Nix store
431+ '--volume' , '/nix/store:/nix/store:ro' ,
432+ // Apply CPU shares
433+ `--cpu-shares=${ cpuShares } ` ,
434+ // Apply memory limit
435+ `--memory=${ memoryLimit } ` ,
436+ // We use Alpine Linux as a base image because it is very small but has some basic
437+ // shell utilities (lkike ls and uname) that are good for debugging but also sometimes
438+ // required for things like R
439+ 'alpine'
440+ ] . concat (
441+ // Command to execute in the container
442+ command ? command . split ( ' ' ) : DOCKER_DEFAULT_COMMAND
443+ )
444444
445445 if ( daemonize ) shellArgs . splice ( 1 , 0 , '-d' )
446446
@@ -562,9 +562,9 @@ export default class Environment {
562562 /**
563563 * Determine if a Docker container is running using 'docker ps'
564564 *
565- * @param containerId The ID of the container, can either be the long or truncated version .
565+ * @param containerId The ID of the container, must be truncated version (12 alphanumeric characters) .
566566 */
567- private async checkContainerRunning ( containerId : string ) : Promise < boolean > {
567+ async containerIsRunning ( containerId : string ) : Promise < boolean > {
568568 const containerRegex = new RegExp ( / ^ [ ^ _ \W ] { 12 } $ / )
569569 if ( containerRegex . exec ( containerId ) === null ) {
570570 throw new Error ( `'${ containerId } ' is not a valid docker container ID.` )
@@ -593,6 +593,16 @@ export default class Environment {
593593 return dockerProcess . toString ( ) . trim ( ) . substr ( 0 , DOCKER_CONTAINER_ID_SHORT_LENGTH )
594594 }
595595
596+ /**
597+ * Stop a running Docker container. Return true if the container was stopped or false if there was an error.
598+ *
599+ * @param containerId
600+ */
601+ async stopContainer ( containerId : string ) : Promise < boolean > {
602+ const dockerStopProcess = await spawn ( 'docker' , [ 'stop' , containerId ] )
603+ return dockerStopProcess . toString ( ) . trim ( ) . substr ( 0 , DOCKER_CONTAINER_ID_SHORT_LENGTH ) === containerId . substr ( 0 , DOCKER_CONTAINER_ID_SHORT_LENGTH )
604+ }
605+
596606 /**
597607 * Build a Docker container for this environment
598608 */
0 commit comments