platform

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 5, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultSSHTimeout = 60 * time.Second

DefaultSSHTimeout is the default timeout for SSH commands (60 seconds). Set Server.SSHTimeout to override per-server or use SSHWithTimeout for per-call.

Variables

View Source
var (
	// ErrNotFound is returned when a resource doesn't exist
	ErrNotFound = errors.New("resource not found")

	// ErrTimeout is returned when an operation times out
	ErrTimeout = errors.New("operation timed out")

	// ErrUnsupportedRegion is returned when a region is not supported by the provider
	ErrUnsupportedRegion = errors.New("unsupported region")

	// ErrUnsupportedSize is returned when a size is not supported by the provider
	ErrUnsupportedSize = errors.New("unsupported size")
)

Functions

func BackupContainer

func BackupContainer(s *Server, name string)

BackupContainer stops and renames the current container for rollback.

func BuildImage

func BuildImage(s *Server, name, source string) (string, error)

BuildImage uploads source and builds a Docker image on the server. Uses git archive to capture all tracked files, avoiding brittle hardcoded lists.

func ConfigureInsecureRegistry

func ConfigureInsecureRegistry(s *Server, addr string)

ConfigureInsecureRegistry writes /etc/docker/daemon.json on the server to allow pulling from an insecure (HTTP) private registry. Only restarts Docker if the config actually changed.

func RemoveContainer

func RemoveContainer(s *Server, name string)

RemoveContainer force-removes a Docker container by name.

func Rollback

func Rollback(s *Server, name string) error

Rollback restores the previous container version from backup.

func StartService

func StartService(s *Server, service *Service) error

StartService runs a service container on the server.

func StopService

func StopService(s *Server, name string) error

StopService kills and removes a container on the server.

func SyncSecrets

func SyncSecrets(from *Server, to []*Server)

SyncSecrets copies /etc/secrets/* from one server to each target server. Public keys and SSH keys are set to 644 so non-root container users can read them.

func UploadImage

func UploadImage(s *Server, imagePath string) (string, error)

UploadImage uploads a Docker image tar.gz to the server and loads it. Returns the image name extracted from the file path.

func WaitForHealthy

func WaitForHealthy(s *Server, container string, timeoutSecs int) error

WaitForHealthy waits for a Docker container to report healthy status.

func WaitForRegistry

func WaitForRegistry(s *Server, host string)

WaitForRegistry waits for a Docker registry to be reachable from this server.

Types

type Backend

type Backend interface {
	// Servers
	CreateServer(opts ServerOptions) (*Server, error)
	GetServer(name string) (*Server, error)
	DeleteServer(id string) error

	// Volumes
	CreateVolume(name string, sizeGB int, region Region) (*Volume, error)
	GetVolume(name string) (*Volume, error)
	AttachVolume(volumeID, serverID string) error
	DetachVolume(volumeID string) error
}

Backend is the interface that providers implement.

type Healthcheck

type Healthcheck struct {
	Cmd         string // Command to run (e.g., "curl -f http://localhost/health")
	Interval    string // Time between checks (e.g., "30s")
	Timeout     string // Time to wait for check to complete (e.g., "10s")
	Retries     int    // Consecutive failures before unhealthy
	StartPeriod string // Grace period before health checks start (e.g., "5s")
}

Healthcheck configures container health checking

type LoadBalancer

type LoadBalancer struct {
	ID     string
	Name   string
	IP     string // Public IP address
	Status string // "new", "active", "errored"
}

LoadBalancer represents a cloud load balancer.

type LoadBalancerConfig

type LoadBalancerConfig struct {
	Name       string
	Region     Region
	VpcID      string   // VPC to attach to
	ServerIDs  []int    // Backend server IDs
	Tags       []string // Tags to target servers (alternative to ServerIDs)
	Port       int      // Frontend port (default 443)
	TargetPort int      // Backend port (default 80)
	HealthPath string   // Health check path (default "/health")
}

LoadBalancerConfig configures load balancer creation.

type LoadBalancerProvider

type LoadBalancerProvider interface {
	CreateLoadBalancer(cfg LoadBalancerConfig) (*LoadBalancer, error)
	GetLoadBalancer(name string) (*LoadBalancer, error)
	DeleteLoadBalancer(id string) error
}

LoadBalancerProvider is an optional interface for load balancer management.

type LogConfig

type LogConfig struct {
	Driver  string            // Log driver (e.g., "json-file", "syslog")
	Options map[string]string // Driver-specific options
}

LogConfig configures container logging

type Mount

type Mount struct {
	Source string
	Target string
}

Mount binds a host path to a container path

type Platform

type Platform struct {
	Backend
}

Platform provides cloud infrastructure operations. Use providers to create: mock.New(), digitalocean.New(), aws.New(), gcp.New()

type Port

type Port struct {
	Host      int
	Container int
	Bind      string // Optional bind address (e.g., "127.0.0.1")
}

Port maps a host port to a container port

type Region

type Region string

Region represents a geographic location for cloud resources. Use these constants instead of provider-specific region codes.

const (
	// NYC represents the New York region.
	NYC Region = "nyc"
	// SFO represents the San Francisco region.
	SFO Region = "sfo"
	// TOR represents the Toronto region.
	TOR Region = "tor"

	// LON represents the London region.
	LON Region = "lon"
	// AMS represents the Amsterdam region.
	AMS Region = "ams"
	// FRA represents the Frankfurt region.
	FRA Region = "fra"

	// SGP represents the Singapore region.
	SGP Region = "sgp"
	// SYD represents the Sydney region.
	SYD Region = "syd"
	// BLR represents the Bangalore region.
	BLR Region = "blr"
)

Supported cloud regions, grouped by continent.

func AllRegions

func AllRegions() []Region

AllRegions returns all supported regions.

type Resources

type Resources struct {
	CPUs   string // CPU limit (e.g., "0.5", "2")
	Memory string // Memory limit (e.g., "512m", "1g")
}

Resources configures CPU and memory limits

type SSHKeyProvider

type SSHKeyProvider interface {
	// GetSSHKeyFingerprint finds or registers an SSH key, returns fingerprint.
	GetSSHKeyFingerprint(publicKey string) (string, error)
}

SSHKeyProvider is an optional interface for SSH key management.

type Server

type Server struct {
	ID         string
	Name       string
	IP         string
	PrivateIP  string        // Private IP for VPC networking (empty if no VPC)
	Size       string        // e.g., "s-1vcpu-2gb"
	Region     string        // e.g., "nyc1"
	Status     string        // creating, active, off
	SSHTimeout time.Duration // Per-command SSH timeout (0 = DefaultSSHTimeout)
	// contains filtered or unexported fields
}

Server represents a cloud virtual machine

func (*Server) Connect

func (s *Server) Connect(args ...string) error

Connect executes a command with stdin/stdout/stderr connected

func (*Server) Copy

func (s *Server) Copy(local, remote string) error

Copy uploads a file to the server via SCP. Uses a 5-minute timeout to prevent indefinite hangs on large files.

func (*Server) Interactive

func (s *Server) Interactive() error

Interactive opens an interactive SSH shell

func (*Server) RunScript

func (s *Server) RunScript(localPath string) error

RunScript uploads and runs a setup script on the server. If the script file doesn't exist locally, it's a no-op.

func (*Server) SSH

func (s *Server) SSH(args ...string) (string, error)

SSH executes a command on the server and returns output. Uses the configured SSHTimeout (default 60s) to prevent indefinite hangs. SECURITY: Arguments are shell-escaped to prevent command injection.

func (*Server) SSHWithTimeout

func (s *Server) SSHWithTimeout(timeout time.Duration, args ...string) (string, error)

SSHWithTimeout executes a command on the server with a specific timeout. Use this for long-running operations like docker build that need more time.

func (*Server) WaitForSSH

func (s *Server) WaitForSSH(timeout time.Duration) error

WaitForSSH waits until the server accepts SSH connections. Uses a real SSH command to verify full authentication works, not just a TCP dial (sshd may accept connections before it's ready to authenticate).

func (*Server) Write

func (s *Server) Write(remotePath string, data []byte, executable bool) error

Write writes data to a file on the server

type ServerOptions

type ServerOptions struct {
	Name    string
	Size    Size
	Region  Region
	Image   string // Provider-specific image ID/slug
	SSHKey  string // SSH key fingerprint (use GetSSHKeyFingerprint to get)
	Tags    []string
	Backups bool   // Enable provider-managed backups
	VpcID   string // VPC ID for private networking (optional)
}

ServerOptions configures server creation.

type Service

type Service struct {
	Name        string
	Image       string
	Command     []string
	Ports       []Port
	Volumes     []Mount
	Env         map[string]string
	Network     string
	Restart     string
	Healthcheck *Healthcheck
	Resources   *Resources
	Logging     *LogConfig
	Privileged  bool
}

Service represents a Docker container to run on a server

type Size

type Size string

Size represents a server size tier. Use these constants instead of provider-specific instance types.

const (
	// Micro is 1 vCPU, 1GB RAM — suited for dev/testing.
	Micro Size = "micro"
	// Small is 1 vCPU, 2GB RAM — suited for small apps.
	Small Size = "small"
	// Medium is 2 vCPU, 4GB RAM — suited for standard apps.
	Medium Size = "medium"
	// Large is 4 vCPU, 8GB RAM — suited for larger workloads.
	Large Size = "large"
	// XLarge is 8 vCPU, 16GB RAM — suited for high performance.
	XLarge Size = "xlarge"
)

Supported server size tiers with approximate resource allocations.

func AllSizes

func AllSizes() []Size

AllSizes returns all supported sizes.

type TagProvider

type TagProvider interface {
	TagServer(serverID string, tag string) error
}

TagProvider is an optional interface for tagging servers with provider-specific tags.

type VPCProvider

type VPCProvider interface {
	// CreateVPC creates a VPC network, returning its ID.
	CreateVPC(name, region, description string) (string, error)
	// GetVPC looks up a VPC by name and returns its ID.
	GetVPC(name string) (string, error)
}

VPCProvider is an optional interface for VPC management.

type Volume

type Volume struct {
	ID       string
	Name     string
	Size     int // Gigabytes
	Region   string
	ServerID string // Attached server (empty if detached)
}

Volume represents attached block storage

Directories

Path Synopsis
providers

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL