summaryrefslogtreecommitdiff
path: root/src/other_tools/just_mr/setup_utils.cpp
blob: 864aa684e1b59c61918f75fa4a82cb688342b060 (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
// 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/other_tools/just_mr/setup_utils.hpp"

#include <algorithm>
#include <cstdlib>
#include <deque>
#include <exception>
#include <fstream>
#include <iterator>
#include <queue>
#include <unordered_set>
#include <utility>
#include <variant>

#include "nlohmann/json.hpp"
#include "src/buildtool/build_engine/expression/expression.hpp"
#include "src/buildtool/execution_api/remote/config.hpp"
#include "src/buildtool/file_system/file_system_manager.hpp"
#include "src/buildtool/file_system/precomputed_root.hpp"
#include "src/buildtool/logging/log_level.hpp"
#include "src/buildtool/logging/logger.hpp"
#include "src/other_tools/just_mr/exit_codes.hpp"
#include "src/other_tools/utils/parse_precomputed_root.hpp"
#include "src/utils/cpp/expected.hpp"

namespace {

void WarnUnknownKeys(std::string const& name, ExpressionPtr const& repo_def) {
    if (not repo_def->IsMap()) {
        return;
    }
    for (auto const& [key, value] : repo_def->Map()) {
        if (not kRepositoryExpectedFields.contains(key)) {
            Logger::Log(std::any_of(kRepositoryPossibleFieldTrunks.begin(),
                                    kRepositoryPossibleFieldTrunks.end(),
                                    [k = key](auto const& trunk) {
                                        return k.find(trunk) !=
                                               std::string::npos;
                                    })
                            ? LogLevel::Debug
                            : LogLevel::Warning,
                        "Ignoring unknown field {} in repository {}",
                        key,
                        name);
        }
    }
}

[[nodiscard]] auto GetTargetRepoIfPrecomputed(ExpressionPtr const& repos,
                                              std::string const& name)
    -> std::optional<std::string> {
    // Resolve indirections while the root's workspace root is declared
    // implicitly:
    ExpressionPtr root{name};
    while (root.IsNotNull() and root->IsString()) {
        auto const repo = repos->Get(root->String(), Expression::none_t{});
        if (not repo.IsNotNull() or not repo->IsMap()) {
            return std::nullopt;
        }
        root = repo->Get("repository", Expression::none_t{});
    }

    // Check the root is a precomputed root:
    if (auto const precomputed = ParsePrecomputedRoot(root)) {
        return precomputed->GetReferencedRepository();
    }
    return std::nullopt;
}

[[nodiscard]] auto IsAbsent(ExpressionPtr const& repo_def) -> bool {
    if (repo_def.IsNotNull() and repo_def->IsMap()) {
        if (auto repo = repo_def->Get("repository", Expression::none_t{});
            repo.IsNotNull() and repo->IsMap()) {
            if (auto pragma = repo->Get("pragma", Expression::none_t{});
                pragma.IsNotNull() and pragma->IsMap()) {
                auto absent = pragma->Get("absent", Expression::none_t{});
                return absent.IsNotNull() and absent->IsBool() and
                       absent->Bool();
            }
        }
    }
    return false;
}

[[nodiscard]] auto IsNotContentFixed(ExpressionPtr const& repo_def) -> bool {
    if (not repo_def.IsNotNull() or not repo_def->IsMap()) {
        return false;
    }
    if (auto repo = repo_def->Get("repository", Expression::none_t{});
        repo.IsNotNull() and repo->IsMap()) {
        // Check if type == "file"
        auto type = repo->Get("type", Expression::none_t{});
        if (not type.IsNotNull() or not type->IsString()) {
            return false;
        }
        if (type->String() == "file") {
            auto pragma = repo->Get("pragma", Expression::none_t{});
            if (not pragma.IsNotNull() or not pragma->IsMap()) {
                return true;  // not content-fixed if not to_git
            }
            // Check for explicit to_git == true
            if (auto to_git = pragma->Get("to_git", Expression::none_t{});
                to_git.IsNotNull() and to_git->IsBool() and to_git->Bool()) {
                return false;
            }
            // Check for implicit to_git == true
            if (auto special = pragma->Get("special", Expression::none_t{});
                special.IsNotNull() and special->IsString()) {
                auto const& special_str = special->String();
                if (special_str == "resolve-partially" or
                    special_str == "resolve-completely") {
                    return false;
                }
            }
            return true;  // not content-fixed if not to_git
        }
    }
    return false;
}

}  // namespace

namespace JustMR::Utils {

void ReachableRepositories(
    ExpressionPtr const& repos,
    std::string const& main,
    std::shared_ptr<JustMR::SetupRepos> const& setup_repos) {
    // use temporary sets to avoid duplicates
    std::unordered_set<std::string> include_repos_set;
    std::unordered_set<std::string> setup_repos_set;

    bool absent_main = IsAbsent(repos->Get(main, Expression::none_t{}));

    // traverse all bindings of main repository
    for (std::queue<std::string> to_process({main}); not to_process.empty();
         to_process.pop()) {
        auto const& repo_name = to_process.front();

        // Check the repo hasn't been processed yet
        if (not include_repos_set.insert(repo_name).second) {
            continue;
        }
        auto const repos_repo_name =
            repos->Get(repo_name, Expression::none_t{});
        if (not repos_repo_name.IsNotNull()) {
            continue;
        }
        WarnUnknownKeys(repo_name, repos_repo_name);

        // Warn if main repo is marked absent and current repo (including main)
        // is not content-fixed
        if (absent_main and IsNotContentFixed(repos_repo_name)) {
            Logger::Log(LogLevel::Warning,
                        "Found non-content-fixed repository {} as dependency "
                        "of absent main repository {}",
                        nlohmann::json(repo_name).dump(),
                        nlohmann::json(main).dump());
        }

        // If the current repo is a computed one, process its target repo
        if (auto precomputed = GetTargetRepoIfPrecomputed(repos, repo_name)) {
            to_process.push(*std::move(precomputed));
        }

        // check bindings
        auto const bindings =
            repos_repo_name->Get("bindings", Expression::none_t{});
        if (bindings.IsNotNull() and bindings->IsMap()) {
            for (auto const& bound : bindings->Map().Values()) {
                if (bound.IsNotNull() and bound->IsString()) {
                    to_process.push(bound->String());
                }
            }
        }

        for (auto const& layer : kAltDirs) {
            auto const layer_val =
                repos_repo_name->Get(layer, Expression::none_t{});
            if (layer_val.IsNotNull() and layer_val->IsString()) {
                auto const layer_repo_name = layer_val->String();
                setup_repos_set.insert(layer_repo_name);

                // If the overlay repo is a computed one, process its target
                // repo
                if (auto precomputed =
                        GetTargetRepoIfPrecomputed(repos, layer_repo_name)) {
                    to_process.push(*std::move(precomputed));
                }
            }
        }
    }
    setup_repos_set.insert(include_repos_set.begin(), include_repos_set.end());

    // copy to vectors
    setup_repos->to_setup.clear();
    setup_repos->to_setup.reserve(setup_repos_set.size());
    std::copy(
        setup_repos_set.begin(),
        setup_repos_set.end(),
        std::inserter(setup_repos->to_setup, setup_repos->to_setup.end()));
    setup_repos->to_include.clear();
    setup_repos->to_include.reserve(include_repos_set.size());
    std::copy(
        include_repos_set.begin(),
        include_repos_set.end(),
        std::inserter(setup_repos->to_include, setup_repos->to_include.end()));
}

void DefaultReachableRepositories(
    ExpressionPtr const& repos,
    std::shared_ptr<JustMR::SetupRepos> const& setup_repos) {
    setup_repos->to_setup = repos->Map().Keys();
    setup_repos->to_include = setup_repos->to_setup;
}

auto ReadConfiguration(
    std::optional<std::filesystem::path> const& config_file_opt,
    std::optional<std::filesystem::path> const& absent_file_opt)
    -> std::shared_ptr<Configuration> {
    if (not config_file_opt) {
        Logger::Log(LogLevel::Error, "Cannot find repository configuration.");
        std::exit(kExitConfigError);
    }
    auto const& config_file = *config_file_opt;

    auto config = nlohmann::json::object();
    if (not FileSystemManager::IsFile(config_file)) {
        Logger::Log(LogLevel::Error,
                    "Cannot read config file {}.",
                    config_file.string());
        std::exit(kExitConfigError);
    }
    try {
        std::ifstream fs(config_file);
        config = nlohmann::json::parse(fs);
        if (not config.is_object()) {
            Logger::Log(LogLevel::Error,
                        "Config file {} does not contain a JSON object.",
                        config_file.string());
            std::exit(kExitConfigError);
        }
    } catch (std::exception const& e) {
        Logger::Log(LogLevel::Error,
                    "Parsing config file {} failed with error:\n{}",
                    config_file.string(),
                    e.what());
        std::exit(kExitConfigError);
    }

    if (absent_file_opt) {
        if (not FileSystemManager::IsFile(*absent_file_opt)) {
            Logger::Log(LogLevel::Error,
                        "Not file specifying the absent repositories: {}",
                        absent_file_opt->string());
            std::exit(kExitConfigError);
        }
        try {
            std::ifstream fs(*absent_file_opt);
            auto absent = nlohmann::json::parse(fs);
            if (not absent.is_array()) {
                Logger::Log(LogLevel::Error,
                            "Expected {} to contain a list of repository "
                            "names, but found {}",
                            absent_file_opt->string(),
                            absent.dump());
                std::exit(kExitConfigError);
            }
            std::unordered_set<std::string> absent_set{};
            for (auto const& repo : absent) {
                if (not repo.is_string()) {
                    Logger::Log(LogLevel::Error,
                                "Repositories names have to be strings, but "
                                "found entry {} in {}",
                                repo.dump(),
                                absent_file_opt->string());
                    std::exit(kExitConfigError);
                }
                absent_set.insert(repo.get<std::string>());
            }
            auto new_repos = nlohmann::json::object();
            auto repos = config.value("repositories", nlohmann::json::object());
            for (auto const& [key, val] : repos.items()) {
                new_repos[key] = val;
                auto ws = val.value("repository", nlohmann::json::object());
                if (ws.is_object()) {
                    auto pragma = ws.value("pragma", nlohmann::json::object());
                    pragma["absent"] = absent_set.contains(key);
                    ws["pragma"] = pragma;
                    new_repos[key]["repository"] = ws;
                }
            }
            config["repositories"] = new_repos;
        } catch (std::exception const& e) {
            Logger::Log(LogLevel::Error,
                        "Parsing absent-repos file {} failed with error:\n{}",
                        absent_file_opt->string(),
                        e.what());
            std::exit(kExitConfigError);
        }
    }

    try {
        return std::make_shared<Configuration>(Expression::FromJson(config));
    } catch (std::exception const& e) {
        Logger::Log(LogLevel::Error,
                    "Parsing configuration file failed with error:\n{}",
                    e.what());
        std::exit(kExitConfigError);
    }
}

auto CreateAuthConfig(MultiRepoRemoteAuthArguments const& authargs) noexcept
    -> std::optional<Auth> {

    Auth::TLS::Builder tls_builder;
    tls_builder.SetCACertificate(authargs.tls_ca_cert)
        .SetClientCertificate(authargs.tls_client_cert)
        .SetClientKey(authargs.tls_client_key);

    // create auth config (including validation)
    auto result = tls_builder.Build();
    if (result) {
        if (*result) {
            // correctly configured TLS/SSL certification
            return *std::move(*result);
        }
        Logger::Log(LogLevel::Error, result->error());
        return std::nullopt;
    }

    // no TLS/SSL configuration was given, and we currently support no other
    // certification method, so return an empty config (no certification)
    return Auth{};
}

auto CreateLocalExecutionConfig(MultiRepoCommonArguments const& cargs) noexcept
    -> std::optional<LocalExecutionConfig> {

    LocalExecutionConfig::Builder builder;
    if (cargs.local_launcher.has_value()) {
        builder.SetLauncher(*cargs.local_launcher);
    }

    auto config = builder.Build();
    if (config) {
        return *std::move(config);
    }
    Logger::Log(LogLevel::Error, config.error());
    return std::nullopt;
}

auto CreateRemoteExecutionConfig(
    std::optional<std::string> const& remote_exec_addr,
    std::optional<std::string> const& remote_serve_addr) noexcept
    -> std::optional<RemoteExecutionConfig> {
    // if only a serve endpoint address is given, we assume it is one that acts
    // also as remote-execution
    auto remote_addr = remote_exec_addr ? remote_exec_addr : remote_serve_addr;

    RemoteExecutionConfig::Builder builder;
    auto config = builder.SetRemoteAddress(remote_addr).Build();

    if (config) {
        return *std::move(config);
    }

    Logger::Log(LogLevel::Error, config.error());
    return std::nullopt;
}

auto CreateServeConfig(
    std::optional<std::string> const& remote_serve_addr) noexcept
    -> std::optional<RemoteServeConfig> {
    RemoteServeConfig::Builder builder;
    auto config = builder.SetRemoteAddress(remote_serve_addr).Build();

    if (config) {
        return *std::move(config);
    }

    Logger::Log(LogLevel::Error, config.error());
    return std::nullopt;
}

}  // namespace JustMR::Utils