@@ -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 }
0 commit comments