Compare commits

...

8 Commits

Author SHA1 Message Date
9eb89e0411 bump version to 1.0.31 2025-09-07 07:40:09 +04:00
5fcdadcabd bump version to 1.0.30 2025-09-07 07:30:48 +04:00
d542d080fa ... 2025-09-07 07:30:09 +04:00
f6557335ee ... 2025-09-07 07:23:43 +04:00
bbac841427 ... 2025-09-07 07:21:40 +04:00
746d9d16c7 Merge branch 'development' into development_herorun
* development:
  ...
  bump version to 1.0.30
2025-09-07 07:02:09 +04:00
075b6bd124 ... 2025-09-05 14:13:39 +04:00
Mahmoud-Emad
8d03eb822d feat: add HeroRun remote container management library
- Introduce Executor for remote container orchestration
- Add Container lifecycle management with tmux
- Support Alpine and Alpine Python base images
- Auto-install core dependencies on remote node
- Include full usage examples and updated README
2025-09-03 18:32:47 +03:00
27 changed files with 1504 additions and 22 deletions

View File

@@ -34,6 +34,10 @@ jobs:
- run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!" - run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!"
- run: echo "🔎 The name of your branch is ${{ github.ref_name }} and your repository is ${{ github.repository }}." - run: echo "🔎 The name of your branch is ${{ github.ref_name }} and your repository is ${{ github.repository }}."
- uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: latest-stable
- name: Check out repository code - name: Check out repository code
uses: actions/checkout@v4 uses: actions/checkout@v4

View File

@@ -48,7 +48,7 @@ fn do() ! {
mut cmd := Command{ mut cmd := Command{
name: 'hero' name: 'hero'
description: 'Your HERO toolset.' description: 'Your HERO toolset.'
version: '1.0.30' version: '1.0.31'
} }
// herocmds.cmd_run_add_flags(mut cmd) // herocmds.cmd_run_add_flags(mut cmd)

View File

@@ -0,0 +1,105 @@
# HeroRun - AI Agent Optimized Container Management
**Production-ready scripts for fast remote command execution**
## 🎯 Purpose
Optimized for AI agents that need rapid, reliable command execution with minimal latency and clean output.
## 🏗️ Base Image Types
HeroRun supports different base images through the `BaseImage` enum:
```v
pub enum BaseImage {
alpine // Standard Alpine Linux minirootfs (~5MB)
alpine_python // Alpine Linux with Python 3 pre-installed
}
```
### Usage Examples
**Standard Alpine Container:**
```v
base_image: .alpine // Default - minimal Alpine Linux
```
**Alpine with Python:**
```v
base_image: .alpine_python // Python 3 + pip pre-installed
```
## 📋 Three Scripts
### 1. `setup.vsh` - Environment Preparation
Creates container infrastructure on remote node.
```bash
./setup.vsh
```
**Output:** `Setup complete`
### 2. `execute.vsh` - Fast Command Execution
Executes commands on remote node with clean output only.
```bash
./execute.vsh "command" [context_id]
```
**Examples:**
```bash
./execute.vsh "ls /containers"
./execute.vsh "whoami"
./execute.vsh "echo 'Hello World'"
```
**Output:** Command result only (no verbose logging)
### 3. `cleanup.vsh` - Complete Teardown
Removes container and cleans up all resources.
```bash
./cleanup.vsh
```
**Output:** `Cleanup complete`
## ⚡ Performance Features
- **Clean Output**: Execute returns only command results
- **No Verbose Logging**: Silent operation for production use
- **Fast Execution**: Direct SSH without tmux overhead
- **AI Agent Ready**: Perfect for automated command execution
## 🚀 Usage Pattern
```bash
# Setup once
./setup.vsh
# Execute many commands (fast)
./execute.vsh "ls -la"
./execute.vsh "ps aux"
./execute.vsh "df -h"
# Cleanup when done
./cleanup.vsh
```
## 🎯 AI Agent Integration
Perfect for AI agents that need:
- Rapid command execution
- Clean, parseable output
- Minimal setup overhead
- Production-ready reliability
Each execute call returns only the command output, making it ideal for AI agents to parse and process results.

View File

@@ -0,0 +1,19 @@
#!/usr/bin/env -S v -n -w -cg -gc none -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.herolib.virt.herorun
// Create user with SSH key using sshagent module
mut user := herorun.new_user(keyname: 'id_ed25519')!
// Create executor using proper modules
mut executor := herorun.new_executor(
node_ip: '65.21.132.119'
user: 'root'
container_id: 'ai_agent_container'
keyname: 'id_ed25519'
)!
// Cleanup using tmux and osal modules
executor.cleanup()!
println('Cleanup complete')

View File

@@ -0,0 +1,30 @@
#!/usr/bin/env -S v -n -w -cg -gc none -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.herolib.virt.herorun
import os
// Get command from command line args
if os.args.len < 2 {
println('Usage: ./execute.vsh "command" [context_id]')
exit(1)
}
cmd := os.args[1]
// context_id := if os.args.len > 2 { os.args[2] } else { 'default' }
// Create user with SSH key using sshagent module
mut user := herorun.new_user(keyname: 'id_ed25519')!
// Create executor using proper modules
mut executor := herorun.new_executor(
node_ip: '65.21.132.119'
user: 'root'
container_id: 'ai_agent_container'
keyname: 'id_ed25519'
)!
// Execute command using osal module for clean output
output := executor.execute(cmd)!
// Output only the command result
print(output)

View File

@@ -0,0 +1,11 @@
#!/bin/sh
set -e
echo "🎉 Hello from custom container entry point!"
echo "Container ID: $(hostname)"
echo "Current time: $(date)"
echo "Working directory: $(pwd)"
echo "Available commands:"
ls /bin | head -10
echo "..."
echo "✅ Container is working perfectly!"

View File

@@ -0,0 +1,74 @@
#!/bin/sh
set -e
echo "🐍 Starting Python HTTP server..."
# Allow overriding port via environment variable (default: 8000)
PORT=${PORT:-8000}
HOST=${HOST:-0.0.0.0}
# Check if Python is available
if ! command -v python >/dev/null 2>&1 && ! command -v python3 >/dev/null 2>&1; then
echo "❌ Python not found in this container"
echo "💡 To use Python server, you need a container with Python pre-installed"
echo " For now, starting a simple HTTP server using busybox httpd..."
# Create a simple index.html
mkdir -p /tmp/www
cat > /tmp/www/index.html << 'EOF'
<!DOCTYPE html>
<html>
<head>
<title>Container HTTP Server</title>
<style>
body { font-family: Arial, sans-serif; margin: 40px; }
.container { max-width: 600px; margin: 0 auto; }
.status { color: #28a745; }
.info { background: #f8f9fa; padding: 20px; border-radius: 5px; }
</style>
</head>
<body>
<div class="container">
<h1>🎉 Container HTTP Server</h1>
<p class="status">✅ Container is running successfully!</p>
<div class="info">
<h3>Server Information:</h3>
<ul>
<li><strong>Server:</strong> BusyBox httpd</li>
<li><strong>Port:</strong> 8000</li>
<li><strong>Container:</strong> Alpine Linux</li>
<li><strong>Status:</strong> Active</li>
</ul>
</div>
<p><em>Note: Python was not available, so we're using BusyBox httpd instead.</em></p>
</div>
</body>
</html>
EOF
echo "📁 Created simple web content at /tmp/www/"
echo "🌐 Would start HTTP server on $HOST:$PORT (if httpd was available)"
echo ""
echo "🎉 Container executed successfully!"
echo "✅ Entry point script is working"
echo "📋 Container contents:"
ls -la /tmp/www/
echo ""
echo "📄 Sample web content:"
cat /tmp/www/index.html | head -10
echo "..."
echo ""
echo "💡 To run a real HTTP server, use a container image with Python or httpd pre-installed"
else
# Use python3 if available, otherwise python
PYTHON_CMD="python3"
if ! command -v python3 >/dev/null 2>&1; then
PYTHON_CMD="python"
fi
echo "✅ Found Python: $PYTHON_CMD"
echo "🌐 Starting Python HTTP server on $HOST:$PORT"
# Use exec so signals (like Ctrl+C) are properly handled
exec $PYTHON_CMD -m http.server "$PORT" --bind "$HOST"
fi

19
examples/virt/herorun/setup.vsh Executable file
View File

@@ -0,0 +1,19 @@
#!/usr/bin/env -S v -n -w -cg -gc none -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.herolib.virt.herorun
// Create user with SSH key using sshagent module
mut user := herorun.new_user(keyname: 'id_ed25519')!
// Create executor using proper module integration
mut executor := herorun.new_executor(
node_ip: '65.21.132.119'
user: 'root'
container_id: 'ai_agent_container'
keyname: 'id_ed25519'
)!
// Setup using sshagent, tmux, hetznermanager, and osal modules
executor.setup()!
println('Setup complete')

View File

@@ -0,0 +1,55 @@
#!/usr/bin/env -S v -n -w -cg -gc none -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.herolib.virt.herorun
// Create user with SSH key using sshagent module
mut user := herorun.new_user(keyname: 'id_ed25519')!
// Create executor with Alpine Python base image
mut executor := herorun.new_executor(
node_ip: '65.21.132.119'
user: 'root'
container_id: 'python_alpine_container'
keyname: 'id_ed25519'
image_script: 'examples/virt/herorun/images/python_server.sh'
base_image: .alpine_python // Use Alpine with Python pre-installed
)!
// Setup container
executor.setup()!
// Create container with Python Alpine base and Python server script
mut container := executor.get_or_create_container(
name: 'python_alpine_container'
image_script: 'examples/virt/herorun/images/python_server.sh'
base_image: .alpine_python
)!
println(' Setup complete with Python Alpine container')
println('Container: python_alpine_container')
println('Base image: Alpine Linux with Python 3 pre-installed')
println('Entry point: python_server.sh')
// Test the container to show Python is available
println('\n🐍 Testing Python availability...')
python_test := executor.execute('runc exec python_alpine_container python3 --version') or {
println(' Python test failed: ${err}')
return
}
println(' Python version: ${python_test}')
println('\n🚀 Running Python HTTP server...')
println('Note: This will start the server and exit (use runc run for persistent server)')
// Run the container to start the Python server
result := executor.execute('runc run python_alpine_container') or {
println(' Container execution failed: ${err}')
return
}
println('📋 Server output:')
println(result)
println('\n🎉 Python Alpine container executed successfully!')
println('💡 The Python HTTP server would run on port 8000 if started persistently')

View File

@@ -0,0 +1,29 @@
#!/usr/bin/env -S v -n -w -cg -gc none -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.herolib.virt.herorun
// Create user with SSH key using sshagent module
mut user := herorun.new_user(keyname: 'id_ed25519')!
// Create executor with image script for Python server
mut executor := herorun.new_executor(
node_ip: '65.21.132.119'
user: 'root'
container_id: 'python_server_container'
keyname: 'id_ed25519'
image_script: 'examples/virt/herorun/images/python_server.sh' // Path to entry point script
)!
// Setup using sshagent, tmux, hetznermanager, and osal modules
executor.setup()!
// Create container with the Python server script
mut container := executor.get_or_create_container(
name: 'python_server_container'
image_script: 'examples/virt/herorun/images/python_server.sh'
)!
println('Setup complete with Python server container')
println('Container: python_server_container')
println('Entry point: examples/virt/herorun/images/python_server.sh (Python HTTP server)')
println('To start the server: runc run python_server_container')

View File

@@ -0,0 +1,40 @@
#!/usr/bin/env -S v -n -w -cg -gc none -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.herolib.virt.herorun
// Create user with SSH key using sshagent module
mut user := herorun.new_user(keyname: 'id_ed25519')!
// Create executor with hello world script
mut executor := herorun.new_executor(
node_ip: '65.21.132.119'
user: 'root'
container_id: 'hello_world_container'
keyname: 'id_ed25519'
image_script: 'examples/virt/herorun/images/hello_world.sh'
)!
// Setup container
executor.setup()!
// Create container with hello world script
mut container := executor.get_or_create_container(
name: 'hello_world_container'
image_script: 'examples/virt/herorun/images/hello_world.sh'
)!
println(' Setup complete with Hello World container')
println('Container: hello_world_container')
println('Entry point: hello_world.sh')
// Run the container to demonstrate it works
println('\n🚀 Running container...')
result := executor.execute('runc run hello_world_container') or {
println(' Container execution failed: ${err}')
return
}
println('📋 Container output:')
println(result)
println('\n🎉 Container executed successfully!')

View File

@@ -1,4 +0,0 @@
mkdir -p cd /tmp/busyb
cd /tmp/busyb
podman export $(podman create busybox) | tar -C /tmp/busyb -xvf -

View File

@@ -1 +0,0 @@
apt install

View File

@@ -1,6 +0,0 @@
## busybox
- use docker, expand it into a directory

View File

@@ -4,7 +4,7 @@ set -e
os_name="$(uname -s)" os_name="$(uname -s)"
arch_name="$(uname -m)" arch_name="$(uname -m)"
version='1.0.30' version='1.0.31'
# Base URL for GitHub releases # Base URL for GitHub releases
@@ -121,7 +121,9 @@ echo "Download URL for your platform: $url"
# Download the file # Download the file
curl -o /tmp/downloaded_file -L "$url" curl -o /tmp/downloaded_file -L "$url"
# Check if file size is greater than 10 MB set -e
# Check if file size is greater than 2 MB
file_size=$(du -m /tmp/downloaded_file | cut -f1) file_size=$(du -m /tmp/downloaded_file | cut -f1)
if [ "$file_size" -ge 2 ]; then if [ "$file_size" -ge 2 ]; then
# Create the target directory if it doesn't exist # Create the target directory if it doesn't exist
@@ -139,6 +141,6 @@ if [ "$file_size" -ge 2 ]; then
export PATH=$PATH:$hero_bin_path export PATH=$PATH:$hero_bin_path
hero -version hero -version
else else
echo "Downloaded file is less than 10 MB. Process aborted." echo "Downloaded file is less than 2 MB. Process aborted."
exit 1 exit 1
fi fi

View File

@@ -196,7 +196,7 @@ function os_update {
apt autoremove -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" --force-yes apt autoremove -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" --force-yes
fi fi
#apt install apt-transport-https ca-certificates curl software-properties-common -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" --force-yes #apt install apt-transport-https ca-certificates curl software-properties-common -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" --force-yes
package_install "apt-transport-https ca-certificates curl wget software-properties-common tmux make tcc gcc" package_install "apt-transport-https ca-certificates curl wget software-properties-common tmux make gcc"
package_install "rclone rsync mc redis-server screen net-tools git dnsutils htop ca-certificates screen lsb-release binutils pkg-config libssl-dev iproute2" package_install "rclone rsync mc redis-server screen net-tools git dnsutils htop ca-certificates screen lsb-release binutils pkg-config libssl-dev iproute2"
elif [[ "${OSNAME}" == "darwin"* ]]; then elif [[ "${OSNAME}" == "darwin"* ]]; then
@@ -546,12 +546,6 @@ if [ "$REMOVE" = true ]; then
exit 0 exit 0
fi fi
# Handle reset if requested
if [ "$RESET" = true ]; then
remove_all
echo "Reset complete"
fi
# Create code directory if it doesn't exist # Create code directory if it doesn't exist
mkdir -p ~/code mkdir -p ~/code

View File

@@ -84,6 +84,7 @@ pub fn (mut t Tmux) session_create(args SessionCreateArgs) !&Session {
@[params] @[params]
pub struct TmuxNewArgs { pub struct TmuxNewArgs {
pub:
sessionid string sessionid string
} }

107
lib/virt/herorun/README.md Normal file
View File

@@ -0,0 +1,107 @@
# HeroRun - Remote Container Management
A V library for managing remote containers using runc and tmux, with support for multiple cloud providers.
## Features
- **Multi-provider support**: Currently supports Hetzner, with ThreeFold coming soon
- **Automatic setup**: Installs required packages (runc, tmux, curl, xz-utils) automatically
- **Container isolation**: Uses runc for lightweight container management
- **tmux integration**: Each container gets its own tmux session for multiple concurrent shells
- **Clean API**: Simple interface that hides infrastructure complexity
## Project Structure
```txt
lib/virt/herorun/
├── interfaces.v # Shared interfaces and parameter structs
├── nodes.v # Node management and SSH connectivity
├── container.v # Container struct and lifecycle operations
├── executor.v # Optimized command execution engine
├── factory.v # Provider abstraction and backend creation
├── hetzner_backend.v # Hetzner cloud implementation
└── README.md # This file
```
## Usage
### Basic Example
```v
import freeflowuniverse.herolib.virt.herorun
// Create user with SSH key
mut user := herorun.new_user(keyname: 'id_ed25519')!
// Create Hetzner backend
mut backend := herorun.new_hetzner_backend(
node_ip: '65.21.132.119'
user: 'root'
)!
// Connect to node (installs required packages automatically)
backend.connect(keyname: user.keyname)!
// Send a test command to the node
backend.send_command(cmd: 'ls')!
// Get or create container (uses tmux behind the scenes)
mut container := backend.get_or_create_container(name: 'test_container')!
// Attach to container tmux session
container.attach()!
// Send command to container
container.send_command(cmd: 'ls')!
// Get container logs
logs := container.get_logs()!
println('Container logs:')
println(logs)
```
### Running the Example
```bash
# Make the example executable
chmod +x examples/virt/herorun/herorun.vsh
# Run it
./examples/virt/herorun/herorun.vsh
```
## Architecture
### Interfaces
- **NodeBackend**: Defines operations for connecting to and managing remote nodes
- **ContainerBackend**: Defines operations for container lifecycle management
### Providers
- **HetznerBackend**: Implementation for Hetzner cloud servers
- **ThreeFoldBackend**: (Coming soon) Implementation for ThreeFold nodes
### Key Components
1. **SSH Integration**: Uses herolib's sshagent module for secure connections
2. **tmux Management**: Uses herolib's tmux module for session management
3. **Container Runtime**: Uses runc for lightweight container execution
4. **Hetzner Integration**: Uses herolib's hetznermanager module
## Dependencies
- `freeflowuniverse.herolib.osal.sshagent`
- `freeflowuniverse.herolib.osal.tmux`
- `freeflowuniverse.herolib.installers.web.hetznermanager`
- `freeflowuniverse.herolib.ui.console`
## Future Enhancements
- ThreeFold backend implementation
- Support for additional cloud providers (AWS, GCP, etc.)
- Container image management
- Network configuration
- Volume mounting
- Resource limits and monitoring

View File

@@ -0,0 +1,95 @@
module herorun
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.osal.tmux
import time
// Container struct and related functionality
pub struct Container {
pub:
name string
node Node
pub mut:
tmux tmux.Tmux
}
// Implement ContainerBackend interface for Container
pub fn (mut c Container) attach() ! {
console.print_header('🔗 Attaching to container: ${c.name}')
// Create or get the session for this container
if !c.tmux.session_exist(c.name) {
console.print_stdout('Starting new tmux session for container ${c.name}')
// Use the tmux convenience method to create session and window in one go
shell_cmd := 'ssh ${c.node.settings.user}@${c.node.settings.node_ip}'
c.tmux.window_new(
session_name: c.name
name: 'main'
cmd: shell_cmd
reset: true
)!
// Wait for the session and window to be properly created
time.sleep(500 * time.millisecond)
// Rescan to make sure everything is properly registered
c.tmux.scan()!
}
console.print_green('Attached to container session ${c.name}')
}
pub fn (mut c Container) send_command(args ContainerCommandArgs) ! {
console.print_header('📝 Exec in container ${c.name}')
// Ensure session exists
if !c.tmux.session_exist(c.name) {
return error('Container session ${c.name} does not exist. Call attach() first.')
}
// Debug: print session info
mut session := c.tmux.session_get(c.name)!
console.print_debug('Session ${c.name} has ${session.windows.len} windows')
for window in session.windows {
console.print_debug(' Window: ${window.name} (ID: ${window.id})')
}
// Try to get the main window
mut window := session.window_get(name: 'main') or {
// If main window doesn't exist, try to get the first window
if session.windows.len > 0 {
session.windows[0]
} else {
return error('No windows available in session ${c.name}')
}
}
// Refresh window state to get current panes
window.scan()!
// Get the first pane and send the command
if window.panes.len > 0 {
mut pane := window.panes[0]
// Send command to enter the container first, then the actual command
container_enter_cmd := 'cd /containers/${c.name} && runc exec ${c.name} ${args.cmd}'
pane.send_command(container_enter_cmd)!
} else {
return error('No panes available in container ${c.name}')
}
}
pub fn (mut c Container) get_logs() !string {
// Get the session and window
mut session := c.tmux.session_get(c.name)!
mut window := session.window_get(name: 'main')!
// Get logs from the first pane
if window.panes.len > 0 {
mut pane := window.panes[0]
return pane.logs_all()!
} else {
return error('No panes available in container ${c.name}')
}
}

300
lib/virt/herorun/executor.v Normal file
View File

@@ -0,0 +1,300 @@
module herorun
import freeflowuniverse.herolib.osal.tmux
import freeflowuniverse.herolib.osal.sshagent
import freeflowuniverse.herolib.virt.hetznermanager
import freeflowuniverse.herolib.osal.core as osal
import time
import os
// Executor - Optimized for AI agent usage with proper module integration
pub struct Executor {
pub mut:
node Node
container_id string
image_script string
base_image BaseImage
tmux tmux.Tmux
session_name string
window_name string
agent sshagent.SSHAgent
hetzner &hetznermanager.HetznerManager
}
@[params]
pub struct ExecutorArgs {
pub:
node_ip string @[required]
user string @[required]
container_id string @[required]
keyname string @[required]
image_script string // Optional entry point script
base_image BaseImage = .alpine // Base image type (default: alpine)
}
// Create new executor with proper module integration
pub fn new_executor(args ExecutorArgs) !Executor {
node := Node{
settings: NodeSettings{
node_ip: args.node_ip
user: args.user
}
}
// Initialize SSH agent properly
mut agent := sshagent.new_single()!
if !agent.is_agent_responsive() {
return error('SSH agent is not responsive')
}
agent.init()!
// Initialize tmux properly
mut t := tmux.new(sessionid: args.container_id)!
// Initialize Hetzner manager properly
mut hetzner := hetznermanager.get() or { hetznermanager.new()! }
return Executor{
node: node
container_id: args.container_id
image_script: args.image_script
base_image: args.base_image
tmux: t
session_name: args.container_id
window_name: 'main'
agent: agent
hetzner: hetzner
}
}
// Setup - Create container and tmux infrastructure using proper modules
pub fn (mut e Executor) setup() ! {
e.install_requirements()!
e.ensure_container()!
e.ensure_tmux_infrastructure()!
}
// Execute - Fast command execution using osal module
pub fn (mut e Executor) execute(cmd string) !string {
// Handle runc commands specially - they need to be run from container directory
mut final_cmd := cmd
if cmd.starts_with('runc ') {
// Extract container name from runc command
parts := cmd.split(' ')
if parts.len >= 3 {
container_name := parts[2]
final_cmd = 'cd /containers/${container_name} && ${cmd}'
}
}
// Execute via SSH using osal module for clean output
ssh_cmd := 'ssh ${e.node.settings.user}@${e.node.settings.node_ip} "${final_cmd}"'
result := osal.exec(cmd: ssh_cmd, stdout: false, name: 'executor_command')!
return result.output
}
// Execute with tmux - for interactive sessions
pub fn (mut e Executor) execute_tmux(cmd string, context_id string) !string {
// Ensure we have the latest state
if !e.tmux.session_exist(e.session_name) {
return error('Session ${e.session_name} does not exist. Run setup first.')
}
// Get the session and window using tmux module
mut session := e.tmux.session_get(e.session_name)!
mut window := session.window_get(name: e.window_name)!
// Only scan if we have no panes
if window.panes.len == 0 {
window.scan()!
}
if window.panes.len == 0 {
return error('No panes available in window ${e.window_name}')
}
// Get the active pane using tmux module
mut active_pane := window.pane_active() or { window.panes[0] }
// Execute command using tmux pane
active_pane.send_command(cmd)!
// Wait briefly for command to execute
time.sleep(200 * time.millisecond)
// Get output using tmux module
output := active_pane.logs_all() or { '' }
return output
}
// Get or create container with optional image script
pub fn (mut e Executor) get_or_create_container(args NewContainerArgs) !Container {
// Update container_id, image_script, and base_image from args
e.container_id = args.name
e.image_script = args.image_script
e.base_image = args.base_image
e.session_name = args.name
// Ensure container exists
e.ensure_container()!
// Return container instance
return Container{
name: args.name
node: e.node
tmux: e.tmux
}
}
// Cleanup - Remove everything using proper modules
pub fn (mut e Executor) cleanup() ! {
// Kill tmux session using tmux module
if e.tmux.session_exist(e.session_name) {
e.tmux.session_delete(e.session_name)!
}
// Remove container using osal module
e.remove_container()!
}
// Internal helper methods using proper modules
fn (mut e Executor) install_requirements() ! {
// Install all required packages using the installer module
install_all_requirements(e.node.settings.node_ip, e.node.settings.user)!
}
fn (mut e Executor) ensure_container() ! {
// Check if container exists using osal module
check_cmd := 'ssh ${e.node.settings.user}@${e.node.settings.node_ip} "runc list | grep ${e.container_id}"'
result := osal.execute_silent(check_cmd) or { '' }
if result == '' {
e.create_container()!
}
}
fn (mut e Executor) create_container() ! {
// Determine base image and setup command
mut setup_cmd := ''
match e.base_image {
.alpine_python {
// Use Docker to create a Python-enabled Alpine container
setup_cmd = '
mkdir -p /containers/${e.container_id}/rootfs &&
cd /containers/${e.container_id} &&
# Create a simple Dockerfile for Alpine with Python
cat > Dockerfile << EOF
FROM alpine:3.20
RUN apk add --no-cache python3 py3-pip
WORKDIR /
CMD ["/bin/sh"]
EOF
# Build and export the container filesystem
docker build -t ${e.container_id}-base . &&
docker create --name ${e.container_id}-temp ${e.container_id}-base &&
docker export ${e.container_id}-temp | tar -xf - -C rootfs &&
docker rm ${e.container_id}-temp &&
docker rmi ${e.container_id}-base &&
rm Dockerfile &&
runc spec
'
}
.alpine {
// Default: Use standard Alpine minirootfs
ver := '3.20.3'
file := 'alpine-minirootfs-${ver}-x86_64.tar.gz'
url := 'https://dl-cdn.alpinelinux.org/alpine/v${ver[..4]}/releases/x86_64/${file}'
setup_cmd = '
mkdir -p /containers/${e.container_id}/rootfs &&
cd /containers/${e.container_id}/rootfs &&
curl -fSL ${url} | tar -xzf - &&
cd /containers/${e.container_id} &&
rm -f config.json &&
runc spec
'
}
}
setup_cmd=texttools.dedent(setup_cmd)
remote_cmd := 'ssh ${e.node.settings.user}@${e.node.settings.node_ip} "${setup_cmd}"'
osal.exec(cmd: remote_cmd, stdout: false, name: 'container_create')!
// Configure container for non-interactive execution and writable filesystem
config_cmd := "ssh ${e.node.settings.user}@${e.node.settings.node_ip} \"cd /containers/${e.container_id} && if ! command -v jq >/dev/null 2>&1; then if command -v apt-get >/dev/null 2>&1; then apt-get update && apt-get install -y jq; elif command -v apk >/dev/null 2>&1; then apk add --no-cache jq; fi; fi && jq '.process.terminal = false | .root.readonly = false' config.json > config.json.tmp && mv config.json.tmp config.json\""
osal.exec(cmd: config_cmd, stdout: false, name: 'configure_container')!
// If image_script is provided, copy it and configure as entry point
if e.image_script != '' {
e.setup_image_script()!
}
}
fn (mut e Executor) setup_image_script() ! {
// Resolve the script path - handle relative paths
mut script_path := e.image_script
if script_path.starts_with('./') {
// Convert relative path to absolute path
current_dir := os.getwd()
script_path = '${current_dir}/${script_path[2..]}'
}
// Check if file exists
if !os.exists(script_path) {
return error('Image script file not found: ${script_path}')
}
// Read the script content from the resolved path
script_content := osal.file_read(script_path)!
// Create the script on the remote container rootfs
create_script_cmd := 'ssh ${e.node.settings.user}@${e.node.settings.node_ip} "mkdir -p /containers/${e.container_id}/rootfs"'
osal.exec(cmd: create_script_cmd, stdout: false, name: 'create_dir')!
// Write script content to a temporary file and copy it
script_file := '/tmp/entrypoint_${e.container_id}.sh'
osal.file_write(script_file, script_content)!
// Copy script to remote container
copy_cmd := 'scp ${script_file} ${e.node.settings.user}@${e.node.settings.node_ip}:/containers/${e.container_id}/rootfs/entrypoint.sh'
osal.exec(cmd: copy_cmd, stdout: false, name: 'copy_script')!
// Make script executable
chmod_cmd := 'ssh ${e.node.settings.user}@${e.node.settings.node_ip} "chmod +x /containers/${e.container_id}/rootfs/entrypoint.sh"'
osal.exec(cmd: chmod_cmd, stdout: false, name: 'chmod_script')!
// Clean up temporary file
osal.rm(script_file)!
// Install jq if needed and modify config.json to use the script as entry point, disable terminal, and enable writable filesystem
config_update_cmd := "ssh ${e.node.settings.user}@${e.node.settings.node_ip} \"cd /containers/${e.container_id} && if ! command -v jq >/dev/null 2>&1; then if command -v apt-get >/dev/null 2>&1; then apt-get update && apt-get install -y jq; elif command -v apk >/dev/null 2>&1; then apk add --no-cache jq; fi; fi && jq \\\".process.args = [\\\\\\\"/entrypoint.sh\\\\\\\"] | .process.terminal = false | .root.readonly = false\\\" config.json > config.json.tmp && mv config.json.tmp config.json\""
osal.exec(cmd: config_update_cmd, stdout: false, name: 'update_config')!
}
fn (mut e Executor) ensure_tmux_infrastructure() ! {
// Create session and window using tmux module properly
if !e.tmux.session_exist(e.session_name) {
// Create session with window using tmux module
shell_cmd := 'ssh ${e.node.settings.user}@${e.node.settings.node_ip}'
e.tmux.window_new(
session_name: e.session_name
name: e.window_name
cmd: shell_cmd
reset: true
)!
// Wait for setup
time.sleep(300 * time.millisecond)
}
}
fn (mut e Executor) remove_container() ! {
// Use osal module for cleanup
remove_cmd := 'ssh ${e.node.settings.user}@${e.node.settings.node_ip} "runc delete ${e.container_id} --force || true && rm -rf /containers/${e.container_id}"'
osal.exec(cmd: remove_cmd, stdout: false, name: 'container_cleanup') or {
// Ignore cleanup errors
}
}

View File

@@ -0,0 +1,30 @@
module herorun
import freeflowuniverse.herolib.ui.console
// Provider types
pub enum Provider {
hetzner
threefold
}
// Factory function to create appropriate backend
pub fn new_backend(provider Provider, args NewNodeArgs) !NodeBackend {
match provider {
.hetzner {
console.print_header('🏭 Creating Hetzner Backend')
backend := new_hetzner_backend(args)!
return backend
}
.threefold {
console.print_header('🏭 Creating ThreeFold Backend')
// TODO: Implement ThreeFold backend
return error('ThreeFold backend not implemented yet')
}
}
}
// Convenience function for Hetzner (most common case)
pub fn new_hetzner_node(args NewNodeArgs) !NodeBackend {
return new_backend(.hetzner, args)!
}

View File

@@ -0,0 +1,118 @@
module herorun
import os
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.osal.tmux
// HetznerBackend implements NodeBackend for Hetzner cloud servers
pub struct HetznerBackend {
pub mut:
node Node
}
// Create new Hetzner backend
pub fn new_hetzner_backend(args NewNodeArgs) !HetznerBackend {
console.print_header('🖥 Creating Hetzner Backend')
console.print_stdout('IP: ${args.node_ip}')
node := Node{
settings: NodeSettings{
node_ip: args.node_ip
user: args.user
}
}
return HetznerBackend{
node: node
}
}
// Implement NodeBackend interface
pub fn (mut h HetznerBackend) connect(args NodeConnectArgs) ! {
console.print_header('🔌 Connecting to Hetzner node')
console.print_item('Node IP: ${h.node.settings.node_ip}')
console.print_item('User: ${h.node.settings.user}')
// Basic SSH connection test
cmd := 'ssh ${h.node.settings.user}@${h.node.settings.node_ip} -o StrictHostKeyChecking=no exit'
stream_command(cmd)!
console.print_green('Connection successful')
// Ensure required packages are installed
console.print_header('📦 Ensuring required packages')
install_cmd := 'ssh ${h.node.settings.user}@${h.node.settings.node_ip} "apt-get update && apt-get install -y runc tmux curl xz-utils"'
stream_command(install_cmd)!
console.print_green('Dependencies installed')
}
pub fn (h HetznerBackend) send_command(args SendCommandArgs) ! {
console.print_header('💻 Running remote command on Hetzner')
console.print_item('Command: ${args.cmd}')
remote_cmd := 'ssh ${h.node.settings.user}@${h.node.settings.node_ip} "${args.cmd}"'
stream_command(remote_cmd)!
}
pub fn (mut h HetznerBackend) get_or_create_container(args NewContainerArgs) !Container {
console.print_header('📦 Get or create container: ${args.name}')
// Check if container exists
check_cmd := 'ssh ${h.node.settings.user}@${h.node.settings.node_ip} "runc list | grep ${args.name}"'
code := os.system(check_cmd)
if code != 0 {
console.print_stdout('Container not found, creating...')
h.create_container(args.name)!
} else {
console.print_stdout('Container already exists.')
}
// Create tmux session wrapper
mut t := tmux.new(sessionid: args.name)!
return Container{
name: args.name
node: h.node
tmux: t
}
}
pub fn (h HetznerBackend) get_info() !NodeInfo {
return NodeInfo{
ip: h.node.settings.node_ip
user: h.node.settings.user
provider: 'hetzner'
status: 'connected'
}
}
// Internal helper to create container
fn (h HetznerBackend) create_container(name string) ! {
ver := '3.20.3'
file := 'alpine-minirootfs-${ver}-x86_64.tar.gz'
url := 'https://dl-cdn.alpinelinux.org/alpine/v${ver[..4]}/releases/x86_64/${file}'
setup_cmd := '
mkdir -p /containers/${name}/rootfs &&
cd /containers/${name}/rootfs &&
echo "📥 Downloading Alpine rootfs: ${url}" &&
curl -fSL ${url} | tar -xzf - &&
cd /containers/${name} &&
rm -f config.json &&
runc spec
'
remote_cmd := 'ssh ${h.node.settings.user}@${h.node.settings.node_ip} "${setup_cmd}"'
stream_command(remote_cmd)!
console.print_green('Container ${name} rootfs prepared')
}
// Helper function for streaming commands
fn stream_command(cmd string) ! {
console.print_debug_title('Executing', cmd)
code := os.system(cmd)
if code != 0 {
console.print_stderr('Command failed: ${cmd} (exit ${code})')
return error('command failed: ${cmd} (exit ${code})')
}
}

View File

@@ -0,0 +1,240 @@
module herorun
import freeflowuniverse.herolib.osal.core as osal
// Package installer functions for herorun dependencies
// Each function installs a specific package on the remote node
// Install runc container runtime
pub fn install_runc(node_ip string, user string) ! {
// Check if runc is already installed
check_cmd := 'ssh ${user}@${node_ip} "command -v runc"'
result := osal.execute_silent(check_cmd) or { '' }
if result != '' {
// Already installed
return
}
// Detect OS on remote node
os_detect_cmd := "ssh ${user}@${node_ip} \"cat /etc/os-release | grep ^ID= | cut -d= -f2 | tr -d \\\"\" 2>/dev/null || echo \"unknown\""
os_result := osal.execute_silent(os_detect_cmd) or { 'unknown' }
os_id := os_result.trim_space()
mut install_cmd := ''
match os_id {
'ubuntu', 'debian' {
install_cmd = 'ssh ${user}@${node_ip} "apt-get update && apt-get install -y runc"'
}
'alpine' {
install_cmd = 'ssh ${user}@${node_ip} "apk add --no-cache runc"'
}
'centos', 'rhel', 'fedora' {
install_cmd = 'ssh ${user}@${node_ip} "if command -v dnf >/dev/null 2>&1; then dnf install -y runc; else yum install -y runc; fi"'
}
else {
return error('Unsupported OS for runc installation: ${os_id}. Please install runc manually.')
}
}
// Execute installation
osal.exec(cmd: install_cmd, stdout: false, name: 'install_runc')!
// Verify installation
verify_cmd := 'ssh ${user}@${node_ip} "runc --version"'
verify_result := osal.execute_silent(verify_cmd) or { '' }
if verify_result == '' {
return error('runc installation failed - command not found after installation')
}
}
// Install tmux terminal multiplexer
pub fn install_tmux(node_ip string, user string) ! {
// Check if tmux is already installed
check_cmd := 'ssh ${user}@${node_ip} "command -v tmux"'
result := osal.execute_silent(check_cmd) or { '' }
if result != '' {
// Already installed
return
}
// Detect OS on remote node
os_detect_cmd := "ssh ${user}@${node_ip} \"cat /etc/os-release | grep ^ID= | cut -d= -f2 | tr -d \\\"\" 2>/dev/null || echo \"unknown\""
os_result := osal.execute_silent(os_detect_cmd) or { 'unknown' }
os_id := os_result.trim_space()
mut install_cmd := ''
match os_id {
'ubuntu', 'debian' {
install_cmd = 'ssh ${user}@${node_ip} "apt-get update && apt-get install -y tmux"'
}
'alpine' {
install_cmd = 'ssh ${user}@${node_ip} "apk add --no-cache tmux"'
}
'centos', 'rhel', 'fedora' {
install_cmd = 'ssh ${user}@${node_ip} "if command -v dnf >/dev/null 2>&1; then dnf install -y tmux; else yum install -y tmux; fi"'
}
else {
return error('Unsupported OS for tmux installation: ${os_id}. Please install tmux manually.')
}
}
// Execute installation
osal.exec(cmd: install_cmd, stdout: false, name: 'install_tmux')!
// Verify installation
verify_cmd := 'ssh ${user}@${node_ip} "tmux -V"'
verify_result := osal.execute_silent(verify_cmd) or { '' }
if verify_result == '' {
return error('tmux installation failed - command not found after installation')
}
}
// Install curl for downloading files
pub fn install_curl(node_ip string, user string) ! {
// Check if curl is already installed
check_cmd := 'ssh ${user}@${node_ip} "command -v curl"'
result := osal.execute_silent(check_cmd) or { '' }
if result != '' {
// Already installed
return
}
// Detect OS on remote node
os_detect_cmd := "ssh ${user}@${node_ip} \"cat /etc/os-release | grep ^ID= | cut -d= -f2 | tr -d \\\"\" 2>/dev/null || echo \"unknown\""
os_result := osal.execute_silent(os_detect_cmd) or { 'unknown' }
os_id := os_result.trim_space()
mut install_cmd := ''
match os_id {
'ubuntu', 'debian' {
install_cmd = 'ssh ${user}@${node_ip} "apt-get update && apt-get install -y curl"'
}
'alpine' {
install_cmd = 'ssh ${user}@${node_ip} "apk add --no-cache curl"'
}
'centos', 'rhel', 'fedora' {
install_cmd = 'ssh ${user}@${node_ip} "if command -v dnf >/dev/null 2>&1; then dnf install -y curl; else yum install -y curl; fi"'
}
else {
return error('Unsupported OS for curl installation: ${os_id}. Please install curl manually.')
}
}
// Execute installation
osal.exec(cmd: install_cmd, stdout: false, name: 'install_curl')!
// Verify installation
verify_cmd := 'ssh ${user}@${node_ip} "curl --version"'
verify_result := osal.execute_silent(verify_cmd) or { '' }
if verify_result == '' {
return error('curl installation failed - command not found after installation')
}
}
// Install tar for archive extraction
pub fn install_tar(node_ip string, user string) ! {
// Check if tar is already installed
check_cmd := 'ssh ${user}@${node_ip} "command -v tar"'
result := osal.execute_silent(check_cmd) or { '' }
if result != '' {
// Already installed
return
}
// Detect OS on remote node
os_detect_cmd := "ssh ${user}@${node_ip} \"cat /etc/os-release | grep ^ID= | cut -d= -f2 | tr -d \\\"\" 2>/dev/null || echo \"unknown\""
os_result := osal.execute_silent(os_detect_cmd) or { 'unknown' }
os_id := os_result.trim_space()
mut install_cmd := ''
match os_id {
'ubuntu', 'debian' {
install_cmd = 'ssh ${user}@${node_ip} "apt-get update && apt-get install -y tar"'
}
'alpine' {
install_cmd = 'ssh ${user}@${node_ip} "apk add --no-cache tar"'
}
'centos', 'rhel', 'fedora' {
install_cmd = 'ssh ${user}@${node_ip} "if command -v dnf >/dev/null 2>&1; then dnf install -y tar; else yum install -y tar; fi"'
}
else {
return error('Unsupported OS for tar installation: ${os_id}. Please install tar manually.')
}
}
// Execute installation
osal.exec(cmd: install_cmd, stdout: false, name: 'install_tar')!
// Verify installation
verify_cmd := 'ssh ${user}@${node_ip} "tar --version"'
verify_result := osal.execute_silent(verify_cmd) or { '' }
if verify_result == '' {
return error('tar installation failed - command not found after installation')
}
}
// Install git for version control
pub fn install_git(node_ip string, user string) ! {
// Check if git is already installed
check_cmd := 'ssh ${user}@${node_ip} "command -v git"'
result := osal.execute_silent(check_cmd) or { '' }
if result != '' {
// Already installed
return
}
// Detect OS on remote node
os_detect_cmd := "ssh ${user}@${node_ip} \"cat /etc/os-release | grep ^ID= | cut -d= -f2 | tr -d \\\"\" 2>/dev/null || echo \"unknown\""
os_result := osal.execute_silent(os_detect_cmd) or { 'unknown' }
os_id := os_result.trim_space()
mut install_cmd := ''
match os_id {
'ubuntu', 'debian' {
install_cmd = 'ssh ${user}@${node_ip} "apt-get update && apt-get install -y git"'
}
'alpine' {
install_cmd = 'ssh ${user}@${node_ip} "apk add --no-cache git"'
}
'centos', 'rhel', 'fedora' {
install_cmd = 'ssh ${user}@${node_ip} "if command -v dnf >/dev/null 2>&1; then dnf install -y git; else yum install -y git; fi"'
}
else {
return error('Unsupported OS for git installation: ${os_id}. Please install git manually.')
}
}
// Execute installation
osal.exec(cmd: install_cmd, stdout: false, name: 'install_git')!
// Verify installation
verify_cmd := 'ssh ${user}@${node_ip} "git --version"'
verify_result := osal.execute_silent(verify_cmd) or { '' }
if verify_result == '' {
return error('git installation failed - command not found after installation')
}
}
// Install all required packages for herorun
pub fn install_all_requirements(node_ip string, user string) ! {
install_curl(node_ip, user)!
install_tar(node_ip, user)!
install_git(node_ip, user)!
install_tmux(node_ip, user)!
install_runc(node_ip, user)!
}

View File

@@ -0,0 +1,57 @@
module herorun
// Base image types for containers
pub enum BaseImage {
alpine // Standard Alpine Linux minirootfs
alpine_python // Alpine Linux with Python 3 pre-installed
}
// Shared parameter structs used across the module
@[params]
pub struct SendCommandArgs {
pub:
cmd string @[required]
}
@[params]
pub struct NewContainerArgs {
pub:
name string @[required]
image_script string // Optional path to entry point script (e.g., './images/python_server.sh')
base_image BaseImage = .alpine // Base image type (default: alpine)
}
@[params]
pub struct ContainerCommandArgs {
pub:
cmd string @[required]
}
// NodeBackend defines the contract that all node providers must follow
pub interface NodeBackend {
mut:
// Connect to the node and ensure required packages
connect(args NodeConnectArgs) !
// Send command to the node
send_command(args SendCommandArgs) !
// Container lifecycle
get_or_create_container(args NewContainerArgs) !Container
// Get node information
get_info() !NodeInfo
}
// ContainerBackend defines container operations
pub interface ContainerBackend {
mut:
// Attach to container tmux session
attach() !
// Send command to container
send_command(args ContainerCommandArgs) !
// Get container logs
get_logs() !string
}

72
lib/virt/herorun/nodes.v Normal file
View File

@@ -0,0 +1,72 @@
module herorun
import freeflowuniverse.herolib.osal.sshagent
// Node-related structs and functionality
pub struct NodeSettings {
pub:
node_ip string
user string
}
pub struct Node {
pub:
settings NodeSettings
}
@[params]
pub struct NewNodeArgs {
pub:
node_ip string
user string
}
@[params]
pub struct NodeConnectArgs {
pub:
keyname string @[required]
}
// Node information struct
pub struct NodeInfo {
pub:
ip string
user string
provider string
status string
}
// Create new node
pub fn new_node(args NewNodeArgs) Node {
return Node{
settings: NodeSettings{
node_ip: args.node_ip
user: args.user
}
}
}
// User configuration for SSH
pub struct UserConfig {
pub:
keyname string
}
@[params]
pub struct NewUserArgs {
pub:
keyname string @[required]
}
// Create new user with SSH agent setup
pub fn new_user(args NewUserArgs) !UserConfig {
mut agent := sshagent.new_single()!
// Silent check - just ensure agent is working without verbose output
if !agent.is_agent_responsive() {
return error('SSH agent is not responsive')
}
agent.init()!
return UserConfig{
keyname: args.keyname
}
}

91
libarchive/hero_build.yml Normal file
View File

@@ -0,0 +1,91 @@
name: Release Hero
permissions:
contents: write
on:
push:
workflow_dispatch:
jobs:
build:
timeout-minutes: 60
if: startsWith(github.ref, 'refs/tags/')
strategy:
fail-fast: false
matrix:
include:
- target: x86_64-unknown-linux-musl
os: ubuntu-latest
short-name: linux-i64
- target: aarch64-unknown-linux-musl
os: ubuntu-latest
short-name: linux-arm64
- target: aarch64-apple-darwin
os: macos-latest
short-name: macos-arm64
# - target: x86_64-apple-darwin
# os: macos-13
# short-name: macos-i64
runs-on: ${{ matrix.os }}
steps:
- run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event."
- run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!"
- run: echo "🔎 The name of your branch is ${{ github.ref_name }} and your repository is ${{ github.repository }}."
- uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: latest-stable
- name: Check out repository code
uses: actions/checkout@v4
- name: Setup V & Herolib
id: setup
run: ./install_v.sh --herolib
timeout-minutes: 10
# - name: Do all the basic tests
# timeout-minutes: 25
# run: ./test_basic.vsh
- name: Build Hero
timeout-minutes: 15
run: |
set -e
v -w -d use_openssl -enable-globals cli/hero.v -o cli/hero-${{ matrix.target }}
- name: Upload
uses: actions/upload-artifact@v4
with:
name: hero-${{ matrix.target }}
path: cli/hero-${{ matrix.target }}
release_hero:
needs: build
runs-on: ubuntu-latest
permissions:
contents: write
if: startsWith(github.ref, 'refs/tags/')
steps:
- name: Check out repository code
uses: actions/checkout@v4
- name: Download Artifacts
uses: actions/download-artifact@v4
with:
path: cli/bins
merge-multiple: true
- name: Release
uses: softprops/action-gh-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref_name }}
name: Release ${{ github.ref_name }}
draft: false
fail_on_unmatched_files: true
generate_release_notes: true
files: cli/bins/*