Files
interzone/internal/deploy/check.go
T
vvvomitandClaude Opus 5.5 c5c278952b Initial commit: interzone TUI for matrix-docker-ansible-deploy
Interactive wizard (Bubble Tea, Bubbles, Lip Gloss) that picks a homeserver,
opts into any MDAD component, writes the inventory and runs the playbook.
Supports in-country git, container registry, Docker, APT and PyPI mirrors
for installing during an internet blackout.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-24 14:48:25 +03:30

125 lines
3.6 KiB
Go

package deploy
import (
"context"
"fmt"
"net/http"
"os/exec"
"strings"
"sync"
"time"
"interzone/internal/config"
)
type Check struct {
Name string
Target string
OK bool
Detail string
}
// Preflight probes every configured source plus the local toolchain in parallel.
func Preflight(c *config.Config) []Check {
type probe struct {
name, target string
fn func(context.Context) (string, error)
}
var probes []probe
add := func(name, target string, fn func(context.Context) (string, error)) {
probes = append(probes, probe{name, target, fn})
}
for _, bin := range []string{"git", "ansible-playbook", "ansible-galaxy"} {
bin := bin
add("Local tool", bin, func(context.Context) (string, error) {
p, err := lookPath(bin)
if err != nil {
if strings.HasPrefix(bin, "ansible") {
return "", fmt.Errorf("not installed; will be installed from PyPI into a private venv")
}
return "", fmt.Errorf("not installed")
}
return p, nil
})
}
s := c.Sources
add("Playbook git", s.PlaybookGit, func(ctx context.Context) (string, error) { return gitReachable(ctx, s.PlaybookGit) })
if s.RolesGitBase != "" {
u := strings.TrimRight(s.RolesGitBase, "/") + "/mother-of-all-self-hosting/ansible-role-aux"
add("Roles git mirror", s.RolesGitBase, func(ctx context.Context) (string, error) { return gitReachable(ctx, u) })
}
if s.UseRegistry && s.RegistryPrefix != "" {
host := strings.SplitN(strings.TrimPrefix(strings.TrimPrefix(s.RegistryPrefix, "https://"), "http://"), "/", 2)[0]
add("Container registry", s.RegistryPrefix, func(ctx context.Context) (string, error) {
return httpReachable(ctx, "https://"+host+"/v2/")
})
}
if s.DockerPackageURL != "" {
add("Docker packages", s.DockerPackageURL, func(ctx context.Context) (string, error) { return httpReachable(ctx, s.DockerPackageURL) })
}
if s.APTMirror != "" {
add("APT mirror", s.APTMirror, func(ctx context.Context) (string, error) { return httpReachable(ctx, s.APTMirror) })
}
if s.PyPIIndex != "" {
u := strings.TrimRight(s.PyPIIndex, "/") + "/ansible/"
add("PyPI mirror", s.PyPIIndex, func(ctx context.Context) (string, error) { return httpReachable(ctx, u) })
}
out := make([]Check, len(probes))
var wg sync.WaitGroup
for i, p := range probes {
wg.Add(1)
go func(i int, p probe) {
defer wg.Done()
ctx, cancel := context.WithTimeout(context.Background(), 12*time.Second)
defer cancel()
detail, err := p.fn(ctx)
out[i] = Check{Name: p.name, Target: p.target, OK: err == nil, Detail: detail}
if err != nil {
out[i].Detail = err.Error()
}
}(i, p)
}
wg.Wait()
return out
}
func gitReachable(ctx context.Context, url string) (string, error) {
cmd := exec.CommandContext(ctx, "git", "ls-remote", url, "HEAD")
cmd.Env = append(cmd.Environ(), "GIT_TERMINAL_PROMPT=0")
b, err := cmd.Output()
if err != nil {
if ctx.Err() != nil {
return "", fmt.Errorf("timed out")
}
return "", fmt.Errorf("unreachable")
}
if f := strings.Fields(string(b)); len(f) > 0 {
return "HEAD " + f[0][:min(10, len(f[0]))], nil
}
return "reachable", nil
}
func httpReachable(ctx context.Context, url string) (string, error) {
if !strings.Contains(url, "://") {
url = "https://" + url
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return "", err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
if ctx.Err() != nil {
return "", fmt.Errorf("timed out")
}
return "", fmt.Errorf("unreachable")
}
resp.Body.Close()
if resp.StatusCode >= 500 {
return "", fmt.Errorf("HTTP %d", resp.StatusCode)
}
return fmt.Sprintf("HTTP %d", resp.StatusCode), nil
}