npmci/ts/mod_docker/mod.classes.dockerfile.ts

94 lines
3.3 KiB
TypeScript
Raw Normal View History

2017-08-27 23:03:59 +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'
import { DockerRegistry } from './mod.classes.dockerregistry'
import * as helpers from './mod.helpers'
/**
* class Dockerfile represents a Dockerfile on disk in npmci
*/
export class Dockerfile {
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
this.containerName = 'dockerfile-' + this.version
if (options.filePath && options.read) {
this.content = plugins.smartfile.fs.toStringSync(plugins.path.resolve(options.filePath))
}
this.baseImage = helpers.dockerBaseImage(this.content)
this.localBaseImageDependent = false
}
/**
* builds the Dockerfile
*/
async build () {
plugins.beautylog.info('now building Dockerfile for ' + this.cleanTag)
2017-08-28 17:11:24 +00:00
let buildArgsString = await helpers.getDockerBuildArgs()
let buildCommand = `docker build -t ${this.buildTag} -f ${this.filePath} ${buildArgsString} .`
2017-08-27 23:03:59 +00:00
await bash(buildCommand)
return
}
/**
* pushes the Dockerfile to a registry
*/
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}`)
}
/**
* pulls the Dockerfile from a registry
*/
async pull (registryArg: DockerRegistry, versionSuffixArg: string = null) {
let pullTag = helpers.getDockerTagString(registryArg.registryUrl,this.repo, this.version, versionSuffixArg)
2017-08-28 00:43:43 +00:00
await bash(`docker pull ${pullTag}`)
2017-08-27 23:03:59 +00:00
await bash(`docker tag ${pullTag} ${this.buildTag}`)
}
/**
* tests the Dockerfile;
*/
async test () {
let testFile: string = plugins.path.join(paths.NpmciTestDir, 'test_' + this.version + '.sh')
let testFileExists: boolean = plugins.smartfile.fs.fileExistsSync(testFile)
if (testFileExists) {
// run tests
2017-10-11 16:14:05 +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`)
2017-10-11 16:15:22 +00:00
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 {
plugins.beautylog.warn('skipping tests for ' + this.cleanTag + ' because no testfile was found!')
}
}
/**
* gets the id of a Dockerfile
*/
async getId () {
let containerId = await bash('docker inspect --type=image --format=\"{{.Id}}\" ' + this.buildTag)
return containerId
}
}