-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathmain.go
104 lines (94 loc) · 2.84 KB
/
main.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
package main
import (
"errors"
"fmt"
"net/url"
"os"
"slices"
"strings"
"github.com/coder/envbuilder/options"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/envbuilder"
"github.com/coder/envbuilder/log"
"github.com/coder/serpent"
// *Never* remove this. Certificates are not bundled as part
// of the container, so this is necessary for all connections
// to not be insecure.
_ "github.com/breml/rootcerts"
)
func main() {
cmd := envbuilderCmd()
err := cmd.Invoke().WithOS().Run()
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error: %v", err)
os.Exit(1)
}
}
func envbuilderCmd() serpent.Command {
var o options.Options
cmd := serpent.Command{
Use: "envbuilder",
Options: o.CLI(),
Handler: func(inv *serpent.Invocation) error {
o.SetDefaults()
var preExecs []func()
preExec := func() {
for _, fn := range preExecs {
fn()
}
preExecs = nil
}
defer preExec() // Ensure cleanup in case of error.
o.Logger = log.New(os.Stderr, o.Verbose)
if o.CoderAgentURL != "" {
if o.CoderAgentToken == "" {
return errors.New("CODER_AGENT_URL must be set if CODER_AGENT_TOKEN is set")
}
u, err := url.Parse(o.CoderAgentURL)
if err != nil {
return fmt.Errorf("unable to parse CODER_AGENT_URL as URL: %w", err)
}
coderLog, closeLogs, err := log.Coder(inv.Context(), u, o.CoderAgentToken)
if err == nil {
o.Logger = log.Wrap(o.Logger, coderLog)
preExecs = append(preExecs, func() {
closeLogs()
})
// This adds the envbuilder subsystem.
// If telemetry is enabled in a Coder deployment,
// this will be reported and help us understand
// envbuilder usage.
if !slices.Contains(o.CoderAgentSubsystem, string(codersdk.AgentSubsystemEnvbuilder)) {
o.CoderAgentSubsystem = append(o.CoderAgentSubsystem, string(codersdk.AgentSubsystemEnvbuilder))
_ = os.Setenv("CODER_AGENT_SUBSYSTEM", strings.Join(o.CoderAgentSubsystem, ","))
}
} else {
// Failure to log to Coder should cause a fatal error.
o.Logger(log.LevelError, "unable to send logs to Coder: %s", err.Error())
}
}
if o.GitSSHPrivateKeyPath != "" && o.GitSSHPrivateKeyBase64 != "" {
return errors.New("cannot have both GIT_SSH_PRIVATE_KEY_PATH and GIT_SSH_PRIVATE_KEY_BASE64 set")
}
if o.GetCachedImage {
img, err := envbuilder.RunCacheProbe(inv.Context(), o)
if err != nil {
o.Logger(log.LevelError, "error: %s", err)
return err
}
digest, err := img.Digest()
if err != nil {
return fmt.Errorf("get cached image digest: %w", err)
}
_, _ = fmt.Fprintf(inv.Stdout, "ENVBUILDER_CACHED_IMAGE=%s@%s\n", o.CacheRepo, digest.String())
return nil
}
err := envbuilder.Run(inv.Context(), o, preExec)
if err != nil {
o.Logger(log.LevelError, "error: %s", err)
}
return err
},
}
return cmd
}