update to new cli scheme

This commit is contained in:
2017-08-27 15:24:17 +02:00
parent e6bc38c7e5
commit 6e816f35f6
62 changed files with 1372 additions and 1596 deletions

View File

@ -1,25 +0,0 @@
import * as plugins from './mod.plugins'
import { bash } from '../npmci.bash'
import * as env from '../npmci.env'
import * as npmciMods from '../npmci.mods'
/**
* defines possible build services
*/
export type TBuildService = 'docker'
/**
* builds for a specific service
*/
export let build = async (argvArg): Promise<void> => {
let whatToPublish: string = argvArg._[1]
switch (whatToPublish) {
case 'docker':
let modDocker = await npmciMods.modDocker.load()
await modDocker.build(argvArg)
break
default:
plugins.beautylog.log('build target ' + whatToPublish + ' not recognised!')
}
return
}

View File

@ -0,0 +1,27 @@
import * as plugins from './mod.plugins'
let npmciCflare = new plugins.cflare.CflareAccount()
/**
* handle cli input
* @param argvArg
*/
export let handleCli = async (argvArg) => {
if (argvArg._.length >= 2) {
let action: string = argvArg._[1]
switch (action) {
default:
plugins.beautylog.error(`>>npmci node ...<< action >>${action}<< not supported`)
}
} else {
plugins.beautylog.log(`>>npmci node ...<< cli arguments invalid... Please read the documentation.`)
}
}
export let purge = async (argvArg) => {
npmciCflare.auth({
email: '',
key: ''
})
npmciCflare.purgeZone(argvArg._[1])
}

View File

@ -4,4 +4,4 @@ import * as cflare from 'cflare'
export {
cflare
}
}

View File

@ -1 +1,376 @@
export * from './mod.builddocker'
import * as plugins from './mod.plugins'
import * as paths from '../npmci.paths'
import * as NpmciEnv from '../npmci.env'
import { bash } from '../npmci.bash'
export let modArgvArg // will be set through the build command
/**
* handle cli input
* @param argvArg
*/
export let handleCli = async (argvArg) => {
modArgvArg = argvArg
if (argvArg._.length >= 2) {
let action: string = argvArg._[1]
switch (action) {
case 'build':
await build()
break
case 'prepare':
await prepare()
break
case 'test':
await test()
break
default:
plugins.beautylog.error(`>>npmci node ...<< action >>${action}<< not supported`)
}
} else {
plugins.beautylog.log(`>>npmci node ...<< cli arguments invalid... Please read the documentation.`)
}
}
/**
* logs in docker
*/
export let prepare = async () => {
NpmciEnv.setDockerRegistry('docker.io') // TODO: checkup why we set this here
// handle registries
plugins.smartparam.forEachMinimatch(process.env, 'NPMCI_LOGIN_DOCKER*', async (envString) => {
let dockerRegexResultArray = envString.split('|')
if (dockerRegexResultArray.length !== 3) {
plugins.beautylog.error('malformed docker env var...')
process.exit(1)
return
}
let registry = dockerRegexResultArray[0]
let username = dockerRegexResultArray[1]
let password = dockerRegexResultArray[2]
if (registry === 'docker.io') {
await bash(`docker login -u ${username} -p ${password}`)
plugins.beautylog.info('Logged in to standard docker hub')
} else {
await bash(`docker login -u ${username} -p ${password} ${registry}`)
}
plugins.beautylog.success(`docker authenticated for ${registry}!`)
})
// 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!')
return
}
await bash(`docker login -u gitlab-ci-token -p ${process.env.CI_BUILD_TOKEN} registry.gitlab.com`)
plugins.beautylog.success(`docker authenticated for registry.gitlab.com!`)
return
}
/**
* builds a cwd of Dockerfiles by triggering a promisechain
*/
export let build = async () => {
plugins.beautylog.log('now building Dockerfiles...')
await readDockerfiles()
.then(sortDockerfiles)
.then(mapDockerfiles)
.then(buildDockerfiles)
}
export let push = async () => {
await readDockerfiles()
.then(sortDockerfiles)
.then(mapDockerfiles)
.then(pushDockerfiles)
}
export let pull = async () => {
return await readDockerfiles()
.then(pullDockerfileImages)
}
export let test = async () => {
return await readDockerfiles()
.then(testDockerfiles)
}
/**
* 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*')
// create the Dockerfile array
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)
}
return readDockerfilesArray
}
/**
* sorts Dockerfiles into a dependency chain
* @param sortableArrayArg an array of instances of class 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)
}
if (cleanTagsOriginal.indexOf(dockerfileArg.baseImage) !== -1) {
dockerfileArg.localBaseImageDependent = true
}
})
if (sortableArrayArg.length === sortedArray.length) {
let counter = 1
for (let dockerfile of sortedArray) {
plugins.beautylog.log(`tag ${counter}: -> ${dockerfile.cleanTag}`)
counter++
}
done.resolve(sortedArray)
} else if (sorterFunctionCounter < 10) {
sorterFunctionCounter++
sorterFunction()
}
}
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) => {
if (dockerfileArg.localBaseImageDependent) {
sortedArray.forEach((dockfile2: Dockerfile) => {
if (dockfile2.cleanTag === dockerfileArg.baseImage) {
dockerfileArg.localBaseDockerfile = dockfile2
}
})
}
})
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()
}
return sortedArrayArg
}
/**
* pushes the real Dockerfile images to a Docker registry
*/
export let pushDockerfiles = async (sortedArrayArg: Dockerfile[]) => {
let stageArg = (function () {
if (modArgvArg._ && modArgvArg._.length >= 3) {
return modArgvArg._[ 2 ]
} else {
return NpmciEnv.buildStage
}
})()
for (let dockerfileArg of sortedArrayArg) {
await dockerfileArg.push(stageArg)
}
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 = async (sortableArrayArg: Dockerfile[], registryArg = 'registry.gitlab.com') => {
for (let dockerfileArg of sortableArrayArg) {
await dockerfileArg.pull(registryArg)
}
return sortableArrayArg
}
/**
* tests all Dockerfiles in by calling class Dockerfile.test();
* @param sortedArrayArg Dockerfile[] that contains all Dockerfiles in cwd
*/
export let testDockerfiles = async (sortedArrayArg: Dockerfile[]) => {
for (let dockerfileArg of sortedArrayArg) {
await dockerfileArg.test()
}
return sortedArrayArg
}
/**
* class Dockerfile represents a Dockerfile on disk in npmci
*/
export class Dockerfile {
filePath: string
repo: string
version: string
cleanTag: string
buildTag: string
gitlabTestTag: string
gitlabReleaseTag: 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.gitlabTestTag = getDockerTagString('docker.io', this.repo, this.version, 'test') // TODO: using docker.io until gitlab is fixed
this.gitlabReleaseTag = getDockerTagString('docker.io', this.repo, this.version) // TODO: using docker.io until gitlab is fixed
// the releaseTag determines where the image gets released
this.releaseTag = getDockerTagString('docker.io', 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))
}
this.baseImage = dockerBaseImage(this.content)
this.localBaseImageDependent = false
}
/**
* builds the Dockerfile
*/
async build () {
plugins.beautylog.info('now building Dockerfile for ' + this.cleanTag)
let buildCommand = `docker build -t ${this.buildTag} -f ${this.filePath} .`
await bash(buildCommand)
NpmciEnv.dockerFilesBuilt.push(this)
return
}
/**
* pushes the Dockerfile to a registry
*/
async push (stageArg) {
await bash(`docker tag ${this.buildTag} ${this.releaseTag}`)
await bash(`docker tag ${this.buildTag} ${this.gitlabReleaseTag}`)
await bash(`docker tag ${this.buildTag} ${this.gitlabTestTag}`)
switch (stageArg) {
case 'release':
await bash(`docker push ${this.gitlabReleaseTag}`)
await bash(`docker push ${this.releaseTag}`)
break
case 'test':
default:
await bash(`docker push ${this.gitlabTestTag}`)
break
}
}
/**
* pulls the Dockerfile from a registry
*/
async pull (registryArg: string) {
await bash(`docker pull ${this.gitlabTestTag}`)
await bash(`docker tag ${this.gitlabTestTag} ${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)
if (testFileExists) {
// run tests
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')
} else {
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
}
}
/**
* 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)
if (regexResultArray && regexResultArray.length === 2) {
versionString = regexResultArray[ 1 ]
} else {
versionString = 'latest'
}
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 ]
}
/**
* returns the docker tag
*/
export let getDockerTagString = function (registryArg: string, repoArg: string, versionArg: string, suffixArg?: string): string {
// determine wether the suffix is needed
let version = versionArg
if (suffixArg) {
version = versionArg + '_' + suffixArg
}
let tagString = `${registryArg}/${repoArg}:${version}`
return tagString
}
/**
*
*/
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
}

View File

@ -1,299 +0,0 @@
import * as plugins from './mod.plugins'
import * as paths from '../npmci.paths'
import * as NpmciEnv from '../npmci.env'
import { bash } from '../npmci.bash'
let modArgvArg // will be set through the build command
/**
* builds a cwd of Dockerfiles by triggering a promisechain
*/
export let build = async (argvArg: any) => {
modArgvArg = argvArg
plugins.beautylog.log('now building Dockerfiles...')
await readDockerfiles(argvArg)
.then(sortDockerfiles)
.then(mapDockerfiles)
.then(buildDockerfiles)
.then(pushDockerfiles)
}
/**
* creates instance of class Dockerfile for all Dockerfiles in cwd
* @returns Promise<Dockerfile[]>
*/
export let readDockerfiles = async (argvArg): Promise<Dockerfile[]> => {
modArgvArg = argvArg
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)
for (let dockerfilePath of fileTree) {
let myDockerfile = new Dockerfile({
filePath: dockerfilePath,
read: true
})
readDockerfilesArray.push(myDockerfile)
}
return readDockerfilesArray
}
/**
* sorts Dockerfiles into a dependency chain
* @param sortableArrayArg an array of instances of class 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)
}
if (cleanTagsOriginal.indexOf(dockerfileArg.baseImage) !== -1) {
dockerfileArg.localBaseImageDependent = true
}
})
if (sortableArrayArg.length === sortedArray.length) {
let counter = 1
for (let dockerfile of sortedArray) {
plugins.beautylog.log(`tag ${counter}: -> ${dockerfile.cleanTag}`)
counter++
}
done.resolve(sortedArray)
} else if (sorterFunctionCounter < 10) {
sorterFunctionCounter++
sorterFunction()
}
}
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) => {
if (dockerfileArg.localBaseImageDependent) {
sortedArray.forEach((dockfile2: Dockerfile) => {
if (dockfile2.cleanTag === dockerfileArg.baseImage) {
dockerfileArg.localBaseDockerfile = dockfile2
}
})
}
})
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()
}
return sortedArrayArg
}
/**
* pushes the real Dockerfile images to a Docker registry
*/
export let pushDockerfiles = async (sortedArrayArg: Dockerfile[]) => {
let stageArg = (function () {
if (modArgvArg._ && modArgvArg._.length >= 3) {
return modArgvArg._[ 2 ]
} else {
return NpmciEnv.buildStage
}
})()
for (let dockerfileArg of sortedArrayArg) {
await dockerfileArg.push(stageArg)
}
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 = async (sortableArrayArg: Dockerfile[], registryArg = 'registry.gitlab.com') => {
for (let dockerfileArg of sortableArrayArg) {
await dockerfileArg.pull(registryArg)
}
return sortableArrayArg
}
/**
* tests all Dockerfiles in by calling class Dockerfile.test();
* @param sortedArrayArg Dockerfile[] that contains all Dockerfiles in cwd
*/
export let testDockerfiles = async (sortedArrayArg: Dockerfile[]) => {
for (let dockerfileArg of sortedArrayArg) {
await dockerfileArg.test()
}
return sortedArrayArg
}
/**
* class Dockerfile represents a Dockerfile on disk in npmci
*/
export class Dockerfile {
filePath: string
repo: string
version: string
cleanTag: string
buildTag: string
gitlabTestTag: string
gitlabReleaseTag: 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.gitlabTestTag = getDockerTagString('docker.io', this.repo, this.version, 'test') // TODO: using docker.io until gitlab is fixed
this.gitlabReleaseTag = getDockerTagString('docker.io', this.repo, this.version) // TODO: using docker.io until gitlab is fixed
// the releaseTag determines where the image gets released
this.releaseTag = getDockerTagString('docker.io', 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))
}
this.baseImage = dockerBaseImage(this.content)
this.localBaseImageDependent = false
}
/**
* builds the Dockerfile
*/
async build () {
plugins.beautylog.info('now building Dockerfile for ' + this.cleanTag)
let buildCommand = `docker build -t ${this.buildTag} -f ${this.filePath} .`
await bash(buildCommand)
NpmciEnv.dockerFilesBuilt.push(this)
return
}
/**
* pushes the Dockerfile to a registry
*/
async push (stageArg) {
await bash(`docker tag ${this.buildTag} ${this.releaseTag}`)
await bash(`docker tag ${this.buildTag} ${this.gitlabReleaseTag}`)
await bash(`docker tag ${this.buildTag} ${this.gitlabTestTag}`)
switch (stageArg) {
case 'release':
await bash(`docker push ${this.gitlabReleaseTag}`)
await bash(`docker push ${this.releaseTag}`)
break
case 'test':
default:
await bash(`docker push ${this.gitlabTestTag}`)
break
}
}
/**
* pulls the Dockerfile from a registry
*/
async pull (registryArg: string) {
await bash(`docker pull ${this.gitlabTestTag}`)
await bash(`docker tag ${this.gitlabTestTag} ${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)
if (testFileExists) {
// run tests
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')
} else {
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
}
}
/**
* 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)
if (regexResultArray && regexResultArray.length === 2) {
versionString = regexResultArray[ 1 ]
} else {
versionString = 'latest'
}
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 ]
}
/**
* returns the docker tag
*/
export let getDockerTagString = function (registryArg: string, repoArg: string, versionArg: string, suffixArg?: string): string {
// determine wether the suffix is needed
let version = versionArg
if (suffixArg) {
version = versionArg + '_' + suffixArg
}
let tagString = `${registryArg}/${repoArg}:${version}`
return tagString
}
/**
*
*/
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
}

View File

@ -1 +0,0 @@
export * from '../npmci.plugins'

View File

@ -1,10 +1,31 @@
import * as plugins from './mod.plugins'
import * as configModule from '../npmci.config'
import * as plugins from '../npmci.plugins'
import * as npmciConfig from '../npmci.config'
import {
bash,
bashNoError,
nvmAvailable,
yarnAvailable } from '../npmci.bash'
yarnAvailable
} from '../npmci.bash'
/**
* handle cli input
* @param argvArg
*/
export let handleCli = async (argvArg) => {
if (argvArg._.length >= 3) {
let action: string = argvArg._[1]
switch (action) {
case 'install':
await install(argvArg._[2])
break
default:
plugins.beautylog.error(`>>npmci node ...<< action >>${action}<< not supported`)
}
} else {
plugins.beautylog.error(`>>npmci node ...<< cli arguments invalid... Please read the documentation.`)
}
}
/**
* Install a specific version of node
@ -21,17 +42,17 @@ export let install = async (versionArg) => {
version = '6'
} else {
version = versionArg
};
}
if (await nvmAvailable.promise) {
await bash(`nvm install ${version} && nvm alias default ${version}`)
plugins.beautylog.success(`Node version ${version} successfully installed!`)
} else {
plugins.beautylog.warn('Nvm not in path so staying at installed node version!')
};
}
await bash('node -v')
await bash('npm -v')
// lets look for further config
await configModule.getConfig()
await npmciConfig.getConfig()
.then(async configArg => {
plugins.beautylog.log('Now checking for needed global npm tools...')
for (let npmTool of configArg.globalNpmTools) {

64
ts/mod_npm/index.ts Normal file
View File

@ -0,0 +1,64 @@
import * as plugins from './mod.plugins'
import * as configModule from '../npmci.config'
import {
bash,
bashNoError,
nvmAvailable,
yarnAvailable
} from '../npmci.bash'
/**
* handle cli input
* @param argvArg
*/
export let handleCli = async (argvArg) => {
if (argvArg._.length >= 2) {
let action: string = argvArg._[1]
switch (action) {
case 'install':
await install()
break
case 'prepare':
await prepare()
break
case 'test':
await test()
break
default:
plugins.beautylog.error(`>>npmci node ...<< action >>${action}<< not supported`)
}
} else {
plugins.beautylog.log(`>>npmci node ...<< cli arguments invalid... Please read the documentation.`)
}
}
/**
* authenticates npm with token from env var
*/
let prepare = async () => {
let npmrcPrefix: string = '//registry.npmjs.org/:_authToken='
let npmToken: string = process.env.NPMCI_TOKEN_NPM
let npmrcFileString: string = npmrcPrefix + npmToken
if (npmToken) {
plugins.beautylog.info('found access token')
} else {
plugins.beautylog.error('no access token found! Exiting!')
process.exit(1)
}
plugins.smartfile.memory.toFsSync(npmrcFileString, '/root/.npmrc')
return
}
let install = async (): Promise<void> => {
plugins.beautylog.info('now installing dependencies:')
if (await yarnAvailable.promise) {
await bash('yarn install')
} else {
await bash('npm install')
}
}
export let test = async (): Promise<void> => {
plugins.beautylog.info('now starting tests:')
await bash('yarn test')
}

View File

@ -1,99 +0,0 @@
import * as plugins from './mod.plugins'
import { bash } from '../npmci.bash'
import * as env from '../npmci.env'
import * as npmciMods from '../npmci.mods'
// types
/**
* defines possible prepare services
*/
export type TPrepService = 'npm' | 'docker' | 'docker-gitlab' | 'ssh'
/**
* authenticates npm with token from env var
*/
let npm = async () => {
let npmrcPrefix: string = '//registry.npmjs.org/:_authToken='
let npmToken: string = process.env.NPMCI_TOKEN_NPM
let npmrcFileString: string = npmrcPrefix + npmToken
if (npmToken) {
plugins.beautylog.info('found access token')
} else {
plugins.beautylog.error('no access token found! Exiting!')
process.exit(1)
}
plugins.smartfile.memory.toFsSync(npmrcFileString, '/root/.npmrc')
return
}
/**
* logs in docker
*/
let docker = async () => {
env.setDockerRegistry('docker.io') // TODO: checkup why we set this here
// handle registries
plugins.smartparam.forEachMinimatch(process.env, 'NPMCI_LOGIN_DOCKER*', async (envString) => {
let dockerRegexResultArray = envString.split('|')
if (dockerRegexResultArray.length !== 3) {
plugins.beautylog.error('malformed docker env var...')
process.exit(1)
return
}
let registry = dockerRegexResultArray[0]
let username = dockerRegexResultArray[1]
let password = dockerRegexResultArray[2]
if (registry === 'docker.io') {
await bash(`docker login -u ${username} -p ${password}`)
plugins.beautylog.info('Logged in to standard docker hub')
} else {
await bash(`docker login -u ${username} -p ${password} ${registry}`)
}
plugins.beautylog.success(`docker authenticated for ${registry}!`)
})
// Always login to GitLab Registry
await dockerGitlab()
return
}
/**
* prepare docker for gitlab registry
*/
let dockerGitlab = async () => {
if (!process.env.CI_BUILD_TOKEN || process.env.CI_BUILD_TOKEN === '') {
plugins.beautylog.error('No registry token specified by gitlab!')
process.exit(1)
}
await bash(`docker login -u gitlab-ci-token -p ${process.env.CI_BUILD_TOKEN} registry.gitlab.com`)
plugins.beautylog.success(`docker authenticated for registry.gitlab.com!`)
return
}
/**
* prepare ssh
*/
let ssh = async () => {
let sshModule = await npmciMods.modSsh.load()
await sshModule.ssh()
}
/**
* the main exported prepare function
* @param servieArg describes the service to prepare
*/
export let prepare = async (serviceArg: TPrepService) => {
switch (serviceArg) {
case 'npm':
return await npm()
case 'docker':
return await docker()
case 'docker-gitlab':
return await dockerGitlab()
case 'ssh':
return await ssh()
default:
break
}
}

View File

@ -1 +0,0 @@
export * from '../npmci.plugins'

View File

@ -1,54 +0,0 @@
import * as plugins from './mod.plugins'
import { bash } from '../npmci.bash'
import * as NpmciEnv from '../npmci.env'
import * as npmciMods from '../npmci.mods'
// import interfaces
import { Dockerfile } from '../mod_docker/index'
/**
* type of supported services
*/
export type TPubService = 'npm' | 'docker'
/**
* the main exported publish function.
* @param pubServiceArg references targeted service to publish to
*/
export let publish = async (argvArg: any) => {
let whatToPublish = argvArg._[1]
switch (whatToPublish) {
case 'docker':
return await publishDocker(argvArg)
case 'npm':
return await publishNpm(argvArg)
default:
plugins.beautylog.info('no publish target specified, thus defaulting to npm...')
return await publishNpm(argvArg)
}
}
/**
* tries to publish current cwd to NPM registry
*/
let publishNpm = async (argvArg) => {
let modPrepare = await npmciMods.modPrepare.load()
await modPrepare.prepare('npm')
await bash('npm publish')
plugins.beautylog.ok('Done!')
}
/**
* tries to publish current cwd to Docker registry
*/
let publishDocker = async (argvArg) => {
let modDocker = await npmciMods.modDocker.load()
return await modDocker.readDockerfiles(argvArg)
.then(modDocker.pullDockerfileImages)
.then(modDocker.pushDockerfiles)
.then(dockerfileArray => {
return dockerfileArray
})
}

View File

@ -1 +0,0 @@
export * from '../npmci.plugins'

View File

@ -1,11 +0,0 @@
import * as plugins from './mod.plugins'
let npmciCflare = new plugins.cflare.CflareAccount()
export let purge = async (argvArg) => {
npmciCflare.auth({
email: '',
key: ''
})
npmciCflare.purgeZone(argvArg._[1])
}

View File

@ -1,7 +0,0 @@
export * from '../npmci.plugins'
import * as cflare from 'cflare'
export {
cflare
}

View File

@ -1,32 +0,0 @@
import * as plugins from '../npmci.plugins'
/**
* servezoneRegex is the regex that parses the servezone connection data
* parses strings in the form of "servezone.example.com|3000|somepassword"
*/
let servezoneRegex = /^(.*)\|(.*)\|(.*)/
/**
* holds the results of the parsed servezone env string
*/
let servezoneRegexResultArray = servezoneRegex.exec(process.env.NPMCI_SERVEZONE)
/**
* the data object that is used for the smartsocket client object
*/
let smartsocketClientConstructorOptions = {
alias: 'npmci',
password: servezoneRegexResultArray[3],
port: parseInt(servezoneRegexResultArray[2]),
role: 'ci',
url: servezoneRegexResultArray[1]
}
/**
* the main run function to submit a service to a servezone
*/
export let run = async (configArg) => {
new plugins.smartsocket.SmartsocketClient(
smartsocketClientConstructorOptions
)
}

View File

@ -1,8 +1,19 @@
import * as plugins from './mod.plugins'
let sshRegex = /^(.*)\|(.*)\|(.*)/
let sshInstance: plugins.smartssh.SshInstance
export let handleCli = async (argvArg) => {
if (argvArg._.length >= 2) {
let action: string = argvArg._[1]
switch (action) {
case 'prepare':
await prepare()
break
default:
plugins.beautylog.error(`action >>${action}<< not supported`)
}
}
}
/**
* checks if not undefined
*/
@ -13,7 +24,7 @@ let notUndefined = (stringArg: string) => {
/**
* checks for ENV vars in form of NPMCI_SSHKEY_* and deploys any found ones
*/
export let ssh = async () => {
export let prepare = async () => {
sshInstance = new plugins.smartssh.SshInstance() // init ssh instance
plugins.smartparam.forEachMinimatch(process.env, 'NPMCI_SSHKEY_*', evaluateSshEnv)
if (!process.env.NPMTS_TEST) {
@ -26,21 +37,21 @@ export let ssh = async () => {
/**
* gets called for each found SSH ENV Var and deploys it
*/
let evaluateSshEnv = async (sshkeyEnvVarArg) => {
let resultArray = sshRegex.exec(sshkeyEnvVarArg)
let evaluateSshEnv = async (sshkeyEnvVarArg: string) => {
let sshEnvArray = sshkeyEnvVarArg.split('|')
let sshKey = new plugins.smartssh.SshKey()
plugins.beautylog.info('Found SSH identity for ' + resultArray[1])
if (notUndefined(resultArray[1])) {
plugins.beautylog.info('Found SSH identity for ' + sshEnvArray[1])
if (notUndefined(sshEnvArray[0])) {
plugins.beautylog.log('---> host defined!')
sshKey.host = resultArray[1]
sshKey.host = sshEnvArray[0]
}
if (notUndefined(resultArray[2])) {
if (notUndefined(sshEnvArray[1])) {
plugins.beautylog.log('---> privKey defined!')
sshKey.privKeyBase64 = resultArray[2]
sshKey.privKeyBase64 = sshEnvArray[1]
}
if (notUndefined(resultArray[3])) {
if (notUndefined(sshEnvArray[2])) {
plugins.beautylog.log('---> pubKey defined!')
sshKey.pubKeyBase64 = resultArray[3]
sshKey.pubKeyBase64 = sshEnvArray[2]
}
sshInstance.addKey(sshKey)

View File

@ -1,45 +0,0 @@
import * as plugins from './mod.plugins'
import { bash, yarnAvailable } from '../npmci.bash'
import * as env from '../npmci.env'
import * as npmciMods from '../npmci.mods'
// interfaces
import { Dockerfile } from '../mod_docker/index'
let npmDependencies = async (): Promise<void> => {
plugins.beautylog.info('now installing dependencies:')
if (await yarnAvailable.promise) {
await bash('yarn install')
} else {
await bash('npm install')
}
}
let npmTest = async (): Promise<void> => {
plugins.beautylog.info('now starting tests:')
await bash('npm test')
}
let testDocker = async (argvArg): Promise<Dockerfile[]> => {
let modDocker = await npmciMods.modDocker.load()
return await modDocker.readDockerfiles(argvArg)
.then(modDocker.pullDockerfileImages)
.then(modDocker.testDockerfiles)
}
/**
* the main test function
* @param versionArg
*/
export let test = async (argvArg): Promise<void> => {
let whatToTest = argvArg._[1]
if (whatToTest === 'docker') {
await testDocker(argvArg)
} else {
let modInstall = await npmciMods.modInstall.load()
await modInstall.install(whatToTest)
.then(npmDependencies)
.then(npmTest)
}
}

View File

@ -1 +0,0 @@
export * from '../npmci.plugins'

View File

@ -10,17 +10,6 @@ import * as npmciMods from './npmci.mods'
let smartcli = new plugins.smartcli.Smartcli()
smartcli.addVersion(npmciInfo.version)
// build
smartcli.addCommand('build')
.then(async argvArg => {
let modBuild = await npmciMods.modBuild.load()
await modBuild.build(argvArg)
NpmciEnv.configStore()
}).catch(err => {
console.log(err)
process.exit(1)
})
// clean
smartcli.addCommand('clean')
.then(async (argv) => {
@ -32,6 +21,16 @@ smartcli.addCommand('clean')
process.exit(1)
})
// cloudflare
smartcli.addCommand('cloudflare')
.then(async (argvArg) => {
let modPurge = await npmciMods.modCloudflare.load()
await modPurge.handleCli(argvArg)
await NpmciEnv.configStore()
}).catch(err => {
console.log(err)
})
// command
smartcli.addCommand('command')
.then(async (argv) => {
@ -43,54 +42,42 @@ smartcli.addCommand('command')
process.exit(1)
})
// purge
smartcli.addCommand('purge')
// build
smartcli.addCommand('docker')
.then(async argvArg => {
let modDocker = await npmciMods.modDocker.load()
await modDocker.handleCli(argvArg)
NpmciEnv.configStore()
}).catch(err => {
console.log(err)
process.exit(1)
})
// node
smartcli.addCommand('node')
.then(async (argvArg) => {
let modPurge = await npmciMods.modPurge.load()
await modPurge.purge(argvArg)
}).catch(err => {
console.log(err)
})
// install
smartcli.addCommand('install')
.then(async (argv) => {
let modInstall = await npmciMods.modInstall.load()
await modInstall.install(argv._[1])
let modNode = await npmciMods.modNode.load()
await modNode.handleCli(argvArg)
await NpmciEnv.configStore()
}).catch(err => {
console.log(err)
process.exit(1)
})
// prepare
smartcli.addCommand('prepare')
.then(async argvArg => {
let modPrepare = await npmciMods.modPrepare.load()
await modPrepare.prepare(argvArg._[1])
// npm
smartcli.addCommand('npm')
.then(async (argvArg) => {
let modNpm = await npmciMods.modNpm.load()
await modNpm.handleCli(argvArg)
await NpmciEnv.configStore()
}).catch(err => {
console.log(err)
process.exit(1)
})
// publish
smartcli.addCommand('publish')
.then(async argvArg => {
let modPublish = await npmciMods.modPublish.load()
await modPublish.publish(argvArg)
await NpmciEnv.configStore()
}).catch(err => {
console.log(err)
process.exit(1)
})
// test
smartcli.addCommand('test')
.then(async (argv) => {
let modTest = await npmciMods.modTest.load()
await modTest.test(argv)
// trigger
smartcli.addCommand('ssh')
.then(async (argvArg) => {
let modSsh = await npmciMods.modSsh.load()
await modSsh.handleCli(argvArg)
await NpmciEnv.configStore()
}).catch(err => {
console.log(err)

View File

@ -1,25 +1,19 @@
import * as _modBuild from './mod_build/index'
import * as _modClean from './mod_clean/index'
import * as _modCloudflare from './mod_cloudflare/index'
import * as _modCommand from './mod_command/index'
import * as _modDocker from './mod_docker/index'
import * as _modInstall from './mod_install/index'
import * as _modPublish from './mod_publish/index'
import * as _modPurge from './mod_purge/index'
import * as _modPrepare from './mod_prepare/index'
import * as _modNpm from './mod_npm/index'
import * as _modNode from './mod_node/index'
import * as _modSsh from './mod_ssh/index'
import * as _modTrigger from './mod_trigger/index'
import * as _modTest from './mod_test/index'
import { LazyModule } from 'smartsystem'
export let modBuild = new LazyModule<typeof _modBuild>('./mod_build/index', __dirname)
export let modClean = new LazyModule<typeof _modClean>('./mod_clean/index', __dirname)
export let modCloudflare = new LazyModule<typeof _modCloudflare>('./mod_cloudflare/index', __dirname)
export let modCommand = new LazyModule<typeof _modCommand>('./mod_command/index', __dirname)
export let modDocker = new LazyModule<typeof _modDocker>('./mod_docker/index', __dirname)
export let modInstall = new LazyModule<typeof _modInstall>('./mod_install/index', __dirname)
export let modPublish = new LazyModule<typeof _modPublish>('./mod_publish/index', __dirname)
export let modPurge = new LazyModule<typeof _modPurge>('./mod_purge/index', __dirname)
export let modPrepare = new LazyModule<typeof _modPrepare>('./mod_prepare/index', __dirname)
export let modNode = new LazyModule<typeof _modNode>('./mod_node/index', __dirname)
export let modNpm = new LazyModule<typeof _modNpm>('./mod_npm/index', __dirname)
export let modSsh = new LazyModule<typeof _modSsh>('./mod_ssh/index', __dirname)
export let modTrigger = new LazyModule<typeof _modTrigger>('./mod_trigger/index', __dirname)
export let modTest = new LazyModule<typeof _modTest>('./mod_test/index',__dirname)