From 5a3d483f98cfeb5a5fe48de01d2cb1269e255e21 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Sun, 29 Oct 2023 01:04:24 +0000 Subject: [PATCH 1/3] cluster: refactor Machine.ReadFile() and Machine.OpenFile() Signed-off-by: Alejandro Mery --- pkg/cluster/machine_file.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pkg/cluster/machine_file.go b/pkg/cluster/machine_file.go index 0dff58e..bb320df 100644 --- a/pkg/cluster/machine_file.go +++ b/pkg/cluster/machine_file.go @@ -11,10 +11,9 @@ import ( // OpenFile opens a file on the machine's config directory with the specified flags func (m *Machine) OpenFile(name string, flags int, args ...any) (fs.File, error) { - base := m.zone.zones.dir fullName := m.getFilename(name, args...) - return fs.OpenFile(base, fullName, flags, 0644) + return m.zone.zones.OpenFile(fullName, flags) } // CreateTruncFile creates or truncates a file on the machine's config directory @@ -49,10 +48,9 @@ func (m *Machine) RemoveFile(name string, args ...any) error { // 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) + return m.zone.zones.ReadFile(fullName) } // WriteStringFile writes the given content to a file on the machine's config directory -- 2.17.1 From 57e1077a85b37e2fb30f1bd802a42ada9dc61363 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Sun, 29 Oct 2023 00:49:56 +0000 Subject: [PATCH 2/3] cluster: add MkdirAll() support Signed-off-by: Alejandro Mery --- pkg/cluster/cluster_file.go | 9 +++++++++ pkg/cluster/machine_file.go | 7 +++++++ 2 files changed, 16 insertions(+) diff --git a/pkg/cluster/cluster_file.go b/pkg/cluster/cluster_file.go index 0d885e1..0747f90 100644 --- a/pkg/cluster/cluster_file.go +++ b/pkg/cluster/cluster_file.go @@ -104,3 +104,12 @@ func (m *Cluster) WriteStringFile(value string, name string, args ...any) error _, err = buf.WriteTo(f) return err } + +// MkdirAll creates directories relative to the cluster's config directory +func (m *Cluster) MkdirAll(name string, args ...any) error { + if len(args) > 0 { + name = fmt.Sprintf(name, args...) + } + + return fs.MkdirAll(m.dir, name, 0755) +} diff --git a/pkg/cluster/machine_file.go b/pkg/cluster/machine_file.go index bb320df..f4bb148 100644 --- a/pkg/cluster/machine_file.go +++ b/pkg/cluster/machine_file.go @@ -60,6 +60,13 @@ func (m *Machine) WriteStringFile(value string, name string, args ...any) error return m.zone.zones.WriteStringFile(value, fullName) } +// MkdirAll creates directories relative to the machine's config directory +func (m *Machine) MkdirAll(name string, args ...any) error { + fullName := m.getFilename(name, args...) + + return m.zone.zones.MkdirAll(fullName) +} + func (m *Machine) getFilename(name string, args ...any) string { if len(args) > 0 { name = fmt.Sprintf(name, args...) -- 2.17.1 From 1492061ab8161d4c56c151d4b962a18d86e6dacf Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Sat, 28 Oct 2023 20:42:16 +0000 Subject: [PATCH 3/3] jpictl: create machine directories on `jpictl write` Signed-off-by: Alejandro Mery --- pkg/cluster/sync.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/pkg/cluster/sync.go b/pkg/cluster/sync.go index ded0abb..7f0dd8b 100644 --- a/pkg/cluster/sync.go +++ b/pkg/cluster/sync.go @@ -3,6 +3,7 @@ package cluster // SyncAll updates all config files func (m *Cluster) SyncAll() error { for _, fn := range []func() error{ + m.SyncMkdirAll, m.SyncAllWireguard, m.SyncAllCeph, m.WriteHosts, @@ -15,6 +16,20 @@ func (m *Cluster) SyncAll() error { return nil } +// SyncMkdirAll creates the directories needed to store files +// required to represent the cluster. +func (m *Cluster) SyncMkdirAll() error { + err := m.MkdirAll(".") + if err == nil { + m.ForEachMachine(func(p *Machine) bool { + err = p.MkdirAll(".") + return err != nil + }) + } + + return err +} + // SyncAllWireguard updates all wireguard config files func (m *Cluster) SyncAllWireguard() error { var err error -- 2.17.1