Skip to content

Commit

Permalink
cml-cloud-runner wait runner no sleep (#317)
Browse files Browse the repository at this point in the history
* [wip] CML contexts

* octokit on demand

* token

* clean env before tests

* print env

* yaml

* yaml

* yaml

* yaml

* check

* fix repo env

* fix repo env

* fix repo env

* fix repo env

* check name

* env github tests

* env github tests

* log check

* log check

* no check tessts

* enviromental tests

* workflow eenv

* gitlab uploads and github_token

* snapshots

* Cml contexts (#314)

* token

* clean env before tests

* print env

* yaml

* yaml

* yaml

* yaml

* check

* fix repo env

* fix repo env

* fix repo env

* fix repo env

* check name

* env github tests

* env github tests

* log check

* log check

* no check tessts

* enviromental tests

* workflow eenv

* gitlab uploads and github_token

* snapshots

* docker entrypooint

* docker entrypoint register_runner

* update templates

* token repo

* token repo

* specify driver

* log env

* log env

* update also github.context

* cml-cloud-runner waits for runner no sleep

* Cml contexts sha fix (#315)

* token

* clean env before tests

* print env

* yaml

* yaml

* yaml

* yaml

* check

* fix repo env

* fix repo env

* fix repo env

* fix repo env

* check name

* env github tests

* env github tests

* log check

* log check

* no check tessts

* enviromental tests

* workflow eenv

* gitlab uploads and github_token

* snapshots

* log env

* log env

* update also github.context

* No log

* point to iterative repos

* point to iterative repos without slash

* point to iterative ai

* test slash

* test slash

* fix strip

* removal of strip

* refactor

* slash

* minor refactor

* no console

* Cml contexts sha (#326)

* git sha

* commit_sha

* log

* log gh sha

* log gh sha

* show origin

* remote as repo

* remote as repo

* tests

* remove one test not working on gitlab

* space

* publish backend

* no banckend in publish
  • Loading branch information
DavidGOrtega authored Oct 28, 2020
1 parent 0749ac9 commit e106048
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 27 deletions.
44 changes: 17 additions & 27 deletions bin/cml-cloud-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ const {
exec,
sleep,
ssh_public_from_private_rsa,
parse_param_newline
parse_param_newline,
randid
} = require('../src/utils');

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

let REPO;
let TOKEN;
let cml;

const TF_FOLDER = '.cml';
const TF_NO_LOCAL = '.nolocal';
Expand Down Expand Up @@ -47,29 +46,27 @@ const ssh_connect = async (opts) => {
};

const setup_runners = async (opts) => {
const { token, repo, driver } = cml;
const {
terraform_state,
username = 'ubuntu',
'repo-token': repo_token = TOKEN,
repo: runner_repo = REPO,
labels: runner_labels,
'idle-timeout': runner_idle_timeout,
name: runner_name,
name: runner_name = randid(),
image = 'dvcorg/cml:latest',
'rsa-private-key': rsa_private_key,
attached,
driver
attached
} = opts;

const tf_path = path.join(TF_FOLDER, 'main.tf');
const tfstate_path = path.join(TF_FOLDER, 'terraform.tfstate');

if (!repo_token)
if (!token)
throw new Error(
'Repository token not set. Your repo_token is not available!'
);

if (!runner_repo)
if (!repo)
throw new Error(
'Repo not set. Your repo must be set to register the runner!'
);
Expand Down Expand Up @@ -109,16 +106,16 @@ const setup_runners = async (opts) => {
-v $(pwd)/terraform.tfstate:/terraform.tfstate \
-v $(pwd)/main.tf:/main.tf \
-e "RUNNER_TF_NAME=iterative_machine.${resource.name}" \
-e "repo_token=${repo_token}" \
-e "RUNNER_REPO=${runner_repo}" \
-e "repo_token=${token}" \
-e "RUNNER_REPO=${repo}" \
-e "RUNNER_DRIVER=${driver}" \
-e "RUNNER_NAME=${runner_name}" \
${runner_labels ? `-e "RUNNER_LABELS=${runner_labels}"` : ''} \
${
runner_idle_timeout
? `-e "RUNNER_IDLE_TIMEOUT=${runner_idle_timeout}"`
: ''
} \
${runner_name ? `-e "RUNNER_NAME=${runner_name}"` : ''} \
${image}`;

console.log(start_runner_cmd);
Expand All @@ -132,6 +129,8 @@ const setup_runners = async (opts) => {

await ssh.dispose();
}

await cml.await_runner({ name: runner_name });
};

const run_terraform = async (opts) => {
Expand Down Expand Up @@ -213,14 +212,10 @@ const shutdown = async () => {
};

const run = async (opts) => {
const cml = new CML(opts);
REPO = cml.env_repo();
TOKEN = cml.env_token();

cml = new CML({ ...opts });
try {
const terraform_state = await run_terraform(opts);
await setup_runners({ terraform_state, ...opts, driver: cml.driver });
await sleep(20);
await setup_runners({ terraform_state, ...opts });
} catch (err) {
await destroy_terraform({});

Expand All @@ -234,9 +229,9 @@ process.on('SIGQUIT', shutdown);

const argv = yargs
.usage(`Usage: $0`)
.default('repo-token')
.default('token')
.describe(
'repo-token',
'token',
'Repository token. Defaults to workflow env variable repo_token.'
)
.default('repo')
Expand Down Expand Up @@ -281,11 +276,6 @@ const argv = yargs
'repo',
'Specifies the repo to be used. If not specified is extracted from the CI ENV.'
)
.default('token')
.describe(
'token',
'Personal access token to be used. If not specified in extracted from ENV repo_token or GITLAB_TOKEN.'
)
.default('driver')
.choices('driver', ['github', 'gitlab'])
.describe('driver', 'If not specify it infers it from the ENV.')
Expand Down
27 changes: 27 additions & 0 deletions src/cml.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,33 @@ class CML {
return await get_driver(this).unregister_runner(opts);
}

async runner_by_name(opts = {}) {
return await get_driver(this).runner_by_name(opts);
}

async await_runner(opts = {}) {
const { name, timer_max = 30, timer_step = 5 } = opts;

let timer = 0;
return new Promise((resolve, reject) => {
const interval = setInterval(async () => {
const runner = await this.runner_by_name({ name });

if (runner) {
clearInterval(interval);
resolve(runner);
}

if (timer_max === timer) {
clearInterval(interval);
reject(new Error('Waiting for runner expiration timeout'));
}

timer += timer_step;
}, timer_step * 1000);
});
}

log_error(e) {
console.error(e.message);
}
Expand Down
3 changes: 3 additions & 0 deletions src/drivers/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ class Github {
}));
}

const runner = runners.filter((runner) => runner.name === name)[0];

if (runner) return { id: runner.id, name: runner.name };
return runners.filter((runner) => runner.name === name)[0];
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/drivers/gitlab.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ class Gitlab {
throw new Error('Gitlab does not support unregister_runner!');
}

async runner_by_name(opts = {}) {
const { name } = opts;

const endpoint = `/runners`;
const runners = await this.request({ endpoint, method: 'GET' });
const runner = runners.filter((runner) => runner.name === name)[0];

if (runner) return { id: runner.id, name: runner.name };
}

async request(opts = {}) {
const { token, api_v4 } = this;
const { endpoint, method = 'GET', body } = opts;
Expand Down

1 comment on commit e106048

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test Comment

Please sign in to comment.