@@ -25,10 +25,9 @@ import (
2525const deployCommand = "deploy"
2626
2727type planParams struct {
28- appPlans []* apiv1.AppPlan
29- depPlans []* apiv1.DependencyPlan
30- args map [string ]interface {}
31- firstPass bool
28+ appPlans []* apiv1.AppPlan
29+ depPlans []* apiv1.DependencyPlan
30+ args map [string ]interface {}
3231}
3332
3433type Deploy struct {
@@ -99,10 +98,11 @@ func (d *Deploy) Run(ctx context.Context) error {
9998 return nil
10099 }
101100
102- return d .showStateStatus (state .Apps , state .Dependencies )
101+ return d .showStateStatus (state .Apps , state .Dependencies , state . DNSRecords )
103102}
104103
105104func (d * Deploy ) planAndApply (ctx context.Context , verify bool , state * types.StateData , stateRes * apiv1.GetStateResponse_State ) (canceled bool , err error ) {
105+ domains := d .cfg .DomainInfoProto ()
106106 stateBeforeStr , _ := json .Marshal (state )
107107
108108 // Plan and apply.
@@ -129,10 +129,10 @@ func (d *Deploy) planAndApply(ctx context.Context, verify bool, state *types.Sta
129129 spinner , _ := d .log .Spinner ().Start ("Planning..." )
130130
131131 // Proceed with plan - reset state apps and deps.
132- state . Apps = make ( map [ string ] * apiv1. AppState )
133- state .Dependencies = make ( map [ string ] * apiv1. DependencyState )
132+ oldState := * state
133+ state .Reset ( )
134134
135- planRetMap , err := plan (ctx , state , planMap , verify , destroy )
135+ planRetMap , err := plan (ctx , state , planMap , domains , verify , destroy )
136136 if err != nil {
137137 spinner .Stop ()
138138
@@ -141,11 +141,11 @@ func (d *Deploy) planAndApply(ctx context.Context, verify bool, state *types.Sta
141141 return false , err
142142 }
143143
144- deployChanges , dnsChanges := computeChange (d .cfg , state , planRetMap )
144+ deployChanges := computeChange (d .cfg , & oldState , state , planRetMap )
145145
146146 spinner .Stop ()
147147
148- empty , canceled := planPrompt (d .log , deployChanges , dnsChanges , d .opts .AutoApprove , d .opts .ForceApprove )
148+ empty , canceled := planPrompt (d .log , deployChanges , nil , d .opts .AutoApprove , d .opts .ForceApprove )
149149
150150 shouldApply := ! canceled && ! empty
151151
@@ -155,8 +155,8 @@ func (d *Deploy) planAndApply(ctx context.Context, verify bool, state *types.Sta
155155
156156 // Apply if needed.
157157 if shouldApply {
158- callback := applyProgress (d .log , deployChanges , dnsChanges )
159- err = apply (context .Background (), state , planMap , destroy , callback )
158+ callback := applyProgress (d .log , deployChanges , nil )
159+ err = apply (context .Background (), state , planMap , domains , destroy , callback )
160160 }
161161
162162 // Merge state with current apps/deps if needed (they might not have a state defined).
@@ -195,16 +195,11 @@ func (d *Deploy) planAndApply(ctx context.Context, verify bool, state *types.Sta
195195 return canceled , err
196196}
197197
198- type dnsSetup struct {
199- record string
200- dns * apiv1.DNSState
201- }
202-
203- func (d * Deploy ) prepareAppDNSMap (appStates map [string ]* apiv1.AppState ) (map [string ]* apiv1.DNSState , error ) {
204- dnsMap := make (map [string ]* apiv1.DNSState )
198+ func (d * Deploy ) prepareAppSSLMap (appStates map [string ]* apiv1.AppState ) (map [string ]* apiv1.DNSState , error ) {
199+ sslMap := make (map [string ]* apiv1.DNSState )
205200
206201 for _ , appState := range appStates {
207- if appState .Dns == nil || ! appState .Dns .Manual || (appState .Dns .Cname == "" && appState .Dns .Ip == "" ) {
202+ if appState .Dns == nil || appState .Dns .SslStatus == apiv1 . DNSState_SSL_STATUS_UNSPECIFIED || (appState .Dns .Cname == "" && appState .Dns .Ip == "" ) {
208203 continue
209204 }
210205
@@ -213,46 +208,38 @@ func (d *Deploy) prepareAppDNSMap(appStates map[string]*apiv1.AppState) (map[str
213208 return nil , err
214209 }
215210
216- dnsMap [host ] = appState .Dns
211+ sslMap [host ] = appState .Dns
217212 }
218213
219- return dnsMap , nil
214+ return sslMap , nil
220215}
221216
222- func (d * Deploy ) showStateStatus (appStates map [string ]* apiv1.AppState , dependencyStates map [string ]* apiv1.DependencyState ) error {
223- dnsMap , err := d .prepareAppDNSMap (appStates )
224- if err != nil {
225- return err
226- }
227-
228- var dns []* dnsSetup
217+ func (d * Deploy ) showStateStatus (appStates map [string ]* apiv1.AppState , dependencyStates map [string ]* apiv1.DependencyState , dnsRecords types.DNSRecordMap ) error {
218+ var dns []* apiv1.DNSRecord
229219
230- for host , v := range dnsMap {
231- dns = append (dns , & dnsSetup {
232- record : host ,
233- dns : v ,
234- })
220+ for k , v := range dnsRecords {
221+ if ! v .Created {
222+ dns = append (dns , & apiv1.DNSRecord {
223+ Record : k .Record ,
224+ Type : k .Type ,
225+ Value : v .Value ,
226+ })
227+ }
235228 }
236229
237230 sort .Slice (dns , func (i , j int ) bool {
238- return dns [i ].record < dns [j ].record
231+ return dns [i ].Record < dns [j ].Record
239232 })
240233
241- // Show info about manual DNS setup.
234+ // Show info about DNS records still requiring setup.
242235 data := [][]string {
243236 {"Record" , "Type" , "Value" },
244237 }
245238
246239 for _ , v := range dns {
247- typ := "A"
248- val := v .dns .Ip
249-
250- if v .dns .Cname != "" {
251- typ = "CNAME"
252- val = v .dns .Cname
253- }
240+ typ := v .Type .String ()[len ("TYPE_" ):]
254241
255- data = append (data , []string {pterm .Green (v .record ), pterm .Yellow (typ ), val })
242+ data = append (data , []string {pterm .Green (v .Record ), pterm .Yellow (typ ), v . Value })
256243 }
257244
258245 if len (dns ) > 0 {
@@ -320,13 +307,16 @@ func (d *Deploy) showStateStatus(appStates map[string]*apiv1.AppState, dependenc
320307 }
321308
322309 // Show info about SSL status.
323- if len (dnsMap ) > 0 {
324- d .log .Section ().Println ("SSL Certificates" )
310+ sslMap , err := d .prepareAppSSLMap (appStates )
311+ if err != nil {
312+ return err
313+ }
325314
326- data := make ([][]string , 0 , len (dnsMap ))
315+ if len (sslMap ) > 0 {
316+ data := make ([][]string , 0 , len (sslMap ))
327317
328- for host , v := range dnsMap {
329- data = append (data , []string {pterm .Green (host ), pterm .Yellow (v .SslStatus ), v .SslStatusInfo })
318+ for host , v := range sslMap {
319+ data = append (data , []string {pterm .Green (host ), pterm .Yellow (v .SslStatus . String ()[ len ( "SSL_STATUS_" ):] ), v .SslStatusInfo })
330320 }
331321
332322 sort .Slice (data , func (i , j int ) bool {
@@ -335,6 +325,7 @@ func (d *Deploy) showStateStatus(appStates map[string]*apiv1.AppState, dependenc
335325
336326 data = append ([][]string {{"Domain" , "Status" , "Info" }}, data ... )
337327
328+ d .log .Section ().Println ("SSL Certificates" )
338329 _ = d .log .Table ().WithHasHeader ().WithData (pterm .TableData (data )).Render ()
339330 }
340331
@@ -394,8 +385,6 @@ func calculatePlanMap(cfg *config.Project, appStates []*apiv1.AppState, depState
394385 planMap := make (map [* plugins.Plugin ]* planParams )
395386
396387 for _ , appState := range appStates {
397- includeDNS := appState .App .DnsPlugin != "" && appState .App .DnsPlugin == appState .App .DeployPlugin
398-
399388 deployPlugin := cfg .FindLoadedPlugin (appState .App .DeployPlugin )
400389 if deployPlugin == nil {
401390 return nil , fmt .Errorf ("missing deploy plugin: %s used for app: %s" , appState .App .DeployPlugin , appState .App .Name )
@@ -408,37 +397,10 @@ func calculatePlanMap(cfg *config.Project, appStates []*apiv1.AppState, depState
408397 }
409398
410399 appReq := & apiv1.AppPlan {
411- State : appState ,
412- IsDeploy : true ,
413- IsDns : includeDNS ,
400+ State : appState ,
414401 }
415402
416403 planMap [deployPlugin ].appPlans = append (planMap [deployPlugin ].appPlans , appReq )
417- planMap [deployPlugin ].firstPass = ! includeDNS // if dns is handled by different plugin, plan this as a first pass
418-
419- // Add DNS plugin if not already included (handled by same plugin).
420- if includeDNS || appState .App .DnsPlugin == "" {
421- continue
422- }
423-
424- dnsPlugin := cfg .FindLoadedPlugin (appState .App .DnsPlugin )
425- if dnsPlugin == nil {
426- return nil , fmt .Errorf ("missing dns plugin: %s used for app: %s" , appState .App .DnsPlugin , appState .App .Name )
427- }
428-
429- if _ , ok := planMap [dnsPlugin ]; ! ok {
430- planMap [dnsPlugin ] = & planParams {
431- args : dnsPlugin .CommandArgs (deployCommand ),
432- }
433- }
434-
435- appReq = & apiv1.AppPlan {
436- State : appState ,
437- IsDeploy : false ,
438- IsDns : true ,
439- }
440-
441- planMap [dnsPlugin ].appPlans = append (planMap [dnsPlugin ].appPlans , appReq )
442404 }
443405
444406 // Process dependencies.
@@ -482,7 +444,7 @@ func addPlanTargetAndSkipApps(planMap map[*plugins.Plugin]*planParams, targetApp
482444 return planMap
483445}
484446
485- func mergeState (state * types.StateData , pluginName string , pluginState * apiv1.PluginState , appStates map [string ]* apiv1.AppState , depStates map [string ]* apiv1.DependencyState ) {
447+ func mergeState (state * types.StateData , pluginName string , pluginState * apiv1.PluginState , appStates map [string ]* apiv1.AppState , depStates map [string ]* apiv1.DependencyState , dnsRecords [] * apiv1. DNSRecord ) {
486448 state .Plugins [pluginName ] = types .PluginStateFromProto (pluginState )
487449
488450 // Merge state with new changes.
@@ -493,9 +455,13 @@ func mergeState(state *types.StateData, pluginName string, pluginState *apiv1.Pl
493455 for k , v := range depStates {
494456 state .Dependencies [k ] = v
495457 }
458+
459+ for _ , v := range dnsRecords {
460+ state .AddDNSRecord (v )
461+ }
496462}
497463
498- func plan (ctx context.Context , state * types.StateData , planMap map [* plugins.Plugin ]* planParams , verify , destroy bool ) (retMap map [* plugins.Plugin ]* apiv1.PlanResponse , err error ) {
464+ func plan (ctx context.Context , state * types.StateData , planMap map [* plugins.Plugin ]* planParams , domains [] * apiv1. DomainInfo , verify , destroy bool ) (retMap map [* plugins.Plugin ]* apiv1.PlanResponse , err error ) {
499465 if state .Plugins == nil {
500466 state .Plugins = make (map [string ]* types.PluginState )
501467 }
@@ -515,7 +481,7 @@ func plan(ctx context.Context, state *types.StateData, planMap map[*plugins.Plug
515481 retMap [plug ] = ret
516482
517483 // Merge state with new changes.
518- mergeState (state , plug .Name , ret .State , ret .AppStates , ret .DependencyStates )
484+ mergeState (state , plug .Name , ret .State , ret .AppStates , ret .DependencyStates , ret . DnsRecords )
519485
520486 mu .Unlock ()
521487 }
@@ -526,7 +492,7 @@ func plan(ctx context.Context, state *types.StateData, planMap map[*plugins.Plug
526492 params := params
527493
528494 g .Go (func () error {
529- ret , err := plug .Client ().Plan (ctx , state , params .appPlans , params .depPlans , params .args , verify , destroy )
495+ ret , err := plug .Client ().Plan (ctx , state , params .appPlans , params .depPlans , domains , params .args , verify , destroy )
530496 if err != nil {
531497 return err
532498 }
@@ -539,15 +505,10 @@ func plan(ctx context.Context, state *types.StateData, planMap map[*plugins.Plug
539505
540506 err = g .Wait ()
541507
542- // Merge state with new changes.
543- for p , ret := range retMap {
544- mergeState (state , p .Name , ret .State , ret .AppStates , ret .DependencyStates )
545- }
546-
547508 return retMap , err
548509}
549510
550- func apply (ctx context.Context , state * types.StateData , planMap map [* plugins.Plugin ]* planParams , destroy bool , callback func (* apiv1.ApplyAction )) error {
511+ func apply (ctx context.Context , state * types.StateData , planMap map [* plugins.Plugin ]* planParams , domains [] * apiv1. DomainInfo , destroy bool , callback func (* apiv1.ApplyAction )) error {
551512 g , _ := errgroup .WithConcurrency (ctx , defaultConcurrency )
552513
553514 if state .Plugins == nil {
@@ -564,45 +525,20 @@ func apply(ctx context.Context, state *types.StateData, planMap map[*plugins.Plu
564525 mu .Lock ()
565526
566527 // Merge state with new changes.
567- mergeState (state , plug .Name , ret .State , ret .AppStates , ret .DependencyStates )
528+ mergeState (state , plug .Name , ret .State , ret .AppStates , ret .DependencyStates , ret . DnsRecords )
568529
569530 mu .Unlock ()
570531 }
571532
572- // Apply first pass plan (deployments without DNS).
573- for plug , params := range planMap {
574- if ! params .firstPass {
575- continue
576- }
577-
578- g .Go (func () error {
579- ret , err := plug .Client ().Apply (ctx , state , params .appPlans , params .depPlans , params .args , destroy , callback )
580- processResponse (plug , ret )
581-
582- return err
583- })
584- }
585-
586- err := g .Wait ()
587- if err != nil {
588- return err
589- }
590-
591533 // Apply second pass plan (DNS and deployments with DNS).
592534 for plug , params := range planMap {
593- if params .firstPass {
594- continue
595- }
596-
597535 g .Go (func () error {
598- ret , err := plug .Client ().Apply (ctx , state , params .appPlans , params .depPlans , params .args , destroy , callback )
536+ ret , err := plug .Client ().Apply (ctx , state , params .appPlans , params .depPlans , domains , params .args , destroy , callback )
599537 processResponse (plug , ret )
600538
601539 return err
602540 })
603541 }
604542
605- err = g .Wait ()
606-
607- return err
543+ return g .Wait ()
608544}
0 commit comments