2017-05-19 10:09:33 +00:00
|
|
|
import * as plugins from './mod.plugins'
|
|
|
|
import { bash } from '../npmci.bash'
|
|
|
|
import * as env from '../npmci.env'
|
2017-06-15 13:46:08 +00:00
|
|
|
import * as npmciMods from '../npmci.mods'
|
2016-09-04 14:05:47 +00:00
|
|
|
|
2016-11-24 22:21:40 +00:00
|
|
|
// types
|
2016-09-04 14:05:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* defines possible prepare services
|
|
|
|
*/
|
2017-03-08 13:50:41 +00:00
|
|
|
export type TPrepService = 'npm' | 'docker' | 'docker-gitlab' | 'ssh'
|
2016-09-04 14:05:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* authenticates npm with token from env var
|
|
|
|
*/
|
2017-03-08 13:50:41 +00:00
|
|
|
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
|
2016-11-24 22:21:40 +00:00
|
|
|
}
|
2016-06-02 23:58:37 +00:00
|
|
|
|
2016-09-04 14:05:47 +00:00
|
|
|
/**
|
|
|
|
* logs in docker
|
|
|
|
*/
|
2017-03-08 13:50:41 +00:00
|
|
|
let docker = async () => {
|
2017-07-27 11:15:39 +00:00
|
|
|
env.setDockerRegistry('docker.io') // TODO: checkup why we set this here
|
2017-03-08 13:50:41 +00:00
|
|
|
let dockerRegex = /^([a-zA-Z0-9\.]*)\|([a-zA-Z0-9\.]*)/
|
2017-07-27 11:15:39 +00:00
|
|
|
|
|
|
|
// Login external reigstry
|
2017-03-08 13:50:41 +00:00
|
|
|
if (!process.env.NPMCI_LOGIN_DOCKER) {
|
2017-07-27 11:15:39 +00:00
|
|
|
plugins.beautylog.warn('You have to specify Login Data to an external Docker Registry')
|
|
|
|
plugins.beautylog.warn('|- As a result only the gitlab registry is availble for this build.')
|
|
|
|
} else {
|
|
|
|
let dockerRegexResultArray = dockerRegex.exec(process.env.NPMCI_LOGIN_DOCKER)
|
|
|
|
let username = dockerRegexResultArray[1]
|
|
|
|
let password = dockerRegexResultArray[2]
|
|
|
|
await bash('docker login -u ' + username + ' -p ' + password)
|
2017-03-08 13:50:41 +00:00
|
|
|
}
|
2017-07-27 11:15:39 +00:00
|
|
|
|
|
|
|
// Always login to GitLab Registry
|
|
|
|
plugins.shelljs.exec('docker login -u gitlab-ci-token -p ' + process.env.CI_BUILD_TOKEN + ' ' + 'registry.gitlab.com')
|
2017-03-08 13:50:41 +00:00
|
|
|
return
|
2016-06-01 03:42:37 +00:00
|
|
|
}
|
|
|
|
|
2016-09-04 14:05:47 +00:00
|
|
|
/**
|
|
|
|
* prepare docker for gitlab registry
|
|
|
|
*/
|
2017-03-08 13:50:41 +00:00
|
|
|
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
|
2016-06-02 23:58:37 +00:00
|
|
|
}
|
2016-06-01 03:42:37 +00:00
|
|
|
|
2016-09-04 14:05:47 +00:00
|
|
|
/**
|
|
|
|
* prepare ssh
|
|
|
|
*/
|
2017-03-08 13:50:41 +00:00
|
|
|
let ssh = async () => {
|
2017-06-15 13:46:08 +00:00
|
|
|
let sshModule = await npmciMods.modSsh.load()
|
2017-03-08 13:50:41 +00:00
|
|
|
await sshModule.ssh()
|
2016-11-24 22:21:40 +00:00
|
|
|
}
|
2016-06-25 14:29:06 +00:00
|
|
|
|
2016-09-04 14:05:47 +00:00
|
|
|
/**
|
|
|
|
* the main exported prepare function
|
|
|
|
* @param servieArg describes the service to prepare
|
|
|
|
*/
|
2017-03-08 13:50:41 +00:00
|
|
|
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
|
|
|
|
}
|
2016-11-24 22:21:40 +00:00
|
|
|
}
|