npmci/ts/npmci.build.docker.ts

276 lines
8.3 KiB
TypeScript
Raw Permalink 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'
2017-03-07 17:07:03 +00:00
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
*/
2017-03-07 17:07:03 +00:00
export let build = async () => {
await readDockerfiles()
.then(sortDockerfiles)
.then(mapDockerfiles)
.then(buildDockerfiles)
.then(pushDockerfiles)
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[]>
*/
2017-03-07 17:07:03 +00:00
export let readDockerfiles = async (): Promise<Dockerfile[]> => {
let fileTree = await plugins.smartfile.fs.listFileTree(paths.cwd, './Dockerfile*')
// create the Dockerfile array
let readDockerfilesArray: Dockerfile[] = []
for (let dockerfilePath of fileTree) {
let myDockerfile = new Dockerfile({
filePath: dockerfilePath,
read: true
})
readDockerfilesArray.push(myDockerfile)
}
return readDockerfilesArray
}
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[]>
*/
2017-03-11 00:10:37 +00:00
export let sortDockerfiles = (sortableArrayArg: Dockerfile[]): Promise<Dockerfile[]> => {
2017-03-07 17:07:03 +00:00
let done = plugins.q.defer<Dockerfile[]>()
let sortedArray: Dockerfile[] = []
let cleanTagsOriginal = cleanTagsArrayFunction(sortableArrayArg, sortedArray)
let sorterFunctionCounter: number = 0
let sorterFunction = function () {
sortableArrayArg.forEach((dockerfileArg) => {
let cleanTags = cleanTagsArrayFunction(sortableArrayArg, sortedArray)
if (cleanTags.indexOf(dockerfileArg.baseImage) === -1 && sortedArray.indexOf(dockerfileArg) === -1) {
sortedArray.push(dockerfileArg)
};
if (cleanTagsOriginal.indexOf(dockerfileArg.baseImage) !== -1) {
dockerfileArg.localBaseImageDependent = true
};
})
if (sortableArrayArg.length === sortedArray.length) {
done.resolve(sortedArray)
} else if (sorterFunctionCounter < 10) {
sorterFunctionCounter++
sorterFunction()
};
}
sorterFunction()
return done.promise
2016-11-24 22:21:40 +00:00
}
2016-09-04 14:05:47 +00:00
/**
* maps local Dockerfiles dependencies to the correspoding Dockerfile class instances
*/
2017-03-11 00:10:37 +00:00
export let mapDockerfiles = async (sortedArray: Dockerfile[]): Promise<Dockerfile[]> => {
2017-03-07 17:07:03 +00:00
sortedArray.forEach((dockerfileArg) => {
if (dockerfileArg.localBaseImageDependent) {
sortedArray.forEach((dockfile2: Dockerfile) => {
if (dockfile2.cleanTag === dockerfileArg.baseImage) {
dockerfileArg.localBaseDockerfile = dockfile2
}
})
};
})
return sortedArray
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
*/
2017-03-07 17:07:03 +00:00
export let buildDockerfiles = async (sortedArrayArg: Dockerfile[]) => {
2017-03-11 01:27:48 +00:00
for (let dockerfileArg of sortedArrayArg) {
2017-03-07 17:07:03 +00:00
await dockerfileArg.build()
2017-03-11 01:27:48 +00:00
}
2017-03-07 17:07:03 +00:00
return sortedArrayArg
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
*/
2017-03-07 17:07:03 +00:00
export let pushDockerfiles = async (sortedArrayArg: Dockerfile[]) => {
2017-03-11 01:27:48 +00:00
for (let dockerfileArg of sortedArrayArg) {
2017-03-07 17:07:03 +00:00
await dockerfileArg.push(NpmciEnv.buildStage)
2017-03-11 01:27:48 +00:00
}
2017-03-07 17:07:03 +00:00
return sortedArrayArg
}
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.
*/
2017-03-07 17:07:03 +00:00
export let pullDockerfileImages = async (sortableArrayArg: Dockerfile[], registryArg = 'registry.gitlab.com') => {
2017-03-11 01:27:48 +00:00
for (let dockerfileArg of sortableArrayArg) {
2017-03-07 17:07:03 +00:00
await dockerfileArg.pull(registryArg)
2017-03-11 01:27:48 +00:00
}
2017-03-07 17:07:03 +00:00
return sortableArrayArg
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
*/
2017-03-07 17:07:03 +00:00
export let testDockerfiles = async (sortedArrayArg: Dockerfile[]) => {
2017-03-11 01:27:48 +00:00
for (let dockerfileArg of sortedArrayArg) {
2017-03-07 17:07:03 +00:00
await dockerfileArg.test()
2017-03-11 01:27:48 +00:00
}
2017-03-07 17:07:03 +00:00
return sortedArrayArg
2016-11-24 22:21:40 +00:00
}
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 {
2017-03-07 17:07:03 +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-04 23:31:21 +00:00
};
2017-03-07 17:07:03 +00:00
this.baseImage = dockerBaseImage(this.content)
this.localBaseImageDependent = false
};
2016-09-04 14:05:47 +00:00
2017-03-07 17:07:03 +00:00
/**
* builds the Dockerfile
*/
async build() {
plugins.beautylog.info('now building Dockerfile for ' + this.cleanTag)
await bashBare('docker build -t ' + this.buildTag + ' -f ' + this.filePath + ' .')
NpmciEnv.dockerFilesBuilt.push(this)
2017-03-11 01:27:48 +00:00
return
2017-03-07 17:07:03 +00:00
};
2016-09-04 14:05:47 +00:00
2017-03-07 17:07:03 +00:00
/**
* pushes the Dockerfile to a registry
*/
async push(stageArg) {
let pushTag
switch (stageArg) {
case 'release':
pushTag = this.releaseTag
break
case 'test':
default:
pushTag = this.testTag
break
}
await bashBare('docker tag ' + this.buildTag + ' ' + pushTag)
await bashBare('docker push ' + pushTag)
};
2016-09-04 14:05:47 +00:00
2017-03-07 17:07:03 +00:00
/**
* pulls the Dockerfile from a registry
*/
async pull(registryArg: string) {
let pullTag = this.testTag
await bashBare('docker pull ' + pullTag)
await bashBare('docker tag ' + pullTag + ' ' + this.buildTag)
};
2016-09-04 14:05:47 +00:00
2017-03-07 17:07:03 +00:00
/**
* 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
await bashBare('docker run --name npmci_test_container ' + this.buildTag + ' mkdir /npmci_test')
await bashBare('docker cp ' + testFile + ' npmci_test_container:/npmci_test/test.sh')
await bashBare('docker commit npmci_test_container npmci_test_image')
await bashBare('docker run npmci_test_image sh /npmci_test/test.sh')
await bashBare('docker rm npmci_test_container')
await bashBare('docker rmi --force npmci_test_image')
} else {
plugins.beautylog.warn('skipping tests for ' + this.cleanTag + ' because no testfile was found!')
}
};
2016-09-04 14:05:47 +00:00
2017-03-07 17:07:03 +00:00
/**
* gets the id of a Dockerfile
*/
async getId() {
let containerId = await bashBare('docker inspect --type=image --format=\"{{.Id}}\" ' + this.buildTag)
return containerId
};
2016-06-04 23:31:21 +00:00
}
2016-09-04 14:05:47 +00:00
/**
2017-03-07 17:07:03 +00:00
* returns a version for a docker file
* @execution SYNC
2016-09-04 14:05:47 +00:00
*/
2017-03-07 17:07:03 +00:00
export let dockerFileVersion = (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]
} else {
versionString = 'latest'
}
return versionString
2016-06-04 23:31:21 +00:00
}
2016-09-04 14:05:47 +00:00
/**
*
*/
2017-03-07 17:07:03 +00:00
export let dockerBaseImage = function (dockerfileContentArg: string) {
let baseImageRegex = /FROM\s([a-zA-z0-9\/\-\:]*)\n?/
let regexResultArray = baseImageRegex.exec(dockerfileContentArg)
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
/**
*
*/
2017-03-07 17:07:03 +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
};
tagString = registry + '/' + repo + ':' + version
return tagString
2016-11-24 22:21:40 +00:00
}
2016-09-04 14:05:47 +00:00
/**
*
*/
2017-03-07 17:07:03 +00:00
export let cleanTagsArrayFunction = function (dockerfileArrayArg: Dockerfile[], trackingArrayArg: Dockerfile[]): string[] {
let cleanTagsArray: string[] = []
dockerfileArrayArg.forEach(function (dockerfileArg) {
if (trackingArrayArg.indexOf(dockerfileArg) === -1) {
cleanTagsArray.push(dockerfileArg.cleanTag)
}
})
return cleanTagsArray
2016-11-24 22:21:40 +00:00
}