Skip to content

Commit 4fc62e2

Browse files
committed
alpha: add support for skip-dns and domains in state, re-add skip all apps
1 parent 086db5a commit 4fc62e2

File tree

7 files changed

+111
-22
lines changed

7 files changed

+111
-22
lines changed

cmd/deploy.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ func (e *Executor) newDeployCmd() *cobra.Command {
3131
return merry.New("target-apps and skip-apps arguments are mutually exclusive modes")
3232
}
3333

34+
if opts.SkipAllApps && opts.Destroy {
35+
return merry.New("skip-all-apps and destroy are mutually exclusive modes")
36+
}
37+
3438
for _, t := range targetApps {
3539
tsplit := strings.SplitN(t, ".", 2)
3640
if len(tsplit) != 2 {
@@ -40,13 +44,15 @@ func (e *Executor) newDeployCmd() *cobra.Command {
4044
opts.TargetApps = append(opts.TargetApps, config.ComputeAppID(tsplit[0], tsplit[1]))
4145
}
4246

43-
for _, t := range skipApps {
44-
tsplit := strings.SplitN(t, ".", 2)
45-
if len(tsplit) != 2 {
46-
return merry.Errorf("wrong format for skip '%s': specify in a form of <app type>.<name>, e.g.: static.website", t)
47-
}
47+
if !opts.SkipAllApps {
48+
for _, t := range skipApps {
49+
tsplit := strings.SplitN(t, ".", 2)
50+
if len(tsplit) != 2 {
51+
return merry.Errorf("wrong format for skip '%s': specify in a form of <app type>.<name>, e.g.: static.website", t)
52+
}
4853

49-
opts.SkipApps = append(opts.SkipApps, config.ComputeAppID(tsplit[0], tsplit[1]))
54+
opts.SkipApps = append(opts.SkipApps, config.ComputeAppID(tsplit[0], tsplit[1]))
55+
}
5056
}
5157

5258
if opts.Destroy {
@@ -67,6 +73,8 @@ func (e *Executor) newDeployCmd() *cobra.Command {
6773
f.BoolVar(&opts.ForceApprove, "force", false, "force approve even with critical changes")
6874
f.StringSliceVarP(&targetApps, "target-apps", "t", nil, "target only specified apps, can specify multiple or separate values with comma in a form of <app type>.<name>, e.g.: static.website,service.api")
6975
f.StringSliceVarP(&skipApps, "skip-apps", "s", nil, "skip specified apps (if they exist), can specify multiple or separate values with comma in a form of <app type>.<name>, e.g.: static.website,service.api")
76+
f.BoolVar(&opts.SkipAllApps, "skip-all-apps", false, "skip all apps (if they exist)")
77+
f.BoolVar(&opts.SkipDNS, "skip-dns", false, "skip DNS setup (including self managed certificates)")
7078

7179
return cmd
7280
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ require (
1717
github.com/mholt/archiver/v3 v3.5.1
1818
github.com/mitchellh/go-homedir v1.1.0
1919
github.com/otiai10/copy v1.7.0
20-
github.com/outblocks/outblocks-plugin-go v0.0.0-20220114122950-1c90ed7fa3e7
20+
github.com/outblocks/outblocks-plugin-go v0.0.0-20220117144659-320323566af7
2121
github.com/pterm/pterm v0.12.33
2222
github.com/r3labs/diff/v2 v2.14.5
2323
github.com/spf13/cobra v1.3.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -741,8 +741,8 @@ github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6
741741
github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo=
742742
github.com/otiai10/mint v1.3.3 h1:7JgpsBaN0uMkyju4tbYHu0mnM55hNKVYLsXmwr15NQI=
743743
github.com/otiai10/mint v1.3.3/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc=
744-
github.com/outblocks/outblocks-plugin-go v0.0.0-20220114122950-1c90ed7fa3e7 h1:vEb/7U8i+8wzPKnpuRmUtdto1COijVaVLKYMguAtg44=
745-
github.com/outblocks/outblocks-plugin-go v0.0.0-20220114122950-1c90ed7fa3e7/go.mod h1:MAxYiF/O+9jcmjHdDg0S0PUV5VLzVcYALlO8VWL+zd0=
744+
github.com/outblocks/outblocks-plugin-go v0.0.0-20220117144659-320323566af7 h1:X//GgYp4zCean+ypS8eFEZyejqtVysLhS9gp8G72KW4=
745+
github.com/outblocks/outblocks-plugin-go v0.0.0-20220117144659-320323566af7/go.mod h1:MAxYiF/O+9jcmjHdDg0S0PUV5VLzVcYALlO8VWL+zd0=
746746
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
747747
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
748748
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=

pkg/actions/deploy.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ type DeployOptions struct {
5353
AutoApprove bool
5454
ForceApprove bool
5555
TargetApps, SkipApps []string
56+
SkipAllApps bool
57+
SkipDNS bool
5658
}
5759

5860
func NewDeploy(log logger.Logger, cfg *config.Project, opts *DeployOptions) *Deploy {
@@ -92,7 +94,7 @@ func (d *Deploy) stateLockRun(ctx context.Context) error {
9294

9395
// Plan and apply.
9496
stateBeforeStr, _ := json.Marshal(state)
95-
empty, canceled, dur, _, err := d.planAndApply(ctx, verify, state, stateRes, nil)
97+
empty, canceled, dur, _, err := d.planAndApply(ctx, verify, state, stateRes, nil, false)
9698

9799
// Proceed with saving.
98100
stateAfterStr, _ := json.Marshal(state)
@@ -124,6 +126,10 @@ func (d *Deploy) stateLockRun(ctx context.Context) error {
124126
}
125127

126128
func (d *Deploy) lockIDs(state *types.StateData, partialLock bool) []string {
129+
if d.opts.SkipAllApps {
130+
return nil
131+
}
132+
127133
targetAppIDsMap := util.StringArrayToSet(d.opts.TargetApps)
128134
skipAppIDsMap := util.StringArrayToSet(d.opts.SkipApps)
129135
lockIDsMap := make(map[string]struct{})
@@ -247,7 +253,7 @@ func (d *Deploy) multilockRun(ctx context.Context) error { // nolint:gocyclo
247253
// Plan and apply.
248254
stateBefore = state.DeepCopy()
249255

250-
empty, canceled, dur, missingLocks, err = d.planAndApply(ctx, verify, state, stateRes, acquiredLocks)
256+
empty, canceled, dur, missingLocks, err = d.planAndApply(ctx, verify, state, stateRes, acquiredLocks, true)
251257
if err != nil {
252258
_ = statePlugin.Client().ReleaseLocks(ctx, d.cfg.State.Other, acquiredLocks)
253259
return err
@@ -331,16 +337,22 @@ func (d *Deploy) Run(ctx context.Context) error {
331337
return d.stateLockRun(ctx)
332338
}
333339

334-
func (d *Deploy) planAndApply(ctx context.Context, verify bool, state *types.StateData, stateRes *apiv1.GetStateResponse_State, acquiredLocks map[string]string) (canceled, empty bool, dur time.Duration, missingLocks []string, err error) {
335-
domains := d.cfg.DomainInfoProto()
340+
func (d *Deploy) planAndApply(ctx context.Context, verify bool, state *types.StateData, stateRes *apiv1.GetStateResponse_State, acquiredLocks map[string]string, checkLocks bool) (canceled, empty bool, dur time.Duration, missingLocks []string, err error) {
341+
var domains []*apiv1.DomainInfo
342+
343+
if d.opts.SkipDNS {
344+
domains = state.DomainsInfo
345+
} else {
346+
domains = d.cfg.DomainInfoProto()
347+
}
336348

337349
// Plan and apply.
338-
appStates, skipAppIDs, destroy, err := filterApps(d.cfg, state, d.opts.TargetApps, d.opts.SkipApps, d.opts.Destroy)
350+
appStates, skipAppIDs, destroy, err := filterApps(d.cfg, state, d.opts.TargetApps, d.opts.SkipApps, d.opts.SkipAllApps, d.opts.Destroy)
339351
if err != nil {
340352
return false, false, dur, nil, err
341353
}
342354

343-
depStates := filterDependencies(d.cfg, state, d.opts.TargetApps, d.opts.SkipApps)
355+
depStates := filterDependencies(d.cfg, state, d.opts.TargetApps, d.opts.SkipApps, d.opts.SkipAllApps)
344356

345357
planMap, err := calculatePlanMap(d.cfg, appStates, depStates, d.opts.TargetApps, skipAppIDs)
346358
if err != nil {
@@ -374,7 +386,7 @@ func (d *Deploy) planAndApply(ctx context.Context, verify bool, state *types.Sta
374386

375387
spinner.Stop()
376388

377-
if acquiredLocks != nil {
389+
if checkLocks {
378390
var missingLocks []string
379391

380392
for _, chg := range deployChanges {
@@ -426,6 +438,8 @@ func (d *Deploy) planAndApply(ctx context.Context, verify bool, state *types.Sta
426438
state.Dependencies[depState.Dependency.Id] = &apiv1.DependencyState{Dependency: depState.Dependency}
427439
}
428440

441+
state.DomainsInfo = domains
442+
429443
return empty, canceled, time.Since(start), nil, err
430444
}
431445

pkg/actions/deploy_filter.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,19 @@ func filterAppsDestroy(state *types.StateData, targetAppIDsMap, skipAppIDsMap ma
7070
return appsMap, skipAppIDs
7171
}
7272

73-
func filterApps(cfg *config.Project, state *types.StateData, targetAppIDs, skipAppIDs []string, destroy bool) (apps []*apiv1.AppState, retSkipAppIDs []string, retDestroy bool, err error) {
73+
func filterApps(cfg *config.Project, state *types.StateData, targetAppIDs, skipAppIDs []string, skipAllApps, destroy bool) (apps []*apiv1.AppState, retSkipAppIDs []string, retDestroy bool, err error) {
74+
if skipAllApps {
75+
retSkipAppIDs := make([]string, 0, len(state.Apps))
76+
apps = make([]*apiv1.AppState, 0, len(state.Apps))
77+
78+
for _, appState := range state.Apps {
79+
apps = append(apps, appState)
80+
retSkipAppIDs = append(retSkipAppIDs, appState.App.Id)
81+
}
82+
83+
return apps, retSkipAppIDs, destroy, nil
84+
}
85+
7486
// In non target and non skip mode, use config apps and deps.
7587
if len(skipAppIDs) == 0 && len(targetAppIDs) == 0 {
7688
apps = make([]*apiv1.AppState, 0, len(cfg.Apps))
@@ -125,7 +137,16 @@ func filterApps(cfg *config.Project, state *types.StateData, targetAppIDs, skipA
125137
return apps, skipAppIDs, false, err
126138
}
127139

128-
func filterDependencies(cfg *config.Project, state *types.StateData, targetAppIDs, skipAppIDs []string) (deps []*apiv1.DependencyState) {
140+
func filterDependencies(cfg *config.Project, state *types.StateData, targetAppIDs, skipAppIDs []string, skipAllApps bool) (deps []*apiv1.DependencyState) {
141+
if skipAllApps {
142+
deps = make([]*apiv1.DependencyState, 0, len(state.Dependencies))
143+
for _, dep := range state.Dependencies {
144+
deps = append(deps, dep)
145+
}
146+
147+
return deps
148+
}
149+
129150
// In non target and non skip mode, use config apps and deps.
130151
if len(skipAppIDs) == 0 && len(targetAppIDs) == 0 {
131152
deps = make([]*apiv1.DependencyState, 0, len(cfg.Dependencies))

pkg/actions/deploy_state.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"reflect"
77
"sort"
8+
"strings"
89

910
"github.com/ansel1/merry/v2"
1011
apiv1 "github.com/outblocks/outblocks-plugin-go/gen/api/v1"
@@ -19,6 +20,7 @@ type stateDiff struct {
1920
dnsRecords diff.Changelog
2021
pluginsRegistry diff.Changelog
2122
pluginsState diff.Changelog
23+
domainsInfo diff.Changelog
2224
}
2325

2426
func (s *stateDiff) IsEmpty() bool {
@@ -44,9 +46,24 @@ func (s *stateDiff) Apply(state *types.StateData) error {
4446
return merry.New("error applying patch on state.dependencies")
4547
}
4648

49+
// Domains info.
50+
domainsInfo := domainsInfoAsMap(state.DomainsInfo)
51+
52+
ret = diff.Patch(s.domainsInfo, &domainsInfo)
53+
if ret.HasErrors() {
54+
return merry.New("error applying patch on state.domainsinfo")
55+
}
56+
57+
state.DomainsInfo = make([]*apiv1.DomainInfo, 0, len(domainsInfo))
58+
59+
for _, d := range domainsInfo {
60+
state.DomainsInfo = append(state.DomainsInfo, d)
61+
}
62+
63+
// DNS Records.
4764
dnsRecords := dnsRecordsAsMap(state.DNSRecords)
4865

49-
ret = diff.Patch(s.dnsRecords, dnsRecords)
66+
ret = diff.Patch(s.dnsRecords, &dnsRecords)
5067
if ret.HasErrors() {
5168
return merry.New("error applying patch on state.dnsrecords")
5269
}
@@ -57,6 +74,7 @@ func (s *stateDiff) Apply(state *types.StateData) error {
5774
state.DNSRecords[rec.Key] = rec.Val
5875
}
5976

77+
// Plugin state.
6078
if state.Plugins == nil {
6179
state.Plugins = make(map[string]*types.PluginState)
6280
}
@@ -118,6 +136,10 @@ func (s *stateDiff) String() string {
118136
out += fmt.Sprintf("DNS = %s\n", formatIndentedJSON(s.dnsRecords))
119137
}
120138

139+
if len(s.domainsInfo) != 0 {
140+
out += fmt.Sprintf("DomainsInfo = %s\n", formatIndentedJSON(s.domainsInfo))
141+
}
142+
121143
if len(s.pluginsRegistry) != 0 {
122144
out += fmt.Sprintf("Registry = %s\n", formatIndentedJSON(s.pluginsRegistry))
123145
}
@@ -152,6 +174,16 @@ func pluginsRegistryAsMap(plugins map[string]*types.PluginState) (map[string]map
152174
return pluginRegistry, nil
153175
}
154176

177+
func domainsInfoAsMap(domains []*apiv1.DomainInfo) map[string]*apiv1.DomainInfo {
178+
m := make(map[string]*apiv1.DomainInfo, len(domains))
179+
180+
for _, d := range domains {
181+
m[strings.Join(d.Domains, ";")] = d
182+
}
183+
184+
return m
185+
}
186+
155187
type dnsRecordValue struct {
156188
Val types.DNSRecordValue
157189
Key types.DNSRecordKey
@@ -206,6 +238,14 @@ func computeStateDiff(state1, state2 *types.StateData) (*stateDiff, error) {
206238
return nil, err
207239
}
208240

241+
state1DomainsInfo := domainsInfoAsMap(state1.DomainsInfo)
242+
state2DomainsInfo := domainsInfoAsMap(state2.DomainsInfo)
243+
244+
domainsInfo, err := computeDiff(state1DomainsInfo, state2DomainsInfo)
245+
if err != nil {
246+
return nil, err
247+
}
248+
209249
state1PluginRegistry, err := pluginsRegistryAsMap(state1.Plugins)
210250
if err != nil {
211251
return nil, err
@@ -233,6 +273,7 @@ func computeStateDiff(state1, state2 *types.StateData) (*stateDiff, error) {
233273
apps: apps,
234274
deps: deps,
235275
dnsRecords: dnsRecords,
276+
domainsInfo: domainsInfo,
236277
pluginsRegistry: pluginsRegistry,
237278
pluginsState: pluginsState,
238279
}, nil

pkg/config/dns.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"path/filepath"
77
"regexp"
8+
"sort"
89
"strings"
910

1011
"github.com/outblocks/outblocks-cli/pkg/plugins"
@@ -43,11 +44,15 @@ func (s *DNS) Match(d string) bool {
4344
}
4445

4546
func (s *DNS) Normalize(i int, cfg *Project) error {
46-
s.Domains = append(s.Domains, s.Domain)
47-
for i, v := range s.Domains {
48-
s.Domains[i] = strings.ToLower(v)
47+
if s.Domain != "" {
48+
s.Domains = append(s.Domains, s.Domain)
49+
for i, v := range s.Domains {
50+
s.Domains[i] = strings.ToLower(v)
51+
}
4952
}
5053

54+
sort.Strings(s.Domains)
55+
5156
if len(s.Domains) == 0 {
5257
return cfg.yamlError(fmt.Sprintf("$.dns[%d]", i), "at least one domain has to be specified")
5358
}

0 commit comments

Comments
 (0)