Skip to content

Commit aa2b272

Browse files
authored
Merge pull request #50 from FlowCI/develop
Develop
2 parents c72d7b4 + db62d1f commit aa2b272

File tree

14 files changed

+220
-40
lines changed

14 files changed

+220
-40
lines changed

api/client.go

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ type (
5353
CacheDownload(cacheId, workspace, file string, progress io.Writer)
5454

5555
GetSecret(name string) (domain.Secret, error)
56+
GetConfig(name string) (domain.Config, error)
5657

5758
Close()
5859
}
@@ -270,11 +271,9 @@ func (c *client) CacheDownload(cacheId, workspace, file string, progress io.Writ
270271
}
271272

272273
func (c *client) GetSecret(name string) (secret domain.Secret, err error) {
273-
defer func() {
274-
if r := recover(); r != nil {
275-
err = r.(error)
276-
}
277-
}()
274+
defer util.RecoverPanic(func(e error) {
275+
err = e
276+
})
278277

279278
req, _ := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/api/secret/%s", c.server, name), nil)
280279
req.Header.Set(util.HttpHeaderAgentToken, c.token)
@@ -294,7 +293,7 @@ func (c *client) GetSecret(name string) (secret domain.Secret, err error) {
294293
util.PanicIfNil(body.Data, "secret data")
295294

296295
base := body.Data
297-
baseRaw := &domain.SecretResponseRaw{}
296+
baseRaw := &domain.ResponseRaw{}
298297
err = json.Unmarshal(out, baseRaw)
299298
util.PanicIfErr(err)
300299

@@ -322,6 +321,50 @@ func (c *client) GetSecret(name string) (secret domain.Secret, err error) {
322321
return nil, fmt.Errorf("secret '%s' category '%s' is unsupported", base.GetName(), base.GetCategory())
323322
}
324323

324+
func (c *client) GetConfig(name string) (config domain.Config, err error) {
325+
defer util.RecoverPanic(func(e error) {
326+
err = e
327+
})
328+
329+
req, _ := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/api/config/%s", c.server, name), nil)
330+
req.Header.Set(util.HttpHeaderAgentToken, c.token)
331+
332+
resp, err := c.client.Do(req)
333+
util.PanicIfErr(err)
334+
335+
defer resp.Body.Close()
336+
337+
out, err := ioutil.ReadAll(resp.Body)
338+
util.PanicIfErr(err)
339+
340+
configResp, err := c.parseResponse(out, &domain.ConfigResponse{})
341+
util.PanicIfErr(err)
342+
343+
body := configResp.(*domain.ConfigResponse)
344+
util.PanicIfNil(body.Data, "config data")
345+
346+
base := body.Data
347+
baseRaw := &domain.ResponseRaw{}
348+
err = json.Unmarshal(out, baseRaw)
349+
util.PanicIfErr(err)
350+
351+
if base.Category == domain.ConfigCategorySmtp {
352+
auth := &domain.SmtpConfig{}
353+
err = json.Unmarshal(baseRaw.Raw, auth)
354+
util.PanicIfErr(err)
355+
return auth, nil
356+
}
357+
358+
if base.Category == domain.ConfigCategoryText {
359+
rsa := &domain.TextConfig{}
360+
err = json.Unmarshal(baseRaw.Raw, rsa)
361+
util.PanicIfErr(err)
362+
return rsa, nil
363+
}
364+
365+
return nil, fmt.Errorf("config '%s' category '%s' is unsupported", base.GetName(), base.GetCategory())
366+
}
367+
325368
func (c *client) Close() {
326369
if c.conn != nil {
327370
close(c.pending)

app.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"github/flowci/flow-agent-x/util"
1616
)
1717

18-
const version = "0.21.21"
18+
const version = "1.21.33"
1919

2020
func init() {
2121
util.LogInit()

domain/cmd_shell.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ type (
2626
Timeout int `json:"timeout"`
2727
Inputs Variables `json:"inputs"`
2828
EnvFilters []string `json:"envFilters"`
29-
Secrets []string `json:"secrets"`
29+
Secrets []string `json:"secrets"` // secret name list
30+
Configs []string `json:"configs"` // config name list
3031
}
3132

3233
ShellOut struct {
@@ -57,6 +58,10 @@ func (in *ShellIn) HasSecrets() bool {
5758
return in.Secrets != nil && len(in.Secrets) > 0
5859
}
5960

61+
func (in *ShellIn) HasConfigs() bool {
62+
return in.Configs != nil && len(in.Configs) > 0
63+
}
64+
6065
func (in *ShellIn) HasCache() bool {
6166
return in.Cache != nil
6267
}

domain/config.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package domain
2+
3+
const (
4+
ConfigCategorySmtp = "SMTP"
5+
ConfigCategoryText = "TEXT"
6+
)
7+
8+
type (
9+
Config interface {
10+
GetName() string
11+
GetCategory() string
12+
ToEnvs() map[string]string
13+
ConfigMarker()
14+
}
15+
16+
ConfigBase struct {
17+
Name string
18+
Category string
19+
}
20+
21+
ConfigResponse struct {
22+
Response
23+
Data *ConfigBase `json:"data"`
24+
}
25+
)
26+
27+
func (c *ConfigBase) GetName() string {
28+
return c.Name
29+
}
30+
31+
func (c *ConfigBase) GetCategory() string {
32+
return c.Category
33+
}
34+
35+
func (c *ConfigBase) ConfigMarker() {
36+
// placeholder
37+
}

domain/config_smtp.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package domain
2+
3+
type SmtpConfig struct {
4+
ConfigBase
5+
Server string
6+
Port int
7+
SecureType string
8+
Auth *SimpleAuthPair
9+
}
10+
11+
func (c *SmtpConfig) ToEnvs() map[string]string {
12+
return map[string]string{
13+
c.GetName() + "_SERVER": c.Server,
14+
c.GetName() + "_PORT": string(rune(c.Port)),
15+
c.GetName() + "_SECURE_TYPE": c.SecureType,
16+
c.GetName() + "_AUTH_USERNAME": c.Auth.Username,
17+
c.GetName() + "_AUTH_PASSWORD": c.Auth.Password,
18+
}
19+
}

domain/config_text.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package domain
2+
3+
type TextConfig struct {
4+
ConfigBase
5+
Text string
6+
}
7+
8+
func (c *TextConfig) ToEnvs() map[string]string {
9+
return map[string]string{
10+
c.GetName(): c.Text,
11+
}
12+
}

domain/response.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package domain
22

3+
import "encoding/json"
4+
35
const (
46
ok = 200
57
)
@@ -15,6 +17,10 @@ type (
1517
Code int
1618
Message string
1719
}
20+
21+
ResponseRaw struct {
22+
Raw json.RawMessage `json:"data"`
23+
}
1824
)
1925

2026
func (r *Response) IsOk() bool {

domain/secret.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package domain
22

3-
import "encoding/json"
4-
53
const (
64
SecretCategoryAuth = "AUTH"
75
SecretCategorySshRsa = "SSH_RSA"
@@ -15,6 +13,7 @@ type (
1513
GetName() string
1614
GetCategory() string
1715
ToEnvs() map[string]string
16+
SecretMarker()
1817
}
1918

2019
SecretBase struct {
@@ -28,11 +27,7 @@ type (
2827

2928
SecretResponse struct {
3029
Response
31-
Data *SecretBase `json:"data"`
32-
}
33-
34-
SecretResponseRaw struct {
35-
Raw json.RawMessage `json:"data"`
30+
Data *SecretBase `json:"data"`
3631
}
3732
)
3833

@@ -43,3 +38,7 @@ func (s *SecretBase) GetName() string {
4338
func (s *SecretBase) GetCategory() string {
4439
return s.Category
4540
}
41+
42+
func (s *SecretBase) SecretMarker() {
43+
// placeholder
44+
}

domain/secret_rsa.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,5 @@ type (
1616
func (s *RSASecret) ToEnvs() map[string]string {
1717
return map[string]string{
1818
s.GetName() + "_PUBLIC_KEY": s.Pair.PublicKey,
19-
s.GetName() + "_PRIVATE_KEY": s.Pair.PrivateKey,
2019
}
2120
}

executor/docker.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,23 @@ func (d *dockerExecutor) handleErrors(err error) error {
402402
return err
403403
}
404404

405+
func (d *dockerExecutor) findImageLocally(image string) (bool, error) {
406+
list, err := d.cli.ImageList(d.context, types.ImageListOptions{All: true})
407+
if err != nil {
408+
return false, err
409+
}
410+
411+
for _, imageInfo := range list {
412+
for _, t := range imageInfo.RepoTags {
413+
if t == image {
414+
return true, nil
415+
}
416+
}
417+
}
418+
419+
return false, nil
420+
}
421+
405422
func (d *dockerExecutor) pullImage() {
406423
for _, c := range d.configs {
407424
err := d.pullImageWithName(c.Config.Image, c.Auth)
@@ -410,10 +427,19 @@ func (d *dockerExecutor) pullImage() {
410427
}
411428

412429
func (d *dockerExecutor) pullImageWithName(image string, auth *domain.SimpleAuthPair) (out error) {
430+
if isOnLocal, err := d.findImageLocally(image); isOnLocal {
431+
out = err
432+
return
433+
}
434+
435+
if util.HasError(out) {
436+
return
437+
}
438+
413439
fullRef := image
414440

415441
if isDockerHubImage(image) {
416-
fullRef = "docker.io/library/" + image
442+
fullRef = image
417443
if strings.Contains(image, "/") {
418444
fullRef = "docker.io/" + image
419445
}

0 commit comments

Comments
 (0)