Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
41 changes: 41 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Bazel configuration for goback

# Use bzlmod (enabled by default in Bazel 9)
common --enable_bzlmod

# Workspace status for build-time variables
build --workspace_status_command=/workspace/claude-session/goback/workspace_status.sh
build --stamp

# Disable CGO (matching Makefile's CGO_ENABLED=0)
build --@rules_go//go/config:pure

# Use static linking
build --@rules_go//go/config:static

# Go SDK configuration
build --incompatible_enable_cc_toolchain_resolution

# Performance settings
build --jobs=auto

# Error output
build --verbose_failures

# Test configuration
test --test_output=errors
test --test_summary=detailed

# Cache settings
build --disk_cache=~/.cache/bazel-disk-cache

# Color output
common --color=yes

# Show build progress
build --show_progress
build --show_progress_rate_limit=1

# Test environment
test --test_env=GOOS=linux
test --test_env=GOARCH=arm64
1 change: 1 addition & 0 deletions .bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
9.0.0
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ goback-poweroff
build/
config.yaml
state

# Bazel artifacts
/bazel-*
.bazel-cache
203 changes: 203 additions & 0 deletions BAZEL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
# Bazel Build Configuration

This repository is configured to use Bazel 9 as an alternative to the Makefile-based build system.

## Prerequisites

Install bazelisk (a Bazel version manager):

```bash
# On Linux (arm64)
curl -L https://github.com/bazelbuild/bazelisk/releases/download/v1.25.0/bazelisk-linux-arm64 -o /usr/local/bin/bazel
chmod +x /usr/local/bin/bazel

# On Linux (amd64)
curl -L https://github.com/bazelbuild/bazelisk/releases/download/v1.25.0/bazelisk-linux-amd64 -o /usr/local/bin/bazel
chmod +x /usr/local/bin/bazel

# On macOS (with Homebrew)
brew install bazelisk

# Or install using Go
go install github.com/bazelbuild/bazelisk@latest
```

## Building

### Build all binaries

```bash
bazel build //...
```

### Build individual binaries

```bash
# Build CLI binary
bazel build //cmd/cli

# Build server binary
bazel build //cmd/server

# Build power-off utility
bazel build //cmd/power_off

# Or use convenient aliases
bazel build //:goback
bazel build //:goback-server
bazel build //:goback-poweroff
```

### Build output location

Binaries are created in `bazel-bin/cmd/{cli,server,power_off}/`:
- `bazel-bin/cmd/cli/cli_/cli` → goback CLI
- `bazel-bin/cmd/server/server_/server` → goback-server
- `bazel-bin/cmd/power_off/power_off_/power_off` → goback-poweroff

## Running

### Run binaries directly

```bash
# Run CLI with arguments
bazel run //:goback -- --config config.yaml

# Run server
bazel run //:goback-server -- --config config.yaml --listen :9090

# Check version
bazel run //:goback -- --version
bazel run //:goback-server -- --version
```

## Testing

### Run all tests

```bash
bazel test //...
```

### Run specific test packages

```bash
# Test a specific package
bazel test //orchestrator:orchestrator_test
bazel test //server/handlers:handlers_test

# Test with verbose output
bazel test //... --test_output=all

# Run a specific test function
bazel test //orchestrator:orchestrator_test --test_arg=-test.run=TestBackupVMs
```

## Clean

```bash
# Clean all build artifacts
bazel clean

# Deep clean (including external dependencies)
bazel clean --expunge
```

## Configuration

### .bazelrc

The `.bazelrc` file contains Bazel configuration:
- Pure Go builds (CGO_ENABLED=0)
- Static linking
- Workspace status command for build-time variable injection
- Test configuration

### .bazelversion

Specifies Bazel version (9.0.0). Bazelisk uses this to download the correct version.

### MODULE.bazel

Defines module dependencies using Bazel 9's bzlmod system:
- rules_go v0.54.0
- gazelle v0.41.0
- Go SDK 1.23.2
- All Go dependencies from go.mod

### Build-time Variables

The build injects git commit and build timestamp into binaries via:
- `workspace_status.sh` - Script that provides build variables
- `x_defs` in BUILD.bazel files - Links variables to Go package variables

Variables injected into `buildinfo` package:
- `buildTime` - Build timestamp (format: YYYY-MM-DD_HH:MM:SS)
- `gitCommit` - Git commit hash (short)

## Gazelle (Automatic BUILD file generation)

Gazelle automatically generates BUILD.bazel files from Go source:

```bash
# Regenerate all BUILD files
bazel run //:gazelle

# Update Go dependency repositories
bazel run //:gazelle-update-repos
```

## Makefile Equivalents

| Makefile Command | Bazel Equivalent |
|-----------------|------------------|
| `make build` | `bazel build //...` |
| `make build-cli` | `bazel build //cmd/cli` |
| `make build-server` | `bazel build //cmd/server` |
| `make test` | `bazel test //...` |
| `make clean` | `bazel clean` |
| `make fmt` | `bazel run @rules_go//go fmt ./...` (or use go fmt directly) |

## Advantages of Bazel

1. **Incremental builds** - Only rebuilds changed code and dependencies
2. **Hermetic builds** - Reproducible builds with locked dependencies
3. **Caching** - Shared cache across builds and machines
4. **Parallel execution** - Automatic parallelization of builds and tests
5. **Remote execution** - Can distribute builds across multiple machines
6. **Platform support** - Better cross-compilation support

## Troubleshooting

### Build fails with "no such package"

Run gazelle to regenerate BUILD files:
```bash
bazel run //:gazelle
```

### Workspace status command fails

Ensure `workspace_status.sh` is executable:
```bash
chmod +x workspace_status.sh
```

Update the path in `.bazelrc` if repository location changed.

### Clean rebuild needed

Sometimes cache issues require a full rebuild:
```bash
bazel clean --expunge
bazel build //...
```

### Module extension warnings

To fix indirect dependency warnings:
```bash
bazel mod tidy
```

This updates MODULE.bazel with correct dependency declarations.
135 changes: 135 additions & 0 deletions BAZEL_MIGRATION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# Bazel Migration Summary

This document summarizes the Bazel 9 setup that has been configured for the goback project.

## Files Created/Modified

### New Files

1. **MODULE.bazel** - Bazel module configuration (bzlmod)
- Declares module dependencies (rules_go, gazelle)
- Configures Go SDK 1.23.2
- Manages Go dependencies from go.mod

2. **WORKSPACE** - Minimal compatibility file for older Bazel versions

3. **BUILD.bazel** (root) - Root build configuration
- Gazelle targets for BUILD file generation
- Convenient aliases: //:goback, //:goback-server, //:goback-poweroff

4. **BUILD.bazel files** (auto-generated by gazelle)
- Generated in all package directories
- Defines go_library, go_binary, and go_test targets
- Includes x_defs for build-time variable injection

5. **.bazelrc** - Bazel configuration
- Pure Go builds (CGO_ENABLED=0)
- Static linking
- Workspace status command
- Test configuration

6. **.bazelversion** - Pins Bazel version to 9.0.0

7. **workspace_status.sh** - Provides build-time variables
- Git commit hash
- Build timestamp

8. **BAZEL.md** - Comprehensive Bazel usage guide

### Modified Files

1. **.gitignore** - Added Bazel artifacts
- `/bazel-*` directories
- `.bazel-cache`

## Quick Start

```bash
# Install bazelisk (if not already installed)
curl -L https://github.com/bazelbuild/bazelisk/releases/download/v1.25.0/bazelisk-linux-arm64 -o bazel
chmod +x bazel
sudo mv bazel /usr/local/bin/

# Build all binaries
bazel build //...

# Run tests
bazel test //...

# Run a binary
bazel run //:goback -- --version
```

## Feature Parity with Makefile

| Feature | Makefile | Bazel |
|---------|----------|-------|
| Build CLI | `make build-cli` | `bazel build //cmd/cli` |
| Build Server | `make build-server` | `bazel build //cmd/server` |
| Build Power-off | `make build-poweroff` | `bazel build //cmd/power_off` |
| Build All | `make build` | `bazel build //...` |
| Run Tests | `make test` | `bazel test //...` |
| Clean | `make clean` | `bazel clean` |
| Version Info | ✓ (via ldflags) | ✓ (via workspace_status) |
| CGO Disabled | ✓ (CGO_ENABLED=0) | ✓ (--pure flag) |
| Static Linking | ✓ | ✓ (--static flag) |

## Advantages of Bazel

1. **Incremental Builds** - Only rebuilds changed code
2. **Caching** - Shared cache across builds (disk and remote)
3. **Parallel Execution** - Automatic parallelization
4. **Hermetic Builds** - Reproducible builds
5. **Dependency Management** - Strict dependency tracking
6. **Cross-platform** - Better cross-compilation support

## Testing Results

All 10 test suites pass:
- ✓ clients/ipmiclient
- ✓ clients/pbsclient
- ✓ clients/proxmoxclient
- ✓ config
- ✓ logging
- ✓ metrics
- ✓ server/cron
- ✓ server/handlers
- ✓ server/runner
- ✓ workflow

## Build Verification

Binary version information is correctly injected:
```
goback
Built: 2026-01-30_02:32:18
Commit: ce0aa43
```

## Next Steps

The Makefile can now be deprecated in favor of Bazel. Consider:

1. Update CI/CD pipelines to use Bazel
2. Configure remote caching for faster CI builds
3. Set up remote execution for distributed builds
4. Update documentation to reference Bazel commands

## Maintenance

### Updating Dependencies

When go.mod changes:
```bash
bazel run //:gazelle-update-repos
bazel run //:gazelle
```

### Adding New Packages

After adding new Go files:
```bash
bazel run //:gazelle
```

This automatically generates/updates BUILD.bazel files.
Loading