npmci/ts/npmci.build.docker.ts

294 lines
9.4 KiB
TypeScript
Raw Normal View History

2016-11-24 22:21:40 +00:00
import * as plugins from './npmci.plugins'
import * as paths from './npmci.paths'
import * as NpmciEnv from './npmci.env'
import {bashBare} from './npmci.bash'
2016-06-06 18:30:06 +00:00
2016-09-04 14:05:47 +00:00
/**
* builds a cwd of Dockerfiles by triggering a promisechain
*/
2016-06-04 16:41:35 +00:00
export let build = function(){
2016-11-24 22:21:40 +00:00
let done = plugins.q.defer()
readDockerfiles()
.then(sortDockerfiles)
.then(mapDockerfiles)
2016-06-05 14:43:27 +00:00
.then(buildDockerfiles)
2016-06-05 15:17:15 +00:00
.then(pushDockerfiles)
2016-06-05 14:43:27 +00:00
.then(() => {
2016-11-24 22:21:40 +00:00
done.resolve()
})
return done.promise
2016-06-04 16:41:35 +00:00
}
2016-09-04 14:05:47 +00:00
/**
* creates instance of class Dockerfile for all Dockerfiles in cwd
* @returns Promise<Dockerfile[]>
*/
2016-11-24 22:21:40 +00:00
export let readDockerfiles = function(): plugins.q.Promise<Dockerfile[]>{
let done = plugins.q.defer<Dockerfile[]>()
let readDockerfilesArray: Dockerfile[] = []
plugins.gulp.src('./Dockerfile*')
2016-06-05 11:01:45 +00:00
.pipe(plugins.through2.obj(function(file,enc,cb){
let myDockerfile = new Dockerfile({
2016-11-24 22:21:40 +00:00
filePath: file.path,
read: true
})
readDockerfilesArray.push(myDockerfile)
cb(null,file)
2016-06-05 11:01:45 +00:00
},function(){
2016-11-24 22:21:40 +00:00
done.resolve(readDockerfilesArray)
}))
return done.promise
}
2016-06-04 23:22:04 +00:00
2016-09-04 14:05:47 +00:00
/**
* sorts Dockerfiles into a dependency chain
* @param sortableArrayArg an array of instances of class Dockerfile
* @returns Promise<Dockerfile[]>
*/
2016-11-24 22:21:40 +00:00
export let sortDockerfiles = function(sortableArrayArg: Dockerfile[]): plugins.q.Promise<Dockerfile[]>{
let done = plugins.q.defer<Dockerfile[]>()
let sortedArray: Dockerfile[] = []
let cleanTagsOriginal = cleanTagsArrayFunction(sortableArrayArg,sortedArray)
let sorterFunctionCounter: number = 0
2016-06-05 09:08:20 +00:00
let sorterFunction = function(){
2016-11-24 22:21:40 +00:00
sortableArrayArg.forEach((dockerfileArg) => {
let cleanTags = cleanTagsArrayFunction(sortableArrayArg,sortedArray)
if (cleanTags.indexOf(dockerfileArg.baseImage) === -1 && sortedArray.indexOf(dockerfileArg) === -1) {
sortedArray.push(dockerfileArg)
2016-06-07 04:05:13 +00:00
};
2016-11-24 22:21:40 +00:00
if (cleanTagsOriginal.indexOf(dockerfileArg.baseImage) !== -1) {
dockerfileArg.localBaseImageDependent = true
};
2016-11-24 22:21:40 +00:00
})
if (sortableArrayArg.length === sortedArray.length) {
done.resolve(sortedArray)
2016-06-05 11:50:45 +00:00
} else if (sorterFunctionCounter < 10) {
2016-11-24 22:21:40 +00:00
sorterFunctionCounter++
sorterFunction()
2016-06-05 09:08:20 +00:00
};
}
2016-11-24 22:21:40 +00:00
sorterFunction()
return done.promise
}
2016-09-04 14:05:47 +00:00
/**
* maps local Dockerfiles dependencies to the correspoding Dockerfile class instances
*/
2016-11-24 22:21:40 +00:00
export let mapDockerfiles = function(sortedArray: Dockerfile[]): plugins.q.Promise<Dockerfile[]>{
let done = plugins.q.defer<Dockerfile[]>()
sortedArray.forEach((dockerfileArg) => {
2016-11-24 22:21:40 +00:00
if (dockerfileArg.localBaseImageDependent) {
sortedArray.forEach((dockfile2: Dockerfile) => {
if (dockfile2.cleanTag === dockerfileArg.baseImage) {
dockerfileArg.localBaseDockerfile = dockfile2
}
})
};
2016-11-24 22:21:40 +00:00
})
done.resolve(sortedArray)
return done.promise
2016-06-05 04:20:05 +00:00
}
2016-09-04 14:05:47 +00:00
/**
* builds the correspoding real docker image for each Dockerfile class instance
*/
2016-11-24 22:21:40 +00:00
export let buildDockerfiles = (sortedArrayArg: Dockerfile[]) => {
let done = plugins.q.defer()
sortedArrayArg.forEach(function(dockerfileArg){
2016-11-24 22:21:40 +00:00
dockerfileArg.build()
2016-06-05 04:20:05 +00:00
})
2016-11-24 22:21:40 +00:00
done.resolve(sortedArrayArg)
return done.promise
2016-06-05 15:17:15 +00:00
}
2016-09-04 14:05:47 +00:00
/**
* pushes the real Dockerfile images to a Docker registry
*/
2016-11-24 22:21:40 +00:00
export let pushDockerfiles = function(sortedArrayArg: Dockerfile[]){
let done = plugins.q.defer()
2016-06-05 15:17:15 +00:00
sortedArrayArg.forEach(function(dockerfileArg){
2016-11-24 22:21:40 +00:00
dockerfileArg.push(NpmciEnv.buildStage)
})
done.resolve(sortedArrayArg)
return done.promise
}
2016-09-04 14:05:47 +00:00
/**
* pulls corresponding real Docker images for instances of Dockerfile from a registry.
* This is needed if building, testing, and publishing of Docker images is carried out in seperate CI stages.
*/
2016-11-24 22:21:40 +00:00
export let pullDockerfileImages = (sortableArrayArg: Dockerfile[],registryArg = 'registry.gitlab.com') => {
let done = plugins.q.defer()
2016-06-07 02:31:25 +00:00
sortableArrayArg.forEach((dockerfileArg) => {
2016-11-24 22:21:40 +00:00
dockerfileArg.pull(registryArg)
})
done.resolve(sortableArrayArg)
return done.promise
2016-06-07 01:57:43 +00:00
}
2016-09-04 14:05:47 +00:00
/**
* tests all Dockerfiles in by calling class Dockerfile.test();
* @param sortedArrayArg Dockerfile[] that contains all Dockerfiles in cwd
*/
2016-11-24 22:21:40 +00:00
export let testDockerfiles = (sortedArrayArg: Dockerfile[]) => {
let done = plugins.q.defer()
2016-06-07 01:57:43 +00:00
sortedArrayArg.forEach(function(dockerfileArg){
2016-11-24 22:21:40 +00:00
dockerfileArg.test()
})
done.resolve(sortedArrayArg)
return done.promise
}
2016-06-07 01:57:43 +00:00
2016-09-04 14:05:47 +00:00
/**
* class Dockerfile represents a Dockerfile on disk in npmci
*/
export class Dockerfile {
2016-11-24 22:21:40 +00:00
filePath: string
repo: string
version: string
cleanTag: string
buildTag: string
testTag: string
releaseTag: 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 = dockerFileVersion(plugins.path.parse(options.filePath).base)
this.cleanTag = this.repo + ':' + this.version
this.buildTag = this.cleanTag
this.testTag = dockerTag('registry.gitlab.com',this.repo,this.version,'test')
this.releaseTag = dockerTag(NpmciEnv.dockerRegistry,this.repo,this.version)
this.containerName = 'dockerfile-' + this.version
if (options.filePath && options.read) {
this.content = plugins.smartfile.fs.toStringSync(plugins.path.resolve(options.filePath))
2016-06-05 02:19:54 +00:00
};
2016-11-24 22:21:40 +00:00
this.baseImage = dockerBaseImage(this.content)
this.localBaseImageDependent = false
2016-06-04 23:31:21 +00:00
};
2016-09-04 14:05:47 +00:00
/**
* builds the Dockerfile
*/
2016-11-24 22:21:40 +00:00
build() {
let done = plugins.q.defer()
plugins.beautylog.info('now building Dockerfile for ' + this.cleanTag)
bashBare('docker build -t ' + this.buildTag + ' -f ' + this.filePath + ' .')
NpmciEnv.dockerFilesBuilt.push(this)
done.resolve()
return done.promise
2016-06-04 23:31:21 +00:00
};
2016-09-04 14:05:47 +00:00
/**
* pushes the Dockerfile to a registry
*/
2016-11-24 22:21:40 +00:00
push(stageArg) {
let done = plugins.q.defer()
let pushTag
switch (stageArg) {
case 'release':
pushTag = this.releaseTag
break
case 'test':
2016-06-07 20:27:10 +00:00
default:
2016-11-24 22:21:40 +00:00
pushTag = this.testTag
break
2016-06-07 20:27:10 +00:00
}
2016-11-24 22:21:40 +00:00
bashBare('docker tag ' + this.buildTag + ' ' + pushTag)
bashBare('docker push ' + pushTag)
done.resolve()
return done.promise
2016-09-04 14:05:47 +00:00
};
/**
* pulls the Dockerfile from a registry
*/
2016-11-24 22:21:40 +00:00
pull(registryArg: string) {
let pullTag = this.testTag
bashBare('docker pull ' + pullTag)
bashBare('docker tag ' + pullTag + ' ' + this.buildTag)
2016-06-07 01:57:43 +00:00
};
2016-09-04 14:05:47 +00:00
/**
* tests the Dockerfile;
*/
2016-11-24 22:21:40 +00:00
test() {
let testFile: string = plugins.path.join(paths.NpmciTestDir,'test_' + this.version + '.sh')
let testFileExists: boolean = plugins.smartfile.fs.fileExistsSync(testFile)
if (testFileExists) {
bashBare('docker run --name npmci_test_container ' + this.buildTag + ' mkdir /npmci_test')
bashBare('docker cp ' + testFile + ' npmci_test_container:/npmci_test/test.sh')
bashBare('docker commit npmci_test_container npmci_test_image')
bashBare('docker run npmci_test_image sh /npmci_test/test.sh')
bashBare('docker rm npmci_test_container')
bashBare('docker rmi --force npmci_test_image')
} else {
2016-11-24 22:21:40 +00:00
plugins.beautylog.warn('skipping tests for ' + this.cleanTag + ' because no testfile was found!')
}
2016-06-07 01:57:43 +00:00
};
2016-09-04 14:05:47 +00:00
/**
* gets the id of a Dockerfile
*/
2016-11-24 22:21:40 +00:00
getId() {
let containerId = bashBare('docker inspect --type=image --format=\"{{.Id}}\" ' + this.buildTag)
return containerId
2016-06-07 01:57:43 +00:00
};
2016-06-04 23:31:21 +00:00
}
2016-09-04 14:05:47 +00:00
/**
*
*/
2016-11-24 22:21:40 +00:00
export let dockerFileVersion = function(dockerfileNameArg: string): string{
let versionString: string
let versionRegex = /Dockerfile_([a-zA-Z0-9\.]*)$/
let regexResultArray = versionRegex.exec(dockerfileNameArg)
if (regexResultArray && regexResultArray.length === 2) {
versionString = regexResultArray[1]
2016-06-04 23:31:21 +00:00
} else {
2016-11-24 22:21:40 +00:00
versionString = 'latest'
2016-06-04 23:31:21 +00:00
}
2016-11-24 22:21:40 +00:00
return versionString
2016-06-04 23:31:21 +00:00
}
2016-09-04 14:05:47 +00:00
/**
*
*/
2016-11-24 22:21:40 +00:00
export let dockerBaseImage = function(dockerfileContentArg: string){
2016-06-05 11:01:45 +00:00
let baseImageRegex = /FROM\s([a-zA-z0-9\/\-\:]*)\n?/
2016-06-05 02:19:54 +00:00
let regexResultArray = baseImageRegex.exec(dockerfileContentArg)
2016-11-24 22:21:40 +00:00
return regexResultArray[1]
2016-06-04 23:31:21 +00:00
}
2016-06-04 23:22:04 +00:00
2016-09-04 14:05:47 +00:00
/**
*
*/
2016-11-24 22:21:40 +00:00
export let dockerTag = function(registryArg: string,repoArg: string,versionArg: string,suffixArg?: string): string{
let tagString: string
let registry = registryArg
let repo = repoArg
let version = versionArg
if (suffixArg) {
version = versionArg + '_' + suffixArg
2016-06-07 17:41:14 +00:00
};
2016-11-24 22:21:40 +00:00
tagString = registry + '/' + repo + ':' + version
return tagString
}
2016-09-04 14:05:47 +00:00
/**
*
*/
2016-11-24 22:21:40 +00:00
export let cleanTagsArrayFunction = function(dockerfileArrayArg: Dockerfile[],trackingArrayArg: Dockerfile[]): string[]{
let cleanTagsArray: string[] = []
dockerfileArrayArg.forEach(function(dockerfileArg){
2016-11-24 22:21:40 +00:00
if (trackingArrayArg.indexOf(dockerfileArg) === -1) {
cleanTagsArray.push(dockerfileArg.cleanTag)
}
2016-11-24 22:21:40 +00:00
})
return cleanTagsArray
}