Skip to content
This repository has been archived by the owner on Mar 24, 2023. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'origin/master' into kubectl
Browse files Browse the repository at this point in the history
  • Loading branch information
laverya committed Aug 7, 2018
2 parents 92a1322 + 2b307f0 commit 5f3b4fe
Show file tree
Hide file tree
Showing 35 changed files with 235 additions and 211 deletions.
22 changes: 12 additions & 10 deletions pkg/lifecycle/daemon/actions.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package daemon

func MessageActions() []Action {
return []Action{
import "github.com/replicatedhq/ship/pkg/lifecycle/daemon/daemontypes"

func MessageActions() []daemontypes.Action {
return []daemontypes.Action{
{
ButtonType: "primary",
Text: "Confirm",
LoadingText: "Confirming",
OnClick: ActionRequest{
OnClick: daemontypes.ActionRequest{
URI: "/message/confirm",
Method: "POST",
Body: `{"step_name": "message"}`,
Expand All @@ -15,13 +17,13 @@ func MessageActions() []Action {
}
}

func HelmIntroActions() []Action {
return []Action{
func HelmIntroActions() []daemontypes.Action {
return []daemontypes.Action{
{
ButtonType: "primary",
Text: "Get started",
LoadingText: "Confirming",
OnClick: ActionRequest{
OnClick: daemontypes.ActionRequest{
URI: "/message/confirm",
Method: "POST",
Body: `{"step_name": "helm.intro"}`,
Expand All @@ -30,14 +32,14 @@ func HelmIntroActions() []Action {
}
}

func HelmValuesActions() []Action {
return []Action{
func HelmValuesActions() []daemontypes.Action {
return []daemontypes.Action{
{
Sort: 0,
ButtonType: "primary",
Text: "Save values",
LoadingText: "Saving",
OnClick: ActionRequest{
OnClick: daemontypes.ActionRequest{
URI: "/helm-values",
Method: "POST",
Body: `{"step_name": "helm.values"}`,
Expand All @@ -48,7 +50,7 @@ func HelmValuesActions() []Action {
ButtonType: "primary",
Text: "Continue",
LoadingText: "Continuing",
OnClick: ActionRequest{
OnClick: daemontypes.ActionRequest{
URI: "/message/confirm",
Method: "POST",
Body: `{"step_name": "helm.values"}`,
Expand Down
20 changes: 3 additions & 17 deletions pkg/lifecycle/daemon/constructors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,26 @@ import (
"github.com/go-kit/kit/log"
"github.com/mitchellh/cli"
"github.com/replicatedhq/ship/pkg/filetree"
"github.com/replicatedhq/ship/pkg/lifecycle/daemon/daemontypes"
"github.com/replicatedhq/ship/pkg/lifecycle/render/config/resolve"
"github.com/replicatedhq/ship/pkg/patch"
"github.com/replicatedhq/ship/pkg/state"
"github.com/replicatedhq/ship/pkg/ui"
"github.com/spf13/afero"
"github.com/spf13/viper"
)

func NewHeadlessDaemon(
v *viper.Viper,
logger log.Logger,
renderer *resolve.APIConfigRenderer,
stateManager state.Manager,
) Daemon {
return &HeadlessDaemon{
StateManager: stateManager,
Logger: logger,
UI: ui.FromViper(v),
ConfigRenderer: renderer,
}
}

func NewHeadedDaemon(
logger log.Logger,
v *viper.Viper,
webUIFactory WebUIBuilder,
v1Router *V1Routes,
v2Router *V2Routes,
) Daemon {
) daemontypes.Daemon {
return &ShipDaemon{
Logger: log.With(logger, "struct", "daemon"),
WebUIFactory: webUIFactory,
Viper: v,
exitChan: make(chan error),
ExitChan: make(chan error),
V1Routes: v1Router,
V2Routes: v2Router,
}
Expand Down
43 changes: 9 additions & 34 deletions pkg/lifecycle/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/go-kit/kit/log/level"
"github.com/pkg/errors"
"github.com/replicatedhq/ship/pkg/api"
"github.com/replicatedhq/ship/pkg/lifecycle/daemon/daemontypes"
"github.com/replicatedhq/ship/pkg/version"
"github.com/spf13/viper"
)
Expand All @@ -22,42 +23,16 @@ var (
errInternal = errors.New("internal_error")
)

// Daemon is a sort of UI interface. Some implementations start an API to
// power the on-prem web console. A headless implementation logs progress
// to stdout.
//
// A daemon is manipulated by lifecycle step handlers to present the
// correct UI to the user and collect necessary information
type Daemon interface {
EnsureStarted(context.Context, *api.Release) chan error
PushMessageStep(context.Context, Message, []Action)
PushStreamStep(context.Context, <-chan Message)
PushRenderStep(context.Context, Render)
PushHelmIntroStep(context.Context, HelmIntro, []Action)
PushHelmValuesStep(context.Context, HelmValues, []Action)
PushKustomizeStep(context.Context, Kustomize)
SetStepName(context.Context, string)
AllStepsDone(context.Context)
CleanPreviousStep()
MessageConfirmedChan() chan string
ConfigSavedChan() chan interface{}
TerraformConfirmedChan() chan bool
KustomizeSavedChan() chan interface{}

GetCurrentConfig() map[string]interface{}
SetProgress(Progress)
ClearProgress()
}

var _ Daemon = &ShipDaemon{}
var _ daemontypes.Daemon = &ShipDaemon{}

// Daemon runs the ship api server.
type ShipDaemon struct {
Logger log.Logger
WebUIFactory WebUIBuilder
Viper *viper.Viper
exitChan chan error
startOnce sync.Once
// todo private this
ExitChan chan error
StartOnce sync.Once

*V1Routes
*V2Routes
Expand All @@ -66,13 +41,13 @@ type ShipDaemon struct {
// "this is fine"
func (d *ShipDaemon) EnsureStarted(ctx context.Context, release *api.Release) chan error {

go d.startOnce.Do(func() {
go d.StartOnce.Do(func() {
err := d.Serve(ctx, release)
level.Info(d.Logger).Log("event", "daemon.startonce.exit", err, "err")
d.exitChan <- err
d.ExitChan <- err
})

return d.exitChan
return d.ExitChan
}

// Serve starts the server with the given context
Expand Down Expand Up @@ -112,7 +87,7 @@ func (d *ShipDaemon) Serve(ctx context.Context, release *api.Release) error {

select {
case err := <-errChan:
level.Error(d.Logger).Log("event", "shutdown", "reason", "exitChan", "err", err)
level.Error(d.Logger).Log("event", "shutdown", "reason", "ExitChan", "err", err)
return err
case <-ctx.Done():
level.Error(d.Logger).Log("event", "shutdown", "reason", "context", "err", ctx.Err())
Expand Down
5 changes: 3 additions & 2 deletions pkg/lifecycle/daemon/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/mitchellh/cli"
"github.com/replicatedhq/libyaml"
"github.com/replicatedhq/ship/pkg/api"
"github.com/replicatedhq/ship/pkg/lifecycle/daemon/daemontypes"
"github.com/replicatedhq/ship/pkg/testing/logger"
"github.com/spf13/afero"
"github.com/spf13/viper"
Expand Down Expand Up @@ -132,7 +133,7 @@ func TestDaemonAPI(t *testing.T) {
{
name: "read message after 1st step",
test: func(t *testing.T) {
daemon.PushMessageStep(context.Background(), Message{
daemon.PushMessageStep(context.Background(), daemontypes.Message{
Contents: step1.Message.Contents,
Level: step1.Message.Level,
}, MessageActions())
Expand All @@ -154,7 +155,7 @@ func TestDaemonAPI(t *testing.T) {
name: "confirm message that is not current",
test: func(t *testing.T) {
log := &logger.TestLogger{T: t}
daemon.PushMessageStep(context.Background(), Message{
daemon.PushMessageStep(context.Background(), daemontypes.Message{
Contents: step2.Message.Contents,
Level: step2.Message.Level,
}, MessageActions())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package daemon
package daemontypes

import "encoding/json"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,39 @@
package daemon
package daemontypes

import (
"context"

"github.com/replicatedhq/ship/pkg/api"
"github.com/replicatedhq/ship/pkg/filetree"
)

// Daemon is a sort of UI interface. Some implementations start an API to
// power the on-prem web console. A headless implementation logs progress
// to stdout.
//
// A daemon is manipulated by lifecycle step handlers to present the
// correct UI to the user and collect necessary information
type Daemon interface {
EnsureStarted(context.Context, *api.Release) chan error
PushMessageStep(context.Context, Message, []Action)
PushStreamStep(context.Context, <-chan Message)
PushRenderStep(context.Context, Render)
PushHelmIntroStep(context.Context, HelmIntro, []Action)
PushHelmValuesStep(context.Context, HelmValues, []Action)
PushKustomizeStep(context.Context, Kustomize)
SetStepName(context.Context, string)
AllStepsDone(context.Context)
CleanPreviousStep()
MessageConfirmedChan() chan string
ConfigSavedChan() chan interface{}
TerraformConfirmedChan() chan bool
KustomizeSavedChan() chan interface{}

GetCurrentConfig() map[string]interface{}
SetProgress(Progress)
ClearProgress()
}

const StepNameMessage = "message"
const StepNameConfig = "render.config"
const StepNameHelmIntro = "helm.intro"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package daemon
package headless

import (
"context"
Expand All @@ -9,11 +9,12 @@ import (
"github.com/mitchellh/cli"
"github.com/pkg/errors"
"github.com/replicatedhq/ship/pkg/api"
"github.com/replicatedhq/ship/pkg/lifecycle/daemon/daemontypes"
"github.com/replicatedhq/ship/pkg/lifecycle/render/config/resolve"
"github.com/replicatedhq/ship/pkg/state"
)

var _ Daemon = &HeadlessDaemon{}
var _ daemontypes.Daemon = &HeadlessDaemon{}

type HeadlessDaemon struct {
StateManager state.Manager
Expand All @@ -23,9 +24,23 @@ type HeadlessDaemon struct {
ResolvedConfig map[string]interface{}
}

func (d *HeadlessDaemon) PushKustomizeStep(context.Context, Kustomize) {}
func (d *HeadlessDaemon) PushMessageStep(context.Context, Message, []Action) {}
func (d *HeadlessDaemon) PushRenderStep(context.Context, Render) {}
func NewHeadlessDaemon(
ui cli.Ui,
logger log.Logger,
renderer *resolve.APIConfigRenderer,
stateManager state.Manager,
) daemontypes.Daemon {
return &HeadlessDaemon{
StateManager: stateManager,
Logger: logger,
UI: ui,
ConfigRenderer: renderer,
}
}

func (d *HeadlessDaemon) PushKustomizeStep(context.Context, daemontypes.Kustomize) {}
func (d *HeadlessDaemon) PushMessageStep(context.Context, daemontypes.Message, []daemontypes.Action) {}
func (d *HeadlessDaemon) PushRenderStep(context.Context, daemontypes.Render) {}

func (d *HeadlessDaemon) KustomizeSavedChan() chan interface{} {
ch := make(chan interface{}, 1)
Expand All @@ -34,9 +49,10 @@ func (d *HeadlessDaemon) KustomizeSavedChan() chan interface{} {
return ch
}

func (d *HeadlessDaemon) PushHelmIntroStep(context.Context, HelmIntro, []Action) {}
func (d *HeadlessDaemon) PushHelmIntroStep(context.Context, daemontypes.HelmIntro, []daemontypes.Action) {
}

func (d *HeadlessDaemon) PushHelmValuesStep(ctx context.Context, helmValues HelmValues, actions []Action) {
func (d *HeadlessDaemon) PushHelmValuesStep(ctx context.Context, helmValues daemontypes.HelmValues, actions []daemontypes.Action) {
warn := level.Warn(log.With(d.Logger, "struct", "HeadlessDaemon", "method", "PushHelmValuesStep"))
if err := d.HeadlessSaveHelmValues(ctx, helmValues.Values); err != nil {
warn.Log("event", "push helm values step fail", "err", err)
Expand All @@ -54,7 +70,7 @@ func (d *HeadlessDaemon) HeadlessSaveHelmValues(ctx context.Context, helmValues
return nil
}

func (d *HeadlessDaemon) PushStreamStep(context.Context, <-chan Message) {}
func (d *HeadlessDaemon) PushStreamStep(context.Context, <-chan daemontypes.Message) {}

func (d *HeadlessDaemon) CleanPreviousStep() {}

Expand Down Expand Up @@ -152,7 +168,7 @@ func (d *HeadlessDaemon) HeadlessResolve(ctx context.Context, release *api.Relea
return nil
}

func (d *HeadlessDaemon) SetProgress(progress Progress) {
func (d *HeadlessDaemon) SetProgress(progress daemontypes.Progress) {
d.UI.Output(progress.Detail)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package daemon
package headless

import (
"context"
Expand Down
13 changes: 7 additions & 6 deletions pkg/lifecycle/daemon/routes_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/pkg/errors"
"github.com/replicatedhq/ship/pkg/api"
"github.com/replicatedhq/ship/pkg/filetree"
"github.com/replicatedhq/ship/pkg/lifecycle/daemon/daemontypes"
"github.com/replicatedhq/ship/pkg/lifecycle/render/config/resolve"
"github.com/replicatedhq/ship/pkg/patch"
"github.com/replicatedhq/ship/pkg/state"
Expand All @@ -29,16 +30,16 @@ type V1Routes struct {
OpenWebConsole opener

sync.Mutex
currentStep *Step
currentStep *daemontypes.Step
currentStepName string
currentStepConfirmed bool
stepProgress *Progress
stepProgress *daemontypes.Progress
allStepsDone bool
pastSteps []Step
pastSteps []daemontypes.Step

// this is kind of kludged in,
// it only makes sense for Message steps
currentStepActions []Action
currentStepActions []daemontypes.Action

initConfig sync.Once
ConfigSaved chan interface{}
Expand Down Expand Up @@ -125,7 +126,7 @@ func (d *V1Routes) createOrMergePatch(c *gin.Context) {
}
}

func (d *V1Routes) SetProgress(p Progress) {
func (d *V1Routes) SetProgress(p daemontypes.Progress) {
defer d.locker(log.NewNopLogger())()
d.stepProgress = &p
}
Expand Down Expand Up @@ -227,7 +228,7 @@ func (d *V1Routes) getCurrentStep(c *gin.Context) {
d.currentStep.HelmValues.Values = helmValues
}

result := StepResponse{
result := daemontypes.StepResponse{
CurrentStep: *d.currentStep,
Phase: d.currentStepName,
Actions: d.currentStepActions,
Expand Down
Loading

0 comments on commit 5f3b4fe

Please sign in to comment.