Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 87cd77c

Browse files
committed
feat: Support caching provisioner assets
This caches the Terraform binary, and Terraform plugins. Eventually, it could cache other temporary files.
1 parent c31d9cd commit 87cd77c

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

cli/start.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"net/url"
1414
"os"
1515
"os/signal"
16+
"path/filepath"
1617
"strconv"
1718
"time"
1819

@@ -42,6 +43,7 @@ func start() *cobra.Command {
4243
var (
4344
accessURL string
4445
address string
46+
cacheDir string
4547
dev bool
4648
postgresURL string
4749
provisionerDaemonCount uint8
@@ -164,7 +166,7 @@ func start() *cobra.Command {
164166

165167
provisionerDaemons := make([]*provisionerd.Server, 0)
166168
for i := uint8(0); i < provisionerDaemonCount; i++ {
167-
daemonClose, err := newProvisionerDaemon(cmd.Context(), client, logger)
169+
daemonClose, err := newProvisionerDaemon(cmd.Context(), client, logger, cacheDir)
168170
if err != nil {
169171
return xerrors.Errorf("create provisioner daemon: %w", err)
170172
}
@@ -312,6 +314,12 @@ func start() *cobra.Command {
312314
}
313315
root.Flags().StringVarP(&accessURL, "access-url", "", os.Getenv("CODER_ACCESS_URL"), "Specifies the external URL to access Coder (uses $CODER_ACCESS_URL).")
314316
root.Flags().StringVarP(&address, "address", "a", defaultAddress, "The address to serve the API and dashboard (uses $CODER_ADDRESS).")
317+
// systemd uses the CACHE_DIRECTORY environment variable!
318+
defaultCacheDir := os.Getenv("CACHE_DIRECTORY")
319+
if defaultCacheDir == "" {
320+
defaultCacheDir = filepath.Join(os.TempDir(), ".coder-cache")
321+
}
322+
root.Flags().StringVarP(&cacheDir, "cache-dir", "", defaultCacheDir, "Specify a directory to cache binaries for provision operations.")
315323
defaultDev, _ := strconv.ParseBool(os.Getenv("CODER_DEV_MODE"))
316324
root.Flags().BoolVarP(&dev, "dev", "", defaultDev, "Serve Coder in dev mode for tinkering (uses $CODER_DEV_MODE).")
317325
root.Flags().StringVarP(&postgresURL, "postgres-url", "", "",
@@ -381,14 +389,15 @@ func createFirstUser(cmd *cobra.Command, client *codersdk.Client, cfg config.Roo
381389
return nil
382390
}
383391

384-
func newProvisionerDaemon(ctx context.Context, client *codersdk.Client, logger slog.Logger) (*provisionerd.Server, error) {
392+
func newProvisionerDaemon(ctx context.Context, client *codersdk.Client, logger slog.Logger, cacheDir string) (*provisionerd.Server, error) {
385393
terraformClient, terraformServer := provisionersdk.TransportPipe()
386394
go func() {
387395
err := terraform.Serve(ctx, &terraform.ServeOptions{
388396
ServeOptions: &provisionersdk.ServeOptions{
389397
Listener: terraformServer,
390398
},
391-
Logger: logger,
399+
CachePath: cacheDir,
400+
Logger: logger,
392401
})
393402
if err != nil {
394403
panic(err)

coder.service

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ PrivateTmp=yes
1616
PrivateDevices=yes
1717
SecureBits=keep-caps
1818
AmbientCapabilities=CAP_IPC_LOCK
19+
CacheDirectory=coder
1920
CapabilityBoundingSet=CAP_SYSLOG CAP_IPC_LOCK CAP_NET_BIND_SERVICE
2021
NoNewPrivileges=yes
2122
ExecStart=/usr/bin/coder start

provisioner/terraform/provision.go

+8
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ func (t *terraform) Provision(stream proto.DRPCProvisioner_ProvisionStream) erro
8787
})
8888
}
8989
}()
90+
if t.cachePath != "" {
91+
err = terraform.SetEnv(map[string]string{
92+
"TF_PLUGIN_CACHE_DIR": t.cachePath,
93+
})
94+
if err != nil {
95+
return xerrors.Errorf("set terraform plugin cache dir: %w", err)
96+
}
97+
}
9098
terraform.SetStdout(writer)
9199
t.logger.Debug(shutdown, "running initialization")
92100
err = terraform.Init(shutdown)

provisioner/terraform/serve.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type ServeOptions struct {
3434
// BinaryPath specifies the "terraform" binary to use.
3535
// If omitted, the $PATH will attempt to find it.
3636
BinaryPath string
37+
CachePath string
3738
Logger slog.Logger
3839
}
3940

@@ -43,8 +44,9 @@ func Serve(ctx context.Context, options *ServeOptions) error {
4344
binaryPath, err := exec.LookPath("terraform")
4445
if err != nil {
4546
installer := &releases.ExactVersion{
46-
Product: product.Terraform,
47-
Version: version.Must(version.NewVersion("1.1.7")),
47+
InstallDir: options.CachePath,
48+
Product: product.Terraform,
49+
Version: version.Must(version.NewVersion("1.1.7")),
4850
}
4951

5052
execPath, err := installer.Install(ctx)
@@ -58,11 +60,13 @@ func Serve(ctx context.Context, options *ServeOptions) error {
5860
}
5961
return provisionersdk.Serve(ctx, &terraform{
6062
binaryPath: options.BinaryPath,
63+
cachePath: options.CachePath,
6164
logger: options.Logger,
6265
}, options.ServeOptions)
6366
}
6467

6568
type terraform struct {
6669
binaryPath string
70+
cachePath string
6771
logger slog.Logger
6872
}

0 commit comments

Comments
 (0)