added nats-exec

This commit is contained in:
2026-02-21 12:56:58 +01:00
parent 7e6e18e9fa
commit 78d1e44add
5 changed files with 176 additions and 1 deletions

2
.gitignore vendored
View File

@@ -25,3 +25,5 @@ go.work.sum
# env file
.env
./bin
bin

View File

@@ -1,2 +1,29 @@
# nats-upload
Tooling for uploading and executing binaries via NATS Object Store.
## nats-upload
A GitHub Action to upload binaries to a NATS Object Store for self-update.
## nats-exec
A tool to download and execute the latest version of a binary from NATS Object Store.
### Usage
```bash
nats-exec <binary-name> [args...]
```
Example:
```bash
nats-exec mybinary version
```
### Environment Variables
- `NATS_URL`: NATS server URL (default: `nats://localhost:4222`)
- `NATS_BUCKET`: Object store bucket name (default: `binaries`)
- `NATS_CACHE_DIR`: Local cache directory (default: `/tmp/nats-exec-cache`)

View File

@@ -39,4 +39,4 @@ inputs:
default: 'false'
runs:
using: 'go'
main: 'main.go'
main: 'cmd/nats-upload/main.go'

146
cmd/nats-exec/main.go Normal file
View File

@@ -0,0 +1,146 @@
package main
import (
"context"
"fmt"
"io"
"log"
"os"
"path/filepath"
"runtime"
"strings"
"syscall"
"github.com/nats-io/nats.go"
"github.com/nats-io/nats.go/jetstream"
"golang.org/x/mod/semver"
)
func main() {
if len(os.Args) < 2 {
_, _ = fmt.Fprintf(os.Stderr, "Usage: %s <binary-name> [args...]\n", os.Args[0])
os.Exit(1)
}
binaryName := os.Args[1]
remainingArgs := os.Args[2:]
natsURL := getEnv("NATS_URL", "nats://localhost:4222")
bucketName := getEnv("NATS_BUCKET", "binaries")
cacheDir := getEnv("NATS_CACHE_DIR", filepath.Join(os.TempDir(), "nats-exec-cache"))
ctx := context.Background()
nc, err := nats.Connect(natsURL)
if err != nil {
log.Fatalf("Failed to connect to NATS: %v", err)
}
defer nc.Close()
js, err := jetstream.New(nc)
if err != nil {
log.Fatalf("Failed to create JetStream context: %v", err)
}
store, err := js.ObjectStore(ctx, bucketName)
if err != nil {
log.Fatalf("Failed to get object store %s: %v", bucketName, err)
}
// Format: binary/arch/version
arch := runtime.GOARCH
objects, err := store.List(ctx)
if err != nil {
log.Fatalf("Failed to list objects: %v", err)
}
var latestVersion string
var latestKey string
for _, info := range objects {
// Key structure: <binaryName>/<arch>/<version>
parts := strings.Split(info.Name, "/")
if len(parts) != 3 {
continue
}
if parts[0] != binaryName {
continue
}
if parts[1] != arch {
continue
}
version := parts[2]
if !strings.HasPrefix(version, "v") {
version = "v" + version
}
if semver.IsValid(version) {
if latestVersion == "" || semver.Compare(version, latestVersion) > 0 {
latestVersion = version
latestKey = info.Name
}
}
}
if latestKey == "" {
log.Fatalf("No version of %s found for arch %s", binaryName, arch)
}
localPath := filepath.Join(cacheDir, latestKey)
if _, err := os.Stat(localPath); os.IsNotExist(err) {
log.Printf("Downloading %s version %s...", binaryName, latestVersion)
if err := downloadBinary(ctx, store, latestKey, localPath); err != nil {
log.Fatalf("Failed to download binary: %v", err)
}
} else {
log.Printf("Using cached %s version %s", binaryName, latestVersion)
}
if err := os.Chmod(localPath, 0755); err != nil {
log.Fatalf("Failed to make binary executable: %v", err)
}
fullPath, err := filepath.Abs(localPath)
if err != nil {
log.Fatalf("Failed to get absolute path: %v", err)
}
env := os.Environ()
args := append([]string{fullPath}, remainingArgs...)
err = syscall.Exec(fullPath, args, env)
if err != nil {
log.Fatalf("Failed to exec %s: %v", fullPath, err)
}
}
func downloadBinary(ctx context.Context, store jetstream.ObjectStore, key, localPath string) error {
if err := os.MkdirAll(filepath.Dir(localPath), 0755); err != nil {
return err
}
obj, err := store.Get(ctx, key)
if err != nil {
return err
}
f, err := os.OpenFile(localPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0755)
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(f, obj)
return err
}
func getEnv(key, defaultValue string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return defaultValue
}