This is a modern, Python-based configuration management project that replaces the legacy Ansible playbooks for the ETSI MEC Sandbox.
This directory (`pyinfra/`) contains the automated infrastructure-as-code (IaC) deployment framework for the ETSI MEC Sandbox. It utilizes a declarative, Python-based deployment model using [Pyinfra](https://pyinfra.com/).
## Pyinfra Best Practices
## What it does
This project adheres to the following core Pyinfra best practices:
This framework handles the end-to-end provisioning and configuration of the MEC Sandbox environment. Its core responsibilities include:
1.**Idempotency**: All `pyinfra` operations (like `apt.packages`, `files.directory`) declare the *desired state*. Pyinfra only runs commands if the system is not already in that state.
2.**Modularity (DRY)**: Logic is cleanly separated into reusable Python modules within the `tasks/` directory, avoiding rigid directory structures.
3.**Data Separation**: Configuration variables and state definitions are kept in `group_data/all.py`. They are completely decoupled from the execution logic.
4.**Dynamic Inventory**: We use Python's `dotenv` to load host targets dynamically from an `.env` file instead of maintaining static `hosts.ini` files.
-**System Initialization:** Ensuring proper kernel modules, network routing, and core system dependencies are configured.
-**Container Runtimes:** Idempotent installation and configuration of Docker and Containerd.
-**Kubernetes Cluster Setup:** Bootstrapping the Kubernetes control plane and joining worker nodes via `kubeadm`.
-**Development Environment:** Installing specific versions of Golang, Node.js (via NVM), and linting tools.
-**Platform Orchestration (meepctl):** Configuring and deploying the MEC core platform, frontend, and dependencies using `meepctl`.
## Project Structure
Everything in this folder is designed to be **idempotent**—you can safely run the deployment multiple times without causing unintended side effects or breaking the system.
```text
pyinfra-mec-sandbox/
├── inventory.py # Dynamically loads target hosts from .env
├── group_data/ # Replaces Ansible group_vars
│ └── all.py # Global variables (versions, CIDRs, etc.)
├── tasks/ # Replaces Ansible roles
│ ├── common.py
│ ├── kernel.py
│ └── containerd.py
└── deploy.py # Main orchestrator
```
---
## How to Run the Deployment
## Usage
Follow these steps to deploy the infrastructure.
### 1. Configure the Environment
Ensure you have an `.env` file in your working directory (or system environment variables) specifying your target hosts:
### 1. Initial Setup
```env
K8S_MASTERS=localhost
TARGET_USER=ubuntu
Before deploying, you must initialize your local environment. We provide an automated bootstrap script that ensures Python 3 is installed, sets up an isolated virtual environment (`pyinfra-venv`), and installs the `pyinfra` package cleanly.
```bash
cd pyinfra
./setup.sh
```
### 2. Dry Run
Always run a dry run first to see what commands Pyinfra *intends* to execute without making actual changes:
### 2. Configure Environment Variables
If this is your first time running the setup script, it will automatically generate a `.env` file from the `.env.example` template and exit safely to allow you to configure your secrets.
Open the `.env` file in your preferred editor and configure the necessary variables:
-**K8S_MASTERS / K8S_WORKERS:** Set the target IPs for your cluster.
-**MEC_HOST_ADDRESS:** Set the routable IP or domain for the MEC frontend.
-**OAuth Secrets:** Update your GitHub and GitLab OAuth credentials.
### 3. Deploying the Infrastructure
Once the `.env` file is properly configured, activate the virtual environment and execute the Pyinfra deployment.
The deployment process slightly differs depending on whether you are deploying locally or to remote servers.
#### Option A: Local Deployment (Localhost)
If you are deploying the sandbox directly to the machine you are currently logged into:
1. Ensure `K8S_MASTERS="localhost"` in your `.env` file.
2. Run the deployment:
```bash
pyinfra inventory.py deploy.py --dry
source pyinfra-venv/bin/activate
pyinfra inventory.py deploy.py
```
*(Pyinfra will automatically execute commands locally using `sudo` where required).*
#### Option B: Remote Deployment (via SSH)
If you are deploying to remote servers, Pyinfra will execute the deployment over SSH.
### 3. Deploy
Apply the configuration:
1. Ensure `K8S_MASTERS` and `K8S_WORKERS` in your `.env` file contain the remote IP addresses or DNS names (e.g., `K8S_MASTERS="192.168.1.10"`).
2. Ensure you have passwordless SSH access configured for the target servers (e.g., using `ssh-copy-id`).
3. Set the appropriate SSH user by uncommenting and configuring `TARGET_USER` in the `.env` file.