You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.2 KiB
66 lines
1.2 KiB
package zones |
|
|
|
import ( |
|
"fmt" |
|
"io/fs" |
|
"net/netip" |
|
"path/filepath" |
|
"strings" |
|
"sync" |
|
) |
|
|
|
// A Machine is a machine on a Zone |
|
type Machine struct { |
|
mu sync.Mutex |
|
|
|
zone *Zone |
|
ID int |
|
Name string `toml:"name"` |
|
|
|
PublicAddresses []netip.Addr `toml:"public,omitempty"` |
|
RingAddresses []*RingInfo `toml:"rings,omitempty"` |
|
} |
|
|
|
func (m *Machine) String() string { |
|
return m.Name |
|
} |
|
|
|
// 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...) |
|
} |
|
|
|
func (m *Machine) getPeerByName(name string) (*Machine, bool) { |
|
return m.zone.zones.GetMachineByName(name) |
|
}
|
|
|