Skip to content

Commit

Permalink
fix: IConfiguration definition is base
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrasseur-aneo committed Jul 13, 2023
1 parent a4a6357 commit d8cdd96
Show file tree
Hide file tree
Showing 16 changed files with 160 additions and 127 deletions.
11 changes: 5 additions & 6 deletions packages/cpp/ArmoniK.Api.Common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ find_package(Threads)

include(FetchContent)

FetchContent_Declare(
simdjson
GIT_REPOSITORY https://github.com/simdjson/simdjson.git
GIT_SHALLOW TRUE
GIT_TAG tags/v3.2.1
)
FETCHCONTENT_DECLARE(
simdjson
URL https://github.com/simdjson/simdjson/archive/refs/tags/v3.2.1.tar.gz
DOWNLOAD_EXTRACT_TIMESTAMP TRUE
)

FetchContent_GetProperties(simdjson)
if(NOT simdjson_POPULATED)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include <sstream>
#include <utils/IConfiguration.h>

/**
* @brief The armonik namespace contains classes and functions related to the Armonik API.
Expand Down
30 changes: 4 additions & 26 deletions packages/cpp/ArmoniK.Api.Common/header/utils/EnvConfiguration.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma once

/**
* @file EnvConfiguration.h
* @brief Header file for the EnvConfiguration class
Expand All @@ -15,32 +17,8 @@ class EnvConfiguration : public IConfiguration {
/**
* @brief Default constructor
*/
EnvConfiguration() = default;

/**
* @brief Gets the value of an environment variable
* @param string The name of the environment variable
* @return The value of the environment variable, or an empty string if not found
*/
[[nodiscard]] std::string get(const std::string &string) const override {
std::string value = std::getenv(string.c_str());
if (!value.empty()) {
return value;
}
throw std::runtime_error("Can't get server address !");
EnvConfiguration(){
add_env_configuration();
}

/**
* @brief Sets the value of an environment variable
* @param string The name of the environment variable
* @param value The value to set
*/
void set(const std::string &string, const std::string &value) override {}

/**
* @brief Copies the values of another IConfiguration object into this one
* @param other The IConfiguration object to copy from
*/
void set(const IConfiguration &other) override {}
};
} // namespace armonik::api::common::utils
24 changes: 20 additions & 4 deletions packages/cpp/ArmoniK.Api.Common/header/utils/IConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
#pragma once

#include <memory>
#include <set>
#include <string>
#include <unordered_map>
#include <map>
#include <vector>

namespace armonik::api::common::options {
class ComputePlane;
Expand Down Expand Up @@ -35,20 +37,26 @@ class IConfiguration {
* @param string Key to look up.
* @return The value associated with the key, as a string.
*/
[[nodiscard]] virtual std::string get(const std::string &string) const = 0;
[[nodiscard]] std::string get(const std::string &string) const;

/**
* @brief Set the value associated with the given key.
* @param string Key to set the value for.
* @param value Value to set for the key.
*/
virtual void set(const std::string &string, const std::string &value) = 0;
void set(const std::string &string, const std::string &value);

/**
* @brief Set the values from another IConfiguration object.
* @param other IConfiguration object to copy values from.
*/
virtual void set(const IConfiguration &other) = 0;
void set(const IConfiguration &other);

/**
* @brief List defined values of this configuration.
* @note Does not include environment variables
*/
[[nodiscard]] const std::map<std::string, std::string>& list() const;

/**
* @brief Add JSON configuration from a file.
Expand All @@ -68,5 +76,13 @@ class IConfiguration {
* @return A ComputePlane object representing the current configuration.
*/
options::ComputePlane get_compute_plane();

private:
/**
* @brief Storage for the key-value pairs.
*/
std::map<std::string, std::string> options_;
std::set<std::string> above_env_keys_;
bool use_environment_ = false;
};
} // namespace armonik::api::common::utils
27 changes: 2 additions & 25 deletions packages/cpp/ArmoniK.Api.Common/header/utils/JsonConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ namespace armonik::api::common::utils {
*/
class JsonConfiguration : public IConfiguration {
private:
/**
* Parsed values
*/
std::unordered_map<std::string, std::string> values;

JsonConfiguration() = default;

public:
Expand All @@ -26,26 +21,8 @@ class JsonConfiguration : public IConfiguration {
*/
explicit JsonConfiguration(const std::string &filepath);

static void fromPath(IConfiguration& config, const std::string &filepath);
static JsonConfiguration fromString(const std::string &json_string);

/**
* @brief Get the value associated with the given key.
* @param string Key to look up.
* @return The value associated with the key, as a string.
*/
[[nodiscard]] std::string get(const std::string &string) const override { return values.at(string); }

/**
* @brief Set the value associated with the given key.
* @param string Key to set the value for.
* @param value Value to set for the key.
*/
void set(const std::string &string, const std::string &value) override { values.insert_or_assign(string, value); }

/**
* @brief Set the values from another IConfiguration object.
* @param other IConfiguration object to copy values from.
*/
void set(const IConfiguration &other) override {}
static void fromString(IConfiguration& config, const std::string &json_string);
};
} // namespace armonik::api::common::utils
51 changes: 0 additions & 51 deletions packages/cpp/ArmoniK.Api.Common/header/utils/RootConfiguration.h

This file was deleted.

37 changes: 32 additions & 5 deletions packages/cpp/ArmoniK.Api.Common/source/utils/IConfiguration.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,49 @@
#include "utils/IConfiguration.h"

#include "options/ComputePlane.h"
#include "utils/EnvConfiguration.h"
#include "utils/JsonConfiguration.h"

namespace armonik::api::common::utils {
IConfiguration &IConfiguration::add_json_configuration(const std::string &file_path) {
JsonConfiguration json_configuration(file_path);

JsonConfiguration::fromPath(*this, file_path);
return *this;
}

IConfiguration &IConfiguration::add_env_configuration() {
EnvConfiguration env_config;
use_environment_ = true;
above_env_keys_.clear();
return *this;
}

options::ComputePlane IConfiguration::get_compute_plane() {
return *this;
}

options::ComputePlane IConfiguration::get_compute_plane() { return *this; }
void IConfiguration::set(const IConfiguration &other) {
for(auto&& [key, value] : other.list()){
set(key, value);
}
}
void IConfiguration::set(const std::string &key, const std::string &value) {
if(use_environment_){
above_env_keys_.insert(key);
}
options_[key] = value;
}

std::string IConfiguration::get(const std::string &string) const {
if(use_environment_ && above_env_keys_.find(string) == above_env_keys_.end()){
char* value = std::getenv(string.c_str());
if(value != nullptr){
return value;
}
}
auto position = options_.find(string);
return position == options_.end() ? "" : position->second;
}

const std::map<std::string, std::string> &IConfiguration::list() const {
return options_;
}

} // namespace armonik::api::common::utils
18 changes: 13 additions & 5 deletions packages/cpp/ArmoniK.Api.Common/source/utils/JsonConfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ using namespace simdjson;
* @param prefix Prefix for the key
* @param element json element
*/
void populate(armonik::api::common::utils::JsonConfiguration &config, const std::string &prefix,
void populate(armonik::api::common::utils::IConfiguration &config, const std::string &prefix,
const dom::element &element) {
switch (element.type()) {
case dom::element_type::ARRAY: {
Expand All @@ -33,14 +33,22 @@ void populate(armonik::api::common::utils::JsonConfiguration &config, const std:
}

armonik::api::common::utils::JsonConfiguration::JsonConfiguration(const std::string &json_path) {
dom::parser parser;
populate(*this, "", parser.load(json_path));
fromPath(*this, json_path);
}

armonik::api::common::utils::JsonConfiguration
armonik::api::common::utils::JsonConfiguration::fromString(const std::string &json_string) {
JsonConfiguration config;
fromString(config, json_string);
return std::move(config);
}
void armonik::api::common::utils::JsonConfiguration::fromPath(armonik::api::common::utils::IConfiguration &config,
const std::string &filepath) {
dom::parser parser;
populate(config, "", parser.load(filepath));
}
void armonik::api::common::utils::JsonConfiguration::fromString(armonik::api::common::utils::IConfiguration &config,
const std::string &json_string) {
dom::parser parser;
populate(config, "", parser.parse(padded_string(json_string)));
return std::move(config);
}
}
3 changes: 1 addition & 2 deletions packages/cpp/ArmoniK.Api.Worker.Tests/source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include "grpcpp/support/sync_stream.h"
#include "objects.pb.h"

#include "utils/RootConfiguration.h"
#include "utils/WorkerServer.h"
#include "worker_common.pb.h"
#include "worker_service.grpc.pb.h"
Expand All @@ -29,7 +28,7 @@ using namespace armonik::api::common::utils;
int main(int argc, char **argv) {
std::cout << "Starting C++ worker..." << std::endl;

std::shared_ptr<IConfiguration> config = std::make_shared<RootConfiguration>();
std::shared_ptr<IConfiguration> config = std::make_shared<IConfiguration>();

config->set("ComputePlane__WorkerChannel__Address", "/cache/armonik_worker.sock");
config->set("ComputePlane__AgentChannel__Address", "/cache/armonik_agent.sock");
Expand Down
1 change: 1 addition & 0 deletions packages/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ add_subdirectory(ArmoniK.Api.Client)


if (BUILD_TEST OR WIN32)
set(BUILD_DIR "/app/buildtest")
add_subdirectory(ArmoniK.Api.Worker.Tests)
add_subdirectory(ArmoniK.Api.Tests)
endif()
Expand Down
2 changes: 2 additions & 0 deletions packages/cpp/NOTICE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This project uses simdjson (https://github.com/simdjson/simdjson) from The simdjson authors under Apache 2.0 license (https://www.apache.org/licenses/LICENSE-2.0)

36 changes: 36 additions & 0 deletions packages/cpp/tools/BuildEnv.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
FROM ubuntu:23.04

RUN apt-get update && DEBIAN_FRONTEND="noninteractive" TZ="Europe/London" apt-get install -y \
ssh \
gcc \
g++ \
gdb \
clang \
make \
ninja-build \
cmake \
autoconf \
automake \
locales-all \
build-essential \
libc-ares-dev \
protobuf-compiler-grpc \
grpc-proto \
libgrpc-dev \
libgrpc++-dev \
libprotobuf-dev \
&& apt-get clean

ENV protobuf_BUILD_TESTS=OFF

RUN ( \
echo 'LogLevel DEBUG2'; \
echo 'PermitRootLogin yes'; \
echo 'PasswordAuthentication yes'; \
echo 'Subsystem sftp /usr/lib/openssh/sftp-server'; \
) > /etc/ssh/sshd_config_test_clion \
&& mkdir -p /run/sshd

RUN yes password | passwd root

CMD ["/usr/sbin/sshd", "-D", "-e", "-f", "/etc/ssh/sshd_config_test_clion"]
4 changes: 1 addition & 3 deletions packages/cpp/tools/Dockerfile.ubuntu
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ RUN apt-get update && DEBIAN_FRONTEND="noninteractive" TZ="Europe/London" apt-ge
protobuf-compiler-grpc \
grpc-proto \
libgrpc-dev \
libgrpc++-dev \
git
libgrpc++-dev

# Set environment variables for protobuf
ENV protobuf_BUILD_TESTS=OFF
Expand All @@ -24,7 +23,6 @@ ENV PATH="/app/install/bin:$PATH"

# Print the PATH variable
RUN echo $PATH
RUN git config --global --add safe.directory '*'

# Set the working directory for building protobuf
WORKDIR /app/build
Expand Down
Loading

0 comments on commit d8cdd96

Please sign in to comment.