/
cmd_cache.go
221 lines (172 loc) · 6.11 KB
/
cmd_cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"time"
"github.com/spf13/cobra"
"github.com/ovh/cds/sdk"
)
func cmdCache() *cobra.Command {
cmdCacheRoot := &cobra.Command{
Use: "cache",
Long: `
Inside a project, you can create or retrieve a cache from your worker with a tag (useful for vendors for example).
You can access to this cache from any workflow inside a project. You just have to choose a tag that fits with your needs.
For example if you need a different cache for each workflow so choose a tag scoped with your workflow name and workflow version (example of tag value: {{.cds.workflow}}-{{.cds.version}})
## Use Case
Java Developers often use maven to manage dependencies. The mvn install command could be long because all the maven dependencies have to be downloaded on a fresh CDS Job workspace.
With the worker cache feature, you don't have to download the dependencies if they haven't been updated since the last run of the job.
- cache push: take the current .m2/ directory and set it as a cache
- cache pull: download a cache of .m2 directory
Here, an example of a script inside a CDS Job using the cache feature:
#!/bin/bash
tag=($(md5sum pom.xml))
# download the cache of .m2/
if worker cache pull $tag; then
echo ".m2/ getted from cache";
fi
# update the directory .m2/
# as there is a cache, mvn does not need to download all dependencies
# if they are not updated on upstream
mvn install
# put in cache the updated .m2/ directory
worker cache push $tag .m2/
`,
}
cmdCacheRoot.AddCommand(cmdCachePush(), cmdCachePull())
return cmdCacheRoot
}
var cmdStorageIntegrationName string
func cmdCachePush() *cobra.Command {
c := &cobra.Command{
Use: "push",
Aliases: []string{"upload"},
Short: "worker cache push tagValue {{.cds.workspace}}/pathToUpload",
Long: `
Inside a project, you can create a cache from your worker with a tag (useful for vendors for example)
worker cache push <tagValue> dir/file
You can use you storage integration:
worker cache push --destination=MyStorageIntegration <tagValue> dir/file
`,
Example: "worker cache push {{.cds.workflow}}-{{.cds.version}} ./pathToUpload",
Run: cachePushCmd(),
}
c.Flags().StringVar(&cmdStorageIntegrationName, "destination", "", "optional. Your storage integration name")
return c
}
func cachePushCmd() func(cmd *cobra.Command, args []string) {
return func(cmd *cobra.Command, args []string) {
port := MustGetWorkerHTTPPort()
if len(args) < 2 {
sdk.Exit("worker cache push > Wrong usage: Example : worker cache push myTagValue filea fileb filec")
}
files := make([]string, len(args)-1)
for i, arg := range args[1:] {
files[i] = arg
}
cwd, err := os.Getwd()
if err != nil {
sdk.Exit("worker cache push > Cannot find working directory : %s", err)
}
c := sdk.Cache{
Tag: base64.RawURLEncoding.EncodeToString([]byte(args[0])),
Files: files,
WorkingDirectory: cwd,
IntegrationName: cmdStorageIntegrationName,
}
data, errMarshal := json.Marshal(c)
if errMarshal != nil {
sdk.Exit("worker cache push > internal error (%s)", errMarshal)
}
fmt.Printf("Worker cache push in progress... (tag: %s)\n", args[0])
req, errRequest := http.NewRequest(
"POST",
fmt.Sprintf("http://127.0.0.1:%d/cache/push", port),
bytes.NewReader(data),
)
if errRequest != nil {
sdk.Exit("worker cache push > cannot post worker cache push (Request): %s", errRequest)
}
client := http.DefaultClient
client.Timeout = 30 * time.Minute
resp, errDo := client.Do(req)
if errDo != nil {
sdk.Exit("worker cache push > cannot post worker cache push (Do): %s", errDo)
}
defer resp.Body.Close()
if resp.StatusCode >= 300 {
body, err := io.ReadAll(resp.Body)
if err != nil {
sdk.Exit("cache push HTTP %d error %v", resp.StatusCode, err)
}
sdk.Exit("worker cache push failed: %s", string(body))
}
fmt.Printf("Worker cache push with success (tag: %s)\n", args[0])
}
}
func cmdCachePull() *cobra.Command {
c := &cobra.Command{
Use: "pull",
Aliases: []string{"download"},
Short: "worker cache pull tagValue",
Long: `
Inside a project, you can fetch a cache from your worker with a tag
worker cache pull <tagValue>
If you push a cache with:
worker cache push latest {{.cds.workspace}}/pathToUpload
The command:
worker cache pull latest
will create the directory {{.cds.workspace}}/pathToUpload with the content of the cache
If you want to push a cache into a storage integration:
worker cache push latest --from=MyStorageIntegration {{.cds.workspace}}/pathToUpload
`,
Run: cachePullCmd(),
}
c.Flags().StringVar(&cmdStorageIntegrationName, "from", "", "optional. Your storage integration name")
return c
}
func cachePullCmd() func(cmd *cobra.Command, args []string) {
return func(cmd *cobra.Command, args []string) {
port := MustGetWorkerHTTPPort()
if len(args) < 1 {
sdk.Exit("worker cache pull > Wrong usage: Example : worker cache pull myTagValue")
}
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
sdk.Exit("worker cache pull > cannot get current path: %s", err)
}
fmt.Printf("Worker cache pull in progress... (tag: %s)\n", args[0])
req, errRequest := http.NewRequest(
"GET",
fmt.Sprintf("http://127.0.0.1:%d/cache/%s/pull?path=%s&integration=%s", port,
base64.RawURLEncoding.EncodeToString([]byte(args[0])),
url.QueryEscape(dir),
url.QueryEscape(cmdStorageIntegrationName)),
nil,
)
if errRequest != nil {
sdk.Exit("worker cache pull > cannot post worker cache pull with tag %s (Request): %s", args[0], errRequest)
}
client := http.DefaultClient
client.Timeout = 10 * time.Minute
resp, errDo := client.Do(req)
if errDo != nil {
sdk.Exit("worker cache pull > cannot post worker cache pull (Do): %s", errDo)
}
if resp.StatusCode >= 300 {
body, err := io.ReadAll(resp.Body)
if err != nil {
sdk.Exit("cache pull HTTP %d error %v", resp.StatusCode, err)
}
sdk.Exit("worker cache pull failed: %s", string(body))
}
fmt.Printf("Worker cache pull with success (tag: %s)\n", args[0])
}
}