From 920640c4531432c0b5a074c608858cb86f71d50a Mon Sep 17 00:00:00 2001 From: Samy Fodil <76626119+samyfodil@users.noreply.github.com> Date: Sat, 26 Oct 2024 22:52:27 -0500 Subject: [PATCH] [spore-drive] domain key and tau binary issue #268 --- pkg/spore-drive/drive/displace.go | 33 ++++++++++++++++++++++---- pkg/spore-drive/drive/displace_test.go | 13 +++++++--- pkg/spore-drive/drive/new.go | 8 +++++++ pkg/spore-drive/drive/types.go | 3 ++- 4 files changed, 49 insertions(+), 8 deletions(-) diff --git a/pkg/spore-drive/drive/displace.go b/pkg/spore-drive/drive/displace.go index 3029ce1d..f3507c40 100644 --- a/pkg/spore-drive/drive/displace.go +++ b/pkg/spore-drive/drive/displace.go @@ -181,7 +181,7 @@ func (d *sporedrive) writeDomainPrivKeyToTmp(h remoteHost) error { } func (d *sporedrive) writeDomainPubKeyToTmp(h remoteHost) error { - pkr, err := d.parser.Cloud().Domain().Validation().OpenPrivateKey() + pkr, err := d.parser.Cloud().Domain().Validation().OpenPublicKey() if err != nil { return fmt.Errorf("failed to open private domain key: %w", err) } @@ -381,6 +381,17 @@ func listTauInstances(ctx context.Context, h remoteHost) ([]string, error) { return instances, nil } +func (d *sporedrive) isSameTau(ctx context.Context, h remoteHost) bool { + output, err := h.Execute(ctx, "md5sum", "-bz", "/tb/bin/tau") + if err == nil && output != nil { + fields := strings.Fields(string(output)) + if len(fields) > 1 && fields[0] == d.tauBinaryHash { + return true + } + } + return false +} + func (d *sporedrive) displaceHandler(hypha *course.Hypha, progressCh chan<- Progress) func(context.Context, host.Host) error { updatingTau := (d.tauBinary != nil) return func(ctx context.Context, h host.Host) error { @@ -495,6 +506,14 @@ func (d *sporedrive) displaceHandler(hypha *course.Hypha, progressCh chan<- Prog } pushProgress("dependencies", 100) + pushProgress("checking state", 0) + updatingTau = updatingTau && !d.isSameTau(ctx, r) + pushProgress("checking state", 50) + + // TODO: check config changes + + pushProgress("checking state", 100) + // Upload to /tmp if updatingTau { pushProgress("upload tau", 0) @@ -560,7 +579,11 @@ func (d *sporedrive) displaceHandler(hypha *course.Hypha, progressCh chan<- Prog } if _, err = r.Sudo(ctx, "cp", "-f", "/tmp/tau@.service", "/lib/systemd/system/tau@.service"); err != nil { - return pushError("upload tau files", fmt.Errorf("failed to copy tau@.servicee: %w", err)) + return pushError("upload tau files", fmt.Errorf("failed to copy tau@.service: %w", err)) + } + + if _, err = r.Sudo(ctx, "systemctl", "daemon-reload"); err != nil { + return pushError("upload tau files", fmt.Errorf("failed to daemon-reload: %w", err)) } } @@ -598,8 +621,10 @@ func (d *sporedrive) displaceHandler(hypha *course.Hypha, progressCh chan<- Prog } pushProgress("setup tau", 30) - if _, err = r.Sudo(ctx, "cp", "-f", "/tmp/tau", "/tb/bin/"); err != nil { - return pushError("setup tau", fmt.Errorf("failed to copy tau: %w", err)) + if updatingTau { + if _, err = r.Sudo(ctx, "cp", "-f", "/tmp/tau", "/tb/bin/"); err != nil { + return pushError("setup tau", fmt.Errorf("failed to copy tau: %w", err)) + } } pushProgress("setup plugins", 0) diff --git a/pkg/spore-drive/drive/displace_test.go b/pkg/spore-drive/drive/displace_test.go index ee5e98ba..6650fef8 100644 --- a/pkg/spore-drive/drive/displace_test.go +++ b/pkg/spore-drive/drive/displace_test.go @@ -71,6 +71,10 @@ func testDisplace(t *testing.T, sd Spore) { 142.250.115.139 `), nil) + if updatingTau { + rh.On("Execute", ctx, "md5sum", "-bz", "/tb/bin/tau").Once().Return(nil, nil) + } + // upload tau if updatingTau { tauf, _ := fses[h].OpenFile("/tmp/tau", os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0750) @@ -83,6 +87,7 @@ func testDisplace(t *testing.T, sd Spore) { sdcf, _ := fses[h].OpenFile("/tmp/tau@.service", os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644) rh.On("OpenFile", "/tmp/tau@.service", os.O_CREATE|os.O_RDWR|os.O_TRUNC, fs.FileMode(0644)).Once().Return(sdcf, nil) rh.On("Sudo", ctx, "cp", "-f", "/tmp/tau@.service", "/lib/systemd/system/tau@.service").Return(nil, nil) + rh.On("Sudo", ctx, "systemctl", "daemon-reload").Return(nil, nil) } rh.On("Sudo", ctx, "bash", "-c", "mkdir -p /tb/{bin,scripts,priv,cache,logs,storage,config/keys,plugins}").Return(nil, nil) @@ -104,7 +109,9 @@ func testDisplace(t *testing.T, sd Spore) { sh1cf, _ := fses[h].OpenFile("/tmp/shape1.yaml", os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0750) rh.On("OpenFile", "/tmp/shape1.yaml", os.O_CREATE|os.O_RDWR|os.O_TRUNC, fs.FileMode(0750)).Once().Return(sh1cf, nil) rh.On("Sudo", ctx, "cp", "-f", "/tmp/shape1.yaml", "/tb/config/").Return(nil, nil) - rh.On("Sudo", ctx, "cp", "-f", "/tmp/tau", "/tb/bin/").Return(nil, nil) + if updatingTau { + rh.On("Sudo", ctx, "cp", "-f", "/tmp/tau", "/tb/bin/").Return(nil, nil) + } rh.On("Sudo", ctx, "systemctl", "list-units", "--type=service", "--quiet", "--no-pager", "--all", "tau@*.service").Once().Return([]byte(` tau@compute.service loaded inactive dead Description of compute tau@storage.service loaded inactive dead Description of storage @@ -158,9 +165,9 @@ func testDisplace(t *testing.T, sd Spore) { } if updatingTau { - assert.Equal(t, len(steps), 50) + assert.Equal(t, len(steps), 56) } else { - assert.Equal(t, len(steps), 44) + assert.Equal(t, len(steps), 50) } for h, mfs := range fses { diff --git a/pkg/spore-drive/drive/new.go b/pkg/spore-drive/drive/new.go index 03e95b85..4152f81f 100644 --- a/pkg/spore-drive/drive/new.go +++ b/pkg/spore-drive/drive/new.go @@ -1,6 +1,8 @@ package drive import ( + "crypto/md5" + "encoding/hex" "fmt" "github.com/taubyte/tau/pkg/mycelium" @@ -26,6 +28,12 @@ func New(cnf config.Parser, options ...Option) (Spore, error) { } } + if s.tauBinary != nil { + hasher := md5.New() + hasher.Write(s.tauBinary) + s.tauBinaryHash = hex.EncodeToString(hasher.Sum(nil)) + } + return s, nil } diff --git a/pkg/spore-drive/drive/types.go b/pkg/spore-drive/drive/types.go index e50f1283..b449e584 100644 --- a/pkg/spore-drive/drive/types.go +++ b/pkg/spore-drive/drive/types.go @@ -13,7 +13,8 @@ type sporedrive struct { parser config.Parser network *mycelium.Network - tauBinary []byte + tauBinary []byte + tauBinaryHash string hostWrapper func(ctx context.Context, h host.Host) (remoteHost, error) }