npmci/ts/mod_docker/index.ts

139 lines
3.6 KiB
TypeScript
Raw Normal View History

2017-08-27 13:24:17 +00:00
import * as plugins from './mod.plugins'
import * as paths from '../npmci.paths'
import { bash } from '../npmci.bash'
2017-08-27 23:03:59 +00:00
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'
// instances
let npmciRegistryStorage = new RegistryStorage()
export {
Dockerfile,
helpers
}
2017-08-27 13:24:17 +00:00
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) {
2017-08-27 23:03:59 +00:00
let action: string = argvArg._[ 1 ]
2017-08-27 13:24:17 +00:00
switch (action) {
case 'build':
await build()
break
case 'login':
await login()
2017-08-27 13:24:17 +00:00
break
case 'test':
await test()
break
2017-08-27 23:03:59 +00:00
case 'push':
await push(argvArg)
break
case 'pull':
await pull(argvArg)
break
2017-08-27 13:24:17 +00:00
default:
plugins.beautylog.error(`>>npmci docker ...<< action >>${action}<< not supported`)
2017-08-27 13:24:17 +00:00
}
} else {
plugins.beautylog.log(`>>npmci docker ...<< cli arguments invalid... Please read the documentation.`)
2017-08-27 13:24:17 +00:00
}
}
2017-08-27 23:03:59 +00:00
/**
* builds a cwd of Dockerfiles by triggering a promisechain
*/
export let build = async () => {
await prepare()
2017-08-27 23:03:59 +00:00
plugins.beautylog.log('now building Dockerfiles...')
await helpers.readDockerfiles()
.then(helpers.sortDockerfiles)
.then(helpers.mapDockerfiles)
.then(helpers.buildDockerfiles)
}
2017-08-27 13:24:17 +00:00
/**
* login to the DockerRegistries
*/
export let login = async () => {
await prepare()
await npmciRegistryStorage.loginAll()
}
2017-08-27 13:24:17 +00:00
/**
* logs in docker
*/
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!')
2017-08-27 23:35:21 +00:00
process.exit(1)
2017-08-27 13:24:17 +00:00
}
2017-08-27 23:03:59 +00:00
npmciRegistryStorage.addRegistry(new DockerRegistry({
registryUrl: 'registry.gitlab.com',
username: 'gitlab-ci-token',
password: process.env.CI_BUILD_TOKEN
}))
2017-08-27 13:24:17 +00:00
2017-08-27 23:03:59 +00:00
// handle registries
2017-08-27 23:35:21 +00:00
await plugins.smartparam.forEachMinimatch(process.env, 'NPMCI_LOGIN_DOCKER*', async (envString) => {
2017-08-27 23:03:59 +00:00
npmciRegistryStorage.addRegistry(
DockerRegistry.fromEnvString(envString)
)
2017-08-27 13:24:17 +00:00
})
2017-08-27 23:03:59 +00:00
return
2017-08-27 13:24:17 +00:00
}
2017-08-27 23:03:59 +00:00
export let push = async (argvArg) => {
await prepare()
2017-08-27 23:03:59 +00:00
let registryUrlArg = argvArg._[ 2 ]
let suffix = null
if (argvArg._.length >= 4) {
suffix = argvArg._[ 3 ]
2017-08-27 13:24:17 +00:00
}
2017-08-27 23:03:59 +00:00
let dockerfileArray = await helpers.readDockerfiles()
.then(helpers.sortDockerfiles)
.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)
}
2017-08-27 23:03:59 +00:00
for (let dockerfile of dockerfileArray) {
2017-08-28 00:36:12 +00:00
await dockerfile.push(localDockerRegistry, suffix)
2017-08-27 13:24:17 +00:00
}
}
2017-08-27 23:03:59 +00:00
export let pull = async (argvArg) => {
await prepare()
2017-08-27 23:03:59 +00:00
let registryUrlArg = argvArg._[ 2 ]
let suffix = null
if (argvArg._.length >= 4) {
suffix = argvArg._[ 3 ]
2017-08-27 13:24:17 +00:00
}
2017-08-27 23:03:59 +00:00
let localDockerRegistry = npmciRegistryStorage.getRegistryByUrl(registryUrlArg)
let dockerfileArray = await helpers.readDockerfiles()
.then(helpers.sortDockerfiles)
.then(helpers.mapDockerfiles)
for (let dockerfile of dockerfileArray) {
2017-08-28 00:36:23 +00:00
await dockerfile.pull(localDockerRegistry, suffix)
2017-08-27 13:24:17 +00:00
}
}
2017-08-27 23:03:59 +00:00
export let test = async () => {
await prepare()
2017-08-27 23:03:59 +00:00
return await helpers.readDockerfiles()
.then(helpers.testDockerfiles)
2017-08-27 13:24:17 +00:00
}