diff --git a/config.go b/config.go index 33f4b89..808722c 100644 --- a/config.go +++ b/config.go @@ -4,161 +4,20 @@ package zoopb // data to and from config files import ( - "errors" - "fmt" - "os" - "path/filepath" - - "go.wit.com/log" - "google.golang.org/protobuf/proto" + "go.wit.com/lib/config" ) // writes out the cluster information it seperate files // to make it humanly possible to hand edit things as needed -func (m *Machines) ConfigSave() error { - if m == nil { - return fmt.Errorf("ConfigSave() machines == n") - } - data, err := m.Marshal() +func (m *Machines) ConfigSave(fullname string) error { + return config.SavePB(m, fullname) +} + +func (m *Machines) ConfigLoad() (string, error) { + fullname, err := config.LoadPB(m, "/var/lib", "machines") if err != nil { - log.Info("proto.Marshal() failed len", len(data), err) - return err + // log.Info("zoopb.ConfigLoad() failed", err, fullname) } - log.Info("proto.Marshal() worked len", len(data)) - configWrite(data) - return nil -} - -// when running on a single machine, save the file in forge/ -// as .pb -// write to ~/.config/forge/ unless ENV{FORGE_HOME} is set -func (m *Machine) ConfigSave() error { - if os.Getenv("FORGE_HOME") == "" { - homeDir, _ := os.UserHomeDir() - fullpath := filepath.Join(homeDir, ".config/forge") - os.Setenv("FORGE_HOME", fullpath) - } - data, err := m.Marshal() - if err != nil { - log.Info("proto.Marshal() failed len", len(data), err) - return err - } - - log.Info("ConfigSave() proto.Marshal() worked len", len(data)) - - hostname, _ := os.Hostname() - fname := hostname + ".pb" - return m.configWrite(fname, data) -} - -func ConfigSaveRaw(data []byte) error { - configWrite(data) - return nil -} - -func (m *Machines) ConfigLoad() error { - if m == nil { - return errors.New("It's not safe to run ConfigLoad() on a nil ?") - } - - if data, err := loadFile("zookeeper.pb"); err == nil { - if err = proto.Unmarshal(data, m); err != nil { - log.Warn("broken zookeeper.pb config file") - return err - } - } else { - return err - } - return nil -} - -func (m *Machine) ConfigLoad() error { - if m == nil { - return errors.New("It's not safe to run ConfigLoad() on a nil ?") - } - if os.Getenv("FORGE_HOME") == "" { - homeDir, _ := os.UserHomeDir() - fullpath := filepath.Join(homeDir, ".config/forge") - os.Setenv("FORGE_HOME", fullpath) - } - - hostname, _ := os.Hostname() - fname := hostname + ".pb" - - var data []byte - var err error - if data, err = loadFile(fname); err != nil { - // something went wrong loading the file - return err - } - - if data != nil { - if err = proto.Unmarshal(data, m); err != nil { - log.Warn("broken zookeeper.pb config file", fname) - return err - } - return nil - } - - m.Hostname = hostname - m.Distro = detectDistro() - m.initPackages() - log.Log(INFO, "zoopb.ConfigLoad()", m.Hostname, "runs", m.Distro, "with", m.Packages.Len(), "packages") - return nil -} - -func loadFile(filename string) ([]byte, error) { - homeDir, err := os.UserHomeDir() - p := filepath.Join(homeDir, ".config/zookeeper") - fullname := filepath.Join(p, filename) - data, err := os.ReadFile(fullname) - if errors.Is(err, os.ErrNotExist) { - // if file does not exist, just return nil. this - // will cause ConfigLoad() to try the next config file like "forge.text" - // because the user might want to edit the .config by hand - return nil, nil - } - if err != nil { - // log.Info("open config file :", err) - return nil, err - } - return data, nil -} - -func (m *Machine) loadFile(fname string) ([]byte, error) { - fullname := filepath.Join(os.Getenv("FORGE_HOME"), fname) - - data, err := os.ReadFile(fullname) - if err != nil { - // log.Info("open config file :", err) - return nil, err - } - return data, nil -} - -func configWrite(data []byte) error { - homeDir, err := os.UserHomeDir() - p := filepath.Join(homeDir, ".config/zookeeper") - fname := filepath.Join(p, "zookeeper.pb") - cfgfile, err := os.OpenFile(fname, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) - defer cfgfile.Close() - if err != nil { - log.Warn("open config file :", err) - return err - } - cfgfile.Write(data) - return nil -} - -func (m *Machine) configWrite(fname string, data []byte) error { - fullname := filepath.Join(os.Getenv("FORGE_HOME"), fname) - - cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) - defer cfgfile.Close() - if err != nil { - log.Warn("open config file :", err) - return err - } - cfgfile.Write(data) - return nil + fullname, err = config.LoadPB(m, "zookeeper", "machines") + return fullname, err } diff --git a/init.go b/init.go new file mode 100644 index 0000000..4e86058 --- /dev/null +++ b/init.go @@ -0,0 +1,49 @@ +package zoopb + +import ( + "os" + "time" + + "go.wit.com/lib/config" + "go.wit.com/log" +) + +// sent via -ldflags +var VERSION string +var BUILDTIME string + +func (m *Machine) SinceLastUpdate() time.Duration { + age := m.Laststamp.AsTime() + return time.Since(age) +} + +func InitMachine() (*Machine, string) { + var fullname string + var err error + m := new(Machine) + if fullname, err = config.LoadPB(m, "forge", "machine"); err != nil { + log.Info("zoopb.ConfigLoad() failed", err) + } + hostname, _ := os.Hostname() + m.Hostname = hostname + m.Distro = detectDistro() + m.initPackages() + + m.InitWitMirrors() + config.SavePB(m, fullname) + + return m, fullname +} + +func InitDaemon() (*Machine, string) { + var fullname string + var err error + machine := new(Machine) + if fullname, err = config.LoadPB(machine, "/etc/zookeeper", "machine"); err != nil { + log.Info("zoopb.ConfigLoad() failed", err) + } + machine.InitWitMirrors() + config.SavePB(machine, fullname) + + return machine, fullname +} diff --git a/main.go b/main.go deleted file mode 100644 index b066d35..0000000 --- a/main.go +++ /dev/null @@ -1,26 +0,0 @@ -package zoopb - -import ( - "time" - - "go.wit.com/log" -) - -// sent via -ldflags -var VERSION string -var BUILDTIME string - -func InitMachine() *Machine { - machine := new(Machine) - if err := machine.ConfigLoad(); err != nil { - log.Info("zoopb.ConfigLoad() failed", err) - } - machine.InitWit() - - return machine -} - -func (m *Machine) SinceLastUpdate() time.Duration { - age := m.Laststamp.AsTime() - return time.Since(age) -} diff --git a/wit.go b/wit.go index b678a57..2e118c6 100644 --- a/wit.go +++ b/wit.go @@ -19,6 +19,12 @@ func (m *Machine) IsInstalled(name string) bool { } func (m *Machine) FindInstalledByName(name string) *Package { + if m == nil { + panic("m == nil") + } + if m.Packages == nil { + panic("m.Packages == nil") + } for p := range m.Packages.IterByName() { if name == p.Name { // log.Info("package installed:", p.Name, p.Version, p.PkgName) @@ -77,7 +83,7 @@ func (m *Machine) FindByVersion(name string, version string) *Package { // read the package list file from mirrors.wit.com // obviously a hack at this point -func (m *Machine) InitWit() error { +func (m *Machine) InitWitMirrors() error { if m.Wit == nil { m.Wit = new(Packages) }