Skip to content

Commit 965bd21

Browse files
committed
alpha: attempt at proper skip/target apps
1 parent 286215a commit 965bd21

File tree

13 files changed

+227
-120
lines changed

13 files changed

+227
-120
lines changed

cmd/deploy.go

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,20 @@ func (e *Executor) newDeployCmd() *cobra.Command {
3030
for _, t := range targetApps {
3131
tsplit := strings.SplitN(t, ".", 2)
3232
if len(tsplit) != 2 {
33-
return fmt.Errorf("wrong format for 'target' %s: specify in a form of <app type>.<name>, e.g.: static.website", t)
33+
return fmt.Errorf("wrong format for target '%s': specify in a form of <app type>.<name>, e.g.: static.website", t)
3434
}
3535

36-
app := e.cfg.AppMap[config.ComputeAppID(tsplit[0], tsplit[1])]
37-
if app == nil {
38-
return fmt.Errorf("target app '%s' not found", t)
39-
}
40-
41-
opts.TargetApps = append(opts.TargetApps, app)
36+
opts.TargetApps = append(opts.TargetApps, config.ComputeAppID(tsplit[0], tsplit[1]))
4237
}
4338

44-
if opts.SkipAllApps {
45-
opts.SkipApps = e.cfg.Apps
46-
} else {
39+
if !opts.SkipAllApps {
4740
for _, t := range skipApps {
4841
tsplit := strings.SplitN(t, ".", 2)
4942
if len(tsplit) != 2 {
50-
return fmt.Errorf("wrong format for 'skip' %s: specify in a form of <app type>.<name>, e.g.: static.website", t)
51-
}
52-
53-
app := e.cfg.AppMap[config.ComputeAppID(tsplit[0], tsplit[1])]
54-
if app == nil {
55-
return fmt.Errorf("app '%s' not found", t)
43+
return fmt.Errorf("wrong format for skip '%s': specify in a form of <app type>.<name>, e.g.: static.website", t)
5644
}
5745

58-
opts.SkipApps = append(opts.SkipApps, app)
46+
opts.SkipApps = append(opts.SkipApps, config.ComputeAppID(tsplit[0], tsplit[1]))
5947
}
6048
}
6149

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ require (
1515
github.com/mholt/archiver/v3 v3.5.0
1616
github.com/mitchellh/go-homedir v1.1.0
1717
github.com/otiai10/copy v1.6.0
18-
github.com/outblocks/outblocks-plugin-go v0.0.0-20211103180603-373c5d4c00f1
18+
github.com/outblocks/outblocks-plugin-go v0.0.0-20211103194421-5088c86b17e0
1919
github.com/pterm/pterm v0.12.32
2020
github.com/spf13/cobra v1.1.3
2121
github.com/spf13/pflag v1.0.5

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -627,8 +627,8 @@ github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6
627627
github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo=
628628
github.com/otiai10/mint v1.3.2 h1:VYWnrP5fXmz1MXvjuUvcBrXSjGE6xjON+axB/UrpO3E=
629629
github.com/otiai10/mint v1.3.2/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc=
630-
github.com/outblocks/outblocks-plugin-go v0.0.0-20211103180603-373c5d4c00f1 h1:JqlgaWtFJZ4GblEDqGvejUkjWjpa+i1iO1eObOaKOSE=
631-
github.com/outblocks/outblocks-plugin-go v0.0.0-20211103180603-373c5d4c00f1/go.mod h1:xosanOIc+3GlRA9b90/xRoYWzRgdhbew9T7+yTeI/TQ=
630+
github.com/outblocks/outblocks-plugin-go v0.0.0-20211103194421-5088c86b17e0 h1:nbIoc/Ee/aM03vZPy/8gbVs2OzIT9UUEgo2OzdlT9Eg=
631+
github.com/outblocks/outblocks-plugin-go v0.0.0-20211103194421-5088c86b17e0/go.mod h1:xosanOIc+3GlRA9b90/xRoYWzRgdhbew9T7+yTeI/TQ=
632632
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
633633
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
634634
github.com/pelletier/go-toml v1.8.1/go.mod h1:T2/BmBdy8dvIRq1a/8aqjN41wvWlN4lrapLU/GW4pbc=

internal/util/util.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,13 @@ func FlattenEnvMap(m map[string]string) []string {
5656

5757
return out
5858
}
59+
60+
func StringArrayToSet(in []string) map[string]bool {
61+
outMap := make(map[string]bool, len(in))
62+
63+
for _, i := range in {
64+
outMap[i] = true
65+
}
66+
67+
return outMap
68+
}

pkg/actions/deploy.go

Lines changed: 121 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import (
44
"bytes"
55
"context"
66
"encoding/json"
7+
"fmt"
78
"sort"
89
"sync"
910
"time"
1011

1112
dockerclient "github.com/docker/docker/client"
1213
"github.com/outblocks/outblocks-cli/internal/urlutil"
14+
"github.com/outblocks/outblocks-cli/internal/util"
1315
"github.com/outblocks/outblocks-cli/pkg/config"
1416
"github.com/outblocks/outblocks-cli/pkg/logger"
1517
"github.com/outblocks/outblocks-cli/pkg/plugins"
@@ -48,7 +50,7 @@ type DeployOptions struct {
4850
SkipBuild bool
4951
Lock bool
5052
AutoApprove bool
51-
TargetApps, SkipApps []config.App
53+
TargetApps, SkipApps []string
5254
SkipAllApps bool
5355
}
5456

@@ -86,7 +88,12 @@ func (d *Deploy) Run(ctx context.Context) error {
8688
stateBeforeStr, _ := json.Marshal(stateRes.State)
8789

8890
// Plan and apply.
89-
planMap := calculatePlanMap(d.cfg, d.opts.TargetApps, d.opts.SkipApps)
91+
apps, deps, skipAppIDs := filterApps(d.cfg, stateRes.State, d.opts.TargetApps, d.opts.SkipApps, d.opts.SkipAllApps)
92+
93+
planMap, err := calculatePlanMap(d.cfg, apps, deps, d.opts.TargetApps, skipAppIDs)
94+
if err != nil {
95+
return err
96+
}
9097

9198
// Start plugins.
9299
for plug := range planMap {
@@ -106,7 +113,7 @@ func (d *Deploy) Run(ctx context.Context) error {
106113
return err
107114
}
108115

109-
deployChanges, dnsChanges := computeChange(d.cfg.AppMap, d.cfg.DependencyMap, stateRes.State, planRetMap)
116+
deployChanges, dnsChanges := computeChange(d.cfg, stateRes.State, planRetMap)
110117

111118
_ = spinner.Stop()
112119

@@ -333,39 +340,122 @@ func getState(ctx context.Context, cfg *config.Project, lock bool) (*plugin_go.G
333340
return ret, nil
334341
}
335342

336-
func calculatePlanMap(cfg *config.Project, targetApps, skipApps []config.App) map[*plugins.Plugin]*planParams {
337-
planMap := make(map[*plugins.Plugin]*planParams)
343+
func filterApps(cfg *config.Project, state *types.StateData, targetAppIDs, skipAppIDs []string, skipAllApps bool) (apps []*types.App, deps []*types.Dependency, retSkipAppIDs []string) {
344+
if skipAllApps {
345+
retSkipAppIDs := make([]string, 0, len(state.Apps))
346+
apps = make([]*types.App, 0, len(state.Apps))
347+
348+
for _, app := range state.Apps {
349+
apps = append(apps, app)
350+
retSkipAppIDs = append(retSkipAppIDs, app.ID)
351+
}
352+
353+
deps = make([]*types.Dependency, 0, len(state.Dependencies))
354+
for _, dep := range state.Dependencies {
355+
deps = append(deps, dep)
356+
}
357+
358+
return apps, deps, retSkipAppIDs
359+
}
360+
361+
// In non target and non skip mode, use config apps and deps.
362+
if len(skipAppIDs) == 0 && len(targetAppIDs) == 0 {
363+
apps = make([]*types.App, 0, len(cfg.Apps))
364+
for _, app := range cfg.Apps {
365+
apps = append(apps, app.PluginType())
366+
}
367+
368+
deps = make([]*types.Dependency, 0, len(cfg.Dependencies))
369+
for _, dep := range cfg.Dependencies {
370+
deps = append(deps, dep.PluginType())
371+
}
372+
373+
return apps, deps, nil
374+
}
375+
376+
appsMap := make(map[string]*types.App, len(state.Apps))
377+
dependenciesMap := make(map[string]*types.Dependency, len(state.Dependencies))
378+
targetAppIDsMap := util.StringArrayToSet(targetAppIDs)
379+
skipAppIDsMap := util.StringArrayToSet(skipAppIDs)
380+
381+
for key, app := range state.Apps {
382+
appsMap[key] = app
383+
}
384+
385+
for key, dep := range state.Dependencies {
386+
dependenciesMap[key] = dep
387+
}
338388

339389
for _, app := range cfg.Apps {
340-
dnsPlugin := app.DNSPlugin()
341-
deployPlugin := app.DeployPlugin()
390+
if len(targetAppIDsMap) > 0 && !targetAppIDsMap[app.ID()] {
391+
continue
392+
}
393+
394+
if !skipAppIDsMap[app.ID()] {
395+
continue
396+
}
397+
342398
appType := app.PluginType()
399+
appType.Properties = plugin_util.MergeMaps(cfg.Defaults.Deploy.Other, appType.Properties, app.DeployInfo().Other)
400+
appType.Env = plugin_util.MergeStringMaps(cfg.Defaults.Run.Env, appType.Env, app.DeployInfo().Env)
343401

344-
includeDNS := dnsPlugin != nil && dnsPlugin == deployPlugin
402+
appsMap[app.ID()] = appType
403+
}
404+
405+
for _, dep := range cfg.Dependencies {
406+
dependenciesMap[dep.ID()] = dep.PluginType()
407+
}
408+
409+
// Flatten maps to list.
410+
apps = make([]*types.App, 0, len(appsMap))
411+
for _, app := range appsMap {
412+
apps = append(apps, app)
413+
}
414+
415+
deps = make([]*types.Dependency, 0, len(dependenciesMap))
416+
for _, dep := range dependenciesMap {
417+
deps = append(deps, dep)
418+
}
419+
420+
return apps, deps, skipAppIDs
421+
}
422+
423+
func calculatePlanMap(cfg *config.Project, apps []*types.App, deps []*types.Dependency, targetAppIDs, skipAppIDs []string) (map[*plugins.Plugin]*planParams, error) {
424+
planMap := make(map[*plugins.Plugin]*planParams)
425+
426+
for _, app := range apps {
427+
includeDNS := app.DNSPlugin != "" && app.DNSPlugin == app.DeployPlugin
428+
429+
deployPlugin := cfg.FindLoadedPlugin(app.DeployPlugin)
430+
if deployPlugin == nil {
431+
return nil, fmt.Errorf("missing deploy plugin: %s used for app: %s", app.DeployPlugin, app.Name)
432+
}
345433

346434
if _, ok := planMap[deployPlugin]; !ok {
347435
planMap[deployPlugin] = &planParams{
348436
args: deployPlugin.CommandArgs(deployCommand),
349437
}
350438
}
351439

352-
appType.Properties = plugin_util.MergeMaps(cfg.Defaults.Deploy.Other, appType.Properties, app.DeployInfo().Other)
353-
appType.Env = plugin_util.MergeStringMaps(cfg.Defaults.Run.Env, appType.Env, app.DeployInfo().Env)
354-
355440
appReq := &types.AppPlan{
356441
IsDeploy: true,
357442
IsDNS: includeDNS,
358-
App: appType,
443+
App: app,
359444
}
360445

361446
planMap[deployPlugin].apps = append(planMap[deployPlugin].apps, appReq)
362447
planMap[deployPlugin].firstPass = !includeDNS // if dns is handled by different plugin, plan this as a first pass
363448

364449
// Add DNS plugin if not already included (handled by same plugin).
365-
if includeDNS || dnsPlugin == nil {
450+
if includeDNS || app.DNSPlugin == "" {
366451
continue
367452
}
368453

454+
dnsPlugin := cfg.FindLoadedPlugin(app.DNSPlugin)
455+
if dnsPlugin == nil {
456+
return nil, fmt.Errorf("missing dns plugin: %s used for app: %s", app.DNSPlugin, app.Name)
457+
}
458+
369459
if _, ok := planMap[dnsPlugin]; !ok {
370460
planMap[dnsPlugin] = &planParams{
371461
args: dnsPlugin.CommandArgs(deployCommand),
@@ -375,57 +465,49 @@ func calculatePlanMap(cfg *config.Project, targetApps, skipApps []config.App) ma
375465
appReq = &types.AppPlan{
376466
IsDeploy: false,
377467
IsDNS: true,
378-
App: appType,
468+
App: app,
379469
}
380470

381471
planMap[dnsPlugin].apps = append(planMap[dnsPlugin].apps, appReq)
382472
}
383473

384474
// Process dependencies.
385-
for _, dep := range cfg.Dependencies {
386-
t := dep.PluginType()
475+
for _, dep := range deps {
476+
deployPlugin := cfg.FindLoadedPlugin(dep.DeployPlugin)
477+
if deployPlugin == nil {
478+
return nil, fmt.Errorf("missing deploy plugin: %s used for dependency: %s", dep.DeployPlugin, dep.Name)
479+
}
387480

388-
p := dep.DeployPlugin()
389-
if _, ok := planMap[p]; !ok {
390-
planMap[p] = &planParams{
391-
args: p.CommandArgs(deployCommand),
481+
if _, ok := planMap[deployPlugin]; !ok {
482+
planMap[deployPlugin] = &planParams{
483+
args: deployPlugin.CommandArgs(deployCommand),
392484
}
393485
}
394486

395-
planMap[p].deps = append(planMap[p].deps, &types.DependencyPlan{
396-
Dependency: t,
487+
planMap[deployPlugin].deps = append(planMap[deployPlugin].deps, &types.DependencyPlan{
488+
Dependency: dep,
397489
})
398490
}
399491

400-
return addPlanTargetAndSkipApps(planMap, targetApps, skipApps)
492+
return addPlanTargetAndSkipApps(planMap, targetAppIDs, skipAppIDs), nil
401493
}
402494

403-
func addPlanTargetAndSkipApps(planMap map[*plugins.Plugin]*planParams, targetApps, skipApps []config.App) map[*plugins.Plugin]*planParams {
404-
if len(targetApps) == 0 && len(skipApps) == 0 {
495+
func addPlanTargetAndSkipApps(planMap map[*plugins.Plugin]*planParams, targetAppIDs, skipAppIDs []string) map[*plugins.Plugin]*planParams {
496+
if len(targetAppIDs) == 0 && len(skipAppIDs) == 0 {
405497
return planMap
406498
}
407499

408500
// Add target and skip app ids.
409-
targetAppIDsMap := make(map[string]struct{}, len(targetApps))
410-
skipAppIDsMap := make(map[string]struct{}, len(skipApps))
411-
412-
for _, app := range targetApps {
413-
appID := app.ID()
414-
targetAppIDsMap[appID] = struct{}{}
415-
}
416-
417-
for _, app := range skipApps {
418-
appID := app.ID()
419-
skipAppIDsMap[appID] = struct{}{}
420-
}
501+
targetIDsMap := util.StringArrayToSet(targetAppIDs)
502+
skipIDsMap := util.StringArrayToSet(skipAppIDs)
421503

422504
for _, planParam := range planMap {
423505
for _, app := range planParam.apps {
424-
if _, ok := targetAppIDsMap[app.App.ID]; ok {
506+
if _, ok := targetIDsMap[app.App.ID]; ok {
425507
planParam.targetApps = append(planParam.targetApps, app.App.ID)
426508
}
427509

428-
if _, ok := skipAppIDsMap[app.App.ID]; ok {
510+
if _, ok := skipIDsMap[app.App.ID]; ok {
429511
planParam.skipApps = append(planParam.skipApps, app.App.ID)
430512
}
431513
}

pkg/actions/deploy_build.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -188,20 +188,26 @@ func (d *Deploy) buildApps(ctx context.Context) error {
188188
g, _ := errgroup.WithConcurrency(ctx, defaultConcurrency)
189189

190190
apps := d.cfg.Apps
191-
if len(d.opts.TargetApps) > 0 {
192-
apps = d.opts.TargetApps
193-
}
191+
targetAppIDsMap := util.StringArrayToSet(d.opts.TargetApps)
192+
skipAppIDsMap := util.StringArrayToSet(d.opts.SkipApps)
193+
194+
var appsTemp []config.App
194195

195-
appsMap := make(map[config.App]struct{})
196196
for _, app := range apps {
197-
appsMap[app] = struct{}{}
198-
}
197+
if len(targetAppIDsMap) > 0 && !targetAppIDsMap[app.ID()] {
198+
continue
199+
}
199200

200-
for _, app := range d.opts.SkipApps {
201-
delete(appsMap, app)
201+
if !skipAppIDsMap[app.ID()] {
202+
continue
203+
}
204+
205+
appsTemp = append(appsTemp, app)
202206
}
203207

204-
for app := range appsMap {
208+
apps = appsTemp
209+
210+
for _, app := range apps {
205211
// TODO: add build app function
206212
switch app.Type() {
207213
case config.AppTypeStatic:

pkg/actions/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ func formatRunOutput(log logger.Logger, cfg *config.Project, r *plugin_go.RunOut
419419

420420
switch r.Source {
421421
case plugin_go.RunOutpoutSourceApp:
422-
app := cfg.AppMap[r.ID]
422+
app := cfg.AppByID(r.ID)
423423
prefix = fmt.Sprintf("APP:%s:%s:", app.Type(), app.Name())
424424

425425
case plugin_go.RunOutpoutSourceDependency:

0 commit comments

Comments
 (0)