|
|
|
package zones
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
|
|
|
"net/netip"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
// A Machine is a machine on a Zone
|
|
|
|
type Machine struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
|
|
|
|
zone *Zone
|
|
|
|
id int
|
|
|
|
Name string
|
|
|
|
|
|
|
|
PublicAddresses []netip.Addr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Machine) String() string {
|
|
|
|
return m.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
// ID return the index within the [Zone] associated to this [Machine]
|
|
|
|
func (m *Machine) ID() int {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
|
|
|
if m.id == 0 {
|
|
|
|
zoneName := m.zone.Name
|
|
|
|
|
|
|
|
s := m.Name[len(zoneName)+1:]
|
|
|
|
|
|
|
|
id, err := strconv.ParseInt(s, 10, 8)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m.id = int(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.id
|
|
|
|
}
|
|
|
|
|
|
|
|
// FullName returns the Name of the machine including domain name
|
|
|
|
func (m *Machine) FullName() string {
|
|
|
|
if domain := m.zone.zones.domain; domain != "" {
|
|
|
|
var s = []string{
|
|
|
|
m.Name,
|
|
|
|
domain,
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(s, ".")
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadFile reads a file from the machine's config directory
|
|
|
|
func (m *Machine) ReadFile(name string, args ...any) ([]byte, error) {
|
|
|
|
base := m.zone.zones.dir
|
|
|
|
fullName := m.getFilename(name, args...)
|
|
|
|
|
|
|
|
return fs.ReadFile(base, fullName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Machine) getFilename(name string, args ...any) string {
|
|
|
|
if len(args) > 0 {
|
|
|
|
name = fmt.Sprintf(name, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
s := []string{
|
|
|
|
m.zone.Name,
|
|
|
|
m.Name,
|
|
|
|
name,
|
|
|
|
}
|
|
|
|
|
|
|
|
return filepath.Join(s...)
|
|
|
|
}
|