|
|
|
@ -1,43 +1,38 @@
|
|
|
|
|
import * as plugins from './npmci.plugins'
|
|
|
|
|
import * as paths from './npmci.paths'
|
|
|
|
|
import * as NpmciEnv from './npmci.env'
|
|
|
|
|
import {bashBare} from './npmci.bash'
|
|
|
|
|
import { bashBare } from './npmci.bash'
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* builds a cwd of Dockerfiles by triggering a promisechain
|
|
|
|
|
*/
|
|
|
|
|
export let build = function(){
|
|
|
|
|
let done = plugins.q.defer()
|
|
|
|
|
readDockerfiles()
|
|
|
|
|
export let build = async () => {
|
|
|
|
|
await readDockerfiles()
|
|
|
|
|
.then(sortDockerfiles)
|
|
|
|
|
.then(mapDockerfiles)
|
|
|
|
|
.then(buildDockerfiles)
|
|
|
|
|
.then(pushDockerfiles)
|
|
|
|
|
.then(() => {
|
|
|
|
|
done.resolve()
|
|
|
|
|
})
|
|
|
|
|
return done.promise
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* creates instance of class Dockerfile for all Dockerfiles in cwd
|
|
|
|
|
* @returns Promise<Dockerfile[]>
|
|
|
|
|
*/
|
|
|
|
|
export let readDockerfiles = function(): plugins.q.Promise<Dockerfile[]>{
|
|
|
|
|
let done = plugins.q.defer<Dockerfile[]>()
|
|
|
|
|
export let readDockerfiles = async (): Promise<Dockerfile[]> => {
|
|
|
|
|
let fileTree = await plugins.smartfile.fs.listFileTree(paths.cwd, './Dockerfile*')
|
|
|
|
|
|
|
|
|
|
// create the Dockerfile array
|
|
|
|
|
let readDockerfilesArray: Dockerfile[] = []
|
|
|
|
|
plugins.gulp.src('./Dockerfile*')
|
|
|
|
|
.pipe(plugins.through2.obj(function(file,enc,cb){
|
|
|
|
|
for (let dockerfilePath of fileTree) {
|
|
|
|
|
let myDockerfile = new Dockerfile({
|
|
|
|
|
filePath: file.path,
|
|
|
|
|
filePath: dockerfilePath,
|
|
|
|
|
read: true
|
|
|
|
|
})
|
|
|
|
|
readDockerfilesArray.push(myDockerfile)
|
|
|
|
|
cb(null,file)
|
|
|
|
|
},function(){
|
|
|
|
|
done.resolve(readDockerfilesArray)
|
|
|
|
|
}))
|
|
|
|
|
return done.promise
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return readDockerfilesArray
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -45,14 +40,14 @@ export let readDockerfiles = function(): plugins.q.Promise<Dockerfile[]>{
|
|
|
|
|
* @param sortableArrayArg an array of instances of class Dockerfile
|
|
|
|
|
* @returns Promise<Dockerfile[]>
|
|
|
|
|
*/
|
|
|
|
|
export let sortDockerfiles = function(sortableArrayArg: Dockerfile[]): plugins.q.Promise<Dockerfile[]>{
|
|
|
|
|
export let sortDockerfiles = (sortableArrayArg: Dockerfile[]): plugins.q.Promise<Dockerfile[]> => {
|
|
|
|
|
let done = plugins.q.defer<Dockerfile[]>()
|
|
|
|
|
let sortedArray: Dockerfile[] = []
|
|
|
|
|
let cleanTagsOriginal = cleanTagsArrayFunction(sortableArrayArg,sortedArray)
|
|
|
|
|
let cleanTagsOriginal = cleanTagsArrayFunction(sortableArrayArg, sortedArray)
|
|
|
|
|
let sorterFunctionCounter: number = 0
|
|
|
|
|
let sorterFunction = function(){
|
|
|
|
|
let sorterFunction = function () {
|
|
|
|
|
sortableArrayArg.forEach((dockerfileArg) => {
|
|
|
|
|
let cleanTags = cleanTagsArrayFunction(sortableArrayArg,sortedArray)
|
|
|
|
|
let cleanTags = cleanTagsArrayFunction(sortableArrayArg, sortedArray)
|
|
|
|
|
if (cleanTags.indexOf(dockerfileArg.baseImage) === -1 && sortedArray.indexOf(dockerfileArg) === -1) {
|
|
|
|
|
sortedArray.push(dockerfileArg)
|
|
|
|
|
};
|
|
|
|
@ -74,8 +69,7 @@ export let sortDockerfiles = function(sortableArrayArg: Dockerfile[]): plugins.q
|
|
|
|
|
/**
|
|
|
|
|
* maps local Dockerfiles dependencies to the correspoding Dockerfile class instances
|
|
|
|
|
*/
|
|
|
|
|
export let mapDockerfiles = function(sortedArray: Dockerfile[]): plugins.q.Promise<Dockerfile[]>{
|
|
|
|
|
let done = plugins.q.defer<Dockerfile[]>()
|
|
|
|
|
export let mapDockerfiles = async (sortedArray: Dockerfile[]): plugins.q.Promise<Dockerfile[]> => {
|
|
|
|
|
sortedArray.forEach((dockerfileArg) => {
|
|
|
|
|
if (dockerfileArg.localBaseImageDependent) {
|
|
|
|
|
sortedArray.forEach((dockfile2: Dockerfile) => {
|
|
|
|
@ -85,58 +79,49 @@ export let mapDockerfiles = function(sortedArray: Dockerfile[]): plugins.q.Promi
|
|
|
|
|
})
|
|
|
|
|
};
|
|
|
|
|
})
|
|
|
|
|
done.resolve(sortedArray)
|
|
|
|
|
return done.promise
|
|
|
|
|
return sortedArray
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* builds the correspoding real docker image for each Dockerfile class instance
|
|
|
|
|
*/
|
|
|
|
|
export let buildDockerfiles = (sortedArrayArg: Dockerfile[]) => {
|
|
|
|
|
let done = plugins.q.defer()
|
|
|
|
|
sortedArrayArg.forEach(function(dockerfileArg){
|
|
|
|
|
dockerfileArg.build()
|
|
|
|
|
export let buildDockerfiles = async (sortedArrayArg: Dockerfile[]) => {
|
|
|
|
|
sortedArrayArg.forEach(async function (dockerfileArg) {
|
|
|
|
|
await dockerfileArg.build()
|
|
|
|
|
})
|
|
|
|
|
done.resolve(sortedArrayArg)
|
|
|
|
|
return done.promise
|
|
|
|
|
return sortedArrayArg
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* pushes the real Dockerfile images to a Docker registry
|
|
|
|
|
*/
|
|
|
|
|
export let pushDockerfiles = function(sortedArrayArg: Dockerfile[]){
|
|
|
|
|
let done = plugins.q.defer()
|
|
|
|
|
sortedArrayArg.forEach(function(dockerfileArg){
|
|
|
|
|
dockerfileArg.push(NpmciEnv.buildStage)
|
|
|
|
|
export let pushDockerfiles = async (sortedArrayArg: Dockerfile[]) => {
|
|
|
|
|
sortedArrayArg.forEach(async (dockerfileArg) => {
|
|
|
|
|
await dockerfileArg.push(NpmciEnv.buildStage)
|
|
|
|
|
})
|
|
|
|
|
done.resolve(sortedArrayArg)
|
|
|
|
|
return done.promise
|
|
|
|
|
return sortedArrayArg
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
export let pullDockerfileImages = (sortableArrayArg: Dockerfile[],registryArg = 'registry.gitlab.com') => {
|
|
|
|
|
let done = plugins.q.defer()
|
|
|
|
|
sortableArrayArg.forEach((dockerfileArg) => {
|
|
|
|
|
dockerfileArg.pull(registryArg)
|
|
|
|
|
export let pullDockerfileImages = async (sortableArrayArg: Dockerfile[], registryArg = 'registry.gitlab.com') => {
|
|
|
|
|
sortableArrayArg.forEach(async (dockerfileArg) => {
|
|
|
|
|
await dockerfileArg.pull(registryArg)
|
|
|
|
|
})
|
|
|
|
|
done.resolve(sortableArrayArg)
|
|
|
|
|
return done.promise
|
|
|
|
|
return sortableArrayArg
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* tests all Dockerfiles in by calling class Dockerfile.test();
|
|
|
|
|
* @param sortedArrayArg Dockerfile[] that contains all Dockerfiles in cwd
|
|
|
|
|
*/
|
|
|
|
|
export let testDockerfiles = (sortedArrayArg: Dockerfile[]) => {
|
|
|
|
|
let done = plugins.q.defer()
|
|
|
|
|
sortedArrayArg.forEach(function(dockerfileArg){
|
|
|
|
|
dockerfileArg.test()
|
|
|
|
|
export let testDockerfiles = async (sortedArrayArg: Dockerfile[]) => {
|
|
|
|
|
sortedArrayArg.forEach(async (dockerfileArg) => {
|
|
|
|
|
await dockerfileArg.test()
|
|
|
|
|
})
|
|
|
|
|
done.resolve(sortedArrayArg)
|
|
|
|
|
return done.promise
|
|
|
|
|
return sortedArrayArg
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -155,14 +140,14 @@ export class Dockerfile {
|
|
|
|
|
baseImage: string
|
|
|
|
|
localBaseImageDependent: boolean
|
|
|
|
|
localBaseDockerfile: Dockerfile
|
|
|
|
|
constructor(options: {filePath?: string,fileContents?: string|Buffer,read?: boolean}) {
|
|
|
|
|
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.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))
|
|
|
|
@ -174,20 +159,16 @@ export class Dockerfile {
|
|
|
|
|
/**
|
|
|
|
|
* builds the Dockerfile
|
|
|
|
|
*/
|
|
|
|
|
build() {
|
|
|
|
|
let done = plugins.q.defer()
|
|
|
|
|
async build() {
|
|
|
|
|
plugins.beautylog.info('now building Dockerfile for ' + this.cleanTag)
|
|
|
|
|
bashBare('docker build -t ' + this.buildTag + ' -f ' + this.filePath + ' .')
|
|
|
|
|
await bashBare('docker build -t ' + this.buildTag + ' -f ' + this.filePath + ' .')
|
|
|
|
|
NpmciEnv.dockerFilesBuilt.push(this)
|
|
|
|
|
done.resolve()
|
|
|
|
|
return done.promise
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* pushes the Dockerfile to a registry
|
|
|
|
|
*/
|
|
|
|
|
push(stageArg) {
|
|
|
|
|
let done = plugins.q.defer()
|
|
|
|
|
async push(stageArg) {
|
|
|
|
|
let pushTag
|
|
|
|
|
switch (stageArg) {
|
|
|
|
|
case 'release':
|
|
|
|
@ -198,34 +179,33 @@ export class Dockerfile {
|
|
|
|
|
pushTag = this.testTag
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
bashBare('docker tag ' + this.buildTag + ' ' + pushTag)
|
|
|
|
|
bashBare('docker push ' + pushTag)
|
|
|
|
|
done.resolve()
|
|
|
|
|
return done.promise
|
|
|
|
|
await bashBare('docker tag ' + this.buildTag + ' ' + pushTag)
|
|
|
|
|
await bashBare('docker push ' + pushTag)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* pulls the Dockerfile from a registry
|
|
|
|
|
*/
|
|
|
|
|
pull(registryArg: string) {
|
|
|
|
|
async pull(registryArg: string) {
|
|
|
|
|
let pullTag = this.testTag
|
|
|
|
|
bashBare('docker pull ' + pullTag)
|
|
|
|
|
bashBare('docker tag ' + pullTag + ' ' + this.buildTag)
|
|
|
|
|
await bashBare('docker pull ' + pullTag)
|
|
|
|
|
await bashBare('docker tag ' + pullTag + ' ' + this.buildTag)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* tests the Dockerfile;
|
|
|
|
|
*/
|
|
|
|
|
test() {
|
|
|
|
|
let testFile: string = plugins.path.join(paths.NpmciTestDir,'test_' + this.version + '.sh')
|
|
|
|
|
async 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')
|
|
|
|
|
// 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!')
|
|
|
|
|
}
|
|
|
|
@ -234,16 +214,17 @@ export class Dockerfile {
|
|
|
|
|
/**
|
|
|
|
|
* gets the id of a Dockerfile
|
|
|
|
|
*/
|
|
|
|
|
getId() {
|
|
|
|
|
let containerId = bashBare('docker inspect --type=image --format=\"{{.Id}}\" ' + this.buildTag)
|
|
|
|
|
async getId() {
|
|
|
|
|
let containerId = await bashBare('docker inspect --type=image --format=\"{{.Id}}\" ' + this.buildTag)
|
|
|
|
|
return containerId
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* returns a version for a docker file
|
|
|
|
|
* @execution SYNC
|
|
|
|
|
*/
|
|
|
|
|
export let dockerFileVersion = function(dockerfileNameArg: string): string{
|
|
|
|
|
export let dockerFileVersion = (dockerfileNameArg: string): string => {
|
|
|
|
|
let versionString: string
|
|
|
|
|
let versionRegex = /Dockerfile_([a-zA-Z0-9\.]*)$/
|
|
|
|
|
let regexResultArray = versionRegex.exec(dockerfileNameArg)
|
|
|
|
@ -258,7 +239,7 @@ export let dockerFileVersion = function(dockerfileNameArg: string): string{
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
export let dockerBaseImage = function(dockerfileContentArg: string){
|
|
|
|
|
export let dockerBaseImage = function (dockerfileContentArg: string) {
|
|
|
|
|
let baseImageRegex = /FROM\s([a-zA-z0-9\/\-\:]*)\n?/
|
|
|
|
|
let regexResultArray = baseImageRegex.exec(dockerfileContentArg)
|
|
|
|
|
return regexResultArray[1]
|
|
|
|
@ -267,7 +248,7 @@ export let dockerBaseImage = function(dockerfileContentArg: string){
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
export let dockerTag = function(registryArg: string,repoArg: string,versionArg: string,suffixArg?: string): string{
|
|
|
|
|
export let dockerTag = function (registryArg: string, repoArg: string, versionArg: string, suffixArg?: string): string {
|
|
|
|
|
let tagString: string
|
|
|
|
|
let registry = registryArg
|
|
|
|
|
let repo = repoArg
|
|
|
|
@ -282,9 +263,9 @@ export let dockerTag = function(registryArg: string,repoArg: string,versionArg:
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
export let cleanTagsArrayFunction = function(dockerfileArrayArg: Dockerfile[],trackingArrayArg: Dockerfile[]): string[]{
|
|
|
|
|
export let cleanTagsArrayFunction = function (dockerfileArrayArg: Dockerfile[], trackingArrayArg: Dockerfile[]): string[] {
|
|
|
|
|
let cleanTagsArray: string[] = []
|
|
|
|
|
dockerfileArrayArg.forEach(function(dockerfileArg){
|
|
|
|
|
dockerfileArrayArg.forEach(function (dockerfileArg) {
|
|
|
|
|
if (trackingArrayArg.indexOf(dockerfileArg) === -1) {
|
|
|
|
|
cleanTagsArray.push(dockerfileArg.cleanTag)
|
|
|
|
|
}
|
|
|
|
|