laoshanxi / app-mesh

A secure Multi-Tenant, Cloud Native, Micro Service application management platform
MIT License
119 stars 20 forks source link

keep attribute name for CLI & Json consistant #196

Open laoshanxi opened 1 year ago

laoshanxi commented 3 weeks ago

a Marathon app definition might look like this:

json

{
  "id": "/my-app",
  "cmd": "python app.py",
  "cpus": 1,
  "mem": 1024,
  "disk": 2048,
  "instances": 1,
  "container": {
    "type": "DOCKER",
    "docker": {
      "image": "my-app-image:latest"
    }
  },
  "networks": [
    {
      "mode": "host"
    }
  ],
  "ports": [3000],
  "env": {
    "MAX_PROCESSES": "50"
  }
}
#include <string>
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <filesystem>

class CgroupResourceManager {
private:
    std::string cgroup_path;

    void write_to_file(const std::string& file, const std::string& value) {
        std::ofstream ofs(cgroup_path + "/" + file);
        if (!ofs) {
            throw std::runtime_error("Failed to open file: " + file);
        }
        ofs << value;
        if (!ofs) {
            throw std::runtime_error("Failed to write to file: " + file);
        }
    }

    std::string read_from_file(const std::string& file) {
        std::ifstream ifs(cgroup_path + "/" + file);
        if (!ifs) {
            throw std::runtime_error("Failed to open file: " + file);
        }
        std::string value;
        ifs >> value;
        return value;
    }

public:
    CgroupResourceManager(const std::string& path) : cgroup_path(path) {
        if (!std::filesystem::exists(path)) {
            throw std::runtime_error("Cgroup path does not exist: " + path);
        }
    }

    // CPU limits
    void set_cpu_shares(int shares) {
        write_to_file("cpu/cpu.shares", std::to_string(shares));
    }

    void set_cpu_quota(int quota) {
        write_to_file("cpu/cpu.cfs_quota_us", std::to_string(quota));
    }

    int get_cpu_shares() {
        return std::stoi(read_from_file("cpu/cpu.shares"));
    }

    int get_cpu_quota() {
        return std::stoi(read_from_file("cpu/cpu.cfs_quota_us"));
    }

    // Memory limits
    void set_memory_limit(long long bytes) {
        write_to_file("memory/memory.limit_in_bytes", std::to_string(bytes));
    }

    long long get_memory_limit() {
        return std::stoll(read_from_file("memory/memory.limit_in_bytes"));
    }

    // Disk I/O limits
    void set_blkio_weight(int weight) {
        write_to_file("blkio/blkio.weight", std::to_string(weight));
    }

    int get_blkio_weight() {
        return std::stoi(read_from_file("blkio/blkio.weight"));
    }

    // Network bandwidth limit
    void set_network_bandwidth(long long bits_per_second) {
        // This is a simplified example. In reality, network limits are typically
        // set using tc (traffic control) rather than directly through cgroups.
        std::cout << "Setting network bandwidth to " << bits_per_second << " bps" << std::endl;
    }

    // Process limit
    void set_pids_max(int max_pids) {
        write_to_file("pids/pids.max", std::to_string(max_pids));
    }

    int get_pids_max() {
        return std::stoi(read_from_file("pids/pids.max"));
    }
};