@@ -6,6 +6,10 @@ const { prepareKeyValueFromCLIEnvOption, crudFilenameOption } = require('../../h
6
6
const ObjectID = require ( 'mongodb' ) . ObjectID ;
7
7
const { workflow, pipeline, log } = require ( '../../../../logic' ) . api ;
8
8
const authManager = require ( '../../../../logic' ) . auth . manager ;
9
+ const Docker = require ( 'dockerode' ) ;
10
+
11
+ const regex = / # # [ 0 - 9 a - f ] { 24 } # # / i;
12
+ const imageName = 'codefresh/engine:master' ;
9
13
10
14
11
15
const run = new Command ( {
@@ -60,6 +64,11 @@ const run = new Command({
60
64
describe : 'Run pipeline with contexts' ,
61
65
default : [ ] ,
62
66
} )
67
+ . option ( 'local' , {
68
+ describe : 'Run pipeline on your local docker machine' ,
69
+ type : Boolean ,
70
+ default : false ,
71
+ } )
63
72
. example ( 'codefresh run PIPELINE_ID | PIPELINE_NAME -b=master' , 'Defining the source control context using a branch' )
64
73
. example ( 'codefresh run PIPELINE_ID | PIPELINE_NAME -s=52b992e783d2f84dd0123c70ac8623b4f0f938d1' , 'Defining the source control context using a commit' )
65
74
. 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({
83
92
const resetVolume = argv [ 'reset-volume' ] ;
84
93
const variablesFromFile = argv [ 'var-file' ] ;
85
94
const contexts = argv [ 'context' ] ;
95
+ const local = argv . local ;
86
96
87
97
try {
88
98
await pipeline . getPipelineByName ( pipelineName ) ;
@@ -91,64 +101,101 @@ const run = new Command({
91
101
message : `Passed pipeline id: ${ pipelineName } does not exist` ,
92
102
} ) ;
93
103
}
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
+ } ;
94
154
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 ) ;
109
163
const request = _ . cloneDeep ( executionRequestTemplate ) ;
110
164
request . options . variables = variables ;
165
+ request . options . contexts = contexts ;
111
166
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
+ }
120
168
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 ) ;
124
172
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
+ }
144
193
}
194
+ } else {
195
+ console . log ( workflowId ) ;
145
196
}
146
- } else {
147
- console . log ( workflowId ) ;
148
- }
149
- } ) ;
150
-
151
-
197
+ } ) ;
198
+ }
152
199
} ,
153
200
} ) ;
154
201
0 commit comments