Skip to content

Commit 3f78963

Browse files
ziv-codefreshitai-codefresh
authored andcommitted
Support ability to run the engine on the local docker daemon (#242)
1 parent 7bff6c5 commit 3f78963

File tree

4 files changed

+240
-158
lines changed

4 files changed

+240
-158
lines changed

lib/interface/cli/commands/pipeline/run.cmd.js

Lines changed: 97 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ const { prepareKeyValueFromCLIEnvOption, crudFilenameOption } = require('../../h
66
const ObjectID = require('mongodb').ObjectID;
77
const { workflow, pipeline, log } = require('../../../../logic').api;
88
const authManager = require('../../../../logic').auth.manager;
9+
const Docker = require('dockerode');
10+
11+
const regex = /##[0-9a-f]{24}##/i;
12+
const imageName = 'codefresh/engine:master';
913

1014

1115
const run = new Command({
@@ -60,6 +64,11 @@ const run = new Command({
6064
describe: 'Run pipeline with contexts',
6165
default: [],
6266
})
67+
.option('local', {
68+
describe: 'Run pipeline on your local docker machine',
69+
type: Boolean,
70+
default: false,
71+
})
6372
.example('codefresh run PIPELINE_ID | PIPELINE_NAME -b=master', 'Defining the source control context using a branch')
6473
.example('codefresh run PIPELINE_ID | PIPELINE_NAME -s=52b992e783d2f84dd0123c70ac8623b4f0f938d1', 'Defining the source control context using a commit')
6574
.example('codefresh run PIPELINE_ID | PIPELINE_NAME -b=master -v key1=value1 -v key2=value2', 'Setting variables through the command')
@@ -83,6 +92,7 @@ const run = new Command({
8392
const resetVolume = argv['reset-volume'];
8493
const variablesFromFile = argv['var-file'];
8594
const contexts = argv['context'];
95+
const local = argv.local;
8696

8797
try {
8898
await pipeline.getPipelineByName(pipelineName);
@@ -91,64 +101,101 @@ const run = new Command({
91101
message: `Passed pipeline id: ${pipelineName} does not exist`,
92102
});
93103
}
104+
if (local) {
105+
const docker = new Docker();
106+
docker.pull(imageName, (err, stream) => {
107+
docker.modem.followProgress(stream, onFinished, onProgress);
108+
function onFinished(err) {
109+
if (!err) {
110+
console.log('\nDone pulling.');
111+
const currentContext = authManager.getCurrentContext();
112+
docker.run(imageName, [], [], {
113+
Env: [`ACCESS_TOKEN=${currentContext.token}`, `PIPELINE_ID=${pipelineName}`, `BRANCH=${branch}`, `CF_HOST=${currentContext.url}`, 'DOCKER_SOCKET_PATH=/var/run/docker.sock'],
114+
Hostconfig: {
115+
Binds: ['/var/run/docker.sock:/var/run/docker.sock'],
116+
},
117+
}, (err, data) => {
118+
if (err) {
119+
return console.error(err);
120+
}
121+
process.exit(data.StatusCode);
122+
}).on('stream', (stream) => {
123+
stream.on('data', (chunk) => {
124+
const line = chunk.toString();
125+
const include = line.match(regex);
126+
if (include) {
127+
const workflowId = include[0].substring(2, include[0].length - 2);
128+
log.showWorkflowLogs(workflowId, true)
129+
.then(() => Promise.resolve());
130+
}
131+
});
132+
});
133+
} else {
134+
console.log(err);
135+
process.exit(1);
136+
}
137+
}
138+
function onProgress() {
139+
stream.pipe(process.stdout);
140+
}
141+
});
142+
} else {
143+
const executionRequests = [];
144+
const executionRequestTemplate = {
145+
pipelineName,
146+
options: {
147+
noCache,
148+
resetVolume,
149+
branch,
150+
sha,
151+
enableNotifications,
152+
},
153+
};
94154

95-
const executionRequests = [];
96-
const executionRequestTemplate = {
97-
pipelineName,
98-
options: {
99-
noCache,
100-
resetVolume,
101-
branch,
102-
sha,
103-
enableNotifications,
104-
},
105-
};
106-
107-
if (variablesFromFile) {
108-
_.forEach(variablesFromFile, (variables) => {
155+
if (variablesFromFile) {
156+
_.forEach(variablesFromFile, (variables) => {
157+
const request = _.cloneDeep(executionRequestTemplate);
158+
request.options.variables = variables;
159+
executionRequests.push(request);
160+
});
161+
} else {
162+
const variables = prepareKeyValueFromCLIEnvOption(argv.variable);
109163
const request = _.cloneDeep(executionRequestTemplate);
110164
request.options.variables = variables;
165+
request.options.contexts = contexts;
111166
executionRequests.push(request);
112-
});
113-
} else {
114-
const variables = prepareKeyValueFromCLIEnvOption(argv.variable);
115-
const request = _.cloneDeep(executionRequestTemplate);
116-
request.options.variables = variables;
117-
request.options.contexts = contexts;
118-
executionRequests.push(request);
119-
}
167+
}
120168

121-
_.forEach(executionRequests, async ({ pipelineName, options }) => {
122-
let workflowId;
123-
workflowId = await pipeline.runPipelineByName(pipelineName, options);
169+
_.forEach(executionRequests, async ({ pipelineName, options }) => {
170+
let workflowId;
171+
workflowId = await pipeline.runPipelineByName(pipelineName, options);
124172

125-
if (executionRequests.length === 1) {
126-
if (argv.detach) {
127-
console.log(workflowId);
128-
} else {
129-
await log.showWorkflowLogs(workflowId, true);
130-
const workflowInstance = await workflow.getWorkflowById(workflowId);
131-
switch (workflowInstance.getStatus()) {
132-
case 'success':
133-
process.exit(0);
134-
break;
135-
case 'error':
136-
process.exit(1);
137-
break;
138-
case 'terminated':
139-
process.exit(2);
140-
break;
141-
default:
142-
process.exit(100);
143-
break;
173+
if (executionRequests.length === 1) {
174+
if (argv.detach) {
175+
console.log(workflowId);
176+
} else {
177+
await log.showWorkflowLogs(workflowId, true);
178+
const workflowInstance = await workflow.getWorkflowById(workflowId);
179+
switch (workflowInstance.getStatus()) {
180+
case 'success':
181+
process.exit(0);
182+
break;
183+
case 'error':
184+
process.exit(1);
185+
break;
186+
case 'terminated':
187+
process.exit(2);
188+
break;
189+
default:
190+
process.exit(100);
191+
break;
192+
}
144193
}
194+
} else {
195+
console.log(workflowId);
145196
}
146-
} else {
147-
console.log(workflowId);
148-
}
149-
});
150-
151-
197+
});
198+
}
152199
},
153200
});
154201

lib/logic/api/runtimeEnvironments.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ const getRuntimeEvironmentsByNameForAdmin = async (option) => {
146146
qs,
147147
};
148148
const runtimeEnvironments = await sendHttpRequest(options);
149-
if (option.successors) {
149+
if (option.successors || option.history) {
150150
return runtimeEnvironments;
151151
}
152152
const runtimeObj = option.diff ? runtimeEnvironments : new RuntimeEnvironments(_extractFieldsForRuntimeEnvironmentEntity(runtimeEnvironments));

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "codefresh",
3-
"version": "0.9.4",
3+
"version": "0.9.5",
44
"description": "Codefresh command line utility",
55
"main": "index.js",
66
"preferGlobal": true,
@@ -39,6 +39,7 @@
3939
"decompress": "^4.2.0",
4040
"decompress-targz": "^4.1.1",
4141
"diff": "^3.5.0",
42+
"dockerode": "^2.5.7",
4243
"draftlog": "^1.0.12",
4344
"filesize": "^3.5.11",
4445
"firebase": "git+https://github.com/codefresh-io/firebase.git#80b2ed883ff281cd67b53bd0f6a0bbd6f330fed5",

0 commit comments

Comments
 (0)