Skip to content

Commit

Permalink
[spore-drive] domain key and tau binary issue #268
Browse files Browse the repository at this point in the history
  • Loading branch information
samyfodil authored Oct 27, 2024
1 parent a6bcd51 commit 920640c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
33 changes: 29 additions & 4 deletions pkg/spore-drive/drive/displace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -560,7 +579,11 @@ func (d *sporedrive) displaceHandler(hypha *course.Hypha, progressCh chan<- Prog
}

if _, err = r.Sudo(ctx, "cp", "-f", "/tmp/[email protected]", "/lib/systemd/system/[email protected]"); err != nil {
return pushError("upload tau files", fmt.Errorf("failed to copy [email protected]: %w", err))
return pushError("upload tau files", fmt.Errorf("failed to copy [email protected]: %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))
}
}

Expand Down Expand Up @@ -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)
Expand Down
13 changes: 10 additions & 3 deletions pkg/spore-drive/drive/displace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -83,6 +87,7 @@ func testDisplace(t *testing.T, sd Spore) {
sdcf, _ := fses[h].OpenFile("/tmp/[email protected]", os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)
rh.On("OpenFile", "/tmp/[email protected]", os.O_CREATE|os.O_RDWR|os.O_TRUNC, fs.FileMode(0644)).Once().Return(sdcf, nil)
rh.On("Sudo", ctx, "cp", "-f", "/tmp/[email protected]", "/lib/systemd/system/[email protected]").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)
Expand All @@ -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(` [email protected] loaded inactive dead Description of compute
[email protected] loaded inactive dead Description of storage
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions pkg/spore-drive/drive/new.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package drive

import (
"crypto/md5"
"encoding/hex"
"fmt"

"github.com/taubyte/tau/pkg/mycelium"
Expand All @@ -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
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/spore-drive/drive/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 920640c

Please sign in to comment.