Skip to content

Commit 2d567ca

Browse files
authored
[cache] Add cache info command (#2001)
## Summary * Adds new info command * key cache and credentials local cache to current member. ## How was it tested?
1 parent 86392ce commit 2d567ca

File tree

5 files changed

+79
-35
lines changed

5 files changed

+79
-35
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ require (
4040
github.com/wk8/go-ordered-map/v2 v2.1.8
4141
github.com/zealic/go2node v0.1.0
4242
go.jetpack.io/envsec v0.0.16-0.20240329013200-4174c0acdb00
43-
go.jetpack.io/pkg v0.0.0-20240411004921-791796648f19
43+
go.jetpack.io/pkg v0.0.0-20240415190428-d17de207b432
4444
go.jetpack.io/typeid v1.0.1-0.20240410183543-96a4fd53d1e2
4545
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225
4646
golang.org/x/mod v0.16.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,8 @@ go.jetpack.io/envsec v0.0.16-0.20240329013200-4174c0acdb00 h1:Kb+OlWOntAq+1nF+01
362362
go.jetpack.io/envsec v0.0.16-0.20240329013200-4174c0acdb00/go.mod h1:dVG2n8fBAGpQczW8yk/6wuXb9uEhzaJF7wGXkGLRRCU=
363363
go.jetpack.io/pkg v0.0.0-20240411004921-791796648f19 h1:ZloUPW4zNknwS2mYSmMVAtMfl3H2zZUCz+MxU5s79fY=
364364
go.jetpack.io/pkg v0.0.0-20240411004921-791796648f19/go.mod h1:JfVScypw14E4GR2TA2Nv29JqalRtgv2C9QjeQcNS4UM=
365+
go.jetpack.io/pkg v0.0.0-20240415190428-d17de207b432 h1:5B9uUxqzwDLUi+m/SVawTMszOmCK6KnUWb4yiCZJumg=
366+
go.jetpack.io/pkg v0.0.0-20240415190428-d17de207b432/go.mod h1:JfVScypw14E4GR2TA2Nv29JqalRtgv2C9QjeQcNS4UM=
365367
go.jetpack.io/typeid v1.0.1-0.20240410183543-96a4fd53d1e2 h1:w9uWg8BAim374iWzxEuDhf6MJ/cMQVR/0xi/L3DgfT0=
366368
go.jetpack.io/typeid v1.0.1-0.20240410183543-96a4fd53d1e2/go.mod h1:zqBUNjE3NxAL8wRYW2LcOpH5LxCvTqthm9YSuuu1ic4=
367369
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=

internal/boxcli/cache.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package boxcli
55

66
import (
77
"encoding/json"
8+
"fmt"
89
"os/user"
910

1011
"github.com/MakeNowJust/heredoc/v2"
@@ -64,7 +65,7 @@ func cacheCmd() *cobra.Command {
6465
cacheCommand.AddCommand(uploadCommand)
6566
cacheCommand.AddCommand(cacheConfigureCmd())
6667
cacheCommand.AddCommand(cacheCredentialsCmd())
67-
cacheCommand.Hidden = true
68+
cacheCommand.AddCommand(cacheInfoCmd())
6869

6970
return cacheCommand
7071
}
@@ -108,3 +109,25 @@ func cacheCredentialsCmd() *cobra.Command {
108109
},
109110
}
110111
}
112+
113+
func cacheInfoCmd() *cobra.Command {
114+
return &cobra.Command{
115+
Use: "info",
116+
Short: "Output information about the nix cache",
117+
Args: cobra.ExactArgs(0),
118+
RunE: func(cmd *cobra.Command, args []string) error {
119+
// TODO(gcurtis): We can also output info about the daemon config status
120+
// here
121+
uri, err := nixcache.Get().URI(cmd.Context())
122+
if err != nil {
123+
return err
124+
}
125+
if uri == "" {
126+
fmt.Fprintln(cmd.OutOrStdout(), "No cache configured")
127+
return nil
128+
}
129+
fmt.Fprintln(cmd.OutOrStdout(), "Cache URI:", uri)
130+
return err
131+
},
132+
}
133+
}

internal/devbox/packages.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,8 +452,10 @@ func (d *Devbox) installNixPackagesToStore(ctx context.Context, mode installMode
452452
Flags: flags,
453453
Writer: d.stderr,
454454
}
455-
args.ExtraSubstituter, err = d.providers.NixCache.URI(ctx)
456-
if err == nil {
455+
args.ExtraSubstituter, _ = d.providers.NixCache.URI(ctx)
456+
// TODO (Landau): handle errors that are not auth.ErrNotLoggedIn
457+
// Only lookup credentials if we have a cache to use
458+
if args.ExtraSubstituter != "" {
457459
creds, err := d.providers.NixCache.Credentials(ctx)
458460
if err == nil {
459461
args.Env = creds.Env()

internal/devbox/providers/nixcache/nixcache.go

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -185,22 +185,29 @@ func (*Provider) executable() string {
185185
// private cache.
186186
func (p *Provider) Credentials(ctx context.Context) (AWSCredentials, error) {
187187
cache := filecache.New[AWSCredentials]("devbox/providers/nixcache")
188-
creds, err := cache.GetOrSetWithTime("credentials", func() (AWSCredentials, time.Time, error) {
189-
token, err := identity.Get().GenSession(ctx)
190-
if err != nil {
191-
return AWSCredentials{}, time.Time{}, err
192-
}
193-
client := api.NewClient(ctx, build.JetpackAPIHost(), token)
194-
creds, err := client.GetAWSCredentials(ctx)
195-
if err != nil {
196-
return AWSCredentials{}, time.Time{}, err
197-
}
198-
exp := time.Time{}
199-
if t := creds.GetExpiration(); t != nil {
200-
exp = t.AsTime()
201-
}
202-
return newAWSCredentials(creds), exp, nil
203-
})
188+
token, err := identity.Get().GenSession(ctx)
189+
if err != nil {
190+
return AWSCredentials{}, err
191+
}
192+
creds, err := cache.GetOrSetWithTime(
193+
"credentials-"+token.IDClaims().Subject,
194+
func() (AWSCredentials, time.Time, error) {
195+
token, err := identity.Get().GenSession(ctx)
196+
if err != nil {
197+
return AWSCredentials{}, time.Time{}, err
198+
}
199+
client := api.NewClient(ctx, build.JetpackAPIHost(), token)
200+
creds, err := client.GetAWSCredentials(ctx)
201+
if err != nil {
202+
return AWSCredentials{}, time.Time{}, err
203+
}
204+
exp := time.Time{}
205+
if t := creds.GetExpiration(); t != nil {
206+
exp = t.AsTime()
207+
}
208+
return newAWSCredentials(creds), exp, nil
209+
},
210+
)
204211
if err != nil {
205212
return AWSCredentials{}, redact.Errorf("nixcache: get credentials: %w", redact.Safe(err))
206213
}
@@ -212,21 +219,31 @@ func (p *Provider) Credentials(ctx context.Context) (AWSCredentials, error) {
212219
// and a nil error.
213220
func (p *Provider) URI(ctx context.Context) (string, error) {
214221
cache := filecache.New[string]("devbox/providers/nixcache")
215-
uri, err := cache.GetOrSet("uri", func() (string, time.Duration, error) {
216-
token, err := identity.Get().GenSession(ctx)
217-
if err != nil {
218-
return "", 0, err
219-
}
220-
client := api.NewClient(ctx, build.JetpackAPIHost(), token)
221-
resp, err := client.GetBinCache(ctx)
222-
if err != nil {
223-
return "", 0, redact.Errorf("nixcache: get uri: %w", redact.Safe(err))
224-
}
225-
226-
// TODO(gcurtis): do a better job of invalidating the URI after
227-
// logout or after a Nix command fails to query the cache.
228-
return resp.GetNixBinCacheUri(), 24 * time.Hour, nil
229-
})
222+
token, err := identity.Get().GenSession(ctx)
223+
if err != nil {
224+
return "", err
225+
}
226+
// Landau: I think we can probably remove this cache? This endpoint is very
227+
// fast and we only use this for build/upload which are slow.
228+
uri, err := cache.GetOrSet(
229+
"uri-"+token.IDClaims().Subject,
230+
func() (string, time.Duration, error) {
231+
client := api.NewClient(ctx, build.JetpackAPIHost(), token)
232+
resp, err := client.GetBinCache(ctx)
233+
if err != nil {
234+
return "", 0, redact.Errorf("nixcache: get uri: %w", redact.Safe(err))
235+
}
236+
237+
// Don't cache negative responses.
238+
if resp.GetNixBinCacheUri() == "" {
239+
return "", 0, nil
240+
}
241+
242+
// TODO(gcurtis): do a better job of invalidating the URI after
243+
// a Nix command fails to query the cache.
244+
return resp.GetNixBinCacheUri(), 24 * time.Hour, nil
245+
},
246+
)
230247
if err != nil {
231248
return "", redact.Errorf("nixcache: get uri: %w", redact.Safe(err))
232249
}

0 commit comments

Comments
 (0)