function getIds(env) { let uid = env.TLN_UID; if (env.TLN_DOCKER_SUFFIX) { uid = [uid, env.TLN_DOCKER_SUFFIX].join('-'); } return { uid, tag: [uid, env.TLN_VERSION].join(':'), fileName: [uid, env.TLN_VERSION].join('-') }; } module.exports = { tags: async (tln) => [], options: async (tln, args) => { args .prefix('TLN_DOCKER') .option('force', { describe: '', default: false, type: 'boolean' }) .option('tty', { describe: '', default: false, type: 'boolean' }) .option('rm', { describe: '', default: false, type: 'boolean' }) .option('port', { describe: '', default: [], type: 'array' }) .option('build-arg', { describe: '', default: [], type: 'array' }) .option('build-context', { describe: '', default: '.', type: 'string' }) .option('env', { describe: '', default: [], type: 'array' }) .option('env-file', { describe: '', default: [], type: 'array' }) .option('volume', { describe: '', default: [], type: 'array' }) .option('registry', { describe: '', default: null, type: 'string' }) .option('entrypoint', { describe: '', default: null, type: 'string' }) .option('entrypoint-params', { describe: '', default: null, type: 'string' }) .option('dockerfile', { describe: '', default: null, type: 'string' }) .option('config', { describe: '', default: null, type: 'string' }) .option('suffix', { describe: '', default: null, type: 'string' }) ; }, dotenvs: async (tln) => [], inherits: async (tln) => [], depends: async (tln) => [], env: async (tln, env) => {}, steps: async (tln) => [ { id: 'install-default', builder: async (tln, script) => script.set([ 'sudo curl -fsSL https://get.docker.com | bash' ]) }, { id: 'docker-cleanup', builder: async (tln, script) => { const force = script.env.TLN_DOCKER_FORCE?` --force`:'' script.set([` docker container prune${force} docker image prune${force} docker builder prune${force} ` ]) }}, { id: 'docker-rmi-all', builder: async (tln, script) => script.set([ 'docker rmi $(docker images --format "{{.Repository}}:{{.Tag}}" | xargs)' ]) }, { id: 'docker-build', builder: async (tln, script) => { const { uid, tag } = getIds(script.env); const buildArgs = script.env.TLN_DOCKER_BUILD_ARG.map(v => ' --build-arg ' + v).join(''); const file = script.env.TLN_DOCKER_DOCKERFILE?` -f ${script.env.TLN_DOCKER_DOCKERFILE}`:'' script.set([` docker build${buildArgs} -t ${tag}${file} ${script.env.TLN_DOCKER_BUILD_CONTEXT} `]); return true; } }, { id: 'docker-run', builder: async (tln, script) => { const { uid, tag } = getIds(script.env); const tty = script.env.TLN_DOCKER_TTY?' -it':' -d'; const rm = script.env.TLN_DOCKER_RM?' --rm':''; const ports = script.env.TLN_DOCKER_PORT.map(v => ' -p ' + v).join(''); const envs = script.env.TLN_DOCKER_ENV.map(v => ' --env ' + ['\'', v, '\''].join('')).join(''); const envFiles = script.env.TLN_DOCKER_ENV_FILE.map(v => ' --env-file ' + v).join(''); const volumes = script.env.TLN_DOCKER_VOLUME.map(v => ' --volume ' + v).join(''); const entry = script.env.TLN_DOCKER_ENTRYPOINT?` --entrypoint ${script.env.TLN_DOCKER_ENTRYPOINT}`:''; const params = script.env.TLN_DOCKER_ENTRYPOINT_PARAMS?` ${script.env.TLN_DOCKER_ENTRYPOINT_PARAMS}`:''; script.set([` docker run${tty}${rm}${ports}${envFiles}${envs}${volumes}${entry} --name ${uid} ${tag}${params} `]); return true; } }, { id: 'docker-tag', builder: async (tln, script) => { const { uid, tag } = getIds(script.env); const tag2 = [script.env.TLN_DOCKER_REGISTRY, tag].join('/'); script.set([` docker tag ${tag} ${tag2} `]); return true; } }, { id: 'docker-push', builder: async (tln, script) => { const { uid, tag } = getIds(script.env); const tag2 = [script.env.TLN_DOCKER_REGISTRY, tag].join('/'); const config = script.env.TLN_DOCKER_CONFIG?` --config ${script.env.TLN_DOCKER_CONFIG}`:''; script.set([` docker push${config} ${tag2} `]); return true; } }, { id: 'docker-save', builder: async (tln, script) => { const { uid, tag, fileName } = getIds(script.env); script.set([ 'docker save -o ${fileName}.tar ${tag}' ]) } }, { id: 'docker-load', builder: async (tln, script) => { const { uid, tag, fileName } = getIds(script.env); script.set([` docker load -i ${fileName}.tar `]); } }, { id: 'docker-stop', builder: async (tln, script) => { const { uid, tag, fileName } = getIds(script.env); script.set([` docker stop ${uid} || true docker rm ${uid} || true docker rmi ${tag} || true `]); } } ], components: async (tln) => [] }