Line data Source code
1 : /*
2 : gitlab_api.c
3 : Created by Danny Goossen, Gioxa Ltd on 4/3/17.
4 :
5 : MIT License
6 :
7 : Copyright (c) 2017 deployctl, Gioxa Ltd.
8 :
9 : Permission is hereby granted, free of charge, to any person obtaining a copy
10 : of this software and associated documentation files (the "Software"), to deal
11 : in the Software without restriction, including without limitation the rights
12 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 : copies of the Software, and to permit persons to whom the Software is
14 : furnished to do so, subject to the following conditions:
15 :
16 : The above copyright notice and this permission notice shall be included in all
17 : copies or substantial portions of the Software.
18 :
19 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 : SOFTWARE.
26 :
27 : */
28 :
29 :
30 :
31 : #include "deployd.h"
32 :
33 :
34 : /*------------------------------------------------------------------------
35 : * get_release(void * opaque, cJSON ** release)
36 : * returns the gitlab cJSON response of:
37 : * /api/v4/projects/<project id>/repository/tags/<tag>
38 : * with the CI-token
39 : * returns 1 on success
40 : * caller need to free release
41 : *------------------------------------------------------------------------*/
42 4 : int get_release(void * opaque, cJSON ** release)
43 : {
44 4 : cJSON * env_json= ((data_exchange_t *)opaque)->env_json;
45 : CURLcode res;
46 4 : *release=NULL;
47 4 : CURL *curl = curl_easy_init();
48 4 : if(curl)
49 : {
50 : //build request
51 4 : char * request=calloc(1, 0x1000);
52 : //"CI_PROJECT_URL": "https://gitlab.gioxa.com/deployctl/test_deploy_release",
53 :
54 4 : char * buildrepo=cJSON_GetObjectItem(env_json, "CI_PROJECT_URL")->valuestring;
55 :
56 : //CI_PROJECT_PATH="gitlab-org/gitlab-ce"
57 4 : char * project_path=cJSON_GetObjectItem(env_json, "CI_PROJECT_PATH")->valuestring;
58 4 : debug("ci-project_path: %s\n",project_path);
59 : //find position of CI_PROJECT_PATH in url
60 4 : size_t pos = strstr(buildrepo, project_path) - buildrepo;
61 :
62 : // copy till CI_PROJECT_PATH
63 4 : memcpy(request,buildrepo ,pos );
64 : // and add =>api/v4/projects/{CI_PROJECT_ID}/repository/tags/{CI_COMMIT_SHA}
65 4 : sprintf(request+pos, "api/v4/projects/%s/repository/tags/%s",cJSON_get_key(env_json, "CI_PROJECT_ID"),cJSON_get_key(env_json, "CI_COMMIT_TAG"));
66 : //Done
67 :
68 4 : debug("\nRequest API: %s\n",request);
69 : struct MemoryStruct chunk;
70 4 : init_dynamicbuf(&chunk);
71 4 : struct curl_slist *list = NULL;
72 :
73 4 : curl_easy_setopt(curl, CURLOPT_URL, (char*)request);
74 4 : debug("CI_JOB_TOKEN : %s\n",cJSON_get_key(env_json, "CI_JOB_TOKEN"));
75 4 : debug("CI_COMMIT_TAG : %s\n",cJSON_get_key(env_json, "CI_COMMIT_TAG"));
76 : //CI_JOB_TOKEN
77 : char headertoken[255];
78 4 : sprintf(headertoken, "JOB_TOKEN: %s",cJSON_get_key(env_json, "CI_JOB_TOKEN"));
79 4 : debug("-H \"%s\"\n",headertoken);
80 :
81 4 : list = curl_slist_append(list, headertoken);
82 :
83 4 : curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
84 : /* send all data to this function */
85 4 : curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
86 : /* we pass our 'chunk' struct to the callback function */
87 4 : curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
88 4 : res = curl_easy_perform(curl);
89 4 : curl_slist_free_all(list);
90 4 : if (res!=CURLE_OK )
91 : {
92 1 : debug("\n Failed to get %s\n",request);
93 :
94 1 : free_dynamicbuf(&chunk);
95 :
96 1 : if (request) free(request);
97 1 : curl_easy_cleanup(curl);
98 1 : return 1;
99 : }
100 3 : long http_code = 0;
101 3 : curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
102 3 : if (http_code == 200 && res != CURLE_ABORTED_BY_CALLBACK)
103 : {
104 : //result in chunk
105 2 : debug("gitlab_api reply:\n>>>\n%s\n<<<\n",chunk.memory);
106 2 : cJSON * tag = cJSON_Parse(chunk.memory);
107 2 : if (!tag)
108 : {
109 1 : debug("failed json parse request: -->\n%s\n<--\n",chunk.memory);
110 1 : if (request) free(request);
111 1 : curl_easy_cleanup(curl);
112 1 : free_dynamicbuf(&chunk);
113 1 : return 1;
114 : }
115 1 : *release=tag;
116 :
117 1 : free_dynamicbuf(&chunk);
118 1 : if (request) free(request);
119 1 : curl_easy_cleanup(curl);
120 1 : return 0;
121 : }
122 : else
123 : {
124 1 : debug("Failed API request with %d\n%s\n",http_code,chunk.memory);
125 : }
126 1 : free_dynamicbuf(&chunk);
127 1 : if (request) free(request);
128 1 : curl_easy_cleanup(curl);
129 1 : return 0;
130 : }
131 : return 1;
132 : }
133 :
134 : // used to interupt the long polling
135 0 : int progress_callback(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
136 : {
137 0 : stop_struct_t * stop_struct_data=(stop_struct_t *)clientp;
138 0 : if (*(stop_struct_data->exit) || *(stop_struct_data->reload)) return 1;
139 :
140 0 : return 0;
141 : }
142 :
143 0 : cJSON * get_job(cJSON * runner,cJSON * info, void * stop_struct, CURL * curl)
144 : {
145 :
146 0 : char * token=cJSON_get_key(runner, "token");
147 0 : char * domain=cJSON_get_key(runner, "ci_url");
148 0 : if (!domain || !token) return NULL;
149 0 : int * error_count=&(cJSON_GetObjectItem(runner, "errorcount")->valueint);
150 0 : int runner_id=cJSON_GetObjectItem(runner, "id")->valueint;
151 0 : cJSON * result=NULL;
152 : CURLcode res;
153 0 : if(curl)
154 : {
155 : //build request
156 : char request[1024];
157 0 : sprintf(request, "%s/api/v4/jobs/request",domain);
158 :
159 : struct MemoryStruct chunk;
160 : struct headerStruct headers_r;
161 0 : init_dynamicbuf( &chunk);
162 0 : init_header_struct(&headers_r);
163 : // add JSON header
164 0 : struct curl_slist *headers = NULL;
165 0 : headers = curl_slist_append(headers, "Content-Type: application/json");
166 0 : headers = curl_slist_append(headers, "accept: application/json");
167 : //headers = curl_slist_append(headers, "User-Agent: gitlab-runner 10.0.0-RC1");
168 0 : curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
169 :
170 : // Enable intruption for gracefull shutdown
171 0 : curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, &progress_callback);
172 0 : curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, stop_struct);
173 : /* enable progress meter */
174 0 : curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
175 0 : curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L);
176 :
177 0 : curl_easy_setopt(curl, CURLOPT_URL, (char*)request);
178 :
179 0 : cJSON * params=cJSON_CreateObject();
180 0 : cJSON_AddStringToObject(params, "token", token);
181 :
182 :
183 0 : char * tmp=cJSON_get_key(runner, "last_update");
184 0 : if (tmp) cJSON_AddStringToObject(params, "last_update", tmp);
185 :
186 0 : cJSON_AddItemToObject(params, "info", info);
187 0 : char * params_str=cJSON_PrintUnformatted(params);
188 0 : cJSON_DetachItemFromObject(params, "info");
189 0 : cJSON_Delete(params);
190 :
191 : //debug("runner %2d: get_job_sent_params: %s\n",runner_id,params_str);
192 : /* complete within 70 seconds (need 50 for long polling)*/
193 0 : curl_easy_setopt(curl, CURLOPT_TIMEOUT, 70L);
194 0 : curl_easy_setopt(curl, CURLOPT_POSTFIELDS, params_str);
195 : //curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
196 :
197 0 : curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
198 : /* send all data to this function */
199 0 : curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
200 :
201 : /* we pass our 'chunk' struct to the callback function */
202 0 : curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
203 :
204 0 : curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION,WriteHeaderCallback);
205 0 : curl_easy_setopt(curl, CURLOPT_WRITEHEADER, &headers_r);
206 0 : res = curl_easy_perform(curl);
207 0 : curl_slist_free_all(headers);
208 :
209 0 : long http_code = 0;
210 0 : curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
211 0 : if (getdebug())
212 : {
213 0 : char * print_it=cJSON_PrintUnformatted(headers_r.header);
214 0 : if (print_it)
215 : {
216 0 : debug("runner %2d: get_job_receive_headers: %s\n",runner_id,print_it);
217 0 : free(print_it);
218 : }
219 : }
220 0 : char * x_last_update=cJSON_get_key(headers_r.header, "X-GitLab-Last-Update");
221 0 : if (x_last_update)
222 : {
223 0 : if (cJSON_get_key(runner, "last_update"))
224 0 : cJSON_ReplaceItemInObject(runner, "last_update", cJSON_CreateString(x_last_update));
225 : else
226 0 : cJSON_AddItemToObject(runner, "last_update", cJSON_CreateString(x_last_update));
227 : }
228 :
229 0 : if (validate_key(headers_r.header, "Gitlab-Ci-Builds-Polling","yes")==0 )
230 : {
231 0 : alert("runner %2d: Long Polling (%s) ...\n",runner_id, domain);
232 0 : if (cJSON_get_key(runner, "Long Polling"))
233 0 : cJSON_ReplaceItemInObject(runner, "Long Polling", cJSON_CreateString("yes"));
234 : else
235 0 : cJSON_AddItemToObject(runner,"Long Polling", cJSON_CreateString("yes"));
236 :
237 : }
238 : else
239 : {
240 0 : if (cJSON_get_key(runner, "Long Polling"))
241 0 : cJSON_ReplaceItemInObject(runner, "Long Polling", cJSON_CreateString("no"));
242 : else
243 0 : cJSON_AddItemToObject(runner,"Long Polling", cJSON_CreateString("no"));
244 : }
245 0 : if (http_code == 201 && res != CURLE_ABORTED_BY_CALLBACK)
246 : {
247 0 : debug("runner %2d: got a job\n",runner_id);
248 : // printf("%s\n",chunk.memory);
249 0 : result= cJSON_Parse(chunk.memory);
250 0 : if (*error_count) (*error_count)=0;
251 : }
252 0 : else if ((http_code == 204 )&& res != CURLE_ABORTED_BY_CALLBACK)
253 : {
254 0 : debug("runner %2d: no job scheduled : %s\n",runner_id,chunk.memory);
255 0 : if (*error_count) (*error_count)=0;
256 : }
257 : else
258 : {
259 0 : if ((*error_count)<10) (*error_count)++; else (*error_count)=10;
260 0 : if (res)
261 0 : error( "runner %2d: (%2d) getjob failed %s\n",runner_id,(*error_count),curl_easy_strerror(res));
262 : else
263 0 : error("runner %2d: (%2d) X-Request-Id:%s =>getjob failed HTTP_code: %ld >> %s\n",runner_id,(*error_count),cJSON_get_key(headers_r.header, "X-Request-Id") ,http_code,chunk.memory);
264 : }
265 0 : free_dynamicbuf(&chunk);
266 0 : free_header_struct(&headers_r);
267 0 : free(params_str);
268 0 : curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, NULL);
269 0 : curl_easy_setopt(curl, CURLOPT_WRITEHEADER, NULL);
270 0 : curl_easy_setopt(curl, CURLOPT_WRITEDATA, NULL);
271 0 : curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,NULL);
272 0 : curl_easy_setopt(curl, CURLOPT_HTTPPOST, NULL);
273 0 : curl_easy_setopt(curl, CURLOPT_HTTPHEADER, NULL);
274 0 : curl_easy_setopt(curl, CURLOPT_POSTFIELDS, NULL);
275 : }
276 :
277 0 : return result;
278 : }
279 :
280 : // bug gitlab
281 0 : void fix_reset_escape_sequence(char * output_buf,size_t o_read)
282 : {
283 0 : char * walk=output_buf;
284 0 : walk[o_read]=0; // we read max one char less then buffer size!
285 : while (1) {
286 0 : char *p = strstr(walk,"\033[00m");
287 : // walked past last occurrence of needle; copy remaining part
288 0 : if (p == NULL) break;
289 0 : *(p+3)=';'; // change second 0 with ;
290 0 : walk=p+1;
291 0 : }
292 0 : }
293 :
294 0 : int update_details(void * userp)
295 : {
296 0 : struct trace_Struct *trace = (struct trace_Struct *)userp;
297 0 : char * tmp_trace=cJSON_get_key(trace->params,"trace");
298 0 : fix_reset_escape_sequence(tmp_trace,strlen(tmp_trace));
299 0 : int response=1;
300 : struct MemoryStruct chunk;
301 0 : init_dynamicbuf( &chunk);
302 :
303 : ;
304 : CURLcode res;
305 0 : if(trace->curl)
306 : {
307 0 : curl_easy_setopt(trace->curl, CURLOPT_URL, trace->url);
308 0 : char *data=cJSON_Print(trace->params);
309 0 : curl_easy_setopt(trace->curl, CURLOPT_POSTFIELDS, data);
310 :
311 : // add JSON header
312 0 : struct curl_slist *headers = NULL;
313 0 : headers = curl_slist_append(headers, "Content-Type: application/json");
314 0 : headers = curl_slist_append(headers, "accept: application/json");
315 0 : curl_easy_setopt(trace->curl, CURLOPT_HTTPHEADER, headers);
316 : // change request to PUT
317 0 : curl_easy_setopt(trace->curl, CURLOPT_CUSTOMREQUEST, "PUT");
318 :
319 : /* send all data to this function */
320 0 : curl_easy_setopt(trace->curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
321 : /* we pass our 'chunk' struct to the callback function */
322 0 : curl_easy_setopt(trace->curl, CURLOPT_WRITEDATA, (void *)&chunk);
323 : /* complete within 20 seconds */
324 0 : curl_easy_setopt(trace->curl, CURLOPT_TIMEOUT, 20L);
325 : // Do request
326 0 : res = curl_easy_perform(trace->curl);
327 0 : curl_slist_free_all(headers);
328 0 : long http_code = 0;
329 0 : curl_easy_getinfo (trace->curl, CURLINFO_RESPONSE_CODE, &http_code);
330 0 : if (http_code == 200 && res != CURLE_ABORTED_BY_CALLBACK)
331 : {
332 : //Succeeded
333 : //printf("DONE PUT\n");
334 0 : if(strcmp(chunk.memory,"true")==0 || strcmp(chunk.memory,"null")==0)
335 0 : response=0;
336 0 : if(strcmp(chunk.memory,"false")==0)
337 0 : response=-1;
338 : //setdebug();
339 0 : if (response) debug( "update job #%10d: http 200, reply \"true\": >%s< \n",trace->job_id,chunk.memory);
340 0 : else debug( "update job #%10d: http 200, no reply \"true\": >%s< \n",trace->job_id,chunk.memory);
341 : //clrdebug();
342 : }
343 : else
344 : {
345 0 : if (res != CURLE_OK) error( "update job #%10d: failed: %s\n",trace->job_id,
346 : curl_easy_strerror(res));
347 : else
348 : {
349 0 : error( "update job #%10d: HTTP %ld feedback: %s\n",trace->job_id,http_code,chunk.memory);
350 : }
351 : response=1;
352 : }
353 :
354 0 : if (data) free(data);
355 0 : curl_easy_setopt(trace->curl, CURLOPT_WRITEDATA, NULL);
356 0 : curl_easy_setopt(trace->curl, CURLOPT_WRITEFUNCTION,NULL);
357 0 : curl_easy_setopt(trace->curl, CURLOPT_HTTPPOST, NULL);
358 0 : curl_easy_setopt(trace->curl, CURLOPT_HTTPHEADER, NULL);
359 0 : curl_easy_setopt(trace->curl, CURLOPT_POSTFIELDS, NULL);
360 : }
361 0 : if (chunk.memory) free_dynamicbuf(&chunk.memory);
362 0 : return response;
363 : }
364 :
365 :
366 : // Progress of artifacts download
367 : // set's data->progress to 1 if an update
368 : // set's data->artifact json key progress int % progress value.
369 : // scan_artifacts_update will use artifact
370 : // scan progress will use data-> porgress
371 0 : int progress_update_arti(void *userp, curl_off_t dltotal, curl_off_t dlnow,
372 : curl_off_t ultotal, curl_off_t ulnow)
373 : {
374 0 : struct data_thread *data = (struct data_thread *)userp;
375 0 : CURL *curl = data->curl;
376 0 : double curtime = 0;
377 0 : curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &curtime);
378 0 : double lastruntime= data->lastruntime;
379 0 : if((curtime - lastruntime) >= 10)
380 : {
381 0 : data->lastruntime = curtime;
382 0 : debug( "DL TIME: %f => %d / %d \r\n", curtime,(int)dlnow,(int) dltotal);
383 0 : int progress=-1;
384 0 : if ((int) dltotal !=0)
385 : {
386 0 : progress=(100LL*(int)dlnow)/dltotal;
387 : }
388 0 : cJSON_GetObjectItem(data->artifact, "progress")->valueint=progress;
389 0 : data->progress=1;
390 : }
391 0 : return 0;
392 : }
393 :
394 : // for curl version < 0x072000 (7.32)
395 0 : int progress_update_arti_oldcurl(void *userp, double dltotal, double dlnow, double ultotal, double ulnow)
396 : {
397 0 : return progress_update_arti(userp, (curl_off_t)dltotal,
398 : (curl_off_t)dlnow,
399 : (curl_off_t)ultotal,
400 : (curl_off_t)ulnow);
401 : }
402 :
403 0 : void *get_artifact(void *userp)
404 : {
405 : FILE *fp;
406 0 : CURL *curl=NULL;
407 0 : struct data_thread *data = (struct data_thread *)userp;
408 0 : if (!data) {printf("no data structure to pull one url\n"); return 0;}
409 : else
410 0 : debug("start download %s\n",cJSON_get_key(data->artifact, "name"));
411 0 : if (data->error_count==0)
412 0 : cJSON_ReplaceItemInObject(data->artifact, "status", cJSON_CreateString("download"));
413 : else
414 0 : cJSON_ReplaceItemInObject(data->artifact, "status", cJSON_CreateString("retrying"));
415 0 : curl = curl_easy_init();
416 :
417 0 : if (curl)
418 : {
419 0 : data->lastruntime = 0;
420 0 : data->curl = curl;
421 0 : curl_easy_setopt(curl, CURLOPT_URL, cJSON_get_key(data->artifact, "api"));
422 0 : debug("url_artifact: %s ,headerAuth: %s\n",cJSON_get_key(data->artifact, "api"),cJSON_get_key( data->artifact, "header_auth"));
423 0 : struct curl_slist *headers = NULL;
424 0 : headers = curl_slist_append(headers, "Content-Type: application/json");
425 0 : headers = curl_slist_append(headers, cJSON_get_key( data->artifact, "header_auth"));
426 0 : curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
427 :
428 0 : curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L);
429 0 : curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
430 : // Enable intruption for gracefull shutdown
431 0 : curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, &progress_update_arti_oldcurl);
432 0 : curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, userp);
433 : #if LIBCURL_VERSION_NUM >= 0x072000
434 : curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, progress_update_arti);
435 : curl_easy_setopt(curl, CURLOPT_XFERINFODATA, userp);
436 : #endif
437 : /* enable progress meter */
438 0 : curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
439 :
440 0 : int res=1;
441 : char filename[1024];
442 0 : char *path=data->path;
443 0 : char * name=cJSON_get_key(data->artifact, "name");
444 0 : if (!name || !path) error("no name or path %p %p",name,path);
445 :
446 0 : sprintf(filename, "%s/%s.zip",path, name);
447 0 : debug("curl download to %s\n",filename);
448 :
449 0 : long http_code = 0;
450 0 : fp = fopen(filename,"wb");
451 0 : if (fp==NULL) error("ERR: curl pull %s prob %s\n",filename,strerror(errno));
452 : else
453 : {
454 : //curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, 0);
455 0 : curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
456 :
457 0 : res= curl_easy_perform(curl); /* ignores error */
458 0 : fclose(fp);
459 0 : curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
460 0 : data->http_code=http_code;
461 : }
462 0 : curl_slist_free_all(headers);
463 0 : if (http_code == 200 && res != CURLE_ABORTED_BY_CALLBACK)
464 : {
465 0 : cJSON_ReplaceItemInObject(data->artifact, "status", cJSON_CreateString("done"));
466 0 : debug("Finish download %s\n",cJSON_get_key(data->artifact, "name"));
467 : }
468 : else
469 : {
470 0 : data->error=1;
471 0 : if (res)
472 0 : debug( "curl failed for %s : %s\n",cJSON_get_key(data->artifact, "name"),curl_easy_strerror(res));
473 : else {
474 0 : debug("HTTP_code: %ld for %s\n",data->http_code,cJSON_get_key(data->artifact, "name"));
475 : }
476 0 : cJSON_ReplaceItemInObject(data->artifact, "status", cJSON_CreateString("error"));
477 : }
478 0 : curl_easy_setopt(curl, CURLOPT_WRITEDATA, NULL);
479 0 : curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,NULL);
480 0 : curl_easy_setopt(curl, CURLOPT_HTTPPOST, NULL);
481 0 : curl_easy_setopt(curl, CURLOPT_HTTPHEADER, NULL);
482 0 : curl_easy_setopt(curl, CURLOPT_POSTFIELDS, NULL);
483 0 : curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
484 : #if LIBCURL_VERSION_NUM >= 0x072000
485 : curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, NULL);
486 : curl_easy_setopt(curl, CURLOPT_XFERINFODATA, NULL);
487 : #endif
488 0 : curl_easy_cleanup(curl);
489 0 : data->finish=1;
490 : return NULL;
491 : }
492 : else
493 : {
494 0 : data->error=1;
495 0 : cJSON_ReplaceItemInObject(data->artifact, "status", cJSON_CreateString("error"));
496 0 : error("No Curl init\n");
497 0 : return NULL;
498 : }
499 : }
500 :
501 : // Doesn work:
502 :
503 : /*
504 : Started POST "/api/v4/jobs/19715/artifacts/authorize" for 192.168.2.1 at 2017-08-17 13:51:11 +0700
505 : JWT::DecodeError (Nil JSON web token):
506 : /opt/gitlab/embedded/lib/ruby/gems/2.3.0/gems/jwt-1.5.6/lib/jwt.rb:120:in `decode'
507 : /opt/gitlab/embedded/service/gitlab-rails/lib/gitlab/workhorse.rb:166:in `decode_jwt'
508 : /opt/gitlab/embedded/service/gitlab-rails/lib/gitlab/workhorse.rb:162:in `verify_api_request!'
509 : /opt/gitlab/embedded/service/gitlab-rails/lib/api/runner.rb:172:in `block (2 levels) in <class:Runner>'
510 :
511 : GET authorisation for artifacts
512 :
513 : * About to connect() to gitlab.gioxa.com port 443 (#2)
514 : * Trying 171.101.61.55...
515 : * Connected to gitlab.gioxa.com (171.101.61.55) port 443 (#2)
516 : * CAfile: /etc/pki/tls/certs/ca-bundle.crt
517 : CApath: none
518 : * SSL connection using TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
519 : * Server certificate:
520 : * subject: CN=gitlab.gioxa.com
521 : * start date: Jul 25 00:56:00 2017 GMT
522 : * expire date: Oct 23 00:56:00 2017 GMT
523 : * common name: gitlab.gioxa.com
524 : * issuer: CN=Let's Encrypt Authority X3,O=Let's Encrypt,C=US
525 : > POST /api/v4/jobs/19715/artifacts/authorize HTTP/1.1
526 : Host: gitlab.gioxa.com
527 : Accept: * / *
528 : CI_JOB_TOKEN: _Ftoqjy1znjJnVhLbLSe
529 : Content-Length: 15
530 : Content-Type: application/x-www-form-urlencoded
531 :
532 : * upload completely sent off: 15 out of 15 bytes
533 : < HTTP/1.1 500 Internal Server Error
534 : < Server: nginx
535 : < Date: Thu, 17 Aug 2017 06:51:11 GMT
536 : < Content-Type: application/json
537 : < Content-Length: 39
538 : < Connection: keep-alive
539 : < Cache-Control: no-cache
540 : < Vary: Origin
541 : < X-Request-Id: 7c60cc26-0291-4e23-a82e-d54044f1b345
542 : < X-Runtime: 0.028801
543 : <
544 : * Connection #2 to host gitlab.gioxa.com left intact
545 : HTTP_code: 500 for filesize 100
546 : return data: {"message":"500 Internal Server Error"}
547 :
548 : */
549 0 : int get_artifact_auth(cJSON * job)
550 : {
551 0 : CURL *curl=NULL;
552 : struct MemoryStruct chunk;
553 0 : init_dynamicbuf( &chunk);
554 0 : debug("\n****************************************************************\nGET authorisation for artifacts\n\n");
555 0 : curl = curl_easy_init();
556 : char url[1024];
557 0 : sprintf(url,"%s/api/v4/jobs/%d/artifacts/authorize",cJSON_get_key(job, "ci_url"),cJSON_GetObjectItem(job, "id")->valueint);
558 :
559 0 : curl_easy_setopt(curl, CURLOPT_URL, url);
560 :
561 : char JOB_TOKEN[256];
562 0 : sprintf(JOB_TOKEN,"CI_JOB_TOKEN: %s",cJSON_get_key( job, "token"));
563 0 : struct curl_slist *headers = NULL;
564 : // headers = curl_slist_append(headers, "Content-Type: application/json");
565 0 : headers = curl_slist_append(headers, JOB_TOKEN);
566 : // headers = curl_slist_append(headers, "accept: application/json");
567 0 : curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
568 :
569 0 : curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
570 :
571 0 : curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
572 :
573 : // create params cJSON struct
574 :
575 : //cJSON * params=cJSON_CreateObject();
576 : //cJSON_AddNumberToObject(params, "filesize", 100);
577 : //char * params_str=cJSON_PrintUnformatted(params);
578 : //cJSON_Delete(params);
579 :
580 0 : long http_code = 0;
581 : //debug("params: %s\n",params_str);
582 : // post data
583 0 : curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "filesize=202022");
584 : /* send all data to this function */
585 0 : curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
586 : /* we pass our 'chunk' struct to the callback function */
587 0 : curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
588 : /* complete within 20 seconds */
589 0 : curl_easy_setopt(curl, CURLOPT_TIMEOUT, 20L);
590 :
591 0 : int res= curl_easy_perform(curl); /* ignores error */
592 0 : int result_f=0;
593 :
594 0 : curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
595 :
596 0 : if (http_code == 200 && res != CURLE_ABORTED_BY_CALLBACK)
597 : {
598 : //debug("return data: %s\n",chunk.memory);
599 : result_f=0;
600 : }
601 : else
602 : {
603 0 : result_f=res;
604 0 : if (res)
605 0 : debug( "curl failed to get artifacys authorisation with filesize %d",100);
606 : else {
607 0 : debug("HTTP_code: %ld for filesize %d\n",http_code,100);
608 0 : error("Err return data: %s\n",chunk.memory);
609 0 : result_f=(int)http_code;
610 : }
611 : }
612 :
613 : //if (params_str) free(params_str);
614 0 : curl_slist_free_all(headers);
615 0 : if (chunk.memory) free_dynamicbuf(&chunk.memory);
616 0 : curl_easy_cleanup(curl);
617 0 : return result_f;
618 : }
619 :
620 :
621 0 : int upload_artifact(cJSON * job, char * upload_name)
622 : {
623 0 : CURL *curl=NULL;
624 : struct MemoryStruct chunk;
625 0 : init_dynamicbuf( &chunk);
626 0 : debug("\n****************************************************************\nUpLoad artifacts\n\n");
627 0 : curl = curl_easy_init();
628 : char url[1024];
629 0 : sprintf(url,"%s/api/v4/jobs/%d/artifacts",cJSON_get_key(job, "ci_url"),cJSON_GetObjectItem(job, "id")->valueint);
630 :
631 0 : curl_easy_setopt(curl, CURLOPT_URL, url);
632 :
633 : char JOB_TOKEN[256];
634 0 : sprintf(JOB_TOKEN,"JOB-TOKEN: %s",cJSON_get_key( job, "token"));
635 0 : struct curl_slist *headers = NULL;
636 : // headers = curl_slist_append(headers, "Content-Type: application/json");
637 0 : headers = curl_slist_append(headers, JOB_TOKEN);
638 0 : headers = curl_slist_append(headers, "expect:");
639 0 : headers = curl_slist_append(headers, "accept: application/json");
640 : //headers = curl_slist_append(headers,"expect:");
641 0 : curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
642 :
643 0 : curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L);
644 :
645 0 : curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
646 :
647 0 : struct curl_httppost *post=NULL;
648 0 : struct curl_httppost *last=NULL;
649 0 : cJSON * artifacts= cJSON_GetArrayItem( cJSON_GetObjectItem(job,"artifacts"),0);
650 :
651 0 : if (cJSON_get_key(artifacts,"expire_in"))
652 : {
653 0 : debug("expire_in %s\n",cJSON_get_key(artifacts,"expire_in"));
654 0 : curl_formadd(&post, &last,
655 : CURLFORM_COPYNAME, "expire_in",
656 : CURLFORM_COPYCONTENTS, cJSON_get_key(artifacts,"expire_in"), CURLFORM_END);
657 : }
658 : else
659 : {
660 0 : debug("expire_in [default] 1w\n");
661 0 : curl_formadd(&post, &last,
662 : CURLFORM_COPYNAME, "expire_in",
663 : CURLFORM_COPYCONTENTS, "1w", CURLFORM_END);
664 : }
665 : //curl_formadd(&post, &last,
666 : // CURLFORM_COPYNAME, "file",
667 : // CURLFORM_COPYCONTENTS, "artifacts.zip", CURLFORM_END);
668 :
669 : char filename[1024];
670 0 : if (cJSON_get_key(artifacts,"name"))
671 : {
672 0 : sprintf(filename,"%s.zip",cJSON_get_key(artifacts,"name"));
673 : }
674 : else
675 : {
676 0 : sprintf(filename,"artifacts.zip");
677 : }
678 0 : debug("filename=%s\n",filename);
679 0 : curl_formadd(&post, &last,
680 : CURLFORM_COPYNAME, "file",
681 : CURLFORM_CONTENTTYPE, "application/zip",
682 : CURLFORM_FILE, upload_name,
683 : CURLFORM_FILENAME,filename, CURLFORM_END);
684 :
685 : /* Set the form info */
686 0 : curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
687 :
688 0 : long http_code = 0;
689 :
690 : /* send all data to this function */
691 0 : curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
692 : /* we pass our 'chunk' struct to the callback function */
693 0 : curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
694 : /* complete within 20 seconds */
695 0 : curl_easy_setopt(curl, CURLOPT_TIMEOUT, 1200L);
696 :
697 0 : int res= curl_easy_perform(curl);
698 0 : int result_f=0;
699 : /* free the post data again */
700 0 : curl_formfree(post);
701 0 : curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
702 :
703 0 : if (http_code == 201 && res != CURLE_ABORTED_BY_CALLBACK)
704 : {
705 : //debug("return data: %s\n",chunk.memory);
706 : result_f=0;
707 : }
708 : else
709 : {
710 0 : result_f=res;
711 0 : if (res)
712 0 : debug( "curl failed to get artifacys authorisation with filesize %d",100);
713 : else {
714 0 : error("HTTP_code: %ld for upload artifact \n",http_code);
715 0 : error("return data: %s\n",chunk.memory);
716 0 : result_f=(int)http_code;
717 : }
718 : }
719 0 : curl_easy_setopt(curl, CURLOPT_WRITEDATA, NULL);
720 0 : curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,NULL);
721 0 : curl_easy_setopt(curl, CURLOPT_HTTPPOST, NULL);
722 0 : curl_easy_setopt(curl, CURLOPT_HTTPHEADER, NULL);
723 : //if (params_str) free(params_str);
724 0 : curl_slist_free_all(headers);
725 0 : if (chunk.memory) free_dynamicbuf(&chunk.memory);
726 0 : curl_easy_cleanup(curl);
727 0 : return result_f;
728 : }
|