Skip to content

Commit

Permalink
alpha: postgres deployment, new options and customizations in schema
Browse files Browse the repository at this point in the history
  • Loading branch information
23doors committed Oct 20, 2021
1 parent a24fe01 commit 26b59d5
Show file tree
Hide file tree
Showing 16 changed files with 104 additions and 244 deletions.
1 change: 1 addition & 0 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func (e *Executor) newDeployCmd() *cobra.Command {
f.BoolVar(&opts.Verify, "verify", false, "verify existing resources")
f.BoolVar(&opts.Destroy, "destroy", false, "destroy all existing resources")
f.BoolVar(&opts.SkipBuild, "skip-build", false, "skip build command before deploy")
f.BoolVar(&opts.Lock, "lock", true, "lock statefile during deploy")

return cmd
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ require (
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/otiai10/copy v1.6.0
github.com/outblocks/outblocks-plugin-go v0.0.0-20210929140822-b6ae636b0938
github.com/outblocks/outblocks-plugin-go v0.0.0-20211020133857-9e89f6229684
github.com/pelletier/go-toml v1.9.1 // indirect
github.com/pterm/pterm v0.12.29
github.com/spf13/afero v1.6.0 // indirect
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -623,10 +623,8 @@ github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6
github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo=
github.com/otiai10/mint v1.3.2 h1:VYWnrP5fXmz1MXvjuUvcBrXSjGE6xjON+axB/UrpO3E=
github.com/otiai10/mint v1.3.2/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc=
github.com/outblocks/outblocks-plugin-go v0.0.0-20210923144142-1768210d95b8 h1:2r2WxYCZHD/14cyY/ZSfJ98fjfO/8OQ6bVm+Iqi+btQ=
github.com/outblocks/outblocks-plugin-go v0.0.0-20210923144142-1768210d95b8/go.mod h1:50dr9Bbwu7d8RpOAIU4+lS+wTchPBuAmPlt/D9gRgWA=
github.com/outblocks/outblocks-plugin-go v0.0.0-20210929140822-b6ae636b0938 h1:aAfjeH5YcD3bLLlQ6BQNs1ijR2pSMYmc4s/QK74ztCQ=
github.com/outblocks/outblocks-plugin-go v0.0.0-20210929140822-b6ae636b0938/go.mod h1:50dr9Bbwu7d8RpOAIU4+lS+wTchPBuAmPlt/D9gRgWA=
github.com/outblocks/outblocks-plugin-go v0.0.0-20211020133857-9e89f6229684 h1:tmCQ7vXRZ0A3fyzRi3gKqtYylM54X1m7vL7byZhGI9A=
github.com/outblocks/outblocks-plugin-go v0.0.0-20211020133857-9e89f6229684/go.mod h1:50dr9Bbwu7d8RpOAIU4+lS+wTchPBuAmPlt/D9gRgWA=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pelletier/go-toml v1.8.1/go.mod h1:T2/BmBdy8dvIRq1a/8aqjN41wvWlN4lrapLU/GW4pbc=
Expand Down
44 changes: 0 additions & 44 deletions internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,47 +45,3 @@ func CopyMapStringString(m map[string]string) map[string]string {

return out
}

func MergeMaps(a ...map[string]interface{}) map[string]interface{} {
if len(a) == 0 {
return nil
}

out := make(map[string]interface{}, len(a[0]))
for k, v := range a[0] {
out[k] = v
}

for _, m := range a[1:] {
for k, v := range m {
if v, ok := v.(map[string]interface{}); ok {
if bv, ok := out[k]; ok {
if bv, ok := bv.(map[string]interface{}); ok {
out[k] = MergeMaps(bv, v)
continue
}
}
}

out[k] = v
}
}

return out
}

func MergeStringMaps(a ...map[string]string) map[string]string {
if len(a) == 0 {
return nil
}

out := make(map[string]string, len(a[0]))

for _, m := range a {
for k, v := range m {
out[k] = v
}
}

return out
}
16 changes: 10 additions & 6 deletions pkg/actions/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (

dockerclient "github.com/docker/docker/client"
"github.com/outblocks/outblocks-cli/internal/urlutil"
"github.com/outblocks/outblocks-cli/internal/util"
"github.com/outblocks/outblocks-cli/pkg/config"
"github.com/outblocks/outblocks-cli/pkg/logger"
"github.com/outblocks/outblocks-cli/pkg/plugins"
"github.com/outblocks/outblocks-cli/pkg/plugins/client"
plugin_go "github.com/outblocks/outblocks-plugin-go"
"github.com/outblocks/outblocks-plugin-go/types"
plugin_util "github.com/outblocks/outblocks-plugin-go/util"
"github.com/outblocks/outblocks-plugin-go/util/errgroup"
"github.com/pterm/pterm"
)
Expand All @@ -43,6 +43,7 @@ type DeployOptions struct {
Verify bool
Destroy bool
SkipBuild bool
Lock bool
}

func NewDeploy(log logger.Logger, cfg *config.Project, opts *DeployOptions) *Deploy {
Expand All @@ -65,7 +66,7 @@ func (d *Deploy) Run(ctx context.Context) error {
}

// Get state.
stateRes, err := getState(ctx, d.cfg)
stateRes, err := getState(ctx, d.cfg, d.opts.Lock)
if err != nil {
return err
}
Expand Down Expand Up @@ -261,7 +262,7 @@ func saveState(cfg *config.Project, data *types.StateData) (*plugin_go.SaveState
return plug.Client().SaveState(ctx, data, state.Type, state.Other)
}

func getState(ctx context.Context, cfg *config.Project) (*plugin_go.GetStateResponse, error) {
func getState(ctx context.Context, cfg *config.Project, lock bool) (*plugin_go.GetStateResponse, error) {
state := cfg.State
plug := state.Plugin()

Expand All @@ -276,7 +277,7 @@ func getState(ctx context.Context, cfg *config.Project) (*plugin_go.GetStateResp
}, nil
}

ret, err := plug.Client().GetState(ctx, state.Type, state.Other, false, client.YAMLContext{
ret, err := plug.Client().GetState(ctx, state.Type, state.Other, lock, client.YAMLContext{
Prefix: "$.state",
Data: cfg.YAMLData(),
})
Expand All @@ -294,7 +295,7 @@ func calculatePlanMap(cfg *config.Project) map[*plugins.Plugin]*planParams {
dnsPlugin := app.DNSPlugin()
deployPlugin := app.DeployPlugin()
appType := app.PluginType()
deployProps := util.MergeMaps(cfg.Defaults.Deploy.Other, app.DeployInfo().Other)
deployProps := plugin_util.MergeMaps(cfg.Defaults.Deploy.Other, app.DeployInfo().Other)

includeDNS := dnsPlugin != nil && dnsPlugin == deployPlugin

Expand All @@ -308,6 +309,7 @@ func calculatePlanMap(cfg *config.Project) map[*plugins.Plugin]*planParams {
IsDeploy: true,
IsDNS: includeDNS,
App: appType,
Env: plugin_util.MergeStringMaps(cfg.Defaults.Run.Env, app.Env(), app.DeployInfo().Env),
Properties: deployProps,
}

Expand Down Expand Up @@ -346,7 +348,9 @@ func calculatePlanMap(cfg *config.Project) map[*plugins.Plugin]*planParams {
}
}

planMap[p].deps = append(planMap[p].deps, &types.DependencyPlan{Dependency: t})
planMap[p].deps = append(planMap[p].deps, &types.DependencyPlan{
Dependency: t,
})
}

return planMap
Expand Down
2 changes: 2 additions & 0 deletions pkg/actions/deploy_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ func (d *Deploy) buildApps(ctx context.Context) error {
prog, _ := d.log.ProgressBar().WithTotal(len(builders)).WithTitle("Building apps...").Start()

for _, b := range builders {
b := b

g.Go(func() error {
err := b.build()
if err != nil {
Expand Down
11 changes: 5 additions & 6 deletions pkg/actions/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"time"

"github.com/outblocks/outblocks-cli/internal/urlutil"
"github.com/outblocks/outblocks-cli/internal/util"
"github.com/outblocks/outblocks-cli/pkg/actions/run"
"github.com/outblocks/outblocks-cli/pkg/clipath"
"github.com/outblocks/outblocks-cli/pkg/config"
Expand Down Expand Up @@ -164,8 +163,8 @@ func (d *Run) prepareRun(cfg *config.Project) (*runInfo, error) {
IP: loopbackIP,
Port: appPort,
Command: runInfo.Command,
Env: runInfo.Env,
Properties: util.MergeMaps(cfg.Defaults.Run.Other, runInfo.Other),
Env: plugin_util.MergeStringMaps(app.Env(), runInfo.Env),
Properties: plugin_util.MergeMaps(cfg.Defaults.Run.Other, runInfo.Other),
}

info.apps = append(info.apps, appRun)
Expand Down Expand Up @@ -204,7 +203,7 @@ func (d *Run) prepareRun(cfg *config.Project) (*runInfo, error) {
Dependency: depType,
IP: loopbackIP,
Port: depPort,
Properties: util.MergeMaps(cfg.Defaults.Run.Other, dep.Run.Other),
Properties: plugin_util.MergeMaps(cfg.Defaults.Run.Other, dep.Run.Other),
}

info.deps = append(info.deps, depRun)
Expand Down Expand Up @@ -252,14 +251,14 @@ func (d *Run) prepareRun(cfg *config.Project) (*runInfo, error) {

// Fill envs per app/dep.
for _, app := range info.apps {
app.Env = util.MergeStringMaps(cfg.Defaults.Run.Env, app.Env, env)
app.Env = plugin_util.MergeStringMaps(cfg.Defaults.Run.Env, app.Env, env)

app.Env["URL"] = app.URL
app.Env["PORT"] = strconv.Itoa(app.Port)
}

for _, dep := range info.deps {
dep.Env = util.MergeStringMaps(cfg.Defaults.Run.Env, dep.Env, env)
dep.Env = plugin_util.MergeStringMaps(cfg.Defaults.Run.Env, dep.Env, env)

dep.Env["IP"] = dep.IP
dep.Env["PORT"] = strconv.Itoa(dep.Port)
Expand Down
4 changes: 2 additions & 2 deletions pkg/cli/values/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
"strings"

"github.com/goccy/go-yaml"
"github.com/outblocks/outblocks-cli/internal/util"
"github.com/outblocks/outblocks-cli/pkg/getter"
"github.com/outblocks/outblocks-cli/pkg/strvals"
plugin_util "github.com/outblocks/outblocks-plugin-go/util"
)

type Options struct {
Expand Down Expand Up @@ -54,7 +54,7 @@ func (opts *Options) MergeValues(ctx context.Context, root string, p getter.Prov
return nil, fmt.Errorf("failed to parse %s: %w", filePath, err)
}
// Merge with the previous map
base = util.MergeMaps(base, currentMap)
base = plugin_util.MergeMaps(base, currentMap)
}

// User specified a value via --set
Expand Down
7 changes: 7 additions & 0 deletions pkg/config/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type App interface {
Name() string
Dir() string
URL() *url.URL
Env() map[string]string
PathRedirect() string
Normalize(cfg *Project) error
Check(cfg *Project) error
Expand All @@ -45,6 +46,7 @@ type BasicApp struct {
AppType string `json:"type"`
AppURL string `json:"url"`
AppPathRedirect string `json:"pathRedirect"`
AppEnv map[string]string `json:"env"`
AppDir string `json:"dir"`
AppRun *AppRun `json:"run"`
AppDeploy *AppDeploy `json:"deploy"`
Expand All @@ -69,6 +71,7 @@ type AppRun struct {

type AppDeploy struct {
Plugin string `json:"plugin,omitempty"`
Env map[string]string `json:"env,omitempty"`
Other map[string]interface{} `yaml:"-,remain"`
}

Expand Down Expand Up @@ -288,3 +291,7 @@ func (a *BasicApp) DeployInfo() *AppDeploy {
func (a *BasicApp) PathRedirect() string {
return a.AppPathRedirect
}

func (a *BasicApp) Env() map[string]string {
return a.AppEnv
}
10 changes: 8 additions & 2 deletions pkg/config/app_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,20 @@ const (

type ServiceApp struct {
BasicApp `json:",inline"`
Build *ServiceAppBuild `json:"build,omitempty"`
DockerHash string `json:"-"`
Build *ServiceAppBuild `json:"build,omitempty"`
Container *ServiceAppContainer `json:"container,omitempty"`
DockerHash string `json:"-"`
}

type ServiceAppBuild struct {
Dockerfile string `json:"dockerfile,omitempty"`
DockerContext string `json:"context,omitempty"`
}

type ServiceAppContainer struct {
Port int `json:"port,omitempty"`
}

func LoadServiceAppData(path string, data []byte) (App, error) {
out := &ServiceApp{
BasicApp: BasicApp{
Expand Down Expand Up @@ -67,6 +72,7 @@ func (s *ServiceApp) PluginType() *types.App {
}

base.Properties["build"] = s.Build
base.Properties["container"] = s.Container
base.Properties["local_docker_image"] = s.LocalDockerImage()
base.Properties["local_docker_hash"] = s.DockerHash

Expand Down
Loading

0 comments on commit 26b59d5

Please sign in to comment.