update async functions

This commit is contained in:
Philipp Kunz 2017-03-08 14:50:41 +01:00
parent d9b8eb3bf0
commit a54015da16
10 changed files with 197 additions and 236 deletions

View File

@ -1,15 +1,13 @@
import * as plugins from './npmci.plugins'
import {bash} from './npmci.bash'
import { bash } from './npmci.bash'
export let command = () => {
let done = plugins.q.defer()
let wrappedCommand: string = ''
let argvArray = process.argv
for (let i = 3; i < argvArray.length; i++) {
wrappedCommand = wrappedCommand + argvArray[i]
if (i + 1 !== argvArray.length) { wrappedCommand = wrappedCommand + ' ' }
}
bash(wrappedCommand)
done.resolve()
return done.promise
export let command = async () => {
let wrappedCommand: string = ''
let argvArray = process.argv
for (let i = 3; i < argvArray.length; i++) {
wrappedCommand = wrappedCommand + argvArray[i]
if (i + 1 !== argvArray.length) { wrappedCommand = wrappedCommand + ' ' }
}
await bash(wrappedCommand)
return
}

View File

@ -4,16 +4,14 @@ import * as plugins from './npmci.plugins'
import * as paths from './npmci.paths'
export interface INpmciOptions {
globalNpmTools: string[]
globalNpmTools: string[]
}
export let getConfig = () => {
let done = q.defer<INpmciOptions>()
let npmciNpmextra = new plugins.npmextra.Npmextra(paths.cwd)
let defaultConfig: INpmciOptions = {
globalNpmTools: []
}
let npmciConfig = npmciNpmextra.dataFor<INpmciOptions>('npmci', defaultConfig)
done.resolve(npmciConfig)
return done.promise
export let getConfig = async (): Promise<INpmciOptions> => {
let npmciNpmextra = new plugins.npmextra.Npmextra(paths.cwd)
let defaultConfig: INpmciOptions = {
globalNpmTools: []
}
let npmciConfig = npmciNpmextra.dataFor<INpmciOptions>('npmci', defaultConfig)
return npmciConfig
}

View File

@ -1,7 +1,7 @@
import * as plugins from './npmci.plugins'
import * as paths from './npmci.paths'
import {GitRepo} from 'smartstring'
import {Dockerfile} from './npmci.build.docker'
import { GitRepo } from 'smartstring'
import { Dockerfile } from './npmci.build.docker'
export let repo: GitRepo
if (process.env.CI_BUILD_REPO) repo = new GitRepo(process.env.CI_BUILD_REPO)
@ -11,48 +11,47 @@ export let buildStage: string = process.env.CI_BUILD_STAGE
// handling config between commands
export let dockerRegistry: string // will be set by npmci.prepare
export let setDockerRegistry = (dockerRegistryArg: string) => {
dockerRegistry = dockerRegistryArg
dockerRegistry = dockerRegistryArg
}
export let dockerFilesBuilt: Dockerfile[] = []
export let dockerFiles: Dockerfile[] = []
export let config = {
dockerRegistry: undefined, // this will be set later on store
dockerFilesBuilt: dockerFilesBuilt,
dockerFiles: dockerFiles,
project: undefined
dockerRegistry: undefined, // this will be set later on store
dockerFilesBuilt: dockerFilesBuilt,
dockerFiles: dockerFiles,
project: undefined
}
export let configStore = () => {
config.dockerRegistry = dockerRegistry
plugins.smartfile.memory.toFsSync(
JSON.stringify(config),
paths.NpmciPackageConfig
)
config.dockerRegistry = dockerRegistry
plugins.smartfile.memory.toFsSync(
JSON.stringify(config),
paths.NpmciPackageConfig
)
}
let configLoad = () => {
// internal config to transfer information in between npmci shell calls
try {
plugins.lodash.assign(config,plugins.smartfile.fs.toObjectSync(paths.NpmciPackageConfig,'json'))
}
catch (err) {
configStore()
plugins.beautylog.log('config initialized!')
}
// internal config to transfer information in between npmci shell calls
try {
plugins.lodash.assign(config, plugins.smartfile.fs.toObjectSync(paths.NpmciPackageConfig, 'json'))
} catch (err) {
configStore()
plugins.beautylog.log('config initialized!')
}
// project config
try {
if (!config.project) {
config.project = plugins.smartfile.fs.toObjectSync(paths.NpmciProjectDir,'npmci.json')
plugins.beautylog.ok('project config found!')
};
}
catch (err) {
config.project = {}
plugins.beautylog.log('no project config found, so proceeding with default behaviour!')
}
// project config
try {
if (!config.project) {
config.project = plugins.smartfile.fs.toObjectSync(paths.NpmciProjectDir, 'npmci.json')
plugins.beautylog.ok('project config found!')
};
}
catch (err) {
config.project = {}
plugins.beautylog.log('no project config found, so proceeding with default behaviour!')
}
config.dockerRegistry ? dockerRegistry = config.dockerRegistry : void(0)
config.dockerFilesBuilt ? dockerFilesBuilt = config.dockerFilesBuilt : void(0)
config.dockerRegistry ? dockerRegistry = config.dockerRegistry : void (0)
config.dockerFilesBuilt ? dockerFilesBuilt = config.dockerFilesBuilt : void (0)
}
configLoad()

View File

@ -2,8 +2,12 @@ import * as plugins from './npmci.plugins'
import * as configModule from './npmci.config'
import { bash, bashNoError } from './npmci.bash'
import { nvmAvailable } from './npmci.bash'
export let install = (versionArg) => {
let done = plugins.q.defer()
/**
* Install a specific version of node
* @param versionArg
*/
export let install = async (versionArg) => {
plugins.beautylog.log(`now installing node version ${versionArg}`)
let version: string
if (versionArg === 'stable') {
@ -16,30 +20,28 @@ export let install = (versionArg) => {
version = versionArg
};
if (nvmAvailable) {
bash(`nvm install ${version} && nvm alias default ${version}`)
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!')
};
bash('node -v')
bash('npm -v')
await bash('node -v')
await bash('npm -v')
// lets look for further config
configModule.getConfig()
.then(configArg => {
.then(async configArg => {
plugins.beautylog.log('Now checking for needed global npm tools...')
for (let npmTool of configArg.globalNpmTools) {
plugins.beautylog.info(`Checking for global "${npmTool}"`)
let whichOutput = bashNoError(`which ${npmTool}`)
let whichOutput: string = await bashNoError(`which ${npmTool}`)
let toolAvailable: boolean = !((/not\sfound/.test(whichOutput)) || whichOutput === '')
if (toolAvailable) {
plugins.beautylog.log(`Tool ${npmTool} is available`)
} else {
plugins.beautylog.info(`globally installing ${npmTool} from npm`)
bash(`npm install ${npmTool} -q -g`)
await bash(`npm install ${npmTool} -q -g`)
}
}
plugins.beautylog.success('all global npm tools specified in npmextra.json are now available!')
done.resolve()
})
return done.promise
}

View File

@ -1,5 +1,5 @@
import * as plugins from './npmci.plugins'
import {bash} from './npmci.bash'
import { bash } from './npmci.bash'
import * as env from './npmci.env'
import * as sshModule from './npmci.ssh'
@ -9,85 +9,74 @@ import * as sshModule from './npmci.ssh'
/**
* defines possible prepare services
*/
export type TPrepService = 'npm' | 'docker' | 'docker-gitlab' | 'ssh';
export type TPrepService = 'npm' | 'docker' | 'docker-gitlab' | 'ssh'
/**
* authenticates npm with token from env var
*/
let npm = function(){
let done = plugins.q.defer()
let npmrcPrefix: string = '//registry.npmjs.org/:_authToken='
let npmToken: string = process.env.NPMCI_TOKEN_NPM
let npmrcFileString = 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')
done.resolve()
return done.promise
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 = function(){
let done = plugins.q.defer()
env.setDockerRegistry('docker.io')
let dockerRegex = /^([a-zA-Z0-9\.]*)\|([a-zA-Z0-9\.]*)/
if (!process.env.NPMCI_LOGIN_DOCKER) {
plugins.beautylog.error('You have to specify Login Data to the Docker Registry')
process.exit(1)
}
plugins.shelljs.exec('docker login -u gitlab-ci-token -p ' + process.env.CI_BUILD_TOKEN + ' ' + 'registry.gitlab.com') // Always also login to GitLab Registry
let dockerRegexResultArray = dockerRegex.exec(process.env.NPMCI_LOGIN_DOCKER)
let username = dockerRegexResultArray[1]
let password = dockerRegexResultArray[2]
plugins.shelljs.exec('docker login -u ' + username + ' -p ' + password)
done.resolve()
return done.promise
let docker = async () => {
env.setDockerRegistry('docker.io')
let dockerRegex = /^([a-zA-Z0-9\.]*)\|([a-zA-Z0-9\.]*)/
if (!process.env.NPMCI_LOGIN_DOCKER) {
plugins.beautylog.error('You have to specify Login Data to the Docker Registry')
process.exit(1)
}
plugins.shelljs.exec('docker login -u gitlab-ci-token -p ' + process.env.CI_BUILD_TOKEN + ' ' + 'registry.gitlab.com') // Always also login to GitLab Registry
let dockerRegexResultArray = dockerRegex.exec(process.env.NPMCI_LOGIN_DOCKER)
let username = dockerRegexResultArray[1]
let password = dockerRegexResultArray[2]
plugins.shelljs.exec('docker login -u ' + username + ' -p ' + password)
return
}
/**
* prepare docker for gitlab registry
*/
let dockerGitlab = function(){
let done = plugins.q.defer()
env.setDockerRegistry('registry.gitlab.com')
plugins.shelljs.exec('docker login -u gitlab-ci-token -p ' + process.env.CI_BUILD_TOKEN + ' ' + 'registry.gitlab.com')
done.resolve()
return done.promise
let dockerGitlab = async () => {
env.setDockerRegistry('registry.gitlab.com')
plugins.shelljs.exec('docker login -u gitlab-ci-token -p ' + process.env.CI_BUILD_TOKEN + ' ' + 'registry.gitlab.com')
return
}
/**
* prepare ssh
*/
let ssh = function(){
let done = plugins.q.defer()
sshModule.ssh()
.then(done.resolve)
return done.promise
let ssh = async () => {
await sshModule.ssh()
}
/**
* the main exported prepare function
* @param servieArg describes the service to prepare
*/
export let prepare = function(serviceArg: TPrepService){
switch (serviceArg) {
case 'npm':
return npm()
case 'docker':
return docker()
case 'docker-gitlab':
return dockerGitlab()
case 'ssh':
return ssh()
default:
break
}
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,49 +1,46 @@
import * as plugins from './npmci.plugins'
import {prepare} from './npmci.prepare'
import {bash} from './npmci.bash'
import { prepare } from './npmci.prepare'
import { bash } from './npmci.bash'
import * as NpmciEnv from './npmci.env'
import * as NpmciBuildDocker from './npmci.build.docker'
/**
* type of supported services
*/
export type TPubService = 'npm' | 'docker';
export type TPubService = 'npm' | 'docker'
/**
* the main exported publish function.
* @param pubServiceArg references targeted service to publish to
*/
export let publish = (pubServiceArg: TPubService = 'npm') => {
switch (pubServiceArg) {
case 'npm':
return publishNpm()
case 'docker':
return publishDocker()
}
export let publish = async (pubServiceArg: TPubService = 'npm') => {
switch (pubServiceArg) {
case 'npm':
return await publishNpm()
case 'docker':
return await publishDocker()
}
}
/**
* tries to publish current cwd to NPM registry
*/
let publishNpm = function(){
let done = plugins.q.defer()
prepare('npm')
.then(function(){
bash('npm publish')
plugins.beautylog.ok('Done!')
done.resolve()
})
return done.promise
let publishNpm = async () => {
await prepare('npm')
.then(async function () {
await bash('npm publish')
plugins.beautylog.ok('Done!')
})
}
/**
* tries to pubish current cwd to Docker registry
*/
let publishDocker = function(){
let done = plugins.q.defer()
NpmciBuildDocker.readDockerfiles()
.then(NpmciBuildDocker.pullDockerfileImages)
.then(NpmciBuildDocker.pushDockerfiles)
.then(done.resolve)
return done.promise
let publishDocker = async () => {
return await NpmciBuildDocker.readDockerfiles()
.then(NpmciBuildDocker.pullDockerfileImages)
.then(NpmciBuildDocker.pushDockerfiles)
.then(dockerfileArray => {
return dockerfileArray
})
}

View File

@ -25,7 +25,7 @@ let smartsocketClientConstructorOptions = {
/**
* the main run function to submit a service to a servezone
*/
export let run = (configArg) => {
export let run = async (configArg) => {
new plugins.smartsocket.SmartsocketClient(
smartsocketClientConstructorOptions
)

View File

@ -6,45 +6,43 @@ let sshInstance: plugins.smartssh.SshInstance
/**
* checks for ENV vars in form of NPMCI_SSHKEY_* and deploys any found ones
*/
export let ssh = () => {
let done = plugins.q.defer()
sshInstance = new plugins.smartssh.SshInstance() // init ssh instance
plugins.smartparam.forEachMinimatch(process.env,'NPMCI_SSHKEY_*',evaluateSshEnv)
if (!process.env.NPMTS_TEST) {
sshInstance.writeToDisk()
} else {
plugins.beautylog.log('In test mode, so not storing SSH keys to disk!')
};
done.resolve()
return done.promise
export let ssh = async () => {
sshInstance = new plugins.smartssh.SshInstance() // init ssh instance
plugins.smartparam.forEachMinimatch(process.env, 'NPMCI_SSHKEY_*', evaluateSshEnv)
if (!process.env.NPMTS_TEST) {
sshInstance.writeToDisk()
} else {
plugins.beautylog.log('In test mode, so not storing SSH keys to disk!')
};
}
/**
* gets called for each found SSH ENV Var and deploys it
*/
let evaluateSshEnv = (sshkeyEnvVarArg) => {
let resultArray = sshRegex.exec(sshkeyEnvVarArg)
let sshKey = new plugins.smartssh.SshKey()
plugins.beautylog.info('Found SSH identity for ' + resultArray[1])
if (notUndefined(resultArray[1])) {
plugins.beautylog.log('---> host defined!')
sshKey.host = resultArray[1]
}
if (notUndefined(resultArray[2])) {
plugins.beautylog.log('---> privKey defined!')
sshKey.privKeyBase64 = resultArray[2]
};
if (notUndefined(resultArray[3])) {
'---> pubKey defined!'
sshKey.pubKeyBase64 = resultArray[3]
};
let evaluateSshEnv = async (sshkeyEnvVarArg) => {
let resultArray = sshRegex.exec(sshkeyEnvVarArg)
let sshKey = new plugins.smartssh.SshKey()
plugins.beautylog.info('Found SSH identity for ' + resultArray[1])
if (notUndefined(resultArray[1])) {
plugins.beautylog.log('---> host defined!')
sshKey.host = resultArray[1]
}
if (notUndefined(resultArray[2])) {
plugins.beautylog.log('---> privKey defined!')
sshKey.privKeyBase64 = resultArray[2]
};
if (notUndefined(resultArray[3])) {
'---> pubKey defined!'
sshKey.pubKeyBase64 = resultArray[3]
};
sshInstance.addKey(sshKey)
sshInstance.addKey(sshKey)
return
}
/**
* checks if not undefined
*/
let notUndefined = (stringArg: string) => {
return (stringArg && stringArg !== 'undefined' && stringArg !== '##')
return (stringArg && stringArg !== 'undefined' && stringArg !== '##')
}

View File

@ -1,49 +1,32 @@
import * as plugins from './npmci.plugins'
import {bash} from './npmci.bash'
import {install} from './npmci.install'
import { bash } from './npmci.bash'
import { install } from './npmci.install'
import * as env from './npmci.env'
import * as NpmciBuildDocker from './npmci.build.docker'
export let test = (versionArg) => {
let done = plugins.q.defer()
if (versionArg === 'docker') {
testDocker()
.then(() => {
done.resolve()
})
} else {
install(versionArg)
.then(npmDependencies)
.then(npmTest)
.then(() => {
done.resolve()
})
}
return done.promise
export let test = async (versionArg): Promise<void> => {
if (versionArg === 'docker') {
await testDocker()
} else {
await install(versionArg)
.then(npmDependencies)
.then(npmTest)
}
}
let npmDependencies = function(){
let done = plugins.q.defer()
plugins.beautylog.info('now installing dependencies:')
bash('npm install')
done.resolve()
return done.promise
let npmDependencies = async () => {
plugins.beautylog.info('now installing dependencies:')
await bash('npm install')
}
let npmTest = () => {
let done = plugins.q.defer()
plugins.beautylog.info('now starting tests:')
bash('npm test')
done.resolve()
return done.promise
let npmTest = async () => {
plugins.beautylog.info('now starting tests:')
await bash('npm test')
}
let testDocker = function(){
let done = plugins.q.defer()
NpmciBuildDocker.readDockerfiles()
.then(NpmciBuildDocker.pullDockerfileImages)
.then(NpmciBuildDocker.testDockerfiles)
.then(done.resolve)
return done.promise
let testDocker = async (): Promise<NpmciBuildDocker.Dockerfile[]> => {
return await NpmciBuildDocker.readDockerfiles()
.then(NpmciBuildDocker.pullDockerfileImages)
.then(NpmciBuildDocker.testDockerfiles)
}

View File

@ -4,27 +4,24 @@ import { bash } from './npmci.bash'
let triggerValueRegex = /^([a-zA-Z0-9\.]*)\|([a-zA-Z0-9\.]*)\|([a-zA-Z0-9\.]*)\|([a-zA-Z0-9\.]*)\|?([a-zA-Z0-9\.\-\/]*)/
export let trigger = function () {
let done = plugins.q.defer()
plugins.beautylog.info('now running triggers')
plugins.smartparam.forEachMinimatch(process.env, 'NPMCI_TRIGGER_*', evaluateTrigger)
done.resolve()
return done.promise
export let trigger = async () => {
plugins.beautylog.info('now running triggers')
plugins.smartparam.forEachMinimatch(process.env, 'NPMCI_TRIGGER_*', evaluateTrigger)
}
let evaluateTrigger = (triggerEnvVarArg) => {
let triggerRegexResultArray = triggerValueRegex.exec(triggerEnvVarArg)
let regexDomain = triggerRegexResultArray[1]
let regexProjectId = triggerRegexResultArray[2]
let regexProjectTriggerToken = triggerRegexResultArray[3]
let regexRefName = triggerRegexResultArray[4]
let regexTriggerName
if (triggerRegexResultArray.length === 6) {
regexTriggerName = triggerRegexResultArray[5]
} else {
regexTriggerName = 'Unnamed Trigger'
}
plugins.beautylog.info('Found Trigger!')
plugins.beautylog.log('triggering build for ref ' + regexRefName + ' of ' + regexTriggerName)
plugins.request.post('https://gitlab.com/api/v3/projects/' + regexProjectId + '/trigger/builds', { form: { token: regexProjectTriggerToken, ref: regexRefName } })
let evaluateTrigger = async (triggerEnvVarArg) => {
let triggerRegexResultArray = triggerValueRegex.exec(triggerEnvVarArg)
let regexDomain = triggerRegexResultArray[1]
let regexProjectId = triggerRegexResultArray[2]
let regexProjectTriggerToken = triggerRegexResultArray[3]
let regexRefName = triggerRegexResultArray[4]
let regexTriggerName
if (triggerRegexResultArray.length === 6) {
regexTriggerName = triggerRegexResultArray[5]
} else {
regexTriggerName = 'Unnamed Trigger'
}
plugins.beautylog.info('Found Trigger!')
plugins.beautylog.log('triggering build for ref ' + regexRefName + ' of ' + regexTriggerName)
plugins.request.post('https://gitlab.com/api/v3/projects/' + regexProjectId + '/trigger/builds', { form: { token: regexProjectTriggerToken, ref: regexRefName } })
}