<commit_before>/******************************************************************************
 *                    _   _____   __________                                  *
 *                   | | / / _ | / __/_  __/     Visibility                   *
 *                   | |/ / __ |_\ \  / /          Across                     *
 *                   |___/_/ |_/___/ /_/       Space and Time                 *
 *                                                                            *
 * This file is part of VAST. It is subject to the license terms in the       *
 * LICENSE file found in the top-level directory of this distribution and at  *
 * http://vast.io/license. No part of VAST, including this file, may be       *
 * copied, modified, propagated, or distributed except according to the terms *
 * contained in the LICENSE file.                                             *
 ******************************************************************************/

#include "vast/system/node_command.hpp"

#include <caf/event_based_actor.hpp>
#include <caf/scoped_actor.hpp>
#include <caf/stateful_actor.hpp>
#include <caf/io/middleman.hpp>

#ifdef VAST_USE_OPENSSL
#include <caf/openssl/all.hpp>
#endif

#include "vast/concept/parseable/vast/endpoint.hpp"
#include "vast/defaults.hpp"
#include "vast/filesystem.hpp"
#include "vast/logger.hpp"
#include "vast/system/node.hpp"

using namespace caf;

namespace vast::system {

node_command::node_command(command* parent, std::string_view name)
  : command{parent, name},
    node_spawned_{false} {
  // nop
}

node_command::~node_command() {
  // nop
}

expected<actor>
node_command::spawn_or_connect_to_node(scoped_actor& self,
                                       const caf::config_value_map& opts) {
  if (get_or<bool>(opts, "node", false))
    return spawn_node(self, opts);
  return connect_to_node(self, opts);
}

expected<actor> node_command::spawn_node(scoped_actor& self,
                                         const caf::config_value_map& opts) {
  // Fetch values from config.
  auto id = get_or(opts, "id", defaults::command::node_id);
  auto dir = get_or(opts, "dir", defaults::command::directory);
  auto abs_dir = path{dir}.complete();
  VAST_INFO("spawning local node:", id);
  // Pointer to the root command to system::node.
  auto node = self->spawn(system::node, id, abs_dir);
  node_spawned_ = true;
  if (!get_or<bool>(opts, "bare", false)) {
    // If we're not in bare mode, we spawn all core actors.
    auto spawn_component = [&](auto&&... xs) {
      return [&] {
        auto result = error{};
        auto args = make_message(std::move(xs)...);
        self->request(node, infinite, "spawn", std::move(args)).receive(
          [](const actor&) { /* nop */ },
          [&](error& e) { result = std::move(e); }
        );
        return result;
      };
    };
    auto err = error::eval(
      spawn_component("metastore"),
      spawn_component("archive"),
      spawn_component("index"),
      spawn_component("importer")
    );
    if (err) {
      VAST_ERROR(self->system().render(err));
      cleanup(node);
      return err;
    }
  }<commit_msg>Remove check for bare option<commit_after>/******************************************************************************
 *                    _   _____   __________                                  *
 *                   | | / / _ | / __/_  __/     Visibility                   *
 *                   | |/ / __ |_\ \  / /          Across                     *
 *                   |___/_/ |_/___/ /_/       Space and Time                 *
 *                                                                            *
 * This file is part of VAST. It is subject to the license terms in the       *
 * LICENSE file found in the top-level directory of this distribution and at  *
 * http://vast.io/license. No part of VAST, including this file, may be       *
 * copied, modified, propagated, or distributed except according to the terms *
 * contained in the LICENSE file.                                             *
 ******************************************************************************/

#include "vast/system/node_command.hpp"

#include <caf/event_based_actor.hpp>
#include <caf/scoped_actor.hpp>
#include <caf/stateful_actor.hpp>
#include <caf/io/middleman.hpp>

#ifdef VAST_USE_OPENSSL
#include <caf/openssl/all.hpp>
#endif

#include "vast/concept/parseable/vast/endpoint.hpp"
#include "vast/defaults.hpp"
#include "vast/filesystem.hpp"
#include "vast/logger.hpp"
#include "vast/system/node.hpp"

using namespace caf;

namespace vast::system {

node_command::node_command(command* parent, std::string_view name)
  : command{parent, name},
    node_spawned_{false} {
  // nop
}

node_command::~node_command() {
  // nop
}

expected<actor>
node_command::spawn_or_connect_to_node(scoped_actor& self,
                                       const caf::config_value_map& opts) {
  if (get_or<bool>(opts, "node", false))
    return spawn_node(self, opts);
  return connect_to_node(self, opts);
}

expected<actor> node_command::spawn_node(scoped_actor& self,
                                         const caf::config_value_map& opts) {
  // Fetch values from config.
  auto id = get_or(opts, "id", defaults::command::node_id);
  auto dir = get_or(opts, "dir", defaults::command::directory);
  auto abs_dir = path{dir}.complete();
  VAST_INFO("spawning local node:", id);
  // Pointer to the root command to system::node.
  auto node = self->spawn(system::node, id, abs_dir);
  node_spawned_ = true;
  auto spawn_component = [&](auto&&... xs) {
    return [&] {
      auto result = error{};
      auto args = make_message(std::move(xs)...);
      self->request(node, infinite, "spawn", std::move(args)).receive(
        [](const actor&) { /* nop */ },
        [&](error& e) { result = std::move(e); }
      );
      return result;
    };
  };
  auto err = error::eval(
    spawn_component("metastore"),
    spawn_component("archive"),
    spawn_component("index"),
    spawn_component("importer")
  );
  if (err) {
    VAST_ERROR(self->system().render(err));
    cleanup(node);
    return err;
  }
{'fraction_non_alphanumeric': 0.17812449211766618, 'fraction_numerical': 0.0, 'mean_word_length': 2.2803837953091683, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1866194', 'n_tokens_mistral': 1825, 'n_tokens_neox': 1769, 'n_words': 403}