Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create imaginerunner.cpp #488

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
257 changes: 257 additions & 0 deletions .devcontainer/codingrabbitai.cpp
bearycoolAI marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,257 @@
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <functional>
#include <fstream>
#include <filesystem>
#include <thread>
#include <curl/curl.h> // For HTTP requests

// Version Info
const std::string VERSION = "3.1.0";

// OAuth Token (Secure Storage Recommended)
const std::string OAUTH_TOKEN = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6InBFbExHcnRHWHVCMjVWc1RUUGp3VSJ9..."; // Truncated for brevity

// Utilities Namespace
namespace Utils {
// Execute a system command and return the output
std::string executeCommand(const std::string& command) {
char buffer[128];
std::string result;
FILE* pipe = popen(command.c_str(), "r");
if (!pipe) throw std::runtime_error("popen() failed!");
try {
while (fgets(buffer, sizeof buffer, pipe) != nullptr) {
result += buffer;
}
} catch (...) {
pclose(pipe);
throw;
}
pclose(pipe);
return result;
}

// Logging utility
void log(const std::string& message) {
std::cout << "[" << std::chrono::system_clock::now().time_since_epoch().count() << "] " << message << std::endl;
}

// CURL write callback
size_t writeCallback(void* contents, size_t size, size_t nmemb, std::string* userp) {
size_t totalSize = size * nmemb;
userp->append((char*)contents, totalSize);
return totalSize;
}

// Perform an authenticated API request
std::string apiRequest(const std::string& url, const std::string& method = "GET", const std::string& payload = "") {
Utils::log("Performing API Request to: " + url);
CURL* curl;
CURLcode res;
std::string response;

curl = curl_easy_init();
if (curl) {
struct curl_slist* headers = nullptr;
headers = curl_slist_append(headers, ("Authorization: Bearer " + OAUTH_TOKEN).c_str());
headers = curl_slist_append(headers, "Content-Type: application/json");

curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, method.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

if (!payload.empty() && method == "POST") {
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, payload.c_str());
}

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, Utils::writeCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);

res = curl_easy_perform(curl);
if (res != CURLE_OK) {
Utils::log("CURL Error: " + std::string(curl_easy_strerror(res)));
}

curl_easy_cleanup(curl);
} else {
Utils::log("CURL initialization failed");
}

return response;
}
}

// Rabbit AI Namespace
namespace RabbitAI {

class Task {
public:
std::string name;
std::function<void()> action;

Task(const std::string& taskName, std::function<void()> taskAction)
: name(taskName), action(taskAction) {}

void run() {
Utils::log("Running task: " + name);
try {
action();
} catch (const std::exception& e) {
Utils::log("Error in task " + name + ": " + e.what());
}
}
};

class Scheduler {
private:
std::vector<Task> tasks;

public:
void addTask(const Task& task) {
tasks.push_back(task);
}

void runAll() {
Utils::log("Starting all tasks...");
std::vector<std::thread> threads;
for (const auto& task : tasks) {
threads.emplace_back([&]() { task.run(); });
}
for (auto& thread : threads) {
thread.join();
}
Utils::log("All tasks completed.");
}
};

class Environment {
private:
std::map<std::string, std::string> variables;

public:
void setVariable(const std::string& key, const std::string& value) {
variables[key] = value;
}

std::string getVariable(const std::string& key) {
return variables.count(key) ? variables[key] : "";
}

void loadFromFile(const std::string& filepath) {
Utils::log("Loading environment variables from: " + filepath);
if (std::filesystem::exists(filepath)) {
std::ifstream file(filepath);
std::string line;
while (std::getline(file, line)) {
auto delimiterPos = line.find('=');
auto key = line.substr(0, delimiterPos);
auto value = line.substr(delimiterPos + 1);
variables[key] = value;
}
} else {
Utils::log("Environment file not found: " + filepath);
}
}

void print() const {
Utils::log("Environment Variables:");
for (const auto& [key, value] : variables) {
std::cout << key << "=" << value << std::endl;
}
}
};

class Runner {
public:
void execute(const std::string& command) {
Utils::log("Executing command: " + command);
try {
std::string result = Utils::executeCommand(command);
std::cout << result << std::endl;
} catch (const std::exception& e) {
Utils::log("Error executing command: " + std::string(e.what()));
}
}
};
}

// Main Entry Point
int main() {
using namespace RabbitAI;

// Welcome Message
Utils::log("Welcome to CodingRabbitAI Engine v" + VERSION);

// Initialize Environment
Environment env;
try {
env.loadFromFile(".env");
env.print();
} catch (const std::exception& e) {
Utils::log("Error loading environment: " + std::string(e.what()));
}

// Task Scheduler
Scheduler scheduler;

// Add OAuth-Integrated Tasks
scheduler.addTask(Task("Fetch OAuth-Protected Resource", []() {
std::string url = "https://dev-sfpqxik0rm3hw5f1.us.auth0.com/api/v2/users";
std::string response;
try {
response = Utils::apiRequest(url);
Utils::log("API Response: " + response);
} catch (const std::exception& e) {
Utils::log("Error fetching resource: " + std::string(e.what()));
}
}));

// Add RabbitProtocol Tasks
scheduler.addTask(Task("Clone Repository", []() {
Runner runner;
runner.execute("git clone https://github.com/bearycool11/rabbitprotocol.git && cd rabbitprotocol");
}));

scheduler.addTask(Task("Install Dependencies", []() {
Runner runner;
runner.execute("python3 -m pip install -r requirements.txt");
}));

scheduler.addTask(Task("Build Modular Components", []() {
Runner runner;
runner.execute("gcc brain.c -o build/modular_brain_executable");
runner.execute("gcc pml_logic_loop.c -o build/logic_module");
}));

scheduler.addTask(Task("Run Tests", []() {
Runner runner;
runner.execute("./build/modular_brain_executable --test");
runner.execute("./build/logic_module --run-tests");
}));

scheduler.addTask(Task("Build Docker Image", []() {
Runner runner;
runner.execute("docker build -t rabbit_protocol:latest .");
}));

scheduler.addTask(Task("Deploy to Azure", []() {
Runner runner;
runner.execute("az login --service-principal --username $AZURE_USER --password $AZURE_PASSWORD --tenant $AZURE_TENANT");
runner.execute("az cosmosdb create --name ModularBrainDB --resource-group ModularBrain --locations regionName=EastUS");
}));

scheduler.addTask(Task("Clean Up Build Artifacts", []() {
Runner runner;
runner.execute("rm -rf build/");
}));

// Run All Tasks
scheduler.runAll();

Utils::log("CodingRabbitAI Engine finished execution.");

return 0;
}
135 changes: 135 additions & 0 deletions .devcontainer/imaginerunner.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#!/usr/bin/env bash
#-------------------------------------------------------------------------------------------------------------
# CMake Installation Script with Enhanced Logging and Retry Mechanism
#-------------------------------------------------------------------------------------------------------------
set -e

CMAKE_VERSION=${1:-"none"}

if [ "${CMAKE_VERSION}" = "none" ]; then
echo "No CMake version specified, skipping CMake reinstallation."
exit 0
fi

cleanup() {
EXIT_CODE=$?
set +e
if [[ -n "${TMP_DIR}" ]]; then
echo "Executing cleanup of temporary files..."
rm -Rf "${TMP_DIR}"
fi
exit $EXIT_CODE
}
trap cleanup EXIT

echo "Installing CMake version ${CMAKE_VERSION}..."
apt-get -y purge --auto-remove cmake || echo "No existing CMake installation found to purge."
mkdir -p /opt/cmake

architecture=$(dpkg --print-architecture)
case "${architecture}" in
arm64) ARCH=aarch64 ;;
amd64) ARCH=x86_64 ;;
*) echo "Unsupported architecture: ${architecture}." && exit 1 ;;
esac

CMAKE_BINARY_NAME="cmake-${CMAKE_VERSION}-linux-${ARCH}.sh"
CMAKE_CHECKSUM_NAME="cmake-${CMAKE_VERSION}-SHA-256.txt"
TMP_DIR=$(mktemp -d -t cmake-XXXXXXXXXX)

echo "Temporary directory: ${TMP_DIR}"
cd "${TMP_DIR}"

echo "Downloading CMake binary..."
curl --retry 3 --retry-connrefused -sSL "https://github.com/Kitware/CMake/releases/download/v${CMAKE_VERSION}/${CMAKE_BINARY_NAME}" -O
curl --retry 3 --retry-connrefused -sSL "https://github.com/Kitware/CMake/releases/download/v${CMAKE_VERSION}/${CMAKE_CHECKSUM_NAME}" -O

echo "Verifying checksum..."
if ! sha256sum -c --ignore-missing "${CMAKE_CHECKSUM_NAME}"; then
echo "Checksum verification failed. Aborting installation."
exit 1
fi

echo "Installing CMake..."
sh "${TMP_DIR}/${CMAKE_BINARY_NAME}" --prefix=/opt/cmake --skip-license

ln -sf /opt/cmake/bin/cmake /usr/local/bin/cmake
ln -sf /opt/cmake/bin/ctest /usr/local/bin/ctest

echo "CMake ${CMAKE_VERSION} installed successfully."

#!/bin/bash
#-------------------------------------------------------------------------------------------------------------
# Script to Build and Run Runner Images for Ubuntu 24.04 and Windows Server 2025 with Clang
#-------------------------------------------------------------------------------------------------------------

# Variables
UBUNTU_IMAGE_NAME="runner-images-ubuntu-24.04"
WINDOWS_IMAGE_NAME="runner-images-windows-2025"
CONTAINER_NAME="runner-images-container"
UBUNTU_DOCKERFILE_PATH="./Dockerfile.ubuntu"
WINDOWS_DOCKERFILE_PATH="./Dockerfile.windows"
CONTEXT_DIR="."
WORKSPACE_DIR="$(pwd)"
UBUNTU_CLANGFILE_PATH="clangfile.ubuntu.json"
WINDOWS_CLANGFILE_PATH="clangfile.windows.json"
LOG_FILE="runner-images-build.log"
PARALLEL_MODE=false

# Functions

# Cleanup Function
cleanup() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Cleaning up any existing container with the same name..."
docker rm -f ${CONTAINER_NAME} &>/dev/null && echo "Container ${CONTAINER_NAME} removed." || echo "No container to remove."
}

# Build Image Function
build_image() {
local image_name="$1"
local dockerfile_path="$2"
local clangfile_path="$3"

if [ ! -f "${dockerfile_path}" ]; then
echo "[$(date +'%Y-%m-%d %H:%M:%S')] ERROR: Dockerfile not found at ${dockerfile_path}. Aborting."
exit 1
fi

echo "[$(date +'%Y-%m-%d %H:%M:%S')] Building Docker image: ${image_name}..."
docker build -t ${image_name} -f ${dockerfile_path} --build-arg CLANGFILE=${clangfile_path} ${CONTEXT_DIR} | tee -a ${LOG_FILE}
}

# Run Container Function
run_container() {
local image_name="$1"
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Running Docker container: ${CONTAINER_NAME} for ${image_name}..."
docker run -it --rm \
--name ${CONTAINER_NAME} \
--mount type=bind,source=${WORKSPACE_DIR},target=/workspace \
--network none \
${image_name}
}

# Parallel Execution Wrapper
parallel_execution() {
local ubuntu_job=$(build_image ${UBUNTU_IMAGE_NAME} ${UBUNTU_DOCKERFILE_PATH} ${UBUNTU_CLANGFILE_PATH} &)
local windows_job=$(build_image ${WINDOWS_IMAGE_NAME} ${WINDOWS_DOCKERFILE_PATH} ${WINDOWS_CLANGFILE_PATH} &)
wait $ubuntu_job $windows_job
}

# Main Execution Workflow
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Starting Runner Image Setup for Ubuntu and Windows with Clang configurations..."
cleanup

if $PARALLEL_MODE; then
echo "Running in parallel mode..."
parallel_execution
else
build_image ${UBUNTU_IMAGE_NAME} ${UBUNTU_DOCKERFILE_PATH} ${UBUNTU_CLANGFILE_PATH}
run_container ${UBUNTU_IMAGE_NAME}

build_image ${WINDOWS_IMAGE_NAME} ${WINDOWS_DOCKERFILE_PATH} ${WINDOWS_CLANGFILE_PATH}
run_container ${WINDOWS_IMAGE_NAME}
fi

echo "[$(date +'%Y-%m-%d %H:%M:%S')] Runner Image Setup completed successfully."
Loading