Skip to content

Commit

Permalink
Fully implement revlink warmup before terraform commands
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidS-ovm committed May 13, 2024
1 parent c4b6147 commit 342e06d
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 8 deletions.
72 changes: 68 additions & 4 deletions cmd/tea_revlink.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,36 @@ package cmd

import (
"context"
"fmt"

"connectrpc.com/connect"
tea "github.com/charmbracelet/bubbletea"
"golang.org/x/oauth2"
"github.com/overmindtech/sdp-go"
log "github.com/sirupsen/logrus"
)

type revlinkWarmupFinishedMsg struct{}

type revlinkWarmupModel struct {
taskModel

ctx context.Context // note that this ctx is not initialized on NewGetConfigModel to instead get a modified context through the loadSourcesConfigMsg that has a timeout and cancelFunction configured
oi OvermindInstance
token *oauth2.Token
ctx context.Context // note that this ctx is not initialized on NewGetConfigModel to instead get a modified context through the loadSourcesConfigMsg that has a timeout and cancelFunction configured
oi OvermindInstance
// token *oauth2.Token

status chan *sdp.RevlinkWarmupResponse
currentStatus *sdp.RevlinkWarmupResponse
}

func NewRevlinkWarmupModel() tea.Model {
return revlinkWarmupModel{
taskModel: NewTaskModel("Discover and link all available resources"),
status: make(chan *sdp.RevlinkWarmupResponse),
currentStatus: &sdp.RevlinkWarmupResponse{
Status: "pending",
Items: 0,
Edges: 0,
},
}
}

Expand All @@ -33,8 +47,27 @@ func (m revlinkWarmupModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
cmds := []tea.Cmd{}

switch msg := msg.(type) {
case loadSourcesConfigMsg:
m.ctx = msg.ctx
m.oi = msg.oi
case sourcesInitialisedMsg:
m.taskModel.status = taskStatusRunning
// start the spinner
cmds = append(cmds, m.taskModel.spinner.Tick)
// kick off a revlink warmup
cmds = append(cmds, m.revlinkWarmupCmd)
// process status updates
cmds = append(cmds, m.waitForStatusActivity)
case *sdp.RevlinkWarmupResponse:
m.currentStatus = msg
// wait for the next status update
cmds = append(cmds, m.waitForStatusActivity)
case revlinkWarmupFinishedMsg:
m.taskModel.status = taskStatusDone
case fatalError:
if msg.id == m.spinner.ID() {
m.taskModel.status = taskStatusError
}
default:
var taskCmd tea.Cmd
m.taskModel, taskCmd = m.taskModel.Update(msg)
Expand All @@ -46,6 +79,37 @@ func (m revlinkWarmupModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

func (m revlinkWarmupModel) View() string {
view := m.taskModel.View()
switch m.taskModel.status { //nolint:exhaustive // we only care about running and done
case taskStatusRunning, taskStatusDone:
view += fmt.Sprintf(": %v (%v items, %v edges)", m.currentStatus.GetStatus(), m.currentStatus.GetItems(), m.currentStatus.GetEdges())
}

return view
}

// A command that waits for the activity on the status channel.
func (m revlinkWarmupModel) waitForStatusActivity() tea.Msg {
msg := <-m.status
log.Debugf("waitForStatusActivity received %T: %+v", msg, msg)
return msg
}

func (m revlinkWarmupModel) revlinkWarmupCmd() tea.Msg {
ctx := m.ctx

client := AuthenticatedManagementClient(ctx, m.oi)

stream, err := client.RevlinkWarmup(ctx, &connect.Request[sdp.RevlinkWarmupRequest]{
Msg: &sdp.RevlinkWarmupRequest{},
})

if err != nil {
return fatalError{id: m.spinner.ID(), err: fmt.Errorf("error warming up revlink: %w", err)}
}

for stream.Receive() {
m.status <- stream.Msg()
}

return revlinkWarmupFinishedMsg{}
}
4 changes: 2 additions & 2 deletions cmd/terraform_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var terraformApplyCmd = &cobra.Command{
log.WithError(err).Fatal("could not bind `terraform apply` flags")
}
},
Run: CmdWrapper("apply", []string{"changes:write", "config:write", "request:receive"}, NewTfApplyModel),
Run: CmdWrapper("apply", []string{"explore:read", "changes:write", "config:write", "request:receive"}, NewTfApplyModel),
}

type tfApplyModel struct {
Expand Down Expand Up @@ -98,7 +98,7 @@ func (m tfApplyModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.ctx = msg.ctx
m.oi = msg.oi

case sourcesInitialisedMsg:
case revlinkWarmupFinishedMsg:
m.isStarting = true
return m, tea.Batch(
m.startingChangeSnapshot.Init(),
Expand Down
4 changes: 2 additions & 2 deletions cmd/terraform_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var terraformPlanCmd = &cobra.Command{
log.WithError(err).Fatal("could not bind `terraform plan` flags")
}
},
Run: CmdWrapper("plan", []string{"changes:write", "config:write", "request:receive"}, NewTfPlanModel),
Run: CmdWrapper("plan", []string{"explore:read", "changes:write", "config:write", "request:receive"}, NewTfPlanModel),
}

type OvermindCommandHandler func(ctx context.Context, args []string, oi OvermindInstance, token *oauth2.Token) error
Expand Down Expand Up @@ -282,7 +282,7 @@ func (m tfPlanModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.ctx = msg.ctx
m.oi = msg.oi

case sourcesInitialisedMsg:
case revlinkWarmupFinishedMsg:
m.runTfPlan = true
m.planTask.status = taskStatusRunning
// defer the actual command to give the view a chance to show the header
Expand Down
6 changes: 6 additions & 0 deletions demos/plan.tape
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ Set Margin 20
Set MarginFill "#7a70eb" # use Dark.BgMain
Set BorderRadius 10

Set Width 1200
Set Height 900

Set FontSize 15

Hide
Type "cd tmp"
Enter
Expand All @@ -25,6 +30,7 @@ Enter
Type "sso-dogfood"
Sleep 1
Enter
Sleep 60
Sleep 20

# Ctrl+c
Expand Down

0 comments on commit 342e06d

Please sign in to comment.