Skip to content

Commit 04db673

Browse files
authored
Added option for running docker-executor in privileged (#82)
* Added option for running docker-executor in privileged
1 parent e0769d3 commit 04db673

File tree

5 files changed

+25
-13
lines changed

5 files changed

+25
-13
lines changed

src/commander.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {Utils} from "./utils";
66

77
export class Commander {
88

9-
static async runPipeline(parser: Parser, manualArgs: string[]) {
9+
static async runPipeline(parser: Parser, manualArgs: string[], privileged: boolean) {
1010
const jobs = parser.getJobs();
1111
const stages = parser.getStages().concat();
1212

@@ -27,7 +27,7 @@ export class Commander {
2727

2828
if (!job.isRunning() && !job.isFinished()) {
2929
// noinspection ES6MissingAwait
30-
job.start();
30+
job.start(privileged);
3131
}
3232
}
3333

@@ -41,7 +41,7 @@ export class Commander {
4141
const needsConditionMet = job.needs.every((v) => (finishedJobNames.indexOf(v) >= 0));
4242
if (needsConditionMet) {
4343
// noinspection ES6MissingAwait
44-
job.start();
44+
job.start(privileged);
4545
}
4646
}
4747

@@ -86,7 +86,7 @@ export class Commander {
8686
}
8787
}
8888

89-
static async runSingleJob(parser: Parser, jobName: string, needs: boolean) {
89+
static async runSingleJob(parser: Parser, jobName: string, needs: boolean, privileged: boolean) {
9090
const jobs: Job[] = [];
9191
const foundJob = parser.getJobByName(jobName);
9292
jobs.push(foundJob);
@@ -110,7 +110,7 @@ export class Commander {
110110
}
111111

112112
for (const job of jobs) {
113-
await job.start();
113+
await job.start(privileged);
114114
}
115115

116116
await Commander.printReport(jobs);

src/default-cmd.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ export async function handler(argv: any) {
3535
checkFolderAndFile(cwd);
3636
const pipelineIid = await state.getPipelineIid(cwd);
3737
const parser = await Parser.create(cwd, pipelineIid);
38-
await Commander.runSingleJob(parser, argv.job, argv.needs);
38+
await Commander.runSingleJob(parser, argv.job, argv.needs, argv.privileged);
3939
} else {
4040
checkFolderAndFile(cwd);
4141
await state.incrementPipelineIid(cwd);
4242
const pipelineIid = await state.getPipelineIid(cwd);
4343
const parser = await Parser.create(cwd, pipelineIid);
44-
await Commander.runPipeline(parser, argv.manual || []);
44+
await Commander.runPipeline(parser, argv.manual || [], argv.privileged);
4545
}
4646
}
4747

src/index.ts

+7
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ process.on('unhandledRejection', e => {
3131
.command(defaultCmd)
3232
.usage("Find more information at https://github.com/firecow/gitlab-ci-local")
3333
.strictOptions()
34+
.env("GCL")
3435
.option("manual", {
3536
type: "array",
3637
description: "One or more manual jobs to run during a pipeline",
@@ -56,6 +57,12 @@ process.on('unhandledRejection', e => {
5657
description: "Run needed jobs, when executing a single job",
5758
requiresArg: false
5859
})
60+
.option("privileged", {
61+
type: "boolean",
62+
default: false,
63+
description: "Set docker executor to privileged mode",
64+
requiresArg: false
65+
})
5966
.completion("completion", false, async (_, yargsArgv) => {
6067
try {
6168
const cwd = yargsArgv.cwd || process.cwd();

src/job.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ export class Job {
154154
return this._afterScriptsExitCode;
155155
}
156156

157-
async start(): Promise<void> {
157+
async start(privileged: boolean): Promise<void> {
158158
const startTime = process.hrtime();
159159

160160
this.running = true;
@@ -168,7 +168,7 @@ export class Job {
168168
}
169169

170170
const prescripts = this.beforeScripts.concat(this.scripts);
171-
this._prescriptsExitCode = await this.execScripts(prescripts);
171+
this._prescriptsExitCode = await this.execScripts(prescripts, privileged);
172172
if (this.afterScripts.length === 0 && this._prescriptsExitCode > 0 && !this.allowFailure) {
173173
process.stderr.write(`${this.getExitedString(startTime, this._prescriptsExitCode, false)}\n`);
174174
this.running = false;
@@ -196,7 +196,7 @@ export class Job {
196196

197197
this._afterScriptsExitCode = 0;
198198
if (this.afterScripts.length > 0) {
199-
this._afterScriptsExitCode = await this.execScripts(this.afterScripts);
199+
this._afterScriptsExitCode = await this.execScripts(this.afterScripts, privileged);
200200
}
201201

202202
if (this._afterScriptsExitCode > 0) {
@@ -257,7 +257,7 @@ export class Job {
257257
}
258258
}
259259

260-
private async execScripts(scripts: string[]): Promise<number> {
260+
private async execScripts(scripts: string[], privileged: boolean): Promise<number> {
261261
const jobNameStr = this.getJobNameString();
262262
const outputFilesPath = this.getOutputFilesPath();
263263
let time;
@@ -308,7 +308,11 @@ export class Job {
308308
process.stdout.write(`${this.getJobNameString()} ${magentaBright('pulled')} in ${magenta(prettyHrtime(endTime))}\n`);
309309

310310
let dockerCmd = ``;
311-
dockerCmd += `docker create -u 0:0 -i ${this.image} `;
311+
if (privileged) {
312+
dockerCmd += `docker create --privileged -u 0:0 -i ${this.image} `;
313+
} else {
314+
dockerCmd += `docker create -u 0:0 -i ${this.image} `;
315+
}
312316
dockerCmd += `sh -c "\n`
313317
dockerCmd += `if [ -x /usr/local/bin/bash ]; then\n`
314318
dockerCmd += `\texec /usr/local/bin/bash \n`;

tests/cases.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ test('image <test-job>', async () => {
112112
test('image <test-entrypoint>', async () => {
113113
await defaultCmd.handler({
114114
cwd: 'tests/test-cases/image',
115-
job: 'test-entrypoint'
115+
job: 'test-entrypoint',
116+
privileged: true
116117
});
117118

118119
expect(mockProcessStdout).toHaveBeenCalledWith("/\n");

0 commit comments

Comments
 (0)