Skip to content

Commit f76f7e9

Browse files
WIP: feature/consoleDockerCompose (#76)
Added Docker-compose for console runner
1 parent 023c01e commit f76f7e9

File tree

6 files changed

+97
-5
lines changed

6 files changed

+97
-5
lines changed

buildRun.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ Copy-Item -Force -Recurse -Path $PSScriptRoot\playbooks\ -Destination $PSScriptR
44
Copy-Item -Force -Recurse -Path $PSScriptRoot\environments\ -Destination $PSScriptRoot\build
55
Copy-Item -Force -Recurse -Path $PSScriptRoot\runners\ -Destination $PSScriptRoot\build
66
npm test
7-
node $PSScriptRoot\build\engine\run.js
7+
node $PSScriptRoot\build\engine\run.js --skipCommands.console dockerCompose

documentation/Functions.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ The following functions are already implemented:
1313
* runServerJava
1414
* buildNg
1515
* npmInstall
16+
* dockerCompose
1617
* downloadFile
1718
* nextKatacodaStep
1819

@@ -162,6 +163,20 @@ npmInstall("my-thai-star/angular")
162163

163164
***
164165

166+
### dockerCompose
167+
#### parameter
168+
1. Path to the directory where the docker-compose.yml file is located, relative to workspace.
169+
2. Assertion information. Only needed for the console runner to check if the server was started properly.
170+
#### example
171+
dockerCompose("my-thai-star", { "startupTime": 600, "port": 8081, "path": "" })
172+
173+
##### Assertion information
174+
startupTime = Time in seconds to wait before checking if the server is running
175+
port: Port on which the server is running
176+
path: The URL path on which is checked if the server is running
177+
178+
***
179+
165180
### downloadFile
166181
#### parameter
167182
1. URL of the file to be downloaded.

engine/engine.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,33 @@ export class Engine {
2424
(await this.getRunner(this.environment.runners[runnerIndex])).init(this.playbook);
2525
}
2626

27-
for (let stepIndex in this.playbook.steps) {
27+
mainloop: for (let stepIndex in this.playbook.steps) {
2828
for (let lineIndex in this.playbook.steps[stepIndex].lines) {
29+
let foundRunnerToExecuteCommand = false;
2930
for (let runnerIndex in this.environment.runners) {
3031
let runner = await this.getRunner(this.environment.runners[runnerIndex]);
3132
if (runner.supports(this.playbook.steps[stepIndex].lines[lineIndex].name)) {
3233
var result = new RunResult();
34+
if(runner.commandIsSkippable(this.playbook.steps[stepIndex].lines[lineIndex].name)) {
35+
console.log("Command " + this.playbook.steps[stepIndex].lines[lineIndex].name + " will be skipped.");
36+
continue;
37+
}
3338
try {
3439
result = runner.run(this.playbook.steps[stepIndex], this.playbook.steps[stepIndex].lines[lineIndex]);
3540
}
3641
catch (e) {
3742
result.exceptions.push(e);
3843
}
44+
3945
await runner.assert(this.playbook.steps[stepIndex], this.playbook.steps[stepIndex].lines[lineIndex], result);
46+
47+
foundRunnerToExecuteCommand = true;
4048
break;
4149
}
4250
}
51+
if(!foundRunnerToExecuteCommand) {
52+
break mainloop;
53+
}
4354
}
4455
}
4556

engine/run.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ class Run {
7777
traverseArgs(parentName, obj) {
7878

7979
for (let index in obj) {
80-
if (isObject(obj[index])) {
80+
if (obj[index] instanceof Object) {
81+
this.args.set(parentName + index, obj[index]);
8182
this.traverseArgs(parentName + index + ".", obj[index]);
8283
}
8384
else {

engine/runner.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,15 @@ export abstract class Runner {
110110
fs.mkdirSync(path, { recursive: true });
111111
return path;
112112
}
113+
114+
commandIsSkippable(command: String): Boolean {
115+
let returnVal = false;
116+
let runner = this.getVariable("skipCommands." + this.getRunnerName());
117+
if(runner) {
118+
if((runner instanceof Array && runner.indexOf(command) != -1) || runner == command) {
119+
returnVal = true;
120+
}
121+
}
122+
return returnVal;
123+
}
113124
}

runners/console/index.ts

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export class Console extends Runner {
3636
}
3737
this.setVariable(this.workspaceDirectory, path.join(this.getWorkingDirectory()));
3838
this.env = process.env;
39+
3940
}
4041

4142
destroy(playbook: Playbook): void {
@@ -204,6 +205,27 @@ export class Console extends Runner {
204205
return result;
205206
}
206207

208+
209+
runDockerCompose(step: Step, command: Command): RunResult {
210+
let result = new RunResult();
211+
result.returnCode = 0;
212+
213+
let filepath = path.join(this.getVariable(this.workspaceDirectory), command.parameters[0]);
214+
215+
let process = this.executeCommandAsync("docker-compose up", filepath, result);
216+
process.on('close', (code) => {
217+
if (code !== 0) {
218+
result.returnCode = code;
219+
}
220+
});
221+
if(process.pid && command.parameters.length == 2) {
222+
this.asyncProcesses.push({ pid: process.pid, name: "dockerCompose", port: command.parameters[1].port });
223+
}
224+
225+
return result;
226+
227+
}
228+
207229
runRunServerJava(step: Step, command: Command): RunResult {
208230
let result = new RunResult();
209231
result.returnCode = 0;
@@ -231,6 +253,7 @@ export class Console extends Runner {
231253
this.executeCommandSync("git clone " + command.parameters[1], directorypath, result);
232254

233255
return result;
256+
234257
}
235258

236259
runNpmInstall(step: Step, command: Command): RunResult {
@@ -423,7 +446,37 @@ export class Console extends Runner {
423446
throw error;
424447
}
425448
}
426-
449+
450+
async assertDockerCompose(step: Step, command: Command, result: RunResult) {
451+
try {
452+
let assert = new Assertions()
453+
.noErrorCode(result)
454+
.noException(result);
455+
456+
if(command.parameters.length > 1) {
457+
if(!command.parameters[1].startupTime) {
458+
console.warn("No startup time for command dockerCompose has been set")
459+
}
460+
let startupTimeInSeconds = command.parameters[1].startupTime ? command.parameters[1].startupTime : 0;
461+
await this.sleep(command.parameters[1].startupTime);
462+
463+
if(!command.parameters[1].port) {
464+
this.killAsyncProcesses();
465+
throw new Error("Missing arguments for command dockerCompose. You have to specify a port and a path for the server. For further information read the function documentation.");
466+
} else {
467+
let isReachable = await assert.serverIsReachable(command.parameters[1].port, command.parameters[1].path);
468+
if(!isReachable) {
469+
this.killAsyncProcesses();
470+
throw new Error("The server has not become reachable in " + startupTimeInSeconds + " seconds: " + "http://localhost:" + command.parameters[1].port + "/" + command.parameters[1].path);
471+
}
472+
}
473+
}
474+
} catch(error) {
475+
this.cleanUp();
476+
throw error;
477+
}
478+
}
479+
427480
async assertRunServerJava(step: Step, command: Command, result: RunResult) {
428481
try {
429482
let assert = new Assertions()
@@ -658,5 +711,6 @@ export class Console extends Runner {
658711
}
659712
}
660713

714+
661715

662-
}
716+
}

0 commit comments

Comments
 (0)