Skip to content

Commit 662fba3

Browse files
committed
feat(release): add deploy release
1 parent f620e20 commit 662fba3

10 files changed

Lines changed: 124 additions & 10 deletions

File tree

cmd/cmd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ type Commander interface {
9797
RegistryUnset(string, []string) error
9898
ReleasesList(string, int) error
9999
ReleasesInfo(string, int) error
100+
ReleasesDeploy(string, []string, string) error
100101
ReleasesRollback(string, int) error
101102
RoutingInfo(string) error
102103
RoutingEnable(string) error

cmd/pts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (d *DryccCmd) PtsRestart(appID string, targets []string, confirm string) er
103103
quit := progress(d.WOut)
104104
ptypes := strings.Join(targets, ",")
105105
targetMap := map[string]string{
106-
"types": ptypes,
106+
"ptypes": ptypes,
107107
}
108108
err = pts.Restart(s.Client, appID, targetMap)
109109
quit <- true

cmd/pts_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func TestPtsRestart(t *testing.T) {
150150
cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
151151

152152
server.Mux.HandleFunc("/v2/apps/foo/ptypes/restart/", func(w http.ResponseWriter, r *http.Request) {
153-
testutil.AssertBody(t, map[string]string{"types": ""}, r)
153+
testutil.AssertBody(t, map[string]string{"ptypes": ""}, r)
154154
testutil.SetHeaders(w)
155155
w.WriteHeader(http.StatusNoContent)
156156
})
@@ -160,7 +160,7 @@ func TestPtsRestart(t *testing.T) {
160160
assert.NoError(t, err)
161161

162162
server.Mux.HandleFunc("/v2/apps/coolapp/ptypes/restart/", func(w http.ResponseWriter, r *http.Request) {
163-
testutil.AssertBody(t, map[string]string{"types": "web"}, r)
163+
testutil.AssertBody(t, map[string]string{"ptypes": "web"}, r)
164164
testutil.SetHeaders(w)
165165
w.WriteHeader(http.StatusNoContent)
166166
})
@@ -171,7 +171,7 @@ func TestPtsRestart(t *testing.T) {
171171
assert.NoError(t, err)
172172

173173
server.Mux.HandleFunc("/v2/apps/testapp/ptypes/restart/", func(w http.ResponseWriter, r *http.Request) {
174-
testutil.AssertBody(t, map[string]string{"types": "web,worker"}, r)
174+
testutil.AssertBody(t, map[string]string{"ptypes": "web,worker"}, r)
175175
testutil.SetHeaders(w)
176176
w.WriteHeader(http.StatusNoContent)
177177
})

cmd/releases.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package cmd
22

33
import (
44
"fmt"
5+
"strings"
6+
"time"
57

68
"github.com/drycc/controller-sdk-go/releases"
79
)
@@ -65,6 +67,42 @@ func (d *DryccCmd) ReleasesInfo(appID string, version int) error {
6567
return nil
6668
}
6769

70+
// ReleasesDeploy force deploy lastest release.
71+
func (d *DryccCmd) ReleasesDeploy(appID string, targets []string, confirm string) error {
72+
s, appID, err := load(d.ConfigFile, appID)
73+
if err != nil {
74+
return err
75+
}
76+
if len(targets) == 0 && (confirm == "" || confirm != "yes") {
77+
d.Printf(` ! WARNING: Potentially Deploy Action
78+
! This command will deploy all processes of the application ptype
79+
! To proceed, type "yes" !
80+
81+
> `)
82+
83+
fmt.Scanln(&confirm)
84+
if confirm != "yes" {
85+
return fmt.Errorf("cancel the deploy action")
86+
}
87+
}
88+
d.Printf("Deploying process types... but first, %s!\n", drinkOfChoice())
89+
startTime := time.Now()
90+
quit := progress(d.WOut)
91+
ptypes := strings.Join(targets, ",")
92+
targetMap := map[string]string{
93+
"ptypes": ptypes,
94+
}
95+
err = releases.Deploy(s.Client, appID, targetMap)
96+
quit <- true
97+
<-quit
98+
if err != nil {
99+
return err
100+
}
101+
102+
d.Printf("done in %ds\n", int(time.Since(startTime).Seconds()))
103+
return nil
104+
}
105+
68106
// ReleasesRollback rolls an app back to a previous release.
69107
func (d *DryccCmd) ReleasesRollback(appID string, version int) error {
70108
s, appID, err := load(d.ConfigFile, appID)

cmd/releases_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,36 @@ Version: v2
146146
`, "output")
147147
}
148148

149+
func TestReleasesDeploy(t *testing.T) {
150+
t.Parallel()
151+
cf, server, err := testutil.NewTestServerAndClient()
152+
if err != nil {
153+
t.Fatal(err)
154+
}
155+
defer server.Close()
156+
var b bytes.Buffer
157+
cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
158+
server.Mux.HandleFunc("/v2/apps/example-go/releases/deploy/", func(res http.ResponseWriter, req *http.Request) {
159+
testutil.SetHeaders(res)
160+
io.ReadAll(req.Body)
161+
if err != nil {
162+
fmt.Println(err)
163+
res.WriteHeader(http.StatusInternalServerError)
164+
res.Write(nil)
165+
}
166+
167+
res.WriteHeader(http.StatusCreated)
168+
})
169+
err = cmdr.ReleasesDeploy("example-go", []string{}, "yes")
170+
if err != nil {
171+
t.Fatal(err)
172+
}
173+
err = cmdr.ReleasesDeploy("example-go", []string{"web", "task"}, "")
174+
if err != nil {
175+
t.Fatal(err)
176+
}
177+
}
178+
149179
func TestReleasesRollback(t *testing.T) {
150180
t.Parallel()
151181
cf, server, err := testutil.NewTestServerAndClient()

cmd/volumes.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ func (d *DryccCmd) volumesClientGetAll(client *drycc.Client, appID, volumeID, vo
318318
return nil
319319
}
320320

321-
func (d *DryccCmd) volumesClientPostAll(client *drycc.Client, appID, volumeID, volumePath string, localPath string) error {
321+
func (d *DryccCmd) volumesClientPostAll(client *drycc.Client, appID, volumeID, volumePath, localPath string) error {
322322
if file, err := os.Stat(localPath); err != nil {
323323
return err
324324
} else if !file.IsDir() {
@@ -374,7 +374,10 @@ func (d *DryccCmd) volumesClientCp(appID, src, dst string) error {
374374
if err != nil {
375375
return err
376376
}
377-
return d.volumesClientGetAll(s.Client, appID, volumeID, volumePath, mergeDestDir(dst, src))
377+
if dirs, _, err := volumes.ListDir(s.Client, appID, volumeID, volumePath, 3000); err == nil && (len(dirs) != 1 || dirs[0].Type != "file") {
378+
dst = mergeDestDir(dst, volumePath)
379+
}
380+
return d.volumesClientGetAll(s.Client, appID, volumeID, volumePath, dst)
378381
} else if strings.HasPrefix(dst, "vol://") {
379382
volumeID, volumePath, err := parseVol(dst)
380383
if err != nil {
@@ -387,7 +390,10 @@ func (d *DryccCmd) volumesClientCp(appID, src, dst string) error {
387390
} else if strings.Contains(fmt.Sprint(err), "no such file or directory") {
388391
return err
389392
}
390-
return d.volumesClientPostAll(s.Client, appID, volumeID, mergeDestDir(volumePath, src), src)
393+
if file, err := os.Stat(src); err == nil && file.IsDir() {
394+
volumePath = mergeDestDir(volumePath, src)
395+
}
396+
return d.volumesClientPostAll(s.Client, appID, volumeID, volumePath, src)
391397
}
392398
return nil
393399
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.22
55
require (
66
github.com/containerd/console v1.0.4
77
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815
8-
github.com/drycc/controller-sdk-go v0.0.0-20240819081928-b589d0b69f71
8+
github.com/drycc/controller-sdk-go v0.0.0-20240826030716-d88e1466a39a
99
github.com/drycc/pkg v0.0.0-20240225112316-78fc9239f51f
1010
github.com/minio/selfupdate v0.6.0
1111
github.com/olekukonko/tablewriter v0.0.5

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
77
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
88
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 h1:bWDMxwH3px2JBh6AyO7hdCn/PkvCZXii8TGj7sbtEbQ=
99
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
10-
github.com/drycc/controller-sdk-go v0.0.0-20240819081928-b589d0b69f71 h1:NQ027uxfCqPIOozokIUrWxsZi0Tpjo70N573UoYV7bM=
11-
github.com/drycc/controller-sdk-go v0.0.0-20240819081928-b589d0b69f71/go.mod h1:n6eQe1irJqjwLo/7t9+Dhdv6faSESQN+ATnZRBP3/Uc=
10+
github.com/drycc/controller-sdk-go v0.0.0-20240826030716-d88e1466a39a h1:wHuz+RT4RX8J95CD8daSe0qo+h+iRyjHTIFymxIM0KE=
11+
github.com/drycc/controller-sdk-go v0.0.0-20240826030716-d88e1466a39a/go.mod h1:n6eQe1irJqjwLo/7t9+Dhdv6faSESQN+ATnZRBP3/Uc=
1212
github.com/drycc/pkg v0.0.0-20240225112316-78fc9239f51f h1:kgjvUQJeAszDoU1Vo4vTTE92KI8Av3JPb6Qn890niXg=
1313
github.com/drycc/pkg v0.0.0-20240225112316-78fc9239f51f/go.mod h1:n+QxGif6ha9CEoxVnlipxb9IdmerybcUSzTEDFkvjiA=
1414
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=

parser/releases.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Valid commands for releases:
1515
1616
releases:list list an application's release history
1717
releases:info print information about a specific release
18+
releases:deploy deploy the latest release by process types
1819
releases:rollback return to a previous release
1920
2021
Use 'drycc help [command]' to learn more.
@@ -25,6 +26,8 @@ Use 'drycc help [command]' to learn more.
2526
return releasesList(argv, cmdr)
2627
case "releases:info":
2728
return releasesInfo(argv, cmdr)
29+
case "releases:deploy":
30+
return releasesDeploy(argv, cmdr)
2831
case "releases:rollback":
2932
return releasesRollback(argv, cmdr)
3033
default:
@@ -101,6 +104,34 @@ Options:
101104
return cmdr.ReleasesInfo(app, version)
102105
}
103106

107+
func releasesDeploy(argv []string, cmdr cmd.Commander) error {
108+
usage := `
109+
Deploy the latest release by process types.
110+
111+
Usage: drycc releases:deploy [<ptype>...] [options]
112+
113+
Arguments:
114+
<ptype>
115+
the process name as defined in your Procfile, such as 'web' or 'web worker'.
116+
117+
Options:
118+
-a --app=<app>
119+
the uniquely identifiable name for the application.
120+
--confirm=yes
121+
To proceed, type "yes".
122+
`
123+
124+
args, err := docopt.ParseArgs(usage, argv, "")
125+
126+
if err != nil {
127+
return err
128+
}
129+
130+
apps := safeGetString(args, "--app")
131+
confirm := safeGetString(args, "--confirm")
132+
return cmdr.ReleasesDeploy(apps, args["<ptype>"].([]string), confirm)
133+
}
134+
104135
func releasesRollback(argv []string, cmdr cmd.Commander) error {
105136
usage := `
106137
Rolls back to a previous application release.

parser/releases_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ func (d FakeDryccCmd) ReleasesInfo(string, int) error {
2020
return errors.New("releases:info")
2121
}
2222

23+
func (d FakeDryccCmd) ReleasesDeploy(string, []string, string) error {
24+
return errors.New("releases:deploy")
25+
}
26+
2327
func (d FakeDryccCmd) ReleasesRollback(string, int) error {
2428
return errors.New("releases:rollback")
2529
}
@@ -49,6 +53,10 @@ func TestReleases(t *testing.T) {
4953
args: []string{"releases:info", "v1"},
5054
expected: "",
5155
},
56+
{
57+
args: []string{"releases:deploy", "web"},
58+
expected: "",
59+
},
5260
{
5361
args: []string{"releases:rollback"},
5462
expected: "",

0 commit comments

Comments
 (0)