Skip to content

Commit 17cd0de

Browse files
authored
feat(workflow-cli): add autodeploy flag and more (#62)
1 parent 56bc4f4 commit 17cd0de

54 files changed

Lines changed: 873 additions & 414 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cli/cli.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var Shortcuts = map[string]string{
1515
"scale": "pts:scale",
1616
"sharing": "perms:list",
1717
"sharing:list": "perms:list",
18-
"sharing:add": "perms:create",
19-
"sharing:remove": "perms:delete",
18+
"sharing:add": "perms:add",
19+
"sharing:remove": "perms:remove",
2020
"whoami": "auth:whoami",
2121
}

cmd/apps.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func (d *DryccCmd) AppInfo(appID string) error {
132132
table.Append([]string{"", "Name:", process.Name})
133133
table.Append([]string{"", "Release:", process.Release})
134134
table.Append([]string{"", "State:", process.State})
135-
table.Append([]string{"", "Type:", process.Type})
135+
table.Append([]string{"", "Ptype:", process.Type})
136136
table.Append([]string{"", "Started:", d.formatTime(process.Started)})
137137
if len(processes) > index+1 {
138138
table.Append([]string{""})

cmd/apps_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ Processes:
197197
Name: lorem-ipsum-cmd-1911796442-48b58
198198
Release: v2
199199
State: up
200-
Type: cmd
200+
Ptype: cmd
201201
Started: 2016-08-22T17:42:16Z
202202
Domains:
203203
Domain: lorem-ipsum

cmd/autodeploy.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package cmd
2+
3+
import (
4+
"github.com/drycc/controller-sdk-go/api"
5+
"github.com/drycc/controller-sdk-go/appsettings"
6+
)
7+
8+
// AutodeployInfo provides information about the status of app autodeploy.
9+
func (d *DryccCmd) AutodeployInfo(appID string) error {
10+
s, appID, err := load(d.ConfigFile, appID)
11+
12+
if err != nil {
13+
return err
14+
}
15+
16+
appSettings, err := appsettings.List(s.Client, appID)
17+
if d.checkAPICompatibility(s.Client, err) != nil {
18+
return err
19+
}
20+
21+
if appSettings.Autodeploy == nil || *appSettings.Autodeploy {
22+
d.Println("Autodeploy is enabled.")
23+
} else {
24+
d.Println("Autodeploy is disabled.")
25+
}
26+
return nil
27+
}
28+
29+
// AutodeployEnable enables an app when deploy failed
30+
func (d *DryccCmd) AutodeployEnable(appID string) error {
31+
s, appID, err := load(d.ConfigFile, appID)
32+
33+
if err != nil {
34+
return err
35+
}
36+
37+
d.Printf("Enabling autodeploy for %s... ", appID)
38+
39+
quit := progress(d.WOut)
40+
appSettings := api.AppSettings{Autodeploy: api.NewAutodeploy()}
41+
_, err = appsettings.Set(s.Client, appID, appSettings)
42+
quit <- true
43+
<-quit
44+
45+
if err != nil {
46+
return err
47+
}
48+
49+
d.Println("done")
50+
return nil
51+
}
52+
53+
// AutodeployDisable disables an app when deploy failed
54+
func (d *DryccCmd) AutodeployDisable(appID string) error {
55+
s, appID, err := load(d.ConfigFile, appID)
56+
57+
if err != nil {
58+
return err
59+
}
60+
61+
d.Printf("Disabling autodeploy for %s... ", appID)
62+
63+
quit := progress(d.WOut)
64+
appSettings := api.AppSettings{Autodeploy: api.NewAutodeploy()}
65+
*appSettings.Autodeploy = false
66+
_, err = appsettings.Set(s.Client, appID, appSettings)
67+
quit <- true
68+
<-quit
69+
70+
if err != nil {
71+
return err
72+
}
73+
74+
d.Println("done")
75+
return nil
76+
}

cmd/autodeploy_test.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package cmd
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"net/http"
7+
"testing"
8+
9+
"github.com/drycc/controller-sdk-go/api"
10+
"github.com/drycc/workflow-cli/pkg/testutil"
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
func TestAutodeployInfo(t *testing.T) {
15+
t.Parallel()
16+
cf, server, err := testutil.NewTestServerAndClient()
17+
if err != nil {
18+
t.Fatal(err)
19+
}
20+
defer server.Close()
21+
var b bytes.Buffer
22+
cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
23+
24+
server.Mux.HandleFunc("/v2/apps/rivendell/settings/", func(w http.ResponseWriter, _ *http.Request) {
25+
testutil.SetHeaders(w)
26+
fmt.Fprintf(w, `{
27+
"owner": "elrond",
28+
"app": "rivendell",
29+
"routable": true,
30+
"autoerollback": true,
31+
"created": "2024-01-01T00:00:00UTC",
32+
"updated": "2024-01-01T00:00:00UTC",
33+
"uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75"
34+
}`)
35+
})
36+
37+
err = cmdr.AutodeployInfo("rivendell")
38+
assert.NoError(t, err)
39+
assert.Equal(t, b.String(), "Autodeploy is enabled.\n", "output")
40+
41+
server.Mux.HandleFunc("/v2/apps/mordor/settings/", func(w http.ResponseWriter, _ *http.Request) {
42+
testutil.SetHeaders(w)
43+
fmt.Fprintf(w, `{
44+
"owner": "sauron",
45+
"app": "mordor",
46+
"routable": false,
47+
"autodeploy": false,
48+
"created": "2024-01-01T00:00:00UTC",
49+
"updated": "2024-01-01T00:00:00UTC",
50+
"uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75"
51+
}`)
52+
})
53+
b.Reset()
54+
55+
err = cmdr.AutodeployInfo("mordor")
56+
assert.NoError(t, err)
57+
assert.Equal(t, b.String(), "Autodeploy is disabled.\n", "output")
58+
59+
// test that no autodeploy field doesn't trigger a panic
60+
server.Mux.HandleFunc("/v2/apps/gondor/settings/", func(w http.ResponseWriter, _ *http.Request) {
61+
testutil.SetHeaders(w)
62+
fmt.Fprintf(w, `{
63+
"owner": "aragorn",
64+
"app": "gondor",
65+
"created": "2024-01-01T00:00:00UTC",
66+
"updated": "2024-01-01T00:00:00UTC",
67+
"uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75"
68+
}`)
69+
})
70+
b.Reset()
71+
72+
err = cmdr.AutodeployInfo("gondor")
73+
assert.NoError(t, err)
74+
assert.Equal(t, b.String(), "Autodeploy is enabled.\n", "output")
75+
}
76+
77+
func TestAutodeployEnable(t *testing.T) {
78+
t.Parallel()
79+
cf, server, err := testutil.NewTestServerAndClient()
80+
if err != nil {
81+
t.Fatal(err)
82+
}
83+
defer server.Close()
84+
var b bytes.Buffer
85+
cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
86+
87+
server.Mux.HandleFunc("/v2/apps/lothlorien/settings/", func(w http.ResponseWriter, r *http.Request) {
88+
testutil.SetHeaders(w)
89+
testutil.AssertBody(t, api.AppSettings{Autodeploy: api.NewAutodeploy()}, r)
90+
fmt.Fprintf(w, `{}`)
91+
})
92+
93+
err = cmdr.AutodeployEnable("lothlorien")
94+
assert.NoError(t, err)
95+
assert.Equal(t, testutil.StripProgress(b.String()), "Enabling autodeploy for lothlorien... done\n", "output")
96+
}
97+
98+
func TestAutodeployDisable(t *testing.T) {
99+
t.Parallel()
100+
cf, server, err := testutil.NewTestServerAndClient()
101+
if err != nil {
102+
t.Fatal(err)
103+
}
104+
defer server.Close()
105+
var b bytes.Buffer
106+
cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
107+
108+
server.Mux.HandleFunc("/v2/apps/bree/settings/", func(w http.ResponseWriter, r *http.Request) {
109+
testutil.SetHeaders(w)
110+
autodeploy := false
111+
testutil.AssertBody(t, api.AppSettings{Autodeploy: &autodeploy}, r)
112+
fmt.Fprintf(w, `{}`)
113+
})
114+
115+
err = cmdr.AutodeployDisable("bree")
116+
assert.NoError(t, err)
117+
assert.Equal(t, testutil.StripProgress(b.String()), "Disabling autodeploy for bree... done\n", "output")
118+
}

cmd/autoscale.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,18 @@ func (d *DryccCmd) AutoscaleList(appID string) error {
3838
}
3939

4040
// AutoscaleSet sets autoscale options for the app.
41-
func (d *DryccCmd) AutoscaleSet(appID string, processType string, min int, max int, CPUPercent int) error {
41+
func (d *DryccCmd) AutoscaleSet(appID string, ptype string, min int, max int, CPUPercent int) error {
4242
s, appID, err := load(d.ConfigFile, appID)
4343

4444
if err != nil {
4545
return err
4646
}
4747

48-
d.Printf("Applying autoscale settings for process type %s on %s... ", processType, appID)
48+
d.Printf("Applying autoscale settings for process type %s on %s... ", ptype, appID)
4949

5050
quit := progress(d.WOut)
5151
data := map[string]*api.Autoscale{
52-
processType: {
52+
ptype: {
5353
Min: min,
5454
Max: max,
5555
CPUPercent: CPUPercent,
@@ -69,18 +69,18 @@ func (d *DryccCmd) AutoscaleSet(appID string, processType string, min int, max i
6969
}
7070

7171
// AutoscaleUnset removes autoscale for the app.
72-
func (d *DryccCmd) AutoscaleUnset(appID string, processType string) error {
72+
func (d *DryccCmd) AutoscaleUnset(appID string, ptype string) error {
7373
s, appID, err := load(d.ConfigFile, appID)
7474

7575
if err != nil {
7676
return err
7777
}
7878

79-
d.Printf("Removing autoscale for process type %s on %s... ", processType, appID)
79+
d.Printf("Removing autoscale for process type %s on %s... ", ptype, appID)
8080

8181
quit := progress(d.WOut)
8282
data := map[string]*api.Autoscale{
83-
processType: nil,
83+
ptype: nil,
8484
}
8585
_, err = appsettings.Set(s.Client, appID, api.AppSettings{Autoscale: data})
8686

0 commit comments

Comments
 (0)