-
Notifications
You must be signed in to change notification settings - Fork 9
/
debugshell.go
135 lines (111 loc) · 3.66 KB
/
debugshell.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package cmd
import (
"context"
"os"
"path/filepath"
"github.com/spf13/cobra"
"namespacelabs.dev/foundation/internal/artifacts/oci"
"namespacelabs.dev/foundation/internal/build/assets"
"namespacelabs.dev/foundation/internal/build/binary"
"namespacelabs.dev/foundation/internal/cli/fncobra"
"namespacelabs.dev/foundation/internal/compute"
"namespacelabs.dev/foundation/internal/debugshell"
"namespacelabs.dev/foundation/internal/fnfs"
"namespacelabs.dev/foundation/internal/parsing"
"namespacelabs.dev/foundation/internal/runtime"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/cfg"
"namespacelabs.dev/foundation/std/pkggraph"
"namespacelabs.dev/go-ids"
)
func NewDebugShellCmd() *cobra.Command {
var imageRef, binaryPackage string
cmd := &cobra.Command{
Use: "debug-shell [--image <image-id>] [--binary_package <path/to/package>]",
Short: "Starts a debug shell in the runtime in the specified environment (e.g. kubernetes cluster).",
Args: cobra.NoArgs,
}
cmd.Flags().StringVar(&imageRef, "image", imageRef, "If specified, use this image as the basis of the debug shell.")
cmd.Flags().StringVar(&binaryPackage, "binary_package", binaryPackage, "If specified, use the resulting image binary as the basis of the debug shell.")
return fncobra.CmdWithEnv(cmd, func(ctx context.Context, env cfg.Context, args []string) error {
var imageID oci.ImageID
planner, err := runtime.PlannerFor(ctx, env)
if err != nil {
return err
}
platforms, err := planner.TargetPlatforms(ctx)
if err != nil {
return err
}
pl := parsing.NewPackageLoader(env)
loc, err := cwdLoc(env)
if err != nil {
return err
}
switch {
case imageRef != "":
var err error
imageID, err = oci.ParseImageID(imageRef)
if err != nil {
return err
}
case binaryPackage != "":
binaryRef, err := schema.ParsePackageRef(loc.AsPackageName(), binaryPackage)
if err != nil {
return err
}
pkg, err := pl.LoadByName(ctx, binaryRef.AsPackageName())
if err != nil {
return err
}
sealedCtx := pkggraph.MakeSealedContext(env, pl.Seal())
prepared, err := binary.Plan(ctx, pkg, binaryRef.Name, sealedCtx, assets.AvailableBuildAssets{}, binary.BuildImageOpts{Platforms: platforms, UsePrebuilts: true})
if err != nil {
return err
}
imageID, err = binary.EnsureImage(ctx, sealedCtx, planner.Registry(), prepared)
if err != nil {
return err
}
default:
tag := planner.Registry().AllocateName(env.Workspace().ModuleName()+"/debug", "")
sealedCtx := pkggraph.MakeSealedContext(env, pl.Seal())
img, err := debugshell.Image(ctx, sealedCtx, platforms, tag)
if err != nil {
return err
}
imageID, err = compute.GetValue(ctx, img)
if err != nil {
return err
}
}
return runtime.RunAttachedStdio(ctx, env, planner, runtime.DeployableSpec{
PackageRef: schema.MakePackageRef(loc.AsPackageName(), "debug"),
Class: schema.DeployableClass_ONESHOT,
Name: "debug",
Id: ids.NewRandomBase32ID(8),
Attachable: runtime.AttachableKind_WITH_TTY,
MainContainer: runtime.ContainerRunOpts{
Image: imageID,
Command: []string{"bash"},
},
})
})
}
func cwdLoc(env cfg.Context) (*fnfs.Location, error) {
cwd, err := os.Getwd()
if err != nil {
return nil, err
}
relCwd, err := filepath.Rel(env.Workspace().LoadedFrom().AbsPath, cwd)
if err != nil {
return nil, err
}
return &fnfs.Location{
ModuleName: env.Workspace().ModuleName(),
RelPath: relCwd,
}, nil
}