Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
leg100 committed Apr 1, 2024
1 parent 0490bc8 commit c7c3505
Show file tree
Hide file tree
Showing 21 changed files with 414 additions and 324 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ messages.log
.terraform
.terraform.lock.hcl
*.tfstate
*.tfstate.backup
*.tfstate.*
# pug artifacts
pug.yaml
pug
Expand Down
8 changes: 5 additions & 3 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ func Start(args []string) error {
TaskService: tasks,
})
runs := run.NewService(run.ServiceOptions{
TaskService: tasks,
ModuleService: modules,
WorkspaceService: workspaces,
TaskService: tasks,
ModuleService: modules,
WorkspaceService: workspaces,
StateService: states,
DisableReloadAfterApply: cfg.DisableReloadAfterApply,
})

// Construct TUI programme.
Expand Down
15 changes: 9 additions & 6 deletions internal/app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ import (
)

type config struct {
Program string
MaxTasks int
PluginCache bool
LogLevel string
FirstPage string
Debug bool
Program string
MaxTasks int
PluginCache bool
LogLevel string
FirstPage string
Debug bool
DisableReloadAfterApply bool

version bool
}
Expand All @@ -36,6 +37,8 @@ func parse(args []string) (config, error) {
fs.StringEnumVar(&cfg.LogLevel, 'l', "log-level", "Logging level.", "info", "debug", "error", "warn")
_ = fs.String('c', "config", "pug.yaml", "Path to config file.")

fs.BoolVar(&cfg.DisableReloadAfterApply, 0, "disable-reload-after-apply", "Disable automatic reload of state following an apply.")

// Plugin cache is enabled not via pug flags but via terraform config
tfcfg, _ := cliconfig.LoadConfig()
cfg.PluginCache = (tfcfg.PluginCacheDir != "")
Expand Down
6 changes: 3 additions & 3 deletions internal/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ func (r *Run) IsFinished() bool {

func (r *Run) LogValue() slog.Value {
return slog.GroupValue(
slog.String("id", r.ID.String()),
slog.String("workspace", r.ID.String()),
slog.String("module", r.ID.String()),
slog.String("id", r.String()),
slog.String("workspace", r.String()),
slog.String("module", r.String()),
)
}

Expand Down
25 changes: 20 additions & 5 deletions internal/run/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/leg100/pug/internal/module"
"github.com/leg100/pug/internal/pubsub"
"github.com/leg100/pug/internal/resource"
"github.com/leg100/pug/internal/state"
"github.com/leg100/pug/internal/task"
"github.com/leg100/pug/internal/workspace"
)
Expand All @@ -21,22 +22,30 @@ type Service struct {
tasks *task.Service
modules *module.Service
workspaces *workspace.Service
states *state.Service

DisableReloadAfterApply bool
}

type ServiceOptions struct {
TaskService *task.Service
ModuleService *module.Service
WorkspaceService *workspace.Service
StateService *state.Service

DisableReloadAfterApply bool
}

func NewService(opts ServiceOptions) *Service {
broker := pubsub.NewBroker[*Run]()
return &Service{
table: resource.NewTable(broker),
Broker: broker,
tasks: opts.TaskService,
modules: opts.ModuleService,
workspaces: opts.WorkspaceService,
table: resource.NewTable(broker),
Broker: broker,
tasks: opts.TaskService,
modules: opts.ModuleService,
workspaces: opts.WorkspaceService,
states: opts.StateService,
DisableReloadAfterApply: opts.DisableReloadAfterApply,
}
}

Expand Down Expand Up @@ -160,6 +169,12 @@ func (s *Service) Apply(runID resource.ID) (*task.Task, error) {
}
run.ApplyReport = report
run.updateStatus(Applied)
slog.Info("applied plan", "run", run)

if !s.DisableReloadAfterApply {
slog.Info("reloading state following apply", "run", run)
s.states.Reload(run.WorkspaceID())
}
},
})
if err != nil {
Expand Down
25 changes: 23 additions & 2 deletions internal/state/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package state
import (
"context"
"encoding/json"
"errors"
"log/slog"
"slices"

Expand Down Expand Up @@ -39,11 +40,30 @@ func NewService(ctx context.Context, opts ServiceOptions) *Service {
cache: resource.NewTable(broker),
broker: broker,
}

// Whenever a workspace is added, pull its state
go func() {
for event := range opts.WorkspaceService.Subscribe(ctx) {
if event.Type == resource.CreatedEvent {
_, _ = svc.Reload(event.Payload.ID)
}
}
}()

return svc
}

// Reload re-populates the local cache of resources for the state of the given
// workspace, and returns those resources. Synchronous operation.
// Get retrieves the state for a workspace.
func (s *Service) Get(workspaceID resource.ID) (*State, error) {
state, err := s.cache.Get(workspaceID)
if errors.Is(err, resource.ErrNotFound) {
return EmptyState(workspaceID), nil
}
return state, err
}

// Reload creates a task to repopulate the local cache of the state of the given
// workspace.
func (s *Service) Reload(workspaceID resource.ID) (*task.Task, error) {
err := s.updateStateStatus(workspaceID, func(existing *State) error {
return existing.startReload()
Expand Down Expand Up @@ -110,6 +130,7 @@ func (s *Service) Delete(workspaceID resource.ID, addrs ...ResourceAddress) (*ta
},
AfterError: func(t *task.Task) {
s.updateResourceStatus(workspaceID, Idle, addrs...)
slog.Error("deleting resources", "error", t.Err, "resources", addrs)
},
AfterCanceled: func(t *task.Task) {
s.updateResourceStatus(workspaceID, Idle, addrs...)
Expand Down
7 changes: 7 additions & 0 deletions internal/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ const (
ReloadingState = "reloading"
)

func EmptyState(workspaceID resource.ID) *State {
return &State{
WorkspaceID: workspaceID,
State: ReloadingState,
}
}

func NewState(workspaceID resource.ID, file StateFile) *State {
return &State{
WorkspaceID: workspaceID,
Expand Down
Loading

0 comments on commit c7c3505

Please sign in to comment.