2018-04-04 20:25:13 +00:00
|
|
|
import * as plugins from './mod.plugins';
|
|
|
|
import * as NpmciEnv from '../npmci.env';
|
|
|
|
import { bash } from '../npmci.bash';
|
|
|
|
import * as paths from '../npmci.paths';
|
2017-08-27 23:03:59 +00:00
|
|
|
|
2018-04-04 20:25:13 +00:00
|
|
|
import { DockerRegistry } from './mod.classes.dockerregistry';
|
|
|
|
import * as helpers from './mod.helpers';
|
2017-08-27 23:03:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* class Dockerfile represents a Dockerfile on disk in npmci
|
|
|
|
*/
|
|
|
|
export class Dockerfile {
|
2018-04-04 20:25:13 +00:00
|
|
|
filePath: string;
|
|
|
|
repo: string;
|
|
|
|
version: string;
|
|
|
|
cleanTag: string;
|
|
|
|
buildTag: string;
|
|
|
|
containerName: string;
|
|
|
|
content: string;
|
|
|
|
baseImage: string;
|
|
|
|
localBaseImageDependent: boolean;
|
|
|
|
localBaseDockerfile: Dockerfile;
|
|
|
|
constructor(options: { filePath?: string; fileContents?: string | Buffer; read?: boolean }) {
|
|
|
|
this.filePath = options.filePath;
|
|
|
|
this.repo = NpmciEnv.repo.user + '/' + NpmciEnv.repo.repo;
|
|
|
|
this.version = helpers.dockerFileVersion(plugins.path.parse(options.filePath).base);
|
|
|
|
this.cleanTag = this.repo + ':' + this.version;
|
|
|
|
this.buildTag = this.cleanTag;
|
2017-08-27 23:03:59 +00:00
|
|
|
|
2018-04-04 20:25:13 +00:00
|
|
|
this.containerName = 'dockerfile-' + this.version;
|
2017-08-27 23:03:59 +00:00
|
|
|
if (options.filePath && options.read) {
|
2018-04-04 20:25:13 +00:00
|
|
|
this.content = plugins.smartfile.fs.toStringSync(plugins.path.resolve(options.filePath));
|
2017-08-27 23:03:59 +00:00
|
|
|
}
|
2018-04-04 20:25:13 +00:00
|
|
|
this.baseImage = helpers.dockerBaseImage(this.content);
|
|
|
|
this.localBaseImageDependent = false;
|
2017-08-27 23:03:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* builds the Dockerfile
|
|
|
|
*/
|
2018-04-04 20:25:13 +00:00
|
|
|
async build() {
|
|
|
|
plugins.beautylog.info('now building Dockerfile for ' + this.cleanTag);
|
|
|
|
let buildArgsString = await helpers.getDockerBuildArgs();
|
|
|
|
let buildCommand = `docker build -t ${this.buildTag} -f ${this.filePath} ${buildArgsString} .`;
|
|
|
|
await bash(buildCommand);
|
|
|
|
return;
|
2017-08-27 23:03:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* pushes the Dockerfile to a registry
|
|
|
|
*/
|
2018-04-04 20:25:13 +00:00
|
|
|
async push(dockerRegistryArg: DockerRegistry, versionSuffix: string = null) {
|
|
|
|
let pushTag = helpers.getDockerTagString(
|
|
|
|
dockerRegistryArg.registryUrl,
|
|
|
|
this.repo,
|
|
|
|
this.version,
|
|
|
|
versionSuffix
|
|
|
|
);
|
|
|
|
await bash(`docker tag ${this.buildTag} ${pushTag}`);
|
|
|
|
await bash(`docker push ${pushTag}`);
|
2017-08-27 23:03:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* pulls the Dockerfile from a registry
|
|
|
|
*/
|
2018-04-04 20:25:13 +00:00
|
|
|
async pull(registryArg: DockerRegistry, versionSuffixArg: string = null) {
|
|
|
|
let pullTag = helpers.getDockerTagString(
|
|
|
|
registryArg.registryUrl,
|
|
|
|
this.repo,
|
|
|
|
this.version,
|
|
|
|
versionSuffixArg
|
|
|
|
);
|
|
|
|
await bash(`docker pull ${pullTag}`);
|
|
|
|
await bash(`docker tag ${pullTag} ${this.buildTag}`);
|
2017-08-27 23:03:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* tests the Dockerfile;
|
|
|
|
*/
|
2018-04-04 20:25:13 +00:00
|
|
|
async test() {
|
|
|
|
let testFile: string = plugins.path.join(paths.NpmciTestDir, 'test_' + this.version + '.sh');
|
|
|
|
let testFileExists: boolean = plugins.smartfile.fs.fileExistsSync(testFile);
|
2017-08-27 23:03:59 +00:00
|
|
|
if (testFileExists) {
|
|
|
|
// run tests
|
2018-04-04 20:25:13 +00:00
|
|
|
await bash(
|
|
|
|
`docker run --name npmci_test_container --entrypoint="bash" ${
|
|
|
|
this.buildTag
|
|
|
|
} -c "mkdir /npmci_test"`
|
|
|
|
);
|
|
|
|
await bash(`docker cp ${testFile} npmci_test_container:/npmci_test/test.sh`);
|
|
|
|
await bash(`docker commit npmci_test_container npmci_test_image`);
|
|
|
|
await bash(`docker run --entrypoint="bash" npmci_test_image -x /npmci_test/test.sh`);
|
|
|
|
await bash(`docker rm npmci_test_container`);
|
|
|
|
await bash(`docker rmi --force npmci_test_image`);
|
2017-08-27 23:03:59 +00:00
|
|
|
} else {
|
2018-04-04 20:25:13 +00:00
|
|
|
plugins.beautylog.warn(
|
|
|
|
'skipping tests for ' + this.cleanTag + ' because no testfile was found!'
|
|
|
|
);
|
2017-08-27 23:03:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gets the id of a Dockerfile
|
|
|
|
*/
|
2018-04-04 20:25:13 +00:00
|
|
|
async getId() {
|
|
|
|
let containerId = await bash('docker inspect --type=image --format="{{.Id}}" ' + this.buildTag);
|
|
|
|
return containerId;
|
2017-08-27 23:03:59 +00:00
|
|
|
}
|
|
|
|
}
|