Skip to content

Commit b8a5516

Browse files
authored
fix(ssh): Do not create the ssh key if the key exists (#211)
* fix: Fix log Signed-off-by: Ce Gao <cegao@tensorchord.ai> * fix(builder): Fix interval and timeout Signed-off-by: Ce Gao <cegao@tensorchord.ai> * fix: Update mock Signed-off-by: Ce Gao <cegao@tensorchord.ai>
1 parent 40c7d06 commit b8a5516

3 files changed

Lines changed: 20 additions & 30 deletions

File tree

pkg/buildkitd/buildkitd.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,15 @@ import (
3030
)
3131

3232
var (
33-
interval = time.Second * 1
33+
interval = time.Second * 1
34+
timeoutConnection = time.Second * 5
35+
timeoutRun = time.Second * 3
3436
)
3537

3638
// Client is a client for the buildkitd daemon.
3739
// It's up to the caller to close the client.
3840
type Client interface {
3941
BuildkitdAddr() string
40-
Bootstrap(ctx context.Context) (string, error)
4142
// Solve calls Solve on the controller.
4243
Solve(ctx context.Context, def *llb.Definition, opt client.SolveOpt, statusChan chan *client.SolveStatus) (*client.SolveResponse, error)
4344
Close() error
@@ -67,14 +68,15 @@ func NewClient(ctx context.Context) (Client, error) {
6768
}
6869
c.Client = cli
6970

70-
if _, err := c.Bootstrap(ctx); err != nil {
71+
if _, err := c.Bootstrap(ctx, timeoutRun, timeoutConnection); err != nil {
7172
return nil, errors.Wrap(err, "failed to bootstrap the buildkitd")
7273
}
7374
return c, nil
7475
}
7576

76-
func (c *generalClient) Bootstrap(ctx context.Context) (string, error) {
77-
address, err := c.maybeStart(ctx, time.Second*100, time.Second*100)
77+
func (c *generalClient) Bootstrap(ctx context.Context,
78+
runningTimeout, connectingTimeout time.Duration) (string, error) {
79+
address, err := c.maybeStart(ctx, runningTimeout, connectingTimeout)
7880
if err != nil {
7981
return "", err
8082
}

pkg/buildkitd/mock/mock.go

Lines changed: 0 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/ssh/config/key.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"crypto/rsa"
2020
"crypto/x509"
2121
"encoding/pem"
22-
"fmt"
2322
"os"
2423

2524
"github.com/adrg/xdg"
@@ -39,19 +38,19 @@ func KeyExists(public, private string) bool {
3938
// public, private := getKeyPaths()
4039
publicKeyExists, _ := fileutil.FileExists(public)
4140
if !publicKeyExists {
42-
logrus.Infof("%s doesn't exist", public)
41+
logrus.Debugf("%s doesn't exist", public)
4342
return false
4443
}
4544

46-
logrus.Infof("%s already present", public)
45+
logrus.Debugf("%s already present", public)
4746

4847
privateKeyExists, _ := fileutil.FileExists(private)
4948
if !privateKeyExists {
50-
logrus.Infof("%s doesn't exist", private)
49+
logrus.Debugf("%s doesn't exist", private)
5150
return false
5251
}
5352

54-
logrus.Infof("%s already present", private)
53+
logrus.Debugf("%s already present", private)
5554
return true
5655
}
5756

@@ -65,27 +64,31 @@ func GenerateKeys() error {
6564
}
6665

6766
func generateKeys(public, private string, bitSize int) error {
67+
if KeyExists(public, private) {
68+
return nil
69+
}
70+
6871
privateKey, err := generatePrivateKey(bitSize)
6972
if err != nil {
70-
return fmt.Errorf("failed to generate private SSH key: %s", err)
73+
return errors.Wrap(err, "failed to generate private SSH key")
7174
}
7275

7376
publicKeyBytes, err := generatePublicKey(&privateKey.PublicKey)
7477
if err != nil {
75-
return fmt.Errorf("failed to generate public SSH key: %s", err)
78+
return errors.Wrap(err, "failed to generate public SSH key")
7679
}
7780

7881
privateKeyBytes := encodePrivateKeyToPEM(privateKey)
7982

8083
if err := os.WriteFile(public, publicKeyBytes, 0600); err != nil {
81-
return fmt.Errorf("failed to write public SSH key: %s", err)
84+
return errors.Wrap(err, "failed to write public SSH key")
8285
}
8386

8487
if err := os.WriteFile(private, privateKeyBytes, 0600); err != nil {
85-
return fmt.Errorf("failed to write private SSH key: %s", err)
88+
return errors.Wrap(err, "failed to write private SSH key")
8689
}
8790

88-
logrus.Infof("created ssh keypair at %s and %s", public, private)
91+
logrus.Debugf("created ssh keypair at %s and %s", public, private)
8992
return nil
9093
}
9194

0 commit comments

Comments
 (0)