Skip to content

Add resume/pause so it can reuse existing instances #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ inputs:
Specify here which mode you want to use:
- 'start' - to start a new runner;
- 'stop' - to stop the previously created runner.
- 'resume' - to start an existing instance.
- 'pause' - to stop the previously created runner.
required: true
github-token:
description: >-
Expand Down
106 changes: 105 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62856,6 +62856,38 @@ async function startEc2Instance(label, githubRegistrationToken) {
}
}

async function resumeEc2Instance(ec2InstanceId) {
const ec2 = new AWS.EC2();

try {
const result = await ec2.startInstances({InstanceIds: [ec2InstanceId]}).promise();
core.info(`AWS EC2 instance ${ec2InstanceId} is started`);
return ec2InstanceId;
} catch (error) {
core.error('AWS EC2 instance starting error');
throw error;
}

}

async function stopEc2Instance() {
const ec2 = new AWS.EC2();

const params = {
InstanceIds: [config.input.ec2InstanceId],
};

try {
core.info(`AWS EC2 instance ${config.input.ec2InstanceId} stopping`);
await ec2.stopInstances(params).promise();
core.info(`AWS EC2 instance ${config.input.ec2InstanceId} is stopped`);
return;
} catch (error) {
core.error(`AWS EC2 instance ${config.input.ec2InstanceId} stop error`);
throw error;
}
}

async function terminateEc2Instance() {
const ec2 = new AWS.EC2();

Expand Down Expand Up @@ -62890,10 +62922,51 @@ async function waitForInstanceRunning(ec2InstanceId) {
}
}

async function waitForInstanceStopped(ec2InstanceId) {
const ec2 = new AWS.EC2();

const params = {
InstanceIds: [ec2InstanceId],
};

try {
await ec2.waitFor('instanceStopped', params).promise();
} catch (error) {
core.error(`AWS EC2 instance ${ec2InstanceId} stopped error`);
throw error;
}
}

async function startRunner(ec2InstanceId) {
const ssm = new AWS.SSM();

const commands = [
'cd ~/actions-runner/',
'./run.sh',
];

const params = {
DocumentName: 'AWS-RunShellScript',
Targets: [{'Key':'InstanceIds', 'Values':[ec2InstanceId]}],
Parameters: {'commands': [commands]},
}

try {
core.info('Sending command to start GitHub runner')
ssm.sendCommand(params);
} catch (error) {
core.error('Could not send command to instance');
}
}

module.exports = {
startEc2Instance,
resumeEc2Instance,
stopEc2Instance,
terminateEc2Instance,
waitForInstanceRunning,
waitForInstanceStopped,
startRunner,
};


Expand Down Expand Up @@ -62954,6 +63027,14 @@ class Config {
if (!this.input.label || !this.input.ec2InstanceId) {
throw new Error(`Not all the required inputs are provided for the 'stop' mode`);
}
} else if (this.input.mode === 'resume') {
if (!this.input.ec2InstanceId || !this.input.label || !this.input.githubToken) {
throw new Error(`Not all the required inputs are provided for the 'resume' mode`);
}
} else if (this.input.mode === 'pause') {
if (!this.input.label || !this.input.ec2InstanceId) {
throw new Error(`Not all the required inputs are provided for the 'pause' mode`);
}
} else {
throw new Error('Wrong mode. Allowed values: start, stop.');
}
Expand Down Expand Up @@ -63098,9 +63179,32 @@ async function stop() {
await gh.removeRunner();
}

async function resume() {
const label = config.input.label;
const githubRegistrationToken = await gh.getRegistrationToken();
const ec2InstanceId = config.input.ec2InstanceId;
await aws.waitForInstanceStopped(ec2InstanceId);
await aws.resumeEc2Instance(ec2InstanceId);
setOutput(label, ec2InstanceId);
await aws.waitForInstanceRunning(ec2InstanceId);
await gh.waitForRunnerRegistered(label);
}

async function pause() {
await aws.stopEc2Instance();
}

(async function () {
try {
config.input.mode === 'start' ? await start() : await stop();
if (config.input.mode === 'start') {
await start();
} else if (config.input.mode === 'stop') {
await stop();
} else if (config.input.mode === 'resume') {
await resume();
} else if (config.input.mode === 'pause') {
await pause();
}
} catch (error) {
core.error(error);
core.setFailed(error.message);
Expand Down
73 changes: 73 additions & 0 deletions src/aws.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,38 @@ async function startEc2Instance(label, githubRegistrationToken) {
}
}

async function resumeEc2Instance(ec2InstanceId) {
const ec2 = new AWS.EC2();

try {
const result = await ec2.startInstances({InstanceIds: [ec2InstanceId]}).promise();
core.info(`AWS EC2 instance ${ec2InstanceId} is started`);
return ec2InstanceId;
} catch (error) {
core.error('AWS EC2 instance starting error');
throw error;
}

}

async function stopEc2Instance() {
const ec2 = new AWS.EC2();

const params = {
InstanceIds: [config.input.ec2InstanceId],
};

try {
core.info(`AWS EC2 instance ${config.input.ec2InstanceId} stopping`);
await ec2.stopInstances(params).promise();
core.info(`AWS EC2 instance ${config.input.ec2InstanceId} is stopped`);
return;
} catch (error) {
core.error(`AWS EC2 instance ${config.input.ec2InstanceId} stop error`);
throw error;
}
}

async function terminateEc2Instance() {
const ec2 = new AWS.EC2();

Expand Down Expand Up @@ -90,8 +122,49 @@ async function waitForInstanceRunning(ec2InstanceId) {
}
}

async function waitForInstanceStopped(ec2InstanceId) {
const ec2 = new AWS.EC2();

const params = {
InstanceIds: [ec2InstanceId],
};

try {
await ec2.waitFor('instanceStopped', params).promise();
} catch (error) {
core.error(`AWS EC2 instance ${ec2InstanceId} stopped error`);
throw error;
}
}

async function startRunner(ec2InstanceId) {
const ssm = new AWS.SSM();

const commands = [
'cd ~/actions-runner/',
'./run.sh',
];

const params = {
DocumentName: 'AWS-RunShellScript',
Targets: [{'Key':'InstanceIds', 'Values':[ec2InstanceId]}],
Parameters: {'commands': [commands]},
}

try {
core.info('Sending command to start GitHub runner')
ssm.sendCommand(params);
} catch (error) {
core.error('Could not send command to instance');
}
}

module.exports = {
startEc2Instance,
resumeEc2Instance,
stopEc2Instance,
terminateEc2Instance,
waitForInstanceRunning,
waitForInstanceStopped,
startRunner,
};
8 changes: 8 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ class Config {
if (!this.input.label || !this.input.ec2InstanceId) {
throw new Error(`Not all the required inputs are provided for the 'stop' mode`);
}
} else if (this.input.mode === 'resume') {
if (!this.input.ec2InstanceId || !this.input.label || !this.input.githubToken) {
throw new Error(`Not all the required inputs are provided for the 'resume' mode`);
}
} else if (this.input.mode === 'pause') {
if (!this.input.label || !this.input.ec2InstanceId) {
throw new Error(`Not all the required inputs are provided for the 'pause' mode`);
}
} else {
throw new Error('Wrong mode. Allowed values: start, stop.');
}
Expand Down
25 changes: 24 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,32 @@ async function stop() {
await gh.removeRunner();
}

async function resume() {
const label = config.input.label;
const githubRegistrationToken = await gh.getRegistrationToken();
const ec2InstanceId = config.input.ec2InstanceId;
await aws.waitForInstanceStopped(ec2InstanceId);
await aws.resumeEc2Instance(ec2InstanceId);
setOutput(label, ec2InstanceId);
await aws.waitForInstanceRunning(ec2InstanceId);
await gh.waitForRunnerRegistered(label);
}

async function pause() {
await aws.stopEc2Instance();
}

(async function () {
try {
config.input.mode === 'start' ? await start() : await stop();
if (config.input.mode === 'start') {
await start();
} else if (config.input.mode === 'stop') {
await stop();
} else if (config.input.mode === 'resume') {
await resume();
} else if (config.input.mode === 'pause') {
await pause();
}
} catch (error) {
core.error(error);
core.setFailed(error.message);
Expand Down