fix(core): update
This commit is contained in:
@ -12,7 +12,7 @@
|
||||
import * as smartanalytics from '@pushrocks/smartanalytics';
|
||||
const npmdockerAnalytics = new smartanalytics.Analytics({
|
||||
apiEndPoint: 'https://pubapi.lossless.one',
|
||||
appName: 'npmdocker',
|
||||
appName: 'tsdocker',
|
||||
projectId: 'gitzone'
|
||||
});
|
||||
npmdockerAnalytics.recordEvent('npmtoolexecution', {
|
||||
|
@ -1,5 +1,5 @@
|
||||
import './analytics';
|
||||
import * as plugins from './npmdocker.plugins';
|
||||
import * as cli from './npmdocker.cli';
|
||||
import * as plugins from './tsdocker.plugins';
|
||||
import * as cli from './tsdocker.cli';
|
||||
|
||||
cli.run();
|
||||
|
@ -1,75 +0,0 @@
|
||||
import * as plugins from './npmdocker.plugins';
|
||||
import * as paths from './npmdocker.paths';
|
||||
|
||||
// modules
|
||||
import * as ConfigModule from './npmdocker.config';
|
||||
import * as DockerModule from './npmdocker.docker';
|
||||
|
||||
let npmdockerCli = new plugins.smartcli.Smartcli();
|
||||
|
||||
export let run = () => {
|
||||
npmdockerCli.standardTask().subscribe(async argvArg => {
|
||||
let configArg = await ConfigModule.run().then(DockerModule.run);
|
||||
if (configArg.exitCode === 0) {
|
||||
plugins.beautylog.success('container ended all right!');
|
||||
} else {
|
||||
plugins.beautylog.error(`container ended with error! Exit Code is ${configArg.exitCode}`);
|
||||
process.exit(1);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* this command is executed inside docker and meant for use from outside docker
|
||||
*/
|
||||
npmdockerCli.addCommand('runinside').subscribe(async argvArg => {
|
||||
plugins.beautylog.ok('Allright. We are now in Docker!');
|
||||
plugins.beautylog.log('now trying to run your specified command');
|
||||
let configArg = await ConfigModule.run();
|
||||
const smartshellInstance = new plugins.smartshell.Smartshell({
|
||||
executor: 'bash'
|
||||
});
|
||||
await smartshellInstance.exec(configArg.command).then(response => {
|
||||
if (response.exitCode !== 0) {
|
||||
process.exit(1);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
npmdockerCli.addCommand('clean').subscribe(async argvArg => {
|
||||
plugins.beautylog.ora.start();
|
||||
plugins.beautylog.ora.text('cleaning up docker env...');
|
||||
if (argvArg.all) {
|
||||
const smartshellInstance = new plugins.smartshell.Smartshell({
|
||||
executor: 'bash'
|
||||
});
|
||||
plugins.beautylog.ora.text('killing any running docker containers...');
|
||||
await smartshellInstance.exec(`docker kill $(docker ps -q)`);
|
||||
|
||||
plugins.beautylog.ora.text('removing stopped containers...');
|
||||
await smartshellInstance.exec(`docker rm $(docker ps -a -q)`);
|
||||
|
||||
plugins.beautylog.ora.text('removing images...');
|
||||
await smartshellInstance.exec(`docker rmi -f $(docker images -q -f dangling=true)`);
|
||||
|
||||
plugins.beautylog.ora.text('removing all other images...');
|
||||
await smartshellInstance.exec(`docker rmi $(docker images -a -q)`);
|
||||
|
||||
plugins.beautylog.ora.text('removing all volumes...');
|
||||
await smartshellInstance.exec(`docker volume rm $(docker volume ls -f dangling=true -q)`);
|
||||
}
|
||||
plugins.beautylog.ora.endOk('docker environment now is clean!');
|
||||
});
|
||||
|
||||
npmdockerCli.addCommand('speedtest').subscribe(async argvArg => {
|
||||
const smartshellInstance = new plugins.smartshell.Smartshell({
|
||||
executor: 'bash'
|
||||
});
|
||||
plugins.beautylog.figletSync('npmdocker');
|
||||
plugins.beautylog.ok('Starting speedtest');
|
||||
await smartshellInstance.exec(
|
||||
`docker pull tianon/speedtest && docker run --rm tianon/speedtest`
|
||||
);
|
||||
});
|
||||
|
||||
npmdockerCli.startParse();
|
||||
};
|
@ -1,40 +0,0 @@
|
||||
import * as plugins from './npmdocker.plugins';
|
||||
import * as paths from './npmdocker.paths';
|
||||
|
||||
// interfaces
|
||||
import { IKeyValueObject } from '@pushrocks/qenv';
|
||||
|
||||
export interface IConfig {
|
||||
baseImage: string;
|
||||
command: string;
|
||||
dockerSock: boolean;
|
||||
exitCode?: number;
|
||||
keyValueObjectArray: IKeyValueObject[];
|
||||
}
|
||||
|
||||
let getQenvKeyValueObject = async () => {
|
||||
let qenvKeyValueObjectArray: IKeyValueObject[];
|
||||
if (plugins.smartfile.fs.fileExistsSync(plugins.path.join(paths.cwd, 'qenv.yml'))) {
|
||||
qenvKeyValueObjectArray = new plugins.qenv.Qenv(paths.cwd, '.nogit/').keyValueObjectArray;
|
||||
} else {
|
||||
qenvKeyValueObjectArray = [];
|
||||
}
|
||||
return qenvKeyValueObjectArray;
|
||||
};
|
||||
|
||||
let buildConfig = async (qenvKeyValueObjectArrayArg: IKeyValueObject[]) => {
|
||||
let npmextra = new plugins.npmextra.Npmextra(paths.cwd);
|
||||
let config = npmextra.dataFor<IConfig>('npmdocker', {
|
||||
baseImage: 'hosttoday/ht-docker-node:npmdocker',
|
||||
init: 'rm -rf node_nodules/ && yarn install',
|
||||
command: 'npmci npm test',
|
||||
dockerSock: false,
|
||||
keyValueObjectArray: qenvKeyValueObjectArrayArg
|
||||
});
|
||||
return config;
|
||||
};
|
||||
|
||||
export let run = async (): Promise<IConfig> => {
|
||||
let config = await getQenvKeyValueObject().then(buildConfig);
|
||||
return config;
|
||||
};
|
88
ts/tsdocker.cli.ts
Normal file
88
ts/tsdocker.cli.ts
Normal file
@ -0,0 +1,88 @@
|
||||
import * as plugins from './tsdocker.plugins';
|
||||
import * as paths from './tsdocker.paths';
|
||||
|
||||
// modules
|
||||
import * as ConfigModule from './tsdocker.config';
|
||||
import * as DockerModule from './tsdocker.docker';
|
||||
|
||||
import { logger, ora } from './tsdocker.logging';
|
||||
|
||||
const tsdockerCli = new plugins.smartcli.Smartcli();
|
||||
|
||||
export let run = () => {
|
||||
tsdockerCli.standardTask().subscribe(async argvArg => {
|
||||
let configArg = await ConfigModule.run().then(DockerModule.run);
|
||||
if (configArg.exitCode === 0) {
|
||||
logger.log('success', 'container ended all right!');
|
||||
} else {
|
||||
logger.log('error', `container ended with error! Exit Code is ${configArg.exitCode}`);
|
||||
process.exit(1);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* this command is executed inside docker and meant for use from outside docker
|
||||
*/
|
||||
tsdockerCli.addCommand('runinside').subscribe(async argvArg => {
|
||||
logger.log('ok', 'Allright. We are now in Docker!');
|
||||
ora.text('now trying to run your specified command');
|
||||
const configArg = await ConfigModule.run();
|
||||
const smartshellInstance = new plugins.smartshell.Smartshell({
|
||||
executor: 'bash'
|
||||
});
|
||||
ora.stop();
|
||||
await smartshellInstance.exec(configArg.command).then(response => {
|
||||
if (response.exitCode !== 0) {
|
||||
process.exit(1);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
tsdockerCli.addCommand('clean').subscribe(async argvArg => {
|
||||
ora.text('cleaning up docker env...');
|
||||
if (argvArg.all) {
|
||||
const smartshellInstance = new plugins.smartshell.Smartshell({
|
||||
executor: 'bash'
|
||||
});
|
||||
ora.text('killing any running docker containers...');
|
||||
await smartshellInstance.exec(`docker kill $(docker ps -q)`);
|
||||
|
||||
ora.text('removing stopped containers...');
|
||||
await smartshellInstance.exec(`docker rm $(docker ps -a -q)`);
|
||||
|
||||
ora.text('removing images...');
|
||||
await smartshellInstance.exec(`docker rmi -f $(docker images -q -f dangling=true)`);
|
||||
|
||||
ora.text('removing all other images...');
|
||||
await smartshellInstance.exec(`docker rmi $(docker images -a -q)`);
|
||||
|
||||
ora.text('removing all volumes...');
|
||||
await smartshellInstance.exec(`docker volume rm $(docker volume ls -f dangling=true -q)`);
|
||||
}
|
||||
ora.finishSuccess('docker environment now is clean!');
|
||||
});
|
||||
|
||||
tsdockerCli.addCommand('speedtest').subscribe(async argvArg => {
|
||||
const smartshellInstance = new plugins.smartshell.Smartshell({
|
||||
executor: 'bash'
|
||||
});
|
||||
logger.log('ok', 'Starting speedtest');
|
||||
await smartshellInstance.exec(
|
||||
`docker pull tianon/speedtest && docker run --rm tianon/speedtest`
|
||||
);
|
||||
});
|
||||
|
||||
tsdockerCli.addCommand('vscode').subscribe(async argvArg => {
|
||||
const smartshellInstance = new plugins.smartshell.Smartshell({
|
||||
executor: 'bash'
|
||||
});
|
||||
logger.log('ok', `Starting vscode in cwd ${paths.cwd}`);
|
||||
await smartshellInstance.execAndWaitForLine(
|
||||
`docker run -p 127.0.0.1:8443:8443 -v "${paths.cwd}:/home/coder/project" registry.gitlab.com/hosttoday/ht-docker-vscode --allow-http --no-auth`,
|
||||
/Connected to shared process/
|
||||
);
|
||||
await plugins.smartopen.openUrl('testing-vscode.git.zone:8443');
|
||||
});
|
||||
|
||||
tsdockerCli.startParse();
|
||||
};
|
37
ts/tsdocker.config.ts
Normal file
37
ts/tsdocker.config.ts
Normal file
@ -0,0 +1,37 @@
|
||||
import * as plugins from './tsdocker.plugins';
|
||||
import * as paths from './tsdocker.paths';
|
||||
|
||||
export interface IConfig {
|
||||
baseImage: string;
|
||||
command: string;
|
||||
dockerSock: boolean;
|
||||
exitCode?: number;
|
||||
keyValueObject: any[];
|
||||
}
|
||||
|
||||
const getQenvKeyValueObject = async () => {
|
||||
let qenvKeyValueObjectArray: {[key: string]: string | number};
|
||||
if (plugins.smartfile.fs.fileExistsSync(plugins.path.join(paths.cwd, 'qenv.yml'))) {
|
||||
qenvKeyValueObjectArray = new plugins.qenv.Qenv(paths.cwd, '.nogit/').keyValueObject;
|
||||
} else {
|
||||
qenvKeyValueObjectArray = {};
|
||||
}
|
||||
return qenvKeyValueObjectArray;
|
||||
};
|
||||
|
||||
const buildConfig = async (qenvKeyValueObjectArg: {[key: string]: string | number}) => {
|
||||
const npmextra = new plugins.npmextra.Npmextra(paths.cwd);
|
||||
const config = npmextra.dataFor<IConfig>('npmdocker', {
|
||||
baseImage: 'hosttoday/ht-docker-node:npmdocker',
|
||||
init: 'rm -rf node_nodules/ && yarn install',
|
||||
command: 'npmci npm test',
|
||||
dockerSock: false,
|
||||
keyValueObject: qenvKeyValueObjectArg
|
||||
});
|
||||
return config;
|
||||
};
|
||||
|
||||
export let run = async (): Promise<IConfig> => {
|
||||
const config = await getQenvKeyValueObject().then(buildConfig);
|
||||
return config;
|
||||
};
|
@ -1,20 +1,22 @@
|
||||
import * as plugins from './npmdocker.plugins';
|
||||
import * as paths from './npmdocker.paths';
|
||||
import * as snippets from './npmdocker.snippets';
|
||||
import * as plugins from './tsdocker.plugins';
|
||||
import * as paths from './tsdocker.paths';
|
||||
import * as snippets from './tsdocker.snippets';
|
||||
|
||||
import { logger, ora } from './tsdocker.logging';
|
||||
|
||||
const smartshellInstance = new plugins.smartshell.Smartshell({
|
||||
executor: 'bash'
|
||||
});
|
||||
|
||||
// interfaces
|
||||
import { IConfig } from './npmdocker.config';
|
||||
import { IConfig } from './tsdocker.config';
|
||||
|
||||
let config: IConfig;
|
||||
|
||||
/**
|
||||
* the docker data used to build the internal testing container
|
||||
*/
|
||||
let dockerData = {
|
||||
const dockerData = {
|
||||
imageTag: 'npmdocker-temp-image:latest',
|
||||
containerName: 'npmdocker-temp-container',
|
||||
dockerProjectMountString: '',
|
||||
@ -25,12 +27,12 @@ let dockerData = {
|
||||
/**
|
||||
* check if docker is available
|
||||
*/
|
||||
let checkDocker = () => {
|
||||
let done = plugins.smartpromise.defer();
|
||||
plugins.beautylog.ora.text('checking docker...');
|
||||
const checkDocker = () => {
|
||||
const done = plugins.smartpromise.defer();
|
||||
ora.text('checking docker...');
|
||||
|
||||
if (smartshellInstance.exec('which docker')) {
|
||||
plugins.beautylog.ok('Docker found!');
|
||||
logger.log('ok', 'Docker found!');
|
||||
done.resolve();
|
||||
} else {
|
||||
done.reject(new Error('docker not found on this machine'));
|
||||
@ -41,18 +43,18 @@ let checkDocker = () => {
|
||||
/**
|
||||
* builds the Dockerfile according to the config in the project
|
||||
*/
|
||||
let buildDockerFile = () => {
|
||||
let done = plugins.smartpromise.defer();
|
||||
plugins.beautylog.ora.text('building Dockerfile...');
|
||||
let dockerfile: string = snippets.dockerfileSnippet({
|
||||
const buildDockerFile = () => {
|
||||
const done = plugins.smartpromise.defer();
|
||||
ora.text('building Dockerfile...');
|
||||
const dockerfile: string = snippets.dockerfileSnippet({
|
||||
baseImage: config.baseImage,
|
||||
command: config.command
|
||||
});
|
||||
plugins.beautylog.info(`Base image is: ${config.baseImage}`);
|
||||
plugins.beautylog.info(`Command is: ${config.command}`);
|
||||
logger.log('info', `Base image is: ${config.baseImage}`);
|
||||
logger.log('info', `Command is: ${config.command}`);
|
||||
plugins.smartfile.memory.toFsSync(dockerfile, plugins.path.join(paths.cwd, 'npmdocker'));
|
||||
plugins.beautylog.ok('Dockerfile created!');
|
||||
plugins.beautylog.ora.stop();
|
||||
logger.log('ok', 'Dockerfile created!');
|
||||
ora.stop();
|
||||
done.resolve();
|
||||
return done.promise;
|
||||
};
|
||||
@ -60,18 +62,18 @@ let buildDockerFile = () => {
|
||||
/**
|
||||
* builds the Dockerimage from the built Dockerfile
|
||||
*/
|
||||
let buildDockerImage = async () => {
|
||||
plugins.beautylog.info('pulling latest base image from registry...');
|
||||
const buildDockerImage = async () => {
|
||||
logger.log('info', 'pulling latest base image from registry...');
|
||||
await smartshellInstance.exec(`docker pull ${config.baseImage}`);
|
||||
plugins.beautylog.ora.text('building Dockerimage...');
|
||||
let execResult = await smartshellInstance.execSilent(
|
||||
ora.text('building Dockerimage...');
|
||||
const execResult = await smartshellInstance.execSilent(
|
||||
`docker build -f npmdocker -t ${dockerData.imageTag} ${paths.cwd}`
|
||||
);
|
||||
if (execResult.exitCode !== 0) {
|
||||
console.log(execResult.stdout);
|
||||
process.exit(1);
|
||||
}
|
||||
plugins.beautylog.ok('Dockerimage built!');
|
||||
logger.log('ok', 'Dockerimage built!');
|
||||
};
|
||||
|
||||
const buildDockerProjectMountString = async () => {
|
||||
@ -84,9 +86,9 @@ const buildDockerProjectMountString = async () => {
|
||||
* builds an environment string that docker cli understands
|
||||
*/
|
||||
const buildDockerEnvString = async () => {
|
||||
for (let keyValueObjectArg of config.keyValueObjectArray) {
|
||||
let envString = (dockerData.dockerEnvString =
|
||||
dockerData.dockerEnvString + `-e ${keyValueObjectArg.key}=${keyValueObjectArg.value} `);
|
||||
for (const key of Object.keys(config.keyValueObject)) {
|
||||
const envString = (dockerData.dockerEnvString =
|
||||
dockerData.dockerEnvString + `-e ${key}=${config.keyValueObject[key]} `);
|
||||
}
|
||||
};
|
||||
|
||||
@ -102,11 +104,11 @@ const buildDockerSockString = async () => {
|
||||
/**
|
||||
* creates a container by running the built Dockerimage
|
||||
*/
|
||||
let runDockerImage = async () => {
|
||||
let done = plugins.smartpromise.defer();
|
||||
plugins.beautylog.ora.text('starting Container...');
|
||||
plugins.beautylog.ora.end();
|
||||
plugins.beautylog.log('now running Dockerimage');
|
||||
const runDockerImage = async () => {
|
||||
const done = plugins.smartpromise.defer();
|
||||
ora.text('starting Container...');
|
||||
ora.stop();
|
||||
logger.log('info', 'now running Dockerimage');
|
||||
config.exitCode = (await smartshellInstance.exec(
|
||||
`docker run ${dockerData.dockerProjectMountString} ${dockerData.dockerSockString} ${
|
||||
dockerData.dockerEnvString
|
||||
@ -117,14 +119,14 @@ let runDockerImage = async () => {
|
||||
/**
|
||||
* cleans up: deletes the test container
|
||||
*/
|
||||
let deleteDockerContainer = async () => {
|
||||
const deleteDockerContainer = async () => {
|
||||
await smartshellInstance.execSilent(`docker rm -f ${dockerData.containerName}`);
|
||||
};
|
||||
|
||||
/**
|
||||
* cleans up deletes the test image
|
||||
*/
|
||||
let deleteDockerImage = async () => {
|
||||
const deleteDockerImage = async () => {
|
||||
await smartshellInstance.execSilent(`docker rmi ${dockerData.imageTag}`).then(async response => {
|
||||
if (response.exitCode !== 0) {
|
||||
console.log(response.stdout);
|
||||
@ -132,27 +134,26 @@ let deleteDockerImage = async () => {
|
||||
});
|
||||
};
|
||||
|
||||
let preClean = async () => {
|
||||
const preClean = async () => {
|
||||
await deleteDockerImage()
|
||||
.then(deleteDockerContainer)
|
||||
.then(async () => {
|
||||
plugins.beautylog.ok('ensured clean Docker environment!');
|
||||
logger.log('ok', 'ensured clean Docker environment!');
|
||||
});
|
||||
};
|
||||
|
||||
let postClean = async () => {
|
||||
const postClean = async () => {
|
||||
await deleteDockerContainer()
|
||||
.then(deleteDockerImage)
|
||||
.then(async () => {
|
||||
plugins.beautylog.ok('cleaned up!');
|
||||
logger.log('ok', 'cleaned up!');
|
||||
});
|
||||
plugins.smartfile.fs.removeSync(paths.npmdockerFile);
|
||||
};
|
||||
|
||||
export let run = async (configArg: IConfig): Promise<IConfig> => {
|
||||
plugins.beautylog.ora.start();
|
||||
config = configArg;
|
||||
let resultConfig = await checkDocker()
|
||||
const resultConfig = await checkDocker()
|
||||
.then(preClean)
|
||||
.then(buildDockerFile)
|
||||
.then(buildDockerImage)
|
17
ts/tsdocker.logging.ts
Normal file
17
ts/tsdocker.logging.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import * as plugins from './tsdocker.plugins';
|
||||
|
||||
export const logger = new plugins.smartlog.Smartlog({
|
||||
logContext: {
|
||||
company: 'Some Company',
|
||||
companyunit: 'Some CompanyUnit',
|
||||
containerName: 'Some Containername',
|
||||
environment: 'local',
|
||||
runtime: 'node',
|
||||
zone: 'gitzone'
|
||||
},
|
||||
minimumLogLevel: 'silly'
|
||||
});
|
||||
|
||||
logger.addLogDestination(new plugins.smartlogDestinationLocal.DestinationLocal());
|
||||
|
||||
export const ora = new plugins.smartlogSouceOra.SmartlogSourceOra();
|
@ -1,4 +1,4 @@
|
||||
import * as plugins from './npmdocker.plugins';
|
||||
import * as plugins from './tsdocker.plugins';
|
||||
|
||||
// directories
|
||||
export let cwd = process.cwd();
|
@ -1,4 +1,4 @@
|
||||
import * as beautylog from 'beautylog';
|
||||
// pushrocks scope
|
||||
import * as npmextra from '@pushrocks/npmextra';
|
||||
import * as path from 'path';
|
||||
import * as projectinfo from '@pushrocks/projectinfo';
|
||||
@ -6,11 +6,14 @@ import * as smartpromise from '@pushrocks/smartpromise';
|
||||
import * as qenv from '@pushrocks/qenv';
|
||||
import * as smartcli from '@pushrocks/smartcli';
|
||||
import * as smartfile from '@pushrocks/smartfile';
|
||||
import * as smartlog from '@pushrocks/smartlog';
|
||||
import * as smartlogDestinationLocal from '@pushrocks/smartlog-destination-local';
|
||||
import * as smartlogSouceOra from '@pushrocks/smartlog-source-ora';
|
||||
import * as smartopen from '@pushrocks/smartopen';
|
||||
import * as smartshell from '@pushrocks/smartshell';
|
||||
import * as smartstring from '@pushrocks/smartstring';
|
||||
|
||||
export {
|
||||
beautylog,
|
||||
npmextra,
|
||||
path,
|
||||
projectinfo,
|
||||
@ -18,6 +21,10 @@ export {
|
||||
qenv,
|
||||
smartcli,
|
||||
smartfile,
|
||||
smartlog,
|
||||
smartlogDestinationLocal,
|
||||
smartlogSouceOra,
|
||||
smartopen,
|
||||
smartshell,
|
||||
smartstring
|
||||
};
|
@ -1,4 +1,4 @@
|
||||
import * as plugins from './npmdocker.plugins';
|
||||
import * as plugins from './tsdocker.plugins';
|
||||
|
||||
export interface IDockerfileSnippet {
|
||||
baseImage: string;
|
Reference in New Issue
Block a user