Skip to content

feat: Add external provisioner daemons #4935

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add command to start a provisioner daemon
  • Loading branch information
kylecarbs committed Nov 15, 2022
commit 7dda3a2c59f63cc41142a53c10c27e27bb8639a2
4 changes: 2 additions & 2 deletions cli/deployment/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func newConfig() *codersdk.DeploymentConfig {
Name: "Cache Directory",
Usage: "The directory to cache temporary files. If unspecified and $CACHE_DIRECTORY is set, it will be used for compatibility with systemd.",
Flag: "cache-dir",
Default: defaultCacheDir(),
Default: DefaultCacheDir(),
},
InMemoryDatabase: &codersdk.DeploymentConfigField[bool]{
Name: "In Memory Database",
Expand Down Expand Up @@ -632,7 +632,7 @@ func formatEnv(key string) string {
return "CODER_" + strings.ToUpper(strings.NewReplacer("-", "_", ".", "_").Replace(key))
}

func defaultCacheDir() string {
func DefaultCacheDir() string {
defaultCacheDir, err := os.UserCacheDir()
if err != nil {
defaultCacheDir = os.TempDir()
Expand Down
2 changes: 1 addition & 1 deletion cli/gitaskpass.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func gitAskpass() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

ctx, stop := signal.NotifyContext(ctx, interruptSignals...)
ctx, stop := signal.NotifyContext(ctx, InterruptSignals...)
defer stop()

user, host, err := gitauth.ParseAskpass(args[0])
Expand Down
2 changes: 1 addition & 1 deletion cli/gitssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func gitssh() *cobra.Command {

// Catch interrupt signals to ensure the temporary private
// key file is cleaned up on most cases.
ctx, stop := signal.NotifyContext(ctx, interruptSignals...)
ctx, stop := signal.NotifyContext(ctx, InterruptSignals...)
defer stop()

// Early check so errors are reported immediately.
Expand Down
2 changes: 1 addition & 1 deletion cli/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func Server(vip *viper.Viper, newAPI func(context.Context, *coderd.Options) (*co
//
// To get out of a graceful shutdown, the user can send
// SIGQUIT with ctrl+\ or SIGKILL with `kill -9`.
notifyCtx, notifyStop := signal.NotifyContext(ctx, interruptSignals...)
notifyCtx, notifyStop := signal.NotifyContext(ctx, InterruptSignals...)
defer notifyStop()

// Clean up idle connections at the end, e.g.
Expand Down
2 changes: 1 addition & 1 deletion cli/signal_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"syscall"
)

var interruptSignals = []os.Signal{
var InterruptSignals = []os.Signal{
os.Interrupt,
syscall.SIGTERM,
syscall.SIGHUP,
Expand Down
2 changes: 1 addition & 1 deletion cli/signal_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ import (
"os"
)

var interruptSignals = []os.Signal{os.Interrupt}
var InterruptSignals = []os.Signal{os.Interrupt}
157 changes: 157 additions & 0 deletions enterprise/cli/provisionerdaemons.go
Original file line number Diff line number Diff line change
@@ -1 +1,158 @@
package cli

import (
"context"
"fmt"
"os"
"os/signal"
"strings"
"time"

"cdr.dev/slog"
"cdr.dev/slog/sloggers/sloghuman"
agpl "github.com/coder/coder/cli"
"github.com/coder/coder/cli/cliflag"
"github.com/coder/coder/cli/cliui"
"github.com/coder/coder/cli/deployment"
"github.com/coder/coder/coderd/database"
"github.com/coder/coder/codersdk"
"github.com/coder/coder/provisioner/terraform"
"github.com/coder/coder/provisionerd"
provisionerdproto "github.com/coder/coder/provisionerd/proto"
"github.com/coder/coder/provisionersdk"
"github.com/coder/coder/provisionersdk/proto"

"github.com/spf13/cobra"
"golang.org/x/xerrors"
)

func provisionerDaemons() *cobra.Command {
cmd := &cobra.Command{
Use: "provisionerd",
Short: "Manage provisioner daemons",
}
cmd.AddCommand(provisionerDaemonStart())

return cmd
}

func provisionerDaemonStart() *cobra.Command {
var (
cacheDir string
rawTags []string
)
cmd := &cobra.Command{
Use: "start",
Short: "Run a provisioner daemon",
RunE: func(cmd *cobra.Command, args []string) error {
ctx, cancel := context.WithCancel(cmd.Context())
defer cancel()

notifyCtx, notifyStop := signal.NotifyContext(ctx, agpl.InterruptSignals...)
defer notifyStop()

client, err := agpl.CreateClient(cmd)
if err != nil {
return xerrors.Errorf("create client: %w", err)
}
org, err := agpl.CurrentOrganization(cmd, client)
if err != nil {
return xerrors.Errorf("get current organization: %w", err)
}

tags := map[string]string{}
for _, rawTag := range rawTags {
parts := strings.SplitN(rawTag, "=", 2)
if len(parts) < 2 {
return xerrors.Errorf("invalid tag format for %q. must be key=value", rawTag)
}
tags[parts[0]] = parts[1]
}

err = os.MkdirAll(cacheDir, 0o700)
if err != nil {
return xerrors.Errorf("mkdir %q: %w", cacheDir, err)
}

terraformClient, terraformServer := provisionersdk.TransportPipe()
go func() {
<-ctx.Done()
_ = terraformClient.Close()
_ = terraformServer.Close()
}()

logger := slog.Make(sloghuman.Sink(cmd.ErrOrStderr()))
errCh := make(chan error, 1)
go func() {
defer cancel()

err := terraform.Serve(ctx, &terraform.ServeOptions{
ServeOptions: &provisionersdk.ServeOptions{
Listener: terraformServer,
},
CachePath: cacheDir,
Logger: logger.Named("terraform"),
})
if err != nil && !xerrors.Is(err, context.Canceled) {
select {
case errCh <- err:
default:
}
}
}()

tempDir, err := os.MkdirTemp("", "provisionerd")
if err != nil {
return err
}

provisioners := provisionerd.Provisioners{
string(database.ProvisionerTypeTerraform): proto.NewDRPCProvisionerClient(provisionersdk.Conn(terraformClient)),
}
srv := provisionerd.New(func(ctx context.Context) (provisionerdproto.DRPCProvisionerDaemonClient, error) {
return client.ServeProvisionerDaemon(ctx, org.ID, []codersdk.ProvisionerType{
codersdk.ProvisionerTypeTerraform,
}, tags)
}, &provisionerd.Options{
Logger: logger,
PollInterval: 500 * time.Millisecond,
UpdateInterval: 500 * time.Millisecond,
Provisioners: provisioners,
WorkDirectory: tempDir,
})

var exitErr error
select {
case <-notifyCtx.Done():
exitErr = notifyCtx.Err()
_, _ = fmt.Fprintln(cmd.OutOrStdout(), cliui.Styles.Bold.Render(
"Interrupt caught, gracefully exiting. Use ctrl+\\ to force quit",
))
case exitErr = <-errCh:
}
if exitErr != nil && !xerrors.Is(exitErr, context.Canceled) {
cmd.Printf("Unexpected error, shutting down server: %s\n", exitErr)
}

shutdown, shutdownCancel := context.WithTimeout(ctx, time.Minute)
defer shutdownCancel()
err = srv.Shutdown(shutdown)
if err != nil {
return xerrors.Errorf("shutdown: %w", err)
}

cancel()
if xerrors.Is(exitErr, context.Canceled) {
return nil
}
return exitErr
},
}

cliflag.StringVarP(cmd.Flags(), &cacheDir, "cache-dir", "c", "CODER_CACHE_DIRECTORY", deployment.DefaultCacheDir(),
"Specify a directory to cache provisioner job files.")
cliflag.StringArrayVarP(cmd.Flags(), &rawTags, "tag", "t", "CODER_PROVISIONERD_TAGS", []string{},
"Specify a list of tags to target provisioner jobs.")

return cmd
}
1 change: 1 addition & 0 deletions enterprise/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func enterpriseOnly() []*cobra.Command {
features(),
licenses(),
groups(),
provisionerDaemons(),
}
}

Expand Down
1 change: 1 addition & 0 deletions enterprise/coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func New(ctx context.Context, options *Options) (*API, error) {
})
r.Route("/organizations/{organization}/provisionerdaemons", func(r chi.Router) {
r.Use(
api.provisionerDaemonsEnabledMW,
apiKeyMiddleware,
httpmw.ExtractOrganizationParam(api.Database),
)
Expand Down
6 changes: 3 additions & 3 deletions enterprise/coderd/provisionerdaemons.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ func (api *API) provisionerDaemonsEnabledMW(next http.Handler) http.Handler {
api.entitlementsMu.RUnlock()

if !epd {
httpapi.Write(r.Context(), rw, http.Status)
httpapi.Write(r.Context(), rw, http.StatusForbidden, codersdk.Response{
Message: "External provisioner daemons is an Enterprise feature. Contact sales!",
})
return
}

Expand Down Expand Up @@ -146,8 +148,6 @@ func (api *API) provisionerDaemonServe(rw http.ResponseWriter, r *http.Request)
return
}

fmt.Printf("TAGS %+v\n", daemon.Tags)

rawTags, err := json.Marshal(daemon.Tags)
if err != nil {
httpapi.Write(r.Context(), rw, http.StatusInternalServerError, codersdk.Response{
Expand Down
26 changes: 22 additions & 4 deletions enterprise/coderd/provisionerdaemons_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,26 @@ import (

func TestProvisionerDaemonServe(t *testing.T) {
t.Parallel()
t.Run("NoLicense", func(t *testing.T) {
t.Parallel()
client := coderdenttest.New(t, nil)
user := coderdtest.CreateFirstUser(t, client)
_, err := client.ServeProvisionerDaemon(context.Background(), user.OrganizationID, []codersdk.ProvisionerType{
codersdk.ProvisionerTypeEcho,
}, map[string]string{})
require.Error(t, err)
var apiError *codersdk.Error
require.ErrorAs(t, err, &apiError)
require.Equal(t, http.StatusForbidden, apiError.StatusCode())
})

t.Run("Organization", func(t *testing.T) {
t.Parallel()
client := coderdenttest.New(t, nil)
user := coderdtest.CreateFirstUser(t, client)
coderdenttest.AddLicense(t, client, coderdenttest.LicenseOptions{
ExternalProvisionerDaemons: true,
})
srv, err := client.ServeProvisionerDaemon(context.Background(), user.OrganizationID, []codersdk.ProvisionerType{
codersdk.ProvisionerTypeEcho,
}, map[string]string{})
Expand All @@ -33,6 +49,9 @@ func TestProvisionerDaemonServe(t *testing.T) {
t.Parallel()
client := coderdenttest.New(t, nil)
user := coderdtest.CreateFirstUser(t, client)
coderdenttest.AddLicense(t, client, coderdenttest.LicenseOptions{
ExternalProvisionerDaemons: true,
})
another := coderdtest.CreateAnotherUser(t, client, user.OrganizationID)
_, err := another.ServeProvisionerDaemon(context.Background(), user.OrganizationID, []codersdk.ProvisionerType{
codersdk.ProvisionerTypeEcho,
Expand All @@ -49,6 +68,9 @@ func TestProvisionerDaemonServe(t *testing.T) {
t.Parallel()
client := coderdenttest.New(t, nil)
user := coderdtest.CreateFirstUser(t, client)
coderdenttest.AddLicense(t, client, coderdenttest.LicenseOptions{
ExternalProvisionerDaemons: true,
})
closer := coderdtest.NewExternalProvisionerDaemon(t, client, user.OrganizationID, map[string]string{
provisionerdserver.TagScope: provisionerdserver.ScopeUser,
})
Expand Down Expand Up @@ -115,7 +137,3 @@ func TestProvisionerDaemonServe(t *testing.T) {
coderdtest.AwaitWorkspaceBuildJob(t, client, workspace.LatestBuild.ID)
})
}

func TestPostProvisionerDaemon(t *testing.T) {
t.Parallel()
}