tsdocker/ts/tsdocker.docker.ts

170 lines
4.6 KiB
TypeScript
Raw Normal View History

2019-05-10 09:45:20 +00:00
import * as plugins from './tsdocker.plugins';
import * as paths from './tsdocker.paths';
import * as snippets from './tsdocker.snippets';
import { logger, ora } from './tsdocker.logging';
2016-07-19 22:40:37 +00:00
2018-09-16 19:08:13 +00:00
const smartshellInstance = new plugins.smartshell.Smartshell({
executor: 'bash'
2018-10-29 00:07:39 +00:00
});
2018-09-16 19:08:13 +00:00
2016-08-04 20:25:15 +00:00
// interfaces
2019-05-10 09:45:20 +00:00
import { IConfig } from './tsdocker.config';
2017-02-11 19:23:10 +00:00
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
*/
2019-05-10 09:45:20 +00:00
const 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
*/
2019-05-10 09:45:20 +00:00
const checkDocker = () => {
const done = plugins.smartpromise.defer();
ora.text('checking docker...');
2018-10-29 00:07:39 +00:00
2018-09-16 19:08:13 +00:00
if (smartshellInstance.exec('which docker')) {
2019-05-10 09:45:20 +00:00
logger.log('ok', 'Docker found!');
done.resolve();
2017-02-11 19:23:10 +00:00
} else {
done.reject(new Error('docker not found on this machine'));
2017-02-11 19:23:10 +00:00
}
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
*/
2019-05-10 09:45:20 +00:00
const buildDockerFile = () => {
const done = plugins.smartpromise.defer();
ora.text('building Dockerfile...');
const dockerfile: string = snippets.dockerfileSnippet({
2017-02-11 19:23:10 +00:00
baseImage: config.baseImage,
command: config.command
});
2019-05-10 09:45:20 +00:00
logger.log('info', `Base image is: ${config.baseImage}`);
logger.log('info', `Command is: ${config.command}`);
plugins.smartfile.memory.toFsSync(dockerfile, plugins.path.join(paths.cwd, 'npmdocker'));
2019-05-10 09:45:20 +00:00
logger.log('ok', 'Dockerfile created!');
ora.stop();
done.resolve();
return done.promise;
};
2016-07-18 22:37:13 +00:00
/**
* builds the Dockerimage from the built Dockerfile
*/
2019-05-10 09:45:20 +00:00
const buildDockerImage = async () => {
logger.log('info', 'pulling latest base image from registry...');
2018-09-16 19:08:13 +00:00
await smartshellInstance.exec(`docker pull ${config.baseImage}`);
2019-05-10 09:45:20 +00:00
ora.text('building Dockerimage...');
const execResult = await smartshellInstance.execSilent(
2017-10-13 14:44:25 +00:00
`docker build -f npmdocker -t ${dockerData.imageTag} ${paths.cwd}`
);
2017-10-13 14:44:25 +00:00
if (execResult.exitCode !== 0) {
console.log(execResult.stdout);
process.exit(1);
2017-10-13 14:44:25 +00:00
}
2019-05-10 09:45:20 +00:00
logger.log('ok', 'Dockerimage built!');
};
2016-07-18 22:37:13 +00:00
2018-10-29 00:07:39 +00:00
const 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
*/
2018-10-29 00:07:39 +00:00
const buildDockerEnvString = async () => {
2019-05-10 09:45:20 +00:00
for (const key of Object.keys(config.keyValueObject)) {
const envString = (dockerData.dockerEnvString =
dockerData.dockerEnvString + `-e ${key}=${config.keyValueObject[key]} `);
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
*/
2018-10-29 00:07:39 +00:00
const 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
*/
2019-05-10 09:45:20 +00:00
const runDockerImage = async () => {
const done = plugins.smartpromise.defer();
ora.text('starting Container...');
ora.stop();
logger.log('info', 'now running Dockerimage');
2018-09-16 19:08:13 +00:00
config.exitCode = (await smartshellInstance.exec(
`docker run ${dockerData.dockerProjectMountString} ${dockerData.dockerSockString} ${
dockerData.dockerEnvString
} --name ${dockerData.containerName} ${dockerData.imageTag}`
)).exitCode;
};
2016-07-18 22:37:13 +00:00
2017-02-11 19:23:10 +00:00
/**
* cleans up: deletes the test container
*/
2019-05-10 09:45:20 +00:00
const deleteDockerContainer = async () => {
2018-09-16 19:08:13 +00:00
await smartshellInstance.execSilent(`docker rm -f ${dockerData.containerName}`);
};
2016-07-18 22:37:13 +00:00
2017-02-11 19:23:10 +00:00
/**
* cleans up deletes the test image
*/
2019-05-10 09:45:20 +00:00
const deleteDockerImage = async () => {
2018-09-16 19:08:13 +00:00
await smartshellInstance.execSilent(`docker rmi ${dockerData.imageTag}`).then(async response => {
2017-04-02 12:48:23 +00:00
if (response.exitCode !== 0) {
console.log(response.stdout);
2017-04-02 12:48:23 +00:00
}
});
};
2016-07-18 22:37:13 +00:00
2019-05-10 09:45:20 +00:00
const preClean = async () => {
2017-03-28 23:01:37 +00:00
await deleteDockerImage()
2017-02-11 19:23:10 +00:00
.then(deleteDockerContainer)
2017-03-28 23:01:37 +00:00
.then(async () => {
2019-05-10 09:45:20 +00:00
logger.log('ok', 'ensured clean Docker environment!');
});
};
2019-05-10 09:45:20 +00:00
const postClean = async () => {
2017-03-28 23:01:37 +00:00
await deleteDockerContainer()
2017-02-11 19:23:10 +00:00
.then(deleteDockerImage)
2017-03-28 23:01:37 +00:00
.then(async () => {
2019-05-10 09:45:20 +00:00
logger.log('ok', 'cleaned up!');
});
plugins.smartfile.fs.removeSync(paths.npmdockerFile);
};
2017-03-28 23:01:37 +00:00
export let run = async (configArg: IConfig): Promise<IConfig> => {
config = configArg;
2019-05-10 09:45:20 +00:00
const 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)
.catch(err => {
console.log(err);
});
return config;
};