Skip to content

Commit abd3711

Browse files
committed
alpha: changed dns handling, self managed certs
1 parent 504a8df commit abd3711

File tree

11 files changed

+252
-180
lines changed

11 files changed

+252
-180
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ require (
1616
github.com/mholt/archiver/v3 v3.5.1
1717
github.com/mitchellh/go-homedir v1.1.0
1818
github.com/otiai10/copy v1.6.0
19-
github.com/outblocks/outblocks-plugin-go v0.0.0-20211207172419-7e917d12fa92
19+
github.com/outblocks/outblocks-plugin-go v0.0.0-20211213135733-23547102ec07
2020
github.com/pterm/pterm v0.12.33
2121
github.com/spf13/cobra v1.2.1
2222
github.com/spf13/pflag v1.0.5

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -686,8 +686,8 @@ github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6
686686
github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo=
687687
github.com/otiai10/mint v1.3.2 h1:VYWnrP5fXmz1MXvjuUvcBrXSjGE6xjON+axB/UrpO3E=
688688
github.com/otiai10/mint v1.3.2/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc=
689-
github.com/outblocks/outblocks-plugin-go v0.0.0-20211207172419-7e917d12fa92 h1:0AdUlXuACt65jL2vO2MBXM3r8XvOev7l96skx+X7tSY=
690-
github.com/outblocks/outblocks-plugin-go v0.0.0-20211207172419-7e917d12fa92/go.mod h1:YpdRDqsSy9b2WbUiTIv5aXcxr52Rw2WGrCzoFCj4B8Q=
689+
github.com/outblocks/outblocks-plugin-go v0.0.0-20211213135733-23547102ec07 h1:6PwovmPdXNTQd2nc0/5Rzd3w2ahla2sohyrHMwgmZKU=
690+
github.com/outblocks/outblocks-plugin-go v0.0.0-20211213135733-23547102ec07/go.mod h1:YpdRDqsSy9b2WbUiTIv5aXcxr52Rw2WGrCzoFCj4B8Q=
691691
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
692692
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
693693
github.com/pelletier/go-toml v1.8.1/go.mod h1:T2/BmBdy8dvIRq1a/8aqjN41wvWlN4lrapLU/GW4pbc=

pkg/actions/deploy.go

Lines changed: 53 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ import (
2525
const deployCommand = "deploy"
2626

2727
type 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

3433
type 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

105104
func (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
}

pkg/actions/util.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,18 +128,15 @@ func computeChangeInfo(cfg *config.Project, state *types.StateData, plugin *plug
128128
return changes
129129
}
130130

131-
func computeChange(cfg *config.Project, state *types.StateData, planMap map[*plugins.Plugin]*apiv1.PlanResponse) (deploy, dns []*change) {
132-
for plugin, p := range planMap {
133-
if p.Deploy != nil {
134-
deploy = computeChangeInfo(cfg, state, plugin, p.Deploy.Actions)
135-
}
131+
func computeChange(cfg *config.Project, oldState, state *types.StateData, planMap map[*plugins.Plugin]*apiv1.PlanResponse) []*change { // nolint:unparam
132+
var changes []*change
136133

137-
if p.Dns != nil {
138-
dns = computeChangeInfo(cfg, state, plugin, p.Deploy.Actions)
139-
}
134+
for plugin, p := range planMap {
135+
chg := computeChangeInfo(cfg, state, plugin, p.Deploy.Actions)
136+
changes = append(changes, chg...)
140137
}
141138

142-
return deploy, dns
139+
return changes
143140
}
144141

145142
func calculateTotal(chg []*change) (add, change, process, destroy int) {
@@ -248,6 +245,7 @@ func planPrompt(log logger.Logger, deploy, dns []*change, approve, force bool) (
248245
}
249246

250247
// DNS
248+
// TODO: handle dns as a diff of records
251249
dnsInfo, dnsCritical := planChangeInfo("DNS:", dns)
252250
if dnsInfo != "" {
253251
empty = false
@@ -275,7 +273,7 @@ func planPrompt(log logger.Logger, deploy, dns []*change, approve, force bool) (
275273
}
276274

277275
if critical {
278-
prompt.Message = "Some changes are potentially destructive. Are you really sure you want to perform these actions?"
276+
prompt.Message = fmt.Sprintf("%s Some changes are potentially destructive! Are you really sure you want to perform these actions?", pterm.Red("Warning!"))
279277
}
280278

281279
_ = survey.AskOne(prompt, &proceed)

0 commit comments

Comments
 (0)