Skip to content

Commit

Permalink
integrated
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGOrtega committed Jul 11, 2022
1 parent e71fec8 commit 271fd5c
Show file tree
Hide file tree
Showing 11 changed files with 3,349 additions and 1,021 deletions.
11 changes: 10 additions & 1 deletion bin/cml/ci.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@ const kebabcaseKeys = require('kebabcase-keys');

const { GIT_USER_NAME, GIT_USER_EMAIL } = require('../../src/cml');
const CML = require('../../src/cml').default;
const analytics = require('../../src/analytics');

exports.command = 'ci';
exports.description = 'Fixes specific CI setups';

exports.handler = async (opts) => {
const cml = new CML(opts);
console.log((await cml.ci(opts)) || '');
const event = await analytics.jitsuEventPayload({ action: 'ci', cml });

try {
console.log((await cml.ci(opts)) || '');
analytics.send({ event });
} catch (err) {
analytics.send({ ...event, error: err.message });
throw err;
}
};

exports.builder = (yargs) =>
Expand Down
12 changes: 10 additions & 2 deletions bin/cml/pr.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@ const kebabcaseKeys = require('kebabcase-keys');

const { GIT_REMOTE, GIT_USER_NAME, GIT_USER_EMAIL } = require('../../src/cml');
const CML = require('../../src/cml').default;
const analytics = require('../../src/analytics');

exports.command = 'pr <glob path...>';
exports.description = 'Create a pull request with the specified files';

exports.handler = async (opts) => {
const cml = new CML(opts);
const link = await cml.prCreate({ ...opts, globs: opts.globpath });
if (link) console.log(link);
const event = await analytics.jitsuEventPayload({ action: 'pr', cml });
try {
const link = await cml.prCreate({ ...opts, globs: opts.globpath });
if (link) console.log(link);
analytics.send({ event });
} catch (err) {
analytics.send({ ...event, error: err.message });
throw err;
}
};

exports.builder = (yargs) =>
Expand Down
24 changes: 17 additions & 7 deletions bin/cml/publish.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const fs = require('fs').promises;
const kebabcaseKeys = require('kebabcase-keys');
const winston = require('winston');

const CML = require('../../src/cml').default;
const analytics = require('../../src/analytics');

exports.command = 'publish <asset>';
exports.description = 'Upload an image to build a report';
Expand All @@ -19,14 +19,24 @@ exports.handler = async (opts) => {

const path = opts.asset;
const cml = new CML({ ...opts, repo: native ? repo : 'cml' });

const output = await cml.publish({
...opts,
path
const event = await analytics.jitsuEventPayload({
action: 'publish',
cml: new CML(opts)
});

if (!file) console.log(output);
else await fs.writeFile(file, output);
try {
const output = await cml.publish({
...opts,
path
});

if (!file) console.log(output);
else await fs.writeFile(file, output);
analytics.send({ event });
} catch (err) {
analytics.send({ ...event, error: err.message });
throw err;
}
};

exports.builder = (yargs) =>
Expand Down
14 changes: 13 additions & 1 deletion bin/cml/rerun-workflow.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
const kebabcaseKeys = require('kebabcase-keys');

const CML = require('../../src/cml').default;
const analytics = require('../../src/analytics');

exports.command = 'rerun-workflow';
exports.description = 'Reruns a workflow given the jobId or workflow Id';

exports.handler = async (opts) => {
const cml = new CML(opts);
await cml.pipelineRerun(opts);
const event = await analytics.jitsuEventPayload({
action: 'rerun-workflow',
cml
});

try {
await cml.pipelineRerun(opts);
analytics.send({ event });
} catch (err) {
analytics.send({ ...event, error: err.message });
throw err;
}
};

exports.builder = (yargs) =>
Expand Down
16 changes: 12 additions & 4 deletions bin/cml/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ const timestring = require('timestring');
const winston = require('winston');

const CML = require('../../src/cml').default;
const analytics = require('../../src/analytics');
const { randid, sleep } = require('../../src/utils');
const tf = require('../../src/terraform');

let cml;
let event;
let RUNNER;
let RUNNER_SHUTTING_DOWN = false;
let RUNNER_TIMER = 0;
Expand Down Expand Up @@ -92,6 +94,7 @@ const shutdown = async (opts) => {

await destroyTerraform();

analytics.send({ ...event });
process.exit(error ? 1 : 0);
};

Expand Down Expand Up @@ -341,8 +344,6 @@ const run = async (opts) => {
opts.workdir = opts.workdir || `${homedir()}/.cml/${opts.name}`;
const {
driver,
repo,
token,
workdir,
cloud,
labels,
Expand All @@ -352,8 +353,6 @@ const run = async (opts) => {
dockerVolumes
} = opts;

cml = new CML({ driver, repo, token });

await cml.repoTokenCheck();

if (dockerVolumes.length && cml.driver !== 'gitlab')
Expand All @@ -368,6 +367,7 @@ const run = async (opts) => {
`Runner name ${name} is already in use. Please change the name or terminate the existing runner.`
);
winston.info(`Reusing existing runner named ${name}...`);
analytics.send({ ...event });
process.exit(0);
}

Expand All @@ -380,12 +380,14 @@ const run = async (opts) => {
winston.info(
`Reusing existing online runners with the ${labels} labels...`
);
analytics.send({ ...event });
process.exit(0);
}

if (reuseIdle) {
if (driver === 'bitbucket') {
winston.error('cml runner flag --reuse-idle is unsupported by bitbucket');
analytics.send({ ...event });
process.exit(1);
}
winston.info(
Expand All @@ -397,6 +399,7 @@ const run = async (opts) => {
);
if (availableRunner) {
winston.info('Found matching idle runner.', availableRunner);
analytics.send({ ...event });
process.exit(0);
}
}
Expand All @@ -419,6 +422,10 @@ exports.command = 'runner';
exports.description = 'Launch and register a self-hosted runner';

exports.handler = async (opts) => {
const { driver, repo, token } = opts;
cml = new CML({ driver, repo, token });
event = await analytics.jitsuEventPayload({ action: 'runner', cml });

if (process.env.RUNNER_NAME) {
winston.warn(
'ignoring RUNNER_NAME environment variable, use CML_RUNNER_NAME or --name instead'
Expand All @@ -428,6 +435,7 @@ exports.handler = async (opts) => {
await run(opts);
} catch (error) {
await shutdown({ ...opts, error });
analytics.send({ ...event, error: error.message });
}
};

Expand Down
15 changes: 14 additions & 1 deletion bin/cml/send-comment.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
const kebabcaseKeys = require('kebabcase-keys');

const CML = require('../../src/cml').default;
const analytics = require('../../src/analytics');

exports.command = 'send-comment <markdown file>';
exports.description = 'Comment on a commit';

exports.handler = async (opts) => {
opts.markdownFile = opts.markdownfile;

const cml = new CML(opts);
console.log(await cml.commentCreate(opts));
const event = await analytics.jitsuEventPayload({
action: 'send-comment',
cml
});

try {
console.log(await cml.commentCreate(opts));
analytics.send({ event });
} catch (err) {
analytics.send({ ...event, error: err.message });
throw err;
}
};

exports.builder = (yargs) =>
Expand Down
18 changes: 15 additions & 3 deletions bin/cml/send-github-check.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
const fs = require('fs').promises;
const kebabcaseKeys = require('kebabcase-keys');
const CML = require('../../src/cml').default;
const analytics = require('../../src/analytics');

exports.command = 'send-github-check <markdown file>';
exports.description = 'Create a check report';

exports.handler = async (opts) => {
const path = opts.markdownfile;
const report = await fs.readFile(path, 'utf-8');
const cml = new CML({ ...opts });
await cml.checkCreate({ ...opts, report });
const event = await analytics.jitsuEventPayload({
action: 'send-check',
cml
});

try {
const path = opts.markdownfile;
const report = await fs.readFile(path, 'utf-8');
await cml.checkCreate({ ...opts, report });
analytics.send({ ...event });
} catch (err) {
analytics.send({ ...event, error: err.message });
throw err;
}
};

exports.builder = (yargs) =>
Expand Down
104 changes: 60 additions & 44 deletions bin/cml/tensorboard-dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const tempy = require('tempy');
const winston = require('winston');
const { exec, watermarkUri, sleep } = require('../../src/utils');

const CML = require('../../src/cml').default;
const analytics = require('../../src/analytics');

const closeFd = (fd) => {
try {
fd.close();
Expand Down Expand Up @@ -56,53 +59,66 @@ exports.handler = async (opts) => {
rmWatermark
} = opts;

// set credentials
const path = `${homedir()}/.config/tensorboard/credentials`;
await fs.mkdir(path, { recursive: true });
await fs.writeFile(`${path}/uploader-creds.json`, credentials);

// launch tensorboard on background
const help = await exec('tensorboard dev upload -h');
const extraParamsFound =
(name || description) && help.indexOf('--description') >= 0;
const extraParams = extraParamsFound
? `--name "${name}" --description "${description}"`
: '';
const command = `tensorboard dev upload --logdir ${logdir} ${extraParams}`;

const stdoutPath = tempy.file({ extension: 'log' });
const stdoutFd = await fs.open(stdoutPath, 'a');
const stderrPath = tempy.file({ extension: 'log' });
const stderrFd = await fs.open(stderrPath, 'a');

const proc = spawn(command, [], {
detached: true,
shell: true,
stdio: ['ignore', stdoutFd, stderrFd]
const cml = new CML({ ...opts });
const event = await analytics.jitsuEventPayload({
action: 'tensorboard-dev',
cml
});

proc.unref();
proc.on('exit', async (code) => {
if (code) {
try {
// set credentials
const path = `${homedir()}/.config/tensorboard/credentials`;
await fs.mkdir(path, { recursive: true });
await fs.writeFile(`${path}/uploader-creds.json`, credentials);

// launch tensorboard on background
const help = await exec('tensorboard dev upload -h');
const extraParamsFound =
(name || description) && help.indexOf('--description') >= 0;
const extraParams = extraParamsFound
? `--name "${name}" --description "${description}"`
: '';
const command = `tensorboard dev upload --logdir ${logdir} ${extraParams}`;

const stdoutPath = tempy.file({ extension: 'log' });
const stdoutFd = await fs.open(stdoutPath, 'a');
const stderrPath = tempy.file({ extension: 'log' });
const stderrFd = await fs.open(stderrPath, 'a');

const proc = spawn(command, [], {
detached: true,
shell: true,
stdio: ['ignore', stdoutFd, stderrFd]
});

proc.unref();
proc.on('exit', async (code) => {
const error = await fs.readFile(stderrPath, 'utf8');
winston.error(`Tensorboard failed with error: ${error}`);
}
process.exit(code);
});

const url = await exports.tbLink({
stdout: stdoutPath,
stderror: stderrPath,
title,
name,
rmWatermark,
md
});
if (!file) console.log(url);
else await fs.appendFile(file, url);

closeFd(stdoutFd) && closeFd(stderrFd);
process.exit(0);
if (code) {
winston.error(`Tensorboard failed with error: ${error}`);
}
analytics.send({ ...event, error });
process.exit(code);
});

const url = await exports.tbLink({
stdout: stdoutPath,
stderror: stderrPath,
title,
name,
rmWatermark,
md
});
if (!file) console.log(url);
else await fs.appendFile(file, url);

closeFd(stdoutFd) && closeFd(stderrFd);
analytics.send({ ...event });
process.exit(0);
} catch (err) {
analytics.send({ ...event, error: err.message });
throw err;
}
};

exports.builder = (yargs) =>
Expand Down
Loading

1 comment on commit 271fd5c

@github-actions

This comment was marked as off-topic.

Please sign in to comment.