summaryrefslogtreecommitdiff
path: root/src/buildtool/main/serve.cpp
blob: 1a2add16d236d0c6f1da6bc6582e6b33ca3cf086 (plain)
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
// Copyright 2023 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "src/buildtool/main/serve.hpp"

#include <chrono>

#ifndef BOOTSTRAP_BUILD_TOOL

#include <fstream>

#include "nlohmann/json.hpp"
#include "src/buildtool/build_engine/expression/configuration.hpp"
#include "src/buildtool/build_engine/expression/expression.hpp"
#include "src/buildtool/common/remote/retry_parameters.hpp"
#include "src/buildtool/file_system/file_system_manager.hpp"
#include "src/buildtool/logging/log_level.hpp"
#include "src/buildtool/logging/logger.hpp"
#include "src/buildtool/main/exit_codes.hpp"

[[nodiscard]] auto ParseRetryCliOptions(Configuration const& config) noexcept
    -> bool {
    auto max_attempts = config["max-attempts"];
    if (max_attempts.IsNotNull()) {
        if (!max_attempts->IsNumber()) {
            Logger::Log(
                LogLevel::Error,
                "Invalid value for \"max-attempts\" {}. It must be a number.",
                max_attempts->ToString());
            return false;
        }
        if (!Retry::SetMaxAttempts(max_attempts->Number())) {
            Logger::Log(LogLevel::Error,
                        "Invalid value for \"max-attempts\" {}.",
                        max_attempts->Number());
            return false;
        }
    }
    auto initial_backoff = config["initial-backoff-seconds"];
    if (initial_backoff.IsNotNull()) {
        if (!initial_backoff->IsNumber()) {
            Logger::Log(LogLevel::Error,
                        "Invalid value \"initial-backoff-seconds\" {}. It must "
                        "be a number.",
                        initial_backoff->ToString());
            return false;
        }
        if (!Retry::SetMaxAttempts(initial_backoff->Number())) {
            Logger::Log(LogLevel::Error,
                        "Invalid value for \"initial-backoff-seconds\" {}.",
                        initial_backoff->Number());
            return false;
        }
    }
    auto max_backoff = config["max-backoff-seconds"];
    if (max_backoff.IsNotNull()) {
        if (!max_backoff->IsNumber()) {
            Logger::Log(LogLevel::Error,
                        "Invalid value for \"max-backoff-seconds\" {}. It must "
                        "be a number.",
                        max_backoff->ToString());
            return false;
        }
        if (!Retry::SetMaxAttempts(max_backoff->Number())) {
            Logger::Log(LogLevel::Error,
                        "Invalid value for \"max-backoff-seconds\" {}.",
                        max_backoff->Number());
            return false;
        }
    }
    return true;
}

void ReadJustServeConfig(gsl::not_null<CommandLineArguments*> const& clargs) {
    Configuration serve_config{};
    if (FileSystemManager::IsFile(clargs->serve.config)) {
        // json::parse may throw
        try {
            std::ifstream fs(clargs->serve.config);
            auto map = Expression::FromJson(nlohmann::json::parse(fs));
            if (not map->IsMap()) {
                Logger::Log(LogLevel::Error,
                            "In just-serve config file {}: expected an object "
                            "but found:\n{}",
                            clargs->serve.config.string(),
                            map->ToString());
                std::exit(kExitFailure);
            }
            serve_config = Configuration{map};
        } catch (std::exception const& e) {
            Logger::Log(LogLevel::Error,
                        "Parsing just-serve config file {} as JSON failed with "
                        "error:\n{}",
                        clargs->serve.config.string(),
                        e.what());
            std::exit(kExitFailure);
        }
    }
    else {
        Logger::Log(LogLevel::Error,
                    "Cannot read just-serve config file {}",
                    clargs->serve.config.string());
        std::exit(kExitFailure);
    }
    // read local build root
    auto local_root = serve_config["local build root"];
    if (local_root.IsNotNull()) {
        if (not local_root->IsString()) {
            Logger::Log(LogLevel::Error,
                        "just serve: configuration-file provided local root "
                        "has to be a string, but found {}",
                        local_root->ToString());
            std::exit(kExitFailure);
        }
        clargs->endpoint.local_root = local_root->String();
    }
    // read paths of additional lookup repositories
    auto repositories = serve_config["repositories"];
    if (repositories.IsNotNull()) {
        if (not repositories->IsList()) {
            Logger::Log(
                LogLevel::Error,
                "just-serve: configuration-file provided repositories has "
                "to be a list of strings, but found {}",
                repositories->ToString());
            std::exit(kExitFailure);
        }
        auto const& repos_list = repositories->List();
        clargs->serve.repositories.reserve(clargs->serve.repositories.size() +
                                           repos_list.size());
        for (auto const& repo : repos_list) {
            if (not repo->IsString()) {
                Logger::Log(LogLevel::Error,
                            "just-serve: expected each repository path to be a "
                            "string, but found {}",
                            repo->ToString());
                std::exit(kExitFailure);
            }
            clargs->serve.repositories.emplace_back(repo->String());
        }
    }
    // read logging arguments
    auto logging = serve_config["logging"];
    if (logging.IsNotNull()) {
        if (not logging->IsMap()) {
            Logger::Log(
                LogLevel::Error,
                "just-serve: configuration-file provided logging arguments has "
                "to be a map, but found {}",
                logging->ToString());
            std::exit(kExitFailure);
        }
        // read in first the append flag
        auto append = logging->Get("append", Expression::none_t{});
        if (append.IsNotNull()) {
            if (not append->IsBool()) {
                Logger::Log(LogLevel::Error,
                            "Configuration-provided log append has to be a "
                            "flag, but found {}",
                            append->ToString());
                std::exit(kExitFailure);
            }
            clargs->log.log_append = append->Bool();
        }
        // read in the plain flag
        auto plain = logging->Get("plain", Expression::none_t{});
        if (plain.IsNotNull()) {
            if (not plain->IsBool()) {
                Logger::Log(LogLevel::Error,
                            "Configuration-provided plain log has to be a "
                            "flag, but found {}",
                            plain->ToString());
                std::exit(kExitFailure);
            }
            clargs->log.plain_log = plain->Bool();
        }
        // read in files field
        auto files = logging->Get("files", Expression::none_t{});
        if (files.IsNotNull()) {
            if (not files->IsList()) {
                Logger::Log(LogLevel::Error,
                            "Configuration-provided log files has to be a "
                            "list, but found {}",
                            files->ToString());
                std::exit(kExitFailure);
            }
            auto const& files_list = files->List();
            clargs->log.log_files.reserve(clargs->log.log_files.size() +
                                          files_list.size());
            for (auto const& file : files_list) {
                if (not file->IsString()) {
                    Logger::Log(LogLevel::Error,
                                "just-serve: expected each log file path to be "
                                "a string, but found {}",
                                file->ToString());
                    std::exit(kExitFailure);
                }
                clargs->log.log_files.emplace_back(file->String());
            }
        }
        // read in limit field
        auto limit = logging->Get("limit", Expression::none_t{});
        if (limit.IsNotNull()) {
            if (not limit->IsNumber()) {
                Logger::Log(LogLevel::Error,
                            "Configuration-provided log limit has to be "
                            "numeric, but found {}",
                            limit->ToString());
                std::exit(kExitFailure);
            }
            clargs->log.log_limit = ToLogLevel(limit->Number());
        }
    }
    // read client TLS authentication arguments
    auto auth_args = serve_config["authentication"];
    if (auth_args.IsNotNull()) {
        if (not auth_args->IsMap()) {
            Logger::Log(LogLevel::Error,
                        "just-serve: configuration-file provided "
                        "authentication has to be a map, but found {}",
                        auth_args->ToString());
            std::exit(kExitFailure);
        }
        // read the TLS CA certificate
        auto cacert = auth_args->Get("ca cert", Expression::none_t{});
        if (cacert.IsNotNull()) {
            if (not cacert->IsString()) {
                Logger::Log(LogLevel::Error,
                            "Configuration-provided TLS CA certificate has to "
                            "be a string, but found {}",
                            cacert->ToString());
                std::exit(kExitFailure);
            }
            clargs->auth.tls_ca_cert = cacert->String();
        }
        // read the TLS client certificate
        auto client_cert = auth_args->Get("client cert", Expression::none_t{});
        if (client_cert.IsNotNull()) {
            if (not client_cert->IsString()) {
                Logger::Log(LogLevel::Error,
                            "Configuration-provided TLS client certificate has "
                            "to be a string, but found {}",
                            client_cert->ToString());
                std::exit(kExitFailure);
            }
            clargs->cauth.tls_client_cert = client_cert->String();
        }
        // read the TLS client key
        auto client_key = auth_args->Get("client key", Expression::none_t{});
        if (client_key.IsNotNull()) {
            if (not client_key->IsString()) {
                Logger::Log(LogLevel::Error,
                            "Configuration-provided TLS client key has to be a "
                            "string, but found {}",
                            client_key->ToString());
                std::exit(kExitFailure);
            }
            clargs->cauth.tls_client_key = client_key->String();
        }
    }
    // read remote service arguments
    auto remote_service = serve_config["remote service"];
    if (remote_service.IsNotNull()) {
        if (not remote_service->IsMap()) {
            Logger::Log(LogLevel::Error,
                        "just-serve: configuration-file provided remote "
                        "service has to be a map, but found {}",
                        remote_service->ToString());
            std::exit(kExitFailure);
        }
        // read the interface
        auto interface = remote_service->Get("interface", Expression::none_t{});
        if (interface.IsNotNull()) {
            if (not interface->IsString()) {
                Logger::Log(LogLevel::Error,
                            "Configuration-provided remote service interface "
                            "has to be a string, but found {}",
                            interface->ToString());
                std::exit(kExitFailure);
            }
            clargs->service.interface = interface->String();
        }
        // read the port
        auto port = remote_service->Get("port", Expression::none_t{});
        if (port.IsNotNull()) {
            if (not port->IsNumber()) {
                Logger::Log(LogLevel::Error,
                            "Configuration-provided remote service port has to "
                            "be numeric, but found {}",
                            port->ToString());
                std::exit(kExitFailure);
            }
            double val{};
            if (std::modf(port->Number(), &val) != 0.0) {
                Logger::Log(LogLevel::Error,
                            "Configuration-provided remote service port has to "
                            "be an integer, but found {}",
                            interface->ToString());
                std::exit(kExitFailure);
            }
            // we are sure now that the port is an integer
            clargs->service.port = std::nearbyint(val);
        }
        // read the pid file
        auto pid_file = remote_service->Get("pid file", Expression::none_t{});
        if (pid_file.IsNotNull()) {
            if (not pid_file->IsString()) {
                Logger::Log(LogLevel::Error,
                            "Configuration-provided remote service pid file "
                            "has to be a string, but found {}",
                            pid_file->ToString());
                std::exit(kExitFailure);
            }
            clargs->service.pid_file = pid_file->String();
        }
        // read the info file
        auto info_file = remote_service->Get("info file", Expression::none_t{});
        if (info_file.IsNotNull()) {
            if (not info_file->IsString()) {
                Logger::Log(LogLevel::Error,
                            "Configuration-provided remote service info file "
                            "has to be a string, but found {}",
                            info_file->ToString());
                std::exit(kExitFailure);
            }
            clargs->service.info_file = info_file->String();
        }
        // read the TLS server certificate
        auto server_cert =
            remote_service->Get("server cert", Expression::none_t{});
        if (server_cert.IsNotNull()) {
            if (not server_cert->IsString()) {
                Logger::Log(LogLevel::Error,
                            "Configuration-provided TLS server certificate has "
                            "to be a string, but found {}",
                            server_cert->ToString());
                std::exit(kExitFailure);
            }
            clargs->sauth.tls_server_cert = server_cert->String();
        }
        // read the TLS server key
        auto server_key =
            remote_service->Get("server key", Expression::none_t{});
        if (server_key.IsNotNull()) {
            if (not server_key->IsString()) {
                Logger::Log(LogLevel::Error,
                            "Configuration-provided TLS server key has to be a "
                            "string, but found {}",
                            server_key->ToString());
                std::exit(kExitFailure);
            }
            clargs->sauth.tls_server_key = server_key->String();
        }
    }
    // read execution endpoint arguments
    auto exec_endpoint = serve_config["execution endpoint"];
    if (exec_endpoint.IsNotNull()) {
        if (not exec_endpoint->IsMap()) {
            Logger::Log(LogLevel::Error,
                        "just-serve: configuration-file provided execution "
                        "endpoint has to be a map, but found {}",
                        exec_endpoint->ToString());
            std::exit(kExitFailure);
        }
        // read the compatible flag
        auto compatible =
            exec_endpoint->Get("compatible", Expression::none_t{});
        if (compatible.IsNotNull()) {
            if (not compatible->IsBool()) {
                Logger::Log(LogLevel::Error,
                            "just-serve: expected execution endpoint "
                            "compatible to be a flag, but found {}",
                            compatible->ToString());
                std::exit(kExitFailure);
            }
            // compatibility is set immediately if flag is true
            if (compatible->Bool()) {
                Compatibility::SetCompatible();
            }
        }
        // read the address
        auto address = exec_endpoint->Get("address", Expression::none_t{});
        if (address.IsNotNull()) {
            if (not address->IsString()) {
                Logger::Log(LogLevel::Error,
                            "Configuration-provided execution endpoint address "
                            "has to be a string, but found {}",
                            address->ToString());
                std::exit(kExitFailure);
            }
            clargs->endpoint.remote_execution_address = address->String();
        }
        if (!ParseRetryCliOptions(serve_config)) {
            std::exit(kExitFailure);
        }
    }
    // read jobs value
    auto jobs = serve_config["jobs"];
    if (jobs.IsNotNull()) {
        if (not jobs->IsNumber()) {
            Logger::Log(LogLevel::Error,
                        "just serve: configuration-file provided jobs has to "
                        "be a number, but found {}",
                        jobs->ToString());
            std::exit(kExitFailure);
        }
        clargs->common.jobs = jobs->Number();
    }
    // read build options
    auto build_args = serve_config["build"];
    if (build_args.IsNotNull()) {
        if (not build_args->IsMap()) {
            Logger::Log(LogLevel::Error,
                        "just-serve: configuration-file provided build "
                        "arguments has to be a map, but found {}",
                        build_args->ToString());
            std::exit(kExitFailure);
        }
        // read the build jobs
        auto build_jobs = build_args->Get("build jobs", Expression::none_t{});
        if (build_jobs.IsNotNull()) {
            if (not build_jobs->IsNumber()) {
                Logger::Log(
                    LogLevel::Error,
                    "just serve: configuration-file provided build jobs "
                    "has to be a number, but found {}",
                    build_jobs->ToString());
                std::exit(kExitFailure);
            }
            clargs->build.build_jobs = build_jobs->Number();
        }
        else {
            clargs->build.build_jobs = clargs->common.jobs;
        }
        // read action timeout
        auto timeout = build_args->Get("action timeout", Expression::none_t{});
        if (timeout.IsNotNull()) {
            if (not timeout->IsNumber()) {
                Logger::Log(LogLevel::Error,
                            "just serve: configuration-file provided action "
                            "timeout has to be a number, but found {}",
                            timeout->ToString());
                std::exit(kExitFailure);
            }
            clargs->build.timeout =
                std::size_t(timeout->Number()) * std::chrono::seconds{1};
        }
        auto strategy = build_args->Get("target-cache write strategy",
                                        Expression::none_t{});
        if (strategy.IsNotNull()) {
            if (not strategy->IsString()) {
                Logger::Log(
                    LogLevel::Error,
                    "just serve: configuration-file provided target-cache "
                    "write strategy has to be a string, but found {}",
                    strategy->ToString());
                std::exit(kExitFailure);
            }
            auto s_value = ToTargetCacheWriteStrategy(strategy->String());
            if (not s_value) {
                Logger::Log(LogLevel::Error,
                            "just serve: configuration-file provided unknown "
                            "target-cache write strategy {}",
                            strategy->ToString());
                std::exit(kExitFailure);
            }
            clargs->tc.target_cache_write_strategy = *s_value;
        }
    }
}

#endif  // BOOTSTRAP_BUILD_TOOL