Skip to content

Commit 2ad051c

Browse files
committed
alpha: monitoring plugin support
1 parent 69f7263 commit 2ad051c

File tree

18 files changed

+497
-39
lines changed

18 files changed

+497
-39
lines changed

cmd/deploy.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ func (e *Executor) newDeployCmd() *cobra.Command {
103103
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")
104104
f.BoolVar(&opts.SkipAllApps, "skip-all-apps", false, "skip all apps (if they exist)")
105105
f.BoolVar(&opts.SkipDNS, "skip-dns", false, "skip DNS setup (including self managed certificates)")
106+
f.BoolVar(&opts.SkipMonitoring, "skip-monitoring", false, "skip monitoring setup")
106107

107108
return cmd
108109
}

cmd/status.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func (e *Executor) newStatusCmd() *cobra.Command {
2222
SkipAllApps: true,
2323
SkipBuild: true,
2424
SkipDNS: true,
25+
SkipMonitoring: true,
2526
SkipApply: true,
2627
SkipDiff: true,
2728
SkipStateCreate: true,

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ require (
2121
github.com/mholt/archiver/v3 v3.5.1
2222
github.com/mitchellh/go-homedir v1.1.0
2323
github.com/otiai10/copy v1.7.0
24-
github.com/outblocks/outblocks-plugin-go v0.0.0-20220712161354-7d8111dfc469
24+
github.com/outblocks/outblocks-plugin-go v0.0.0-20220803192450-7744c1c50028
2525
github.com/pkg/errors v0.9.1
2626
github.com/pterm/pterm v0.12.41
2727
github.com/santhosh-tekuri/jsonschema/v5 v5.0.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,8 @@ github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6
339339
github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo=
340340
github.com/otiai10/mint v1.3.3 h1:7JgpsBaN0uMkyju4tbYHu0mnM55hNKVYLsXmwr15NQI=
341341
github.com/otiai10/mint v1.3.3/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc=
342-
github.com/outblocks/outblocks-plugin-go v0.0.0-20220712161354-7d8111dfc469 h1:KviouI7jaoEij8qlvLlKf6EMcUwCWWrwtQ7kqFo+OnY=
343-
github.com/outblocks/outblocks-plugin-go v0.0.0-20220712161354-7d8111dfc469/go.mod h1:Wnb50otE4YCKHxyxpnJi1G3iYVW5pnTAeLHpo3Mu0Lk=
342+
github.com/outblocks/outblocks-plugin-go v0.0.0-20220803192450-7744c1c50028 h1:PjHUEGVTudKUesESpBm9ukAIwvMYvTZTsOpBOQqITGI=
343+
github.com/outblocks/outblocks-plugin-go v0.0.0-20220803192450-7744c1c50028/go.mod h1:Wnb50otE4YCKHxyxpnJi1G3iYVW5pnTAeLHpo3Mu0Lk=
344344
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
345345
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
346346
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=

internal/statefile/state.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,9 @@ type StateData struct {
9999
Dependencies map[string]*apiv1.DependencyState `json:"dependencies"`
100100
Plugins map[string]*PluginState `json:"plugins_state"` // plugin name -> object -> state
101101

102-
DNSRecords DNSRecordMap `json:"dns_records"`
103-
DomainsInfo []*apiv1.DomainInfo `json:"domains_info"`
102+
DNSRecords DNSRecordMap `json:"dns_records"`
103+
DomainsInfo []*apiv1.DomainInfo `json:"domains_info"`
104+
Monitoring *apiv1.MonitoringData `json:"monitoring"`
104105
}
105106

106107
func NewStateData() *StateData {

pkg/actions/deploy.go

Lines changed: 79 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ type DeployOptions struct {
6868
TargetApps, SkipApps []string
6969
SkipAllApps bool
7070
SkipDNS bool
71+
SkipMonitoring bool
7172
SkipDiff bool
7273
SkipApply bool
7374
SkipStateCreate bool
@@ -104,7 +105,7 @@ func (d *Deploy) stateLockDeploy(ctx context.Context, state *statefile.StateData
104105
return nil, err
105106
}
106107

107-
res, err := d.planAndApplyDeploy(ctx, verify, state, nil, false)
108+
res, err := d.planAndApply(ctx, verify, state, nil, false)
108109
if res == nil && err != nil {
109110
return nil, err
110111
}
@@ -255,7 +256,7 @@ func (d *Deploy) checkIfDNSAreUsed(stateApps map[string]*apiv1.AppState, showWar
255256
continue
256257
}
257258

258-
u, _ := config.ParseAppURL(app.App.Url)
259+
u, _ := config.ParseURL(app.App.Url, false)
259260
if u == nil {
260261
continue
261262
}
@@ -374,7 +375,7 @@ func (d *Deploy) multilockPlanAndApplyDeploy(ctx context.Context, state *statefi
374375

375376
showWarnings = false
376377

377-
res, err := d.planAndApplyDeploy(ctx, verify, state, acquiredLocks, true)
378+
res, err := d.planAndApply(ctx, verify, state, acquiredLocks, true)
378379
if err != nil {
379380
_ = releaseLocks(d.cfg, acquiredLocks)
380381
d.log.Debugf("Released locks: %s\n", acquiredLocks)
@@ -512,11 +513,15 @@ func (d *Deploy) Run(ctx context.Context) error {
512513
return nil
513514
}
514515

515-
func (d *Deploy) promptDiff(deployChanges, dnsChanges []*change, acquiredLocks map[string]string, checkLocks bool) (empty, canceled bool, missingLocks []string) {
516+
func (d *Deploy) promptDiff(deployChanges, dnsChanges, monitoringChanges []*change, acquiredLocks map[string]string, checkLocks bool) (empty, canceled bool, missingLocks []string) {
516517
missingLocksMap := make(map[string]struct{})
517518

519+
changes := deployChanges
520+
changes = append(changes, dnsChanges...)
521+
changes = append(changes, monitoringChanges...)
522+
518523
if checkLocks {
519-
for _, chg := range append(deployChanges, dnsChanges...) {
524+
for _, chg := range changes {
520525
var lockID string
521526

522527
switch {
@@ -543,10 +548,10 @@ func (d *Deploy) promptDiff(deployChanges, dnsChanges []*change, acquiredLocks m
543548
}
544549

545550
if d.opts.SkipDiff {
546-
return len(deployChanges) == 0 && len(dnsChanges) == 0, false, missingLocks
551+
return len(changes) == 0, false, missingLocks
547552
}
548553

549-
empty, canceled = planPrompt(d.log, d.cfg.Env(), deployChanges, dnsChanges, d.opts.AutoApprove, d.opts.ForceApprove)
554+
empty, canceled = planPrompt(d.log, d.cfg.Env(), deployChanges, dnsChanges, monitoringChanges, d.opts.AutoApprove, d.opts.ForceApprove)
550555

551556
return empty, canceled, missingLocks
552557
}
@@ -572,7 +577,7 @@ func computeDomainsInfo(cfg *config.Project, state *statefile.StateData) []*apiv
572577
continue
573578
}
574579

575-
u, _ := config.ParseAppURL(app.App.Url)
580+
u, _ := config.ParseURL(app.App.Url, false)
576581
if u == nil {
577582
continue
578583
}
@@ -644,14 +649,22 @@ func computeDomainsInfo(cfg *config.Project, state *statefile.StateData) []*apiv
644649
return ret
645650
}
646651

647-
func (d *Deploy) planAndApplyDeploy(ctx context.Context, verify bool, state *statefile.StateData, acquiredLocks map[string]string, checkLocks bool) (*planAndApplyResults, error) { // nolint: gocyclo
652+
func (d *Deploy) planAndApply(ctx context.Context, verify bool, state *statefile.StateData, acquiredLocks map[string]string, checkLocks bool) (*planAndApplyResults, error) { // nolint: gocyclo
648653
var domains []*apiv1.DomainInfo
649654

650655
ret := &planAndApplyResults{
651656
acquiredLocks: acquiredLocks,
652657
}
653658
stateBefore := state.DeepCopy()
654659

660+
monitoring := d.cfg.Monitoring.Proto()
661+
662+
if d.opts.SkipMonitoring || d.opts.Destroy {
663+
monitoring = state.Monitoring
664+
} else {
665+
state.Monitoring = monitoring
666+
}
667+
655668
if d.opts.SkipDNS || d.opts.Destroy {
656669
domains = state.DomainsInfo
657670
} else {
@@ -715,12 +728,19 @@ func (d *Deploy) planAndApplyDeploy(ctx context.Context, verify bool, state *sta
715728
return nil, err
716729
}
717730

731+
monitoringChanges, err := d.planMonitoring(ctx, state, monitoring, verify, destroy)
732+
if err != nil {
733+
spinner.Stop()
734+
735+
return nil, err
736+
}
737+
718738
spinner.Stop()
719739

720740
deployChanges := computeDeployChange(d.cfg, &oldState, state, planRetMap)
721741
dnsChanges := computeDNSChange(d.cfg, &oldState, state, planDNSRetMap)
722742

723-
ret.empty, ret.canceled, ret.missingLocks = d.promptDiff(deployChanges, dnsChanges, acquiredLocks, checkLocks)
743+
ret.empty, ret.canceled, ret.missingLocks = d.promptDiff(deployChanges, dnsChanges, monitoringChanges, acquiredLocks, checkLocks)
724744
if len(ret.missingLocks) != 0 {
725745
return ret, nil
726746
}
@@ -749,6 +769,13 @@ func (d *Deploy) planAndApplyDeploy(ctx context.Context, verify bool, state *sta
749769
prog.Stop()
750770
}
751771

772+
if err == nil {
773+
prog, cb := applyProgress(d.log, monitoringChanges)
774+
err = d.applyMonitoring(context.Background(), state, monitoring, destroy, cb)
775+
776+
prog.Stop()
777+
}
778+
752779
if err == nil {
753780
err = d.postApplyHook(ctx, state, apps, deps, verify, destroy)
754781
}
@@ -1425,3 +1452,45 @@ func (d *Deploy) applyDNS(ctx context.Context, state *statefile.StateData, planM
14251452

14261453
return g.Wait()
14271454
}
1455+
1456+
func (d *Deploy) planMonitoring(ctx context.Context, state *statefile.StateData, monitoring *apiv1.MonitoringData, verify, destroy bool) ([]*change, error) {
1457+
if monitoring.Plugin == "" {
1458+
return nil, nil
1459+
}
1460+
1461+
plug := d.cfg.FindLoadedPlugin(monitoring.Plugin)
1462+
if plug == nil {
1463+
return nil, merry.Errorf("missing monitoring plugin: %s", plug.Name)
1464+
}
1465+
1466+
ret, err := plug.Client().PlanMonitoring(ctx, state, monitoring, plug.CommandArgs(deployCommand), verify, destroy)
1467+
if err != nil {
1468+
return nil, err
1469+
}
1470+
1471+
mergeState(state, plug.Name, ret.State, nil, nil, nil, nil)
1472+
1473+
chg := computeChangeInfo(d.cfg, state, plug, ret.Plan.Actions)
1474+
1475+
return chg, nil
1476+
}
1477+
1478+
func (d *Deploy) applyMonitoring(ctx context.Context, state *statefile.StateData, monitoring *apiv1.MonitoringData, destroy bool, callback func(*apiv1.ApplyAction)) error {
1479+
if monitoring.Plugin == "" {
1480+
return nil
1481+
}
1482+
1483+
plug := d.cfg.FindLoadedPlugin(monitoring.Plugin)
1484+
if plug == nil {
1485+
return merry.Errorf("missing monitoring plugin: %s", plug.Name)
1486+
}
1487+
1488+
ret, err := plug.Client().ApplyMonitoring(ctx, state, monitoring, plug.CommandArgs(deployCommand), destroy, callback)
1489+
if err != nil {
1490+
return err
1491+
}
1492+
1493+
mergeState(state, plug.Name, ret.State, nil, nil, nil, nil)
1494+
1495+
return nil
1496+
}

pkg/actions/run.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,13 @@ func (d *Run) prepareRun(cfg *config.Project) (*runInfo, error) {
196196
appType.Properties = plugin_util.MustNewStruct(mergedProps)
197197

198198
appRun := &apiv1.AppRun{
199-
App: appType,
200-
Url: d.localURL(app.URL(), appPort, app.PathRedirect()),
201-
Ip: loopbackIP,
202-
Port: int32(appPort),
203-
Command: app.RunInfo().Command.Array(),
204-
Env: app.RunInfo().Env,
205-
Other: plugin_util.MustNewStruct(app.RunInfo().Other),
199+
App: appType,
200+
Url: d.localURL(app.URL(), appPort, app.PathRedirect()),
201+
Ip: loopbackIP,
202+
Port: int32(appPort),
203+
Command: app.RunInfo().Command.Array(),
204+
Env: app.RunInfo().Env,
205+
Properties: plugin_util.MustNewStruct(app.RunInfo().Other),
206206
}
207207

208208
info.apps = append(info.apps, appRun)

pkg/actions/util.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,11 @@ func computeDeployChange(cfg *config.Project, oldState, state *statefile.StateDa
135135

136136
for plugin, reslist := range m {
137137
for _, p := range reslist {
138-
if p.Deploy == nil {
138+
if p.Plan == nil {
139139
continue
140140
}
141141

142-
chg := computeChangeInfo(cfg, state, plugin, p.Deploy.Actions)
142+
chg := computeChangeInfo(cfg, state, plugin, p.Plan.Actions)
143143
changes = append(changes, chg...)
144144
}
145145
}
@@ -151,11 +151,11 @@ func computeDNSChange(cfg *config.Project, oldState, state *statefile.StateData,
151151
var changes []*change
152152

153153
for plugin, p := range m {
154-
if p.Dns == nil {
154+
if p.Plan == nil {
155155
continue
156156
}
157157

158-
chg := computeChangeInfo(cfg, state, plugin, p.Dns.Actions)
158+
chg := computeChangeInfo(cfg, state, plugin, p.Plan.Actions)
159159
changes = append(changes, chg...)
160160
}
161161

@@ -242,7 +242,7 @@ func planChangeInfo(header string, changes []*change) (info string, anyCritical
242242
return info, anyCritical
243243
}
244244

245-
func planPrompt(log logger.Logger, env string, deploy, dns []*change, approve, force bool) (empty, canceled bool) {
245+
func planPrompt(log logger.Logger, env string, deploy, dns, monitoring []*change, approve, force bool) (empty, canceled bool) {
246246
sort.Slice(deploy, func(i, j int) bool {
247247
if deploy[i].app == nil && deploy[j].app != nil {
248248
return false
@@ -275,13 +275,21 @@ func planPrompt(log logger.Logger, env string, deploy, dns []*change, approve, f
275275
info = append(info, dnsInfo)
276276
}
277277

278+
// Monitoring
279+
monitoringInfo, monitoringCritical := planChangeInfo("Monitoring:", monitoring)
280+
if monitoringInfo != "" {
281+
empty = false
282+
283+
info = append(info, monitoringInfo)
284+
}
285+
278286
if empty {
279287
log.Println("No changes detected.")
280288

281289
return true, false
282290
}
283291

284-
critical = deployCritical || dnsCritical
292+
critical = deployCritical || dnsCritical || monitoringCritical
285293

286294
log.Println(strings.Join(info, "\n\n"))
287295

pkg/config/app.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ type AppDeployInfo struct {
6060

6161
func (i *AppDeployInfo) Proto() *apiv1.AppDeployInfo {
6262
return &apiv1.AppDeployInfo{
63-
Plugin: i.Plugin,
64-
Env: i.Env,
65-
Other: plugin_util.MustNewStruct(i.Other),
63+
Plugin: i.Plugin,
64+
Env: i.Env,
65+
Properties: plugin_util.MustNewStruct(i.Other),
6666
}
6767
}
6868

@@ -132,7 +132,7 @@ func (a *BasicApp) Validate() error {
132132
)
133133
}
134134

135-
func ParseAppURL(u string) (*url.URL, error) {
135+
func ParseURL(u string, normalize bool) (*url.URL, error) {
136136
if u == "" {
137137
return nil, nil
138138
}
@@ -146,6 +146,12 @@ func ParseAppURL(u string) (*url.URL, error) {
146146
return nil, err
147147
}
148148

149+
if normalize {
150+
if parsed.Path == "" {
151+
parsed.Path = "/"
152+
}
153+
}
154+
149155
return parsed, nil
150156
}
151157

@@ -188,7 +194,7 @@ func (a *BasicApp) Normalize(cfg *Project) error {
188194
a.AppRun.Plugin = strings.ToLower(a.AppRun.Plugin)
189195
a.AppURL = strings.ToLower(a.AppURL)
190196

191-
a.url, err = ParseAppURL(a.AppURL)
197+
a.url, err = ParseURL(a.AppURL, false)
192198
if err != nil {
193199
return a.YAMLError("$.url", "url is invalid")
194200
}

pkg/config/dns.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ func (s *DNS) Proto() *apiv1.DomainInfo {
4444
}
4545

4646
return &apiv1.DomainInfo{
47-
Domains: s.Domains,
48-
Cert: s.SSLInfo.loadedCert,
49-
Key: s.SSLInfo.loadedKey,
50-
DnsPlugin: dnsPlugin,
51-
Other: plugin_util.MustNewStruct(s.Other),
47+
Domains: s.Domains,
48+
Cert: s.SSLInfo.loadedCert,
49+
Key: s.SSLInfo.loadedKey,
50+
DnsPlugin: dnsPlugin,
51+
Properties: plugin_util.MustNewStruct(s.Other),
5252
}
5353
}
5454

0 commit comments

Comments
 (0)