forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git.go
157 lines (141 loc) · 4.63 KB
/
git.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
package git
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"os/user"
"strings"
log "github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh"
git "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/config"
"gopkg.in/src-d/go-git.v4/plumbing/transport"
"gopkg.in/src-d/go-git.v4/plumbing/transport/http"
ssh2 "gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
"github.com/argoproj/argo/errors"
wfv1 "github.com/argoproj/argo/pkg/apis/workflow/v1alpha1"
)
// GitArtifactDriver is the artifact driver for a git repo
type GitArtifactDriver struct {
Username string
Password string
SSHPrivateKey string
InsecureIgnoreHostKey bool
}
// Load download artifacts from an git URL
func (g *GitArtifactDriver) Load(inputArtifact *wfv1.Artifact, path string) error {
if g.SSHPrivateKey != "" {
signer, err := ssh.ParsePrivateKey([]byte(g.SSHPrivateKey))
if err != nil {
return errors.InternalWrapError(err)
}
auth := &ssh2.PublicKeys{User: "git", Signer: signer}
if g.InsecureIgnoreHostKey {
auth.HostKeyCallback = ssh.InsecureIgnoreHostKey()
}
return gitClone(path, inputArtifact, auth, g.SSHPrivateKey)
}
if g.Username != "" || g.Password != "" {
auth := &http.BasicAuth{Username: g.Username, Password: g.Password}
return gitClone(path, inputArtifact, auth, "")
}
return gitClone(path, inputArtifact, nil, "")
}
// Save is unsupported for git output artifacts
func (g *GitArtifactDriver) Save(path string, outputArtifact *wfv1.Artifact) error {
return errors.Errorf(errors.CodeBadRequest, "Git output artifacts unsupported")
}
func writePrivateKey(key string, insecureIgnoreHostKey bool) error {
usr, err := user.Current()
if err != nil {
return errors.InternalWrapError(err)
}
sshDir := fmt.Sprintf("%s/.ssh", usr.HomeDir)
err = os.MkdirAll(sshDir, 0700)
if err != nil {
return errors.InternalWrapError(err)
}
if insecureIgnoreHostKey {
sshConfig := `Host *
StrictHostKeyChecking no
UserKnownHostsFile /dev/null`
err = ioutil.WriteFile(fmt.Sprintf("%s/config", sshDir), []byte(sshConfig), 0644)
if err != nil {
return errors.InternalWrapError(err)
}
}
err = ioutil.WriteFile(fmt.Sprintf("%s/id_rsa", sshDir), []byte(key), 0600)
if err != nil {
return errors.InternalWrapError(err)
}
return nil
}
func gitClone(path string, inputArtifact *wfv1.Artifact, auth transport.AuthMethod, privateKey string) error {
cloneOptions := git.CloneOptions{
URL: inputArtifact.Git.Repo,
RecurseSubmodules: git.DefaultSubmoduleRecursionDepth,
Auth: auth,
}
if inputArtifact.Git.Depth != nil {
cloneOptions.Depth = int(*inputArtifact.Git.Depth)
}
repo, err := git.PlainClone(path, false, &cloneOptions)
if err != nil {
return errors.InternalWrapError(err)
}
if inputArtifact.Git.Fetch != nil {
refSpecs := make([]config.RefSpec, len(inputArtifact.Git.Fetch))
for i, spec := range inputArtifact.Git.Fetch {
refSpecs[i] = config.RefSpec(spec)
}
fetchOptions := git.FetchOptions{
RefSpecs: refSpecs,
}
if inputArtifact.Git.Depth != nil {
fetchOptions.Depth = int(*inputArtifact.Git.Depth)
}
err = fetchOptions.Validate()
if err != nil {
return errors.InternalWrapError(err)
}
err = repo.Fetch(&fetchOptions)
if err != nil {
return errors.InternalWrapError(err)
}
}
if inputArtifact.Git.Revision != "" {
// We still rely on forking git for checkout, since go-git does not have a reliable
// way of resolving revisions (e.g. mybranch, HEAD^, v1.2.3)
log.Infof("Checking out revision %s", inputArtifact.Git.Revision)
cmd := exec.Command("git", "checkout", inputArtifact.Git.Revision)
cmd.Dir = path
output, err := cmd.Output()
if err != nil {
if exErr, ok := err.(*exec.ExitError); ok {
log.Errorf("`%s` stderr:\n%s", cmd.Args, string(exErr.Stderr))
return errors.InternalError(strings.Split(string(exErr.Stderr), "\n")[0])
}
return errors.InternalWrapError(err)
}
log.Errorf("`%s` stdout:\n%s", cmd.Args, string(output))
if privateKey != "" {
err := writePrivateKey(privateKey, inputArtifact.Git.InsecureIgnoreHostKey)
if err != nil {
return errors.InternalWrapError(err)
}
}
submodulesCmd := exec.Command("git", "submodule", "update", "--init", "--recursive", "--force")
submodulesCmd.Dir = path
submoduleOutput, err := submodulesCmd.Output()
if err != nil {
if exErr, ok := err.(*exec.ExitError); ok {
log.Errorf("`%s` stderr:\n%s", submodulesCmd.Args, string(exErr.Stderr))
return errors.InternalError(strings.Split(string(exErr.Stderr), "\n")[0])
}
return errors.InternalWrapError(err)
}
log.Errorf("`%s` stdout:\n%s", submodulesCmd.Args, string(submoduleOutput))
}
return nil
}