-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathinit.c
418 lines (376 loc) · 12.6 KB
/
init.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#include <stdio.h>
#include <stdlib.h>
#include <libconfig.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <limits.h>
#include <grp.h>
#include <pwd.h>
#include "init.h"
builder_config_t builder_config;
int init(char *config_path) {
FILE *config_file;
config_t config;
config_file = fopen(config_path, "r");
if(config_file == NULL) {
printf("Fatal: Error openning config file: %s.\n", strerror(errno));
return -1;
}
config_init(&config);
if(!config_read(&config, config_file)) {
printf("FATAL: Error parsing config file:\n");
int line = config_error_line(&config);
const char *text = config_error_text(&config);
printf("FATAL: Line #%d: %s.\n", line, text);
config_destroy(&config);
goto err;
}
fclose(config_file);
char *log_level;
int req_res;
req_res = config_lookup_string(&config, log_level_path, (const char **)&log_level);
if (!req_res) {
log_level = getenv("LOG_LEVEL");
if (log_level == NULL) {
log_level = (char *)default_log_level;
}
}
if (init_logger(log_level) < 0) {
printf("FATAL: Failed to initialize logger.\n");
goto err;
}
register_thread("Main");
log_printf(LOG_DEBUG, "Config parsed successfuly.\n");
if(!thread_setup()) {
log_printf(LOG_FATAL, "Failed to setup openssl mutexes.\n");
goto err;
}
log_printf(LOG_INFO, "Starting DNS check.\n");
if (check_dns() < 0) {
log_printf(LOG_FATAL, "DNS check failed, can't resolve github.com.\n");
goto err;
} else {
log_printf(LOG_INFO, "Successfuly resolved github.\n");
}
if(get_api_info(&config) < 0) {
goto err;
}
log_printf(LOG_INFO, "Found api base url: %s\n", builder_config.abf_api_url);
if (get_query_string(&config) < 0) {
goto err;
}
init_api(builder_config.abf_api_url, builder_config.api_token, builder_config.query_string);
if (init_strings(&config) < 0) {
goto err;
}
if (get_platform_list(&config) < 0) {
goto err;
}
if(start_statistics_thread(builder_config.query_string)) {
log_printf(LOG_FATAL, "Failed to initialize statistics thread.\n");
goto err;
}
config_destroy(&config);
return 0;
err:
config_destroy(&config);
return -1;
}
static int get_api_info(config_t *config) {
int req_res;
char *tmp;
req_res = config_lookup_string(config, api_url_path, (const char **)&tmp);
if(!req_res) {
log_printf(LOG_FATAL, "%s must be set.\n", api_url_path);
return -1;
}
builder_config.abf_api_url = xstrdup(tmp);
req_res = config_lookup_string(config, file_store_url_path, (const char **)&tmp);
if(!req_res) {
log_printf(LOG_FATAL, "%s must be set.\n", file_store_url_path);
return -1;
}
builder_config.file_store_url = xstrdup(tmp);
req_res = config_lookup_string(config, build_token_path, (const char **)&tmp);
if(!req_res) {
tmp = getenv(build_token_env);
if(tmp == NULL) {
log_printf(LOG_FATAL, "%s or env variable %s must be set.\n", build_token_path, build_token_env);
return -1;
}
}
builder_config.api_token = xstrdup(tmp);
return 0;
}
static int get_query_string(config_t *config) {
char *tmp, *arches, *native_arches, *platforms, *platform_types;
int supported_arches_exist = config_lookup_string(
config,
supported_arches_path,
(const char **)&arches
);
if(!supported_arches_exist) {
tmp = getenv(supported_arches_env);
if(tmp != NULL) {
arches = xstrdup(tmp);
supported_arches_exist = 1;
}
}
int native_arches_exist = config_lookup_string(
config,
native_arches_path,
(const char **)&native_arches
);
if(!native_arches_exist) {
tmp = getenv(native_arches_env);
if(tmp != NULL) {
native_arches = xstrdup(tmp);
native_arches_exist = 1;
}
}
int supported_platforms_exist = config_lookup_string(
config,
supported_platforms_path,
(const char **)&platforms
);
if(!supported_platforms_exist) {
tmp = getenv(supported_platforms_env);
if(tmp != NULL) {
platforms = xstrdup(tmp);
supported_platforms_exist = 1;
}
}
int supported_platform_types_exist = config_lookup_string(
config,
supported_platform_types_path,
(const char **)&platform_types
);
if(!supported_platform_types_exist) {
tmp = getenv(supported_platform_types_env);
if(tmp != NULL) {
platform_types = xstrdup(tmp);
supported_platform_types_exist = 1;
}
}
int len = (supported_arches_exist ? strlen(arches) : 0) +
(native_arches_exist ? strlen(native_arches) : 0) +
(supported_platforms_exist ? strlen(platforms) : 0) +
(supported_platform_types_exist ? strlen(platform_types) : 0);
char *query_string;
if(len) {
char *pointer;
query_string = xmalloc(len + 80);
pointer = query_string;
if(supported_arches_exist) {
pointer += sprintf(pointer, "arches=%s&", arches);
log_printf(LOG_INFO, "Found supported arches: %s\n", arches);
}
if(native_arches_exist) {
pointer += sprintf(pointer, "native_arches=%s&", native_arches);
log_printf(LOG_INFO, "Found native arches: %s\n", native_arches);
}
if(supported_platforms_exist) {
pointer += sprintf(pointer, "platforms=%s&", platforms);
log_printf(LOG_INFO, "Found supported platforms: %s\n", platforms);
}
if(supported_platform_types_exist) {
pointer += sprintf(pointer, "platform_types=%s", platform_types);
log_printf(LOG_INFO, "Found supported platform types: %s\n", platform_types);
}
pointer-=1;
if(*pointer == '&') {
*pointer = '\0';
}
}
else {
query_string = NULL;
}
builder_config.query_string = query_string;
return 0;
}
static int init_strings(config_t *config) {
char *work_dir, *git_scripts_dir;
DIR *test;
int work_dir_exists = config_lookup_string(
config,
work_dir_path,
(const char **)&work_dir
);
if (!work_dir_exists) {
builder_config.work_dir = getenv(work_dir_env);
if (builder_config.work_dir == NULL) {
builder_config.work_dir = xstrdup(default_work_dir);
}
} else {
if (work_dir[strlen(work_dir) - 1] == '/') {
work_dir[strlen(work_dir) - 1] = '\0';
}
builder_config.work_dir = xstrdup(work_dir);
}
test = opendir(builder_config.work_dir);
if (test == NULL) {
log_printf(LOG_FATAL, "Can't access work directory %s: %s\n", builder_config.work_dir, strerror(errno));
return -1;
} else {
closedir(test);
}
builder_config.output_dir = alloc_sprintf(output_fmt, builder_config.work_dir);
log_printf(LOG_DEBUG, "Work directory is %s\n", builder_config.work_dir);
log_printf(LOG_DEBUG, "Output directory is %s\n", builder_config.output_dir);
int git_scripts_dir_exists = config_lookup_string(
config,
git_scripts_dir_path,
(const char **)&git_scripts_dir
);
if (!git_scripts_dir_exists) {
builder_config.git_scripts_dir = xstrdup(default_git_scripts_dir);
} else {
if (git_scripts_dir[strlen(git_scripts_dir) - 1] == '/') {
git_scripts_dir[strlen(git_scripts_dir) - 1] = '\0';
}
builder_config.git_scripts_dir = xstrdup(git_scripts_dir);
}
test = opendir(builder_config.git_scripts_dir);
if (test == NULL) {
log_printf(LOG_FATAL, "Can't access git scripts directory %s: %s\n", builder_config.git_scripts_dir, strerror(errno));
return -1;
} else {
closedir(test);
}
log_printf(LOG_DEBUG, "Git scripts directory is %s\n", builder_config.git_scripts_dir);
char hostname[128];
if(gethostname(hostname, 128) < 0) {
hostname[127] = '\0';
}
builder_config.strings.hostname = getenv(builder_id_env);
if (builder_config.strings.hostname == NULL) {
builder_config.strings.hostname = xstrdup(hostname);
}
log_printf(LOG_DEBUG, "hostname is %s\n", builder_config.strings.hostname);
builder_config.strings.move_output_cmd = alloc_sprintf(move_output_cmd_fmt, builder_config.work_dir, builder_config.output_dir);
log_printf(LOG_DEBUG, "move_output_cmd is %s\n", builder_config.strings.move_output_cmd);
builder_config.strings.container_data_path = alloc_sprintf(container_data_path_fmt, builder_config.output_dir);
log_printf(LOG_DEBUG, "container_data_path is %s\n", builder_config.strings.container_data_path);
builder_config.strings.commit_hash_path = alloc_sprintf(commit_hash_path_fmt, builder_config.work_dir);
log_printf(LOG_DEBUG, "commit_hash_path is %s\n", builder_config.strings.commit_hash_path);
builder_config.strings.fail_reason_path = alloc_sprintf(fail_reason_path_fmt, builder_config.work_dir);
log_printf(LOG_DEBUG, "fail_reason_path is %s\n", builder_config.strings.fail_reason_path);
return 0;
}
static int get_platform_list(config_t *config) {
config_setting_t *platforms_list = config_lookup(config, platforms_path);
if (platforms_list == NULL) {
log_printf(LOG_FATAL, "No scripts found in the config.\n");
return -1;
}
if (config_setting_type(platforms_list) != CONFIG_TYPE_GROUP) {
log_printf(LOG_FATAL, "%s must be a group.\n", platforms_path);
return -1;
}
int len = config_setting_length(platforms_list);
builder_config.builder_scripts_len = len;
builder_config.builder_scripts = xmalloc(sizeof(builder_t) * len);
for (int i = 0; i < len; i++) {
config_setting_t *platform = config_setting_get_elem(platforms_list, i);
char *name = config_setting_name(platform);
if (config_setting_type(platform) != CONFIG_TYPE_GROUP) {
log_printf(LOG_FATAL, "\"%s\" must be a group.\n", name);
return -1;
}
builder_config.builder_scripts[i].type = xstrdup(name);
const char *git, *path;
int git_exists = config_setting_lookup_string(platform, "git", &git);
int path_exists = config_setting_lookup_string(platform, "path", &path);
char *platform_path;
if (git_exists && path_exists) {
log_printf(LOG_FATAL, "Platform %s: Only one of git, path is expected.\n", name);
return -1;
} else if(git_exists && !path_exists) {
const char *branch;
int branch_exists = config_setting_lookup_string(platform, "branch", &branch);
if (!branch_exists) {
branch = default_branch;
}
if (load_scripts(git, branch, name) < 0) {
return -1;
}
platform_path = alloc_sprintf("%s/%s", builder_config.git_scripts_dir, name);
builder_config.builder_scripts[i].is_git = 1;
builder_config.builder_scripts[i].branch = xstrdup(branch);
} else if(!git_exists && path_exists) {
platform_path = xstrdup(path);
builder_config.builder_scripts[i].is_git = 0;
} else {
log_printf(LOG_FATAL, "Platform %s: One of git, path is expected.\n", name);
return -1;
}
const char *script_name, *interpreter;
int script_name_exists = config_setting_lookup_string(platform, "script_name", &script_name);
if (!script_name_exists) {
script_name = default_script_name;
}
int interpreter_exists = config_setting_lookup_string(platform, "interpreter", &interpreter);
if (!interpreter_exists) {
interpreter = default_interpreter;
}
char *script_path = alloc_sprintf("%s/%s", platform_path, script_name);
char *real_script_path = realpath(script_path, NULL);
if (!real_script_path) {
log_printf(LOG_FATAL, "Path %s: %s\n", script_path, strerror(errno));
return -1;
}
char *cmd = alloc_sprintf("%s %s", interpreter, real_script_path);
builder_config.builder_scripts[i].cmd = cmd;
free(script_path);
free(real_script_path);
free(platform_path);
log_printf(LOG_DEBUG, "Platform %s: command is %s\n", name, cmd);
const char *run_as_user, *run_as_group;
int run_as_user_exists = config_setting_lookup_string(platform, "run_as_user", &run_as_user);
if (!run_as_user_exists) {
run_as_user = default_run_as_user;
}
int run_as_group_exists = config_setting_lookup_string(platform, "run_as_group", &run_as_group);
if (!run_as_group_exists) {
run_as_group = default_run_as_group;
}
struct passwd *pwd = getpwnam(run_as_user);
if (pwd == NULL) {
log_printf(LOG_FATAL, "Platform %s: Error retrieving user %s: %s\n", name, run_as_user, strerror(errno));
return -1;
}
builder_config.builder_scripts[i].run_as_uid = pwd->pw_uid;
struct group *grp = getgrnam(run_as_group);
if (grp == NULL) {
log_printf(LOG_FATAL, "Platform %s: Error retrieving group %s: %s\n", name, run_as_group, strerror(errno));
return -1;
}
builder_config.builder_scripts[i].run_as_gid = grp->gr_gid;
log_printf(LOG_DEBUG, "Platform %s: run script as uid %d, gid %d\n", name, pwd->pw_uid, grp->gr_gid);
}
return 0;
}
static int load_scripts(const char *git_repo, const char *git_branch, const char *platform_type) {
char *cmd = alloc_sprintf(clone_cmd, builder_config.git_scripts_dir, platform_type, git_branch, git_repo, platform_type);
char *output;
int res = system_with_output(cmd, &output);
if (res < 0) {
log_printf(LOG_FATAL, "Failed to start command %s.\n", cmd);
if (output != NULL) {
free(output);
}
return -1;
}
int success = 0;
if (!WIFEXITED(res) || WEXITSTATUS(res) != 0) {
log_printf(LOG_FATAL, "Error cloning %s, git output:\n", git_repo);
log_printf(LOG_FATAL, output);
success = -1;
}
free(cmd);
free(output);
return success;
}