2017-05-18 20:40:09 +00:00
|
|
|
import * as plugins from './mod.plugins'
|
|
|
|
import * as paths from '../npmci.paths'
|
|
|
|
import * as NpmciEnv from '../npmci.env'
|
|
|
|
import { bash } from '../npmci.bash'
|
2016-06-06 18:30:06 +00:00
|
|
|
|
2017-07-27 11:15:39 +00:00
|
|
|
let modArgvArg // will be set through the build command
|
|
|
|
|
2016-09-04 14:05:47 +00:00
|
|
|
/**
|
|
|
|
* builds a cwd of Dockerfiles by triggering a promisechain
|
|
|
|
*/
|
2017-07-27 11:15:39 +00:00
|
|
|
export let build = async (argvArg: any) => {
|
|
|
|
modArgvArg = argvArg
|
2017-05-15 13:36:09 +00:00
|
|
|
plugins.beautylog.log('now building Dockerfiles...')
|
2017-07-27 12:20:56 +00:00
|
|
|
await readDockerfiles(argvArg)
|
2017-03-07 17:07:03 +00:00
|
|
|
.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-07-27 12:20:56 +00:00
|
|
|
export let readDockerfiles = async (argvArg): Promise<Dockerfile[]> => {
|
|
|
|
modArgvArg = argvArg
|
2017-05-15 13:36:09 +00:00
|
|
|
let fileTree = await plugins.smartfile.fs.listFileTree(paths.cwd, 'Dockerfile*')
|
2017-03-07 17:07:03 +00:00
|
|
|
|
|
|
|
// create the Dockerfile array
|
|
|
|
let readDockerfilesArray: Dockerfile[] = []
|
2017-05-15 13:36:09 +00:00
|
|
|
plugins.beautylog.info(`found ${fileTree.length} Dockerfiles:`)
|
|
|
|
console.log(fileTree)
|
2017-03-07 17:07:03 +00:00
|
|
|
for (let dockerfilePath of fileTree) {
|
|
|
|
let myDockerfile = new Dockerfile({
|
2017-05-15 14:35:16 +00:00
|
|
|
filePath: dockerfilePath,
|
2017-03-07 17:07:03 +00:00
|
|
|
read: true
|
|
|
|
})
|
|
|
|
readDockerfilesArray.push(myDockerfile)
|
|
|
|
}
|
|
|
|
|
|
|
|
return readDockerfilesArray
|
|
|
|
|
2016-06-05 00:17:55 +00:00
|
|
|
}
|
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[]>()
|
2017-05-15 16:10:24 +00:00
|
|
|
plugins.beautylog.info('sorting Dockerfiles:')
|
2017-03-07 17:07:03 +00:00
|
|
|
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)
|
2017-05-15 16:10:24 +00:00
|
|
|
}
|
2017-03-07 17:07:03 +00:00
|
|
|
if (cleanTagsOriginal.indexOf(dockerfileArg.baseImage) !== -1) {
|
|
|
|
dockerfileArg.localBaseImageDependent = true
|
2017-05-15 16:10:24 +00:00
|
|
|
}
|
2017-03-07 17:07:03 +00:00
|
|
|
})
|
|
|
|
if (sortableArrayArg.length === sortedArray.length) {
|
2017-05-15 16:10:24 +00:00
|
|
|
let counter = 1
|
|
|
|
for (let dockerfile of sortedArray) {
|
|
|
|
plugins.beautylog.log(`tag ${counter}: -> ${dockerfile.cleanTag}`)
|
|
|
|
counter++
|
|
|
|
}
|
2017-03-07 17:07:03 +00:00
|
|
|
done.resolve(sortedArray)
|
|
|
|
} else if (sorterFunctionCounter < 10) {
|
|
|
|
sorterFunctionCounter++
|
|
|
|
sorterFunction()
|
2017-05-15 16:10:24 +00:00
|
|
|
}
|
2017-03-07 17:07:03 +00:00
|
|
|
}
|
|
|
|
sorterFunction()
|
|
|
|
return done.promise
|
2016-11-24 22:21:40 +00:00
|
|
|
}
|
2016-06-05 12:27:56 +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
|
|
|
|
}
|
|
|
|
})
|
2017-07-27 12:20:56 +00:00
|
|
|
}
|
2017-03-07 17:07:03 +00:00
|
|
|
})
|
|
|
|
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-07-27 11:15:39 +00:00
|
|
|
let stageArg = (function () {
|
|
|
|
if (modArgvArg._ && modArgvArg._.length >= 3) {
|
|
|
|
return modArgvArg._[2]
|
|
|
|
} else {
|
|
|
|
return NpmciEnv.buildStage
|
|
|
|
}
|
|
|
|
})()
|
2017-03-11 01:27:48 +00:00
|
|
|
for (let dockerfileArg of sortedArrayArg) {
|
2017-07-27 11:15:39 +00:00
|
|
|
await dockerfileArg.push(stageArg)
|
2017-03-11 01:27:48 +00:00
|
|
|
}
|
2017-03-07 17:07:03 +00:00
|
|
|
return sortedArrayArg
|
2016-06-05 03:16:14 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2016-06-05 00:17:55 +00:00
|
|
|
export class Dockerfile {
|
2017-03-07 17:07:03 +00:00
|
|
|
filePath: string
|
|
|
|
repo: string
|
|
|
|
version: string
|
|
|
|
cleanTag: string
|
|
|
|
buildTag: string
|
2017-04-02 20:56:40 +00:00
|
|
|
gitlabTestTag: string
|
|
|
|
gitlabReleaseTag: string
|
2017-03-07 17:07:03 +00:00
|
|
|
releaseTag: string
|
|
|
|
containerName: string
|
|
|
|
content: string
|
|
|
|
baseImage: string
|
|
|
|
localBaseImageDependent: boolean
|
|
|
|
localBaseDockerfile: Dockerfile
|
2017-07-27 11:15:39 +00:00
|
|
|
constructor(options: { filePath?: string, fileContents?: string | Buffer, read?: boolean }) {
|
2017-03-07 17:07:03 +00:00
|
|
|
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
|
2017-04-02 20:56:40 +00:00
|
|
|
this.gitlabTestTag = dockerTag('registry.gitlab.com', this.repo, this.version, 'test')
|
|
|
|
this.gitlabReleaseTag = dockerTag('registry.gitlab.com', this.repo, this.version)
|
2017-03-07 17:07:03 +00:00
|
|
|
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))
|
2017-07-27 12:20:56 +00:00
|
|
|
}
|
2017-03-07 17:07:03 +00:00
|
|
|
this.baseImage = dockerBaseImage(this.content)
|
|
|
|
this.localBaseImageDependent = false
|
2017-07-27 12:20:56 +00:00
|
|
|
}
|
2016-09-04 14:05:47 +00:00
|
|
|
|
2017-03-07 17:07:03 +00:00
|
|
|
/**
|
|
|
|
* builds the Dockerfile
|
|
|
|
*/
|
2017-05-15 13:54:09 +00:00
|
|
|
async build () {
|
2017-03-07 17:07:03 +00:00
|
|
|
plugins.beautylog.info('now building Dockerfile for ' + this.cleanTag)
|
2017-05-15 13:54:09 +00:00
|
|
|
let buildCommand = `docker build -t ${this.buildTag} -f ${this.filePath} .`
|
2017-05-15 14:07:05 +00:00
|
|
|
await bash(buildCommand)
|
2017-03-07 17:07:03 +00:00
|
|
|
NpmciEnv.dockerFilesBuilt.push(this)
|
2017-03-11 01:27:48 +00:00
|
|
|
return
|
2017-07-27 12:20:56 +00:00
|
|
|
}
|
2016-09-04 14:05:47 +00:00
|
|
|
|
2017-03-07 17:07:03 +00:00
|
|
|
/**
|
|
|
|
* pushes the Dockerfile to a registry
|
|
|
|
*/
|
2017-07-27 11:15:39 +00:00
|
|
|
async push (stageArg) {
|
2017-03-07 17:07:03 +00:00
|
|
|
switch (stageArg) {
|
|
|
|
case 'release':
|
2017-05-15 14:07:05 +00:00
|
|
|
await bash(`docker tag ${this.buildTag} ${this.releaseTag}`)
|
|
|
|
await bash(`docker push ${this.releaseTag}`)
|
2017-08-26 07:40:40 +00:00
|
|
|
await bash(`docker tag ${this.buildTag} ${this.gitlabReleaseTag}`)
|
|
|
|
await bash(`docker push ${this.gitlabReleaseTag}`)
|
2017-03-07 17:07:03 +00:00
|
|
|
break
|
|
|
|
case 'test':
|
|
|
|
default:
|
2017-05-15 14:07:05 +00:00
|
|
|
await bash(`docker tag ${this.buildTag} ${this.gitlabTestTag}`)
|
|
|
|
await bash(`docker push ${this.gitlabTestTag}`)
|
2017-03-07 17:07:03 +00:00
|
|
|
break
|
|
|
|
}
|
2017-07-27 12:20:56 +00:00
|
|
|
}
|
2016-09-04 14:05:47 +00:00
|
|
|
|
2017-03-07 17:07:03 +00:00
|
|
|
/**
|
|
|
|
* pulls the Dockerfile from a registry
|
|
|
|
*/
|
2017-07-27 11:15:39 +00:00
|
|
|
async pull (registryArg: string) {
|
2017-04-02 20:56:40 +00:00
|
|
|
let pullTag = this.gitlabTestTag
|
2017-05-15 14:07:05 +00:00
|
|
|
await bash('docker pull ' + pullTag)
|
|
|
|
await bash('docker tag ' + pullTag + ' ' + this.buildTag)
|
2017-07-27 12:20:56 +00:00
|
|
|
}
|
2016-09-04 14:05:47 +00:00
|
|
|
|
2017-03-07 17:07:03 +00:00
|
|
|
/**
|
|
|
|
* tests the Dockerfile;
|
|
|
|
*/
|
2017-07-27 11:15:39 +00:00
|
|
|
async test () {
|
2017-03-07 17:07:03 +00:00
|
|
|
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-05-15 14:07:05 +00:00
|
|
|
await bash('docker run --name npmci_test_container ' + this.buildTag + ' 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 npmci_test_image sh /npmci_test/test.sh')
|
|
|
|
await bash('docker rm npmci_test_container')
|
|
|
|
await bash('docker rmi --force npmci_test_image')
|
2017-03-07 17:07:03 +00:00
|
|
|
} else {
|
|
|
|
plugins.beautylog.warn('skipping tests for ' + this.cleanTag + ' because no testfile was found!')
|
|
|
|
}
|
2017-07-27 12:20:56 +00:00
|
|
|
}
|
2016-09-04 14:05:47 +00:00
|
|
|
|
2017-03-07 17:07:03 +00:00
|
|
|
/**
|
|
|
|
* gets the id of a Dockerfile
|
|
|
|
*/
|
2017-07-27 11:15:39 +00:00
|
|
|
async getId () {
|
2017-05-15 14:07:05 +00:00
|
|
|
let containerId = await bash('docker inspect --type=image --format=\"{{.Id}}\" ' + this.buildTag)
|
2017-03-07 17:07:03 +00:00
|
|
|
return containerId
|
2017-07-27 12:20:56 +00:00
|
|
|
}
|
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) {
|
2017-07-27 11:15:39 +00:00
|
|
|
versionString = regexResultArray[1]
|
2017-03-07 17:07:03 +00:00
|
|
|
} else {
|
|
|
|
versionString = 'latest'
|
|
|
|
}
|
|
|
|
return versionString
|
2016-06-04 23:31:21 +00:00
|
|
|
}
|
|
|
|
|
2016-09-04 14:05:47 +00:00
|
|
|
/**
|
2017-07-27 12:20:56 +00:00
|
|
|
* returns the docker base image for a Dockerfile
|
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)
|
2017-07-27 11:15:39 +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
|
|
|
/**
|
2017-07-27 12:20:56 +00:00
|
|
|
* returns the docker tag
|
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
|
2017-07-27 12:20:56 +00:00
|
|
|
}
|
2017-03-07 17:07:03 +00:00
|
|
|
tagString = registry + '/' + repo + ':' + version
|
|
|
|
return tagString
|
2016-11-24 22:21:40 +00:00
|
|
|
}
|
2016-06-05 12:27:56 +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
|
|
|
}
|