add option for npm access level
This commit is contained in:
@ -1,76 +1,76 @@
|
||||
import * as plugins from './mod.plugins'
|
||||
import * as paths from '../npmci.paths'
|
||||
import { bash } from '../npmci.bash'
|
||||
import * as plugins from './mod.plugins';
|
||||
import * as paths from '../npmci.paths';
|
||||
import { bash } from '../npmci.bash';
|
||||
|
||||
import * as helpers from './mod.helpers'
|
||||
import * as helpers from './mod.helpers';
|
||||
|
||||
// classes
|
||||
import { Dockerfile } from './mod.classes.dockerfile'
|
||||
import { DockerRegistry } from './mod.classes.dockerregistry'
|
||||
import { RegistryStorage } from './mod.classes.registrystorage'
|
||||
import { Dockerfile } from './mod.classes.dockerfile';
|
||||
import { DockerRegistry } from './mod.classes.dockerregistry';
|
||||
import { RegistryStorage } from './mod.classes.registrystorage';
|
||||
|
||||
// instances
|
||||
let npmciRegistryStorage = new RegistryStorage()
|
||||
let npmciRegistryStorage = new RegistryStorage();
|
||||
|
||||
export {
|
||||
Dockerfile,
|
||||
helpers
|
||||
}
|
||||
export { Dockerfile, helpers };
|
||||
|
||||
export let modArgvArg // will be set through the build command
|
||||
export let modArgvArg; // will be set through the build command
|
||||
|
||||
/**
|
||||
* handle cli input
|
||||
* @param argvArg
|
||||
*/
|
||||
export let handleCli = async (argvArg) => {
|
||||
modArgvArg = argvArg
|
||||
export let handleCli = async argvArg => {
|
||||
modArgvArg = argvArg;
|
||||
if (argvArg._.length >= 2) {
|
||||
let action: string = argvArg._[ 1 ]
|
||||
let action: string = argvArg._[1];
|
||||
switch (action) {
|
||||
case 'build':
|
||||
await build()
|
||||
break
|
||||
await build();
|
||||
break;
|
||||
case 'login':
|
||||
case 'prepare':
|
||||
await login()
|
||||
break
|
||||
await login();
|
||||
break;
|
||||
case 'test':
|
||||
await test()
|
||||
break
|
||||
await test();
|
||||
break;
|
||||
case 'push':
|
||||
await push(argvArg)
|
||||
break
|
||||
await push(argvArg);
|
||||
break;
|
||||
case 'pull':
|
||||
await pull(argvArg)
|
||||
break
|
||||
await pull(argvArg);
|
||||
break;
|
||||
default:
|
||||
plugins.beautylog.error(`>>npmci docker ...<< action >>${action}<< not supported`)
|
||||
plugins.beautylog.error(`>>npmci docker ...<< action >>${action}<< not supported`);
|
||||
}
|
||||
} else {
|
||||
plugins.beautylog.log(`>>npmci docker ...<< cli arguments invalid... Please read the documentation.`)
|
||||
plugins.beautylog.log(
|
||||
`>>npmci docker ...<< cli arguments invalid... Please read the documentation.`
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* builds a cwd of Dockerfiles by triggering a promisechain
|
||||
*/
|
||||
export let build = async () => {
|
||||
await prepare()
|
||||
plugins.beautylog.log('now building Dockerfiles...')
|
||||
await helpers.readDockerfiles()
|
||||
await prepare();
|
||||
plugins.beautylog.log('now building Dockerfiles...');
|
||||
await helpers
|
||||
.readDockerfiles()
|
||||
.then(helpers.sortDockerfiles)
|
||||
.then(helpers.mapDockerfiles)
|
||||
.then(helpers.buildDockerfiles)
|
||||
}
|
||||
.then(helpers.buildDockerfiles);
|
||||
};
|
||||
|
||||
/**
|
||||
* login to the DockerRegistries
|
||||
*/
|
||||
export let login = async () => {
|
||||
await prepare()
|
||||
await npmciRegistryStorage.loginAll()
|
||||
}
|
||||
await prepare();
|
||||
await npmciRegistryStorage.loginAll();
|
||||
};
|
||||
|
||||
/**
|
||||
* logs in docker
|
||||
@ -78,62 +78,65 @@ export let login = async () => {
|
||||
export let prepare = async () => {
|
||||
// Always login to GitLab Registry
|
||||
if (!process.env.CI_BUILD_TOKEN || process.env.CI_BUILD_TOKEN === '') {
|
||||
plugins.beautylog.error('No registry token specified by gitlab!')
|
||||
process.exit(1)
|
||||
plugins.beautylog.error('No registry token specified by gitlab!');
|
||||
process.exit(1);
|
||||
}
|
||||
npmciRegistryStorage.addRegistry(new DockerRegistry({
|
||||
registryUrl: 'registry.gitlab.com',
|
||||
username: 'gitlab-ci-token',
|
||||
password: process.env.CI_BUILD_TOKEN
|
||||
}))
|
||||
npmciRegistryStorage.addRegistry(
|
||||
new DockerRegistry({
|
||||
registryUrl: 'registry.gitlab.com',
|
||||
username: 'gitlab-ci-token',
|
||||
password: process.env.CI_BUILD_TOKEN
|
||||
})
|
||||
);
|
||||
|
||||
// handle registries
|
||||
await plugins.smartparam.forEachMinimatch(process.env, 'NPMCI_LOGIN_DOCKER*', async (envString) => {
|
||||
npmciRegistryStorage.addRegistry(
|
||||
DockerRegistry.fromEnvString(envString)
|
||||
)
|
||||
})
|
||||
return
|
||||
}
|
||||
await plugins.smartparam.forEachMinimatch(process.env, 'NPMCI_LOGIN_DOCKER*', async envString => {
|
||||
npmciRegistryStorage.addRegistry(DockerRegistry.fromEnvString(envString));
|
||||
});
|
||||
return;
|
||||
};
|
||||
|
||||
export let push = async (argvArg) => {
|
||||
await prepare()
|
||||
let registryUrlArg = argvArg._[ 2 ]
|
||||
let suffix = null
|
||||
export let push = async argvArg => {
|
||||
await prepare();
|
||||
let registryUrlArg = argvArg._[2];
|
||||
let suffix = null;
|
||||
if (argvArg._.length >= 4) {
|
||||
suffix = argvArg._[ 3 ]
|
||||
suffix = argvArg._[3];
|
||||
}
|
||||
let dockerfileArray = await helpers.readDockerfiles()
|
||||
let dockerfileArray = await helpers
|
||||
.readDockerfiles()
|
||||
.then(helpers.sortDockerfiles)
|
||||
.then(helpers.mapDockerfiles)
|
||||
let localDockerRegistry = npmciRegistryStorage.getRegistryByUrl(registryUrlArg)
|
||||
.then(helpers.mapDockerfiles);
|
||||
let localDockerRegistry = npmciRegistryStorage.getRegistryByUrl(registryUrlArg);
|
||||
if (!localDockerRegistry) {
|
||||
plugins.beautylog.error(`Cannot push to registry ${registryUrlArg}, because it was not found in the authenticated registry list.`)
|
||||
process.exit(1)
|
||||
plugins.beautylog.error(
|
||||
`Cannot push to registry ${registryUrlArg}, because it was not found in the authenticated registry list.`
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
for (let dockerfile of dockerfileArray) {
|
||||
await dockerfile.push(localDockerRegistry, suffix)
|
||||
await dockerfile.push(localDockerRegistry, suffix);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export let pull = async (argvArg) => {
|
||||
await prepare()
|
||||
let registryUrlArg = argvArg._[ 2 ]
|
||||
let suffix = null
|
||||
export let pull = async argvArg => {
|
||||
await prepare();
|
||||
let registryUrlArg = argvArg._[2];
|
||||
let suffix = null;
|
||||
if (argvArg._.length >= 4) {
|
||||
suffix = argvArg._[ 3 ]
|
||||
suffix = argvArg._[3];
|
||||
}
|
||||
let localDockerRegistry = npmciRegistryStorage.getRegistryByUrl(registryUrlArg)
|
||||
let dockerfileArray = await helpers.readDockerfiles()
|
||||
let localDockerRegistry = npmciRegistryStorage.getRegistryByUrl(registryUrlArg);
|
||||
let dockerfileArray = await helpers
|
||||
.readDockerfiles()
|
||||
.then(helpers.sortDockerfiles)
|
||||
.then(helpers.mapDockerfiles)
|
||||
.then(helpers.mapDockerfiles);
|
||||
for (let dockerfile of dockerfileArray) {
|
||||
await dockerfile.pull(localDockerRegistry, suffix)
|
||||
await dockerfile.pull(localDockerRegistry, suffix);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export let test = async () => {
|
||||
await prepare()
|
||||
return await helpers.readDockerfiles()
|
||||
.then(helpers.testDockerfiles)
|
||||
}
|
||||
await prepare();
|
||||
return await helpers.readDockerfiles().then(helpers.testDockerfiles);
|
||||
};
|
||||
|
@ -1,93 +1,109 @@
|
||||
import * as plugins from './mod.plugins'
|
||||
import * as NpmciEnv from '../npmci.env'
|
||||
import { bash } from '../npmci.bash'
|
||||
import * as paths from '../npmci.paths'
|
||||
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'
|
||||
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
|
||||
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
|
||||
this.containerName = 'dockerfile-' + this.version;
|
||||
if (options.filePath && options.read) {
|
||||
this.content = plugins.smartfile.fs.toStringSync(plugins.path.resolve(options.filePath))
|
||||
this.content = plugins.smartfile.fs.toStringSync(plugins.path.resolve(options.filePath));
|
||||
}
|
||||
this.baseImage = helpers.dockerBaseImage(this.content)
|
||||
this.localBaseImageDependent = false
|
||||
this.baseImage = helpers.dockerBaseImage(this.content);
|
||||
this.localBaseImageDependent = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* builds the Dockerfile
|
||||
*/
|
||||
async build () {
|
||||
plugins.beautylog.info('now building Dockerfile for ' + this.cleanTag)
|
||||
let buildArgsString = await helpers.getDockerBuildArgs()
|
||||
let buildCommand = `docker build -t ${this.buildTag} -f ${this.filePath} ${buildArgsString} .`
|
||||
await bash(buildCommand)
|
||||
return
|
||||
async build() {
|
||||
plugins.beautylog.info('now building Dockerfile for ' + this.cleanTag);
|
||||
let buildArgsString = await helpers.getDockerBuildArgs();
|
||||
let buildCommand = `docker build -t ${this.buildTag} -f ${this.filePath} ${buildArgsString} .`;
|
||||
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}`)
|
||||
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)
|
||||
await bash(`docker pull ${pullTag}`)
|
||||
await bash(`docker tag ${pullTag} ${this.buildTag}`)
|
||||
async pull(registryArg: DockerRegistry, versionSuffixArg: string = null) {
|
||||
let pullTag = helpers.getDockerTagString(
|
||||
registryArg.registryUrl,
|
||||
this.repo,
|
||||
this.version,
|
||||
versionSuffixArg
|
||||
);
|
||||
await bash(`docker pull ${pullTag}`);
|
||||
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)
|
||||
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 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`)
|
||||
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`)
|
||||
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`);
|
||||
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`);
|
||||
} else {
|
||||
plugins.beautylog.warn('skipping tests for ' + this.cleanTag + ' because no testfile was found!')
|
||||
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
|
||||
async getId() {
|
||||
let containerId = await bash('docker inspect --type=image --format="{{.Id}}" ' + this.buildTag);
|
||||
return containerId;
|
||||
}
|
||||
}
|
||||
|
@ -1,47 +1,47 @@
|
||||
import * as plugins from './mod.plugins'
|
||||
import { bash } from '../npmci.bash'
|
||||
import * as plugins from './mod.plugins';
|
||||
import { bash } from '../npmci.bash';
|
||||
|
||||
export interface IDockerRegistryConstructorOptions {
|
||||
registryUrl: string,
|
||||
username: string,
|
||||
password: string
|
||||
registryUrl: string;
|
||||
username: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
export class DockerRegistry {
|
||||
registryUrl: string
|
||||
username: string
|
||||
password: string
|
||||
constructor (optionsArg: IDockerRegistryConstructorOptions) {
|
||||
this.registryUrl = optionsArg.registryUrl
|
||||
this.username = optionsArg.username
|
||||
this.password = optionsArg.password
|
||||
plugins.beautylog.info(`created DockerRegistry for ${this.registryUrl}`)
|
||||
registryUrl: string;
|
||||
username: string;
|
||||
password: string;
|
||||
constructor(optionsArg: IDockerRegistryConstructorOptions) {
|
||||
this.registryUrl = optionsArg.registryUrl;
|
||||
this.username = optionsArg.username;
|
||||
this.password = optionsArg.password;
|
||||
plugins.beautylog.info(`created DockerRegistry for ${this.registryUrl}`);
|
||||
}
|
||||
|
||||
static fromEnvString (envString: string): DockerRegistry {
|
||||
let dockerRegexResultArray = envString.split('|')
|
||||
static fromEnvString(envString: string): DockerRegistry {
|
||||
let dockerRegexResultArray = envString.split('|');
|
||||
if (dockerRegexResultArray.length !== 3) {
|
||||
plugins.beautylog.error('malformed docker env var...')
|
||||
process.exit(1)
|
||||
return
|
||||
plugins.beautylog.error('malformed docker env var...');
|
||||
process.exit(1);
|
||||
return;
|
||||
}
|
||||
let registryUrl = dockerRegexResultArray[0]
|
||||
let username = dockerRegexResultArray[1]
|
||||
let password = dockerRegexResultArray[2]
|
||||
let registryUrl = dockerRegexResultArray[0];
|
||||
let username = dockerRegexResultArray[1];
|
||||
let password = dockerRegexResultArray[2];
|
||||
return new DockerRegistry({
|
||||
registryUrl: registryUrl,
|
||||
username: username,
|
||||
password: password
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
async login () {
|
||||
async login() {
|
||||
if (this.registryUrl === 'docker.io') {
|
||||
await bash(`docker login -u ${this.username} -p ${this.password}`)
|
||||
plugins.beautylog.info('Logged in to standard docker hub')
|
||||
await bash(`docker login -u ${this.username} -p ${this.password}`);
|
||||
plugins.beautylog.info('Logged in to standard docker hub');
|
||||
} else {
|
||||
await bash(`docker login -u ${this.username} -p ${this.password} ${this.registryUrl}`)
|
||||
await bash(`docker login -u ${this.username} -p ${this.password} ${this.registryUrl}`);
|
||||
}
|
||||
plugins.beautylog.ok(`docker authenticated for ${this.registryUrl}!`)
|
||||
plugins.beautylog.ok(`docker authenticated for ${this.registryUrl}!`);
|
||||
}
|
||||
}
|
||||
|
@ -1,28 +1,28 @@
|
||||
import * as plugins from './mod.plugins'
|
||||
import { Objectmap } from 'lik'
|
||||
import * as plugins from './mod.plugins';
|
||||
import { Objectmap } from 'lik';
|
||||
|
||||
import { DockerRegistry } from './mod.classes.dockerregistry'
|
||||
import { DockerRegistry } from './mod.classes.dockerregistry';
|
||||
|
||||
export class RegistryStorage {
|
||||
objectMap = new Objectmap<DockerRegistry>()
|
||||
constructor () {
|
||||
objectMap = new Objectmap<DockerRegistry>();
|
||||
constructor() {
|
||||
// Nothing here
|
||||
}
|
||||
|
||||
addRegistry (registryArg: DockerRegistry) {
|
||||
this.objectMap.add(registryArg)
|
||||
addRegistry(registryArg: DockerRegistry) {
|
||||
this.objectMap.add(registryArg);
|
||||
}
|
||||
|
||||
getRegistryByUrl (registryUrlArg: string) {
|
||||
getRegistryByUrl(registryUrlArg: string) {
|
||||
return this.objectMap.find(registryArg => {
|
||||
return registryArg.registryUrl === registryUrlArg
|
||||
})
|
||||
return registryArg.registryUrl === registryUrlArg;
|
||||
});
|
||||
}
|
||||
|
||||
async loginAll () {
|
||||
async loginAll() {
|
||||
await this.objectMap.forEach(async registryArg => {
|
||||
await registryArg.login()
|
||||
})
|
||||
plugins.beautylog.success('logged in successfully into all available DockerRegistries!')
|
||||
await registryArg.login();
|
||||
});
|
||||
plugins.beautylog.success('logged in successfully into all available DockerRegistries!');
|
||||
}
|
||||
}
|
||||
|
@ -1,33 +1,32 @@
|
||||
import * as plugins from './mod.plugins'
|
||||
import * as paths from '../npmci.paths'
|
||||
import * as NpmciEnv from '../npmci.env'
|
||||
import * as NpmciConfig from '../npmci.config'
|
||||
import { bash } from '../npmci.bash'
|
||||
import * as plugins from './mod.plugins';
|
||||
import * as paths from '../npmci.paths';
|
||||
import * as NpmciEnv from '../npmci.env';
|
||||
import * as NpmciConfig from '../npmci.config';
|
||||
import { bash } from '../npmci.bash';
|
||||
|
||||
import { Dockerfile } from './mod.classes.dockerfile'
|
||||
import { Dockerfile } from './mod.classes.dockerfile';
|
||||
|
||||
/**
|
||||
* creates instance of class Dockerfile for all Dockerfiles in cwd
|
||||
* @returns Promise<Dockerfile[]>
|
||||
*/
|
||||
export let readDockerfiles = async (): Promise<Dockerfile[]> => {
|
||||
let fileTree = await plugins.smartfile.fs.listFileTree(paths.cwd, 'Dockerfile*')
|
||||
let fileTree = await plugins.smartfile.fs.listFileTree(paths.cwd, 'Dockerfile*');
|
||||
|
||||
// create the Dockerfile array
|
||||
let readDockerfilesArray: Dockerfile[] = []
|
||||
plugins.beautylog.info(`found ${fileTree.length} Dockerfiles:`)
|
||||
console.log(fileTree)
|
||||
let readDockerfilesArray: Dockerfile[] = [];
|
||||
plugins.beautylog.info(`found ${fileTree.length} Dockerfiles:`);
|
||||
console.log(fileTree);
|
||||
for (let dockerfilePath of fileTree) {
|
||||
let myDockerfile = new Dockerfile({
|
||||
filePath: dockerfilePath,
|
||||
read: true
|
||||
})
|
||||
readDockerfilesArray.push(myDockerfile)
|
||||
});
|
||||
readDockerfilesArray.push(myDockerfile);
|
||||
}
|
||||
|
||||
return readDockerfilesArray
|
||||
|
||||
}
|
||||
return readDockerfilesArray;
|
||||
};
|
||||
|
||||
/**
|
||||
* sorts Dockerfiles into a dependency chain
|
||||
@ -35,62 +34,65 @@ export let readDockerfiles = async (): Promise<Dockerfile[]> => {
|
||||
* @returns Promise<Dockerfile[]>
|
||||
*/
|
||||
export let sortDockerfiles = (sortableArrayArg: Dockerfile[]): Promise<Dockerfile[]> => {
|
||||
let done = plugins.q.defer<Dockerfile[]>()
|
||||
plugins.beautylog.info('sorting Dockerfiles:')
|
||||
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)
|
||||
let done = plugins.q.defer<Dockerfile[]>();
|
||||
plugins.beautylog.info('sorting Dockerfiles:');
|
||||
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
|
||||
dockerfileArg.localBaseImageDependent = true;
|
||||
}
|
||||
})
|
||||
});
|
||||
if (sortableArrayArg.length === sortedArray.length) {
|
||||
let counter = 1
|
||||
let counter = 1;
|
||||
for (let dockerfile of sortedArray) {
|
||||
plugins.beautylog.log(`tag ${counter}: -> ${dockerfile.cleanTag}`)
|
||||
counter++
|
||||
plugins.beautylog.log(`tag ${counter}: -> ${dockerfile.cleanTag}`);
|
||||
counter++;
|
||||
}
|
||||
done.resolve(sortedArray)
|
||||
done.resolve(sortedArray);
|
||||
} else if (sorterFunctionCounter < 10) {
|
||||
sorterFunctionCounter++
|
||||
sorterFunction()
|
||||
sorterFunctionCounter++;
|
||||
sorterFunction();
|
||||
}
|
||||
}
|
||||
sorterFunction()
|
||||
return done.promise
|
||||
}
|
||||
};
|
||||
sorterFunction();
|
||||
return done.promise;
|
||||
};
|
||||
|
||||
/**
|
||||
* maps local Dockerfiles dependencies to the correspoding Dockerfile class instances
|
||||
*/
|
||||
export let mapDockerfiles = async (sortedArray: Dockerfile[]): Promise<Dockerfile[]> => {
|
||||
sortedArray.forEach((dockerfileArg) => {
|
||||
sortedArray.forEach(dockerfileArg => {
|
||||
if (dockerfileArg.localBaseImageDependent) {
|
||||
sortedArray.forEach((dockfile2: Dockerfile) => {
|
||||
if (dockfile2.cleanTag === dockerfileArg.baseImage) {
|
||||
dockerfileArg.localBaseDockerfile = dockfile2
|
||||
dockerfileArg.localBaseDockerfile = dockfile2;
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
})
|
||||
return sortedArray
|
||||
}
|
||||
});
|
||||
return sortedArray;
|
||||
};
|
||||
|
||||
/**
|
||||
* builds the correspoding real docker image for each Dockerfile class instance
|
||||
*/
|
||||
export let buildDockerfiles = async (sortedArrayArg: Dockerfile[]) => {
|
||||
for (let dockerfileArg of sortedArrayArg) {
|
||||
await dockerfileArg.build()
|
||||
await dockerfileArg.build();
|
||||
}
|
||||
return sortedArrayArg
|
||||
}
|
||||
return sortedArrayArg;
|
||||
};
|
||||
|
||||
/**
|
||||
* tests all Dockerfiles in by calling class Dockerfile.test();
|
||||
@ -98,80 +100,87 @@ export let buildDockerfiles = async (sortedArrayArg: Dockerfile[]) => {
|
||||
*/
|
||||
export let testDockerfiles = async (sortedArrayArg: Dockerfile[]) => {
|
||||
for (let dockerfileArg of sortedArrayArg) {
|
||||
await dockerfileArg.test()
|
||||
await dockerfileArg.test();
|
||||
}
|
||||
return sortedArrayArg
|
||||
}
|
||||
return sortedArrayArg;
|
||||
};
|
||||
|
||||
/**
|
||||
* returns a version for a docker file
|
||||
* @execution SYNC
|
||||
*/
|
||||
export let dockerFileVersion = (dockerfileNameArg: string): string => {
|
||||
let versionString: string
|
||||
let versionRegex = /Dockerfile_([a-zA-Z0-9\.]*)$/
|
||||
let regexResultArray = versionRegex.exec(dockerfileNameArg)
|
||||
let versionString: string;
|
||||
let versionRegex = /Dockerfile_([a-zA-Z0-9\.]*)$/;
|
||||
let regexResultArray = versionRegex.exec(dockerfileNameArg);
|
||||
if (regexResultArray && regexResultArray.length === 2) {
|
||||
versionString = regexResultArray[ 1 ]
|
||||
versionString = regexResultArray[1];
|
||||
} else {
|
||||
versionString = 'latest'
|
||||
versionString = 'latest';
|
||||
}
|
||||
return versionString
|
||||
}
|
||||
return versionString;
|
||||
};
|
||||
|
||||
/**
|
||||
* returns the docker base image for a Dockerfile
|
||||
*/
|
||||
export let dockerBaseImage = function (dockerfileContentArg: string) {
|
||||
let baseImageRegex = /FROM\s([a-zA-z0-9\/\-\:]*)\n?/
|
||||
let regexResultArray = baseImageRegex.exec(dockerfileContentArg)
|
||||
return regexResultArray[ 1 ]
|
||||
}
|
||||
export let dockerBaseImage = function(dockerfileContentArg: string) {
|
||||
let baseImageRegex = /FROM\s([a-zA-z0-9\/\-\:]*)\n?/;
|
||||
let regexResultArray = baseImageRegex.exec(dockerfileContentArg);
|
||||
return regexResultArray[1];
|
||||
};
|
||||
|
||||
/**
|
||||
* returns the docker tag
|
||||
*/
|
||||
export let getDockerTagString = (registryArg: string, repoArg: string, versionArg: string, suffixArg?: string): string => {
|
||||
|
||||
export let getDockerTagString = (
|
||||
registryArg: string,
|
||||
repoArg: string,
|
||||
versionArg: string,
|
||||
suffixArg?: string
|
||||
): string => {
|
||||
// determine wether the repo should be mapped accordingly to the registry
|
||||
let mappedRepo = NpmciConfig.configObject.dockerRegistryRepoMap[registryArg]
|
||||
let mappedRepo = NpmciConfig.configObject.dockerRegistryRepoMap[registryArg];
|
||||
let repo = (() => {
|
||||
if (mappedRepo) {
|
||||
return mappedRepo
|
||||
return mappedRepo;
|
||||
} else {
|
||||
return repoArg
|
||||
return repoArg;
|
||||
}
|
||||
})()
|
||||
})();
|
||||
|
||||
// determine wether the version contais a suffix
|
||||
let version = versionArg
|
||||
let version = versionArg;
|
||||
if (suffixArg) {
|
||||
version = versionArg + '_' + suffixArg
|
||||
version = versionArg + '_' + suffixArg;
|
||||
}
|
||||
|
||||
let tagString = `${registryArg}/${repo}:${version}`
|
||||
return tagString
|
||||
}
|
||||
let tagString = `${registryArg}/${repo}:${version}`;
|
||||
return tagString;
|
||||
};
|
||||
|
||||
export let getDockerBuildArgs = async (): Promise<string> => {
|
||||
plugins.beautylog.info('checking for env vars to be supplied to the docker build')
|
||||
let buildArgsString: string = ''
|
||||
plugins.beautylog.info('checking for env vars to be supplied to the docker build');
|
||||
let buildArgsString: string = '';
|
||||
for (let key in NpmciConfig.configObject.dockerBuildargEnvMap) {
|
||||
let targetValue = process.env[NpmciConfig.configObject.dockerBuildargEnvMap[key]]
|
||||
buildArgsString = `${buildArgsString} --build-arg ${key}=${targetValue}`
|
||||
let targetValue = process.env[NpmciConfig.configObject.dockerBuildargEnvMap[key]];
|
||||
buildArgsString = `${buildArgsString} --build-arg ${key}=${targetValue}`;
|
||||
}
|
||||
return buildArgsString
|
||||
}
|
||||
return buildArgsString;
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
export let cleanTagsArrayFunction = function (dockerfileArrayArg: Dockerfile[], trackingArrayArg: Dockerfile[]): string[] {
|
||||
let cleanTagsArray: string[] = []
|
||||
dockerfileArrayArg.forEach(function (dockerfileArg) {
|
||||
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)
|
||||
cleanTagsArray.push(dockerfileArg.cleanTag);
|
||||
}
|
||||
})
|
||||
return cleanTagsArray
|
||||
}
|
||||
});
|
||||
return cleanTagsArray;
|
||||
};
|
||||
|
@ -1,3 +1 @@
|
||||
export * from '../npmci.plugins'
|
||||
|
||||
|
||||
export * from '../npmci.plugins';
|
||||
|
Reference in New Issue
Block a user