/
gitops.go
459 lines (415 loc) · 11.7 KB
/
gitops.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
package gitops
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
"strings"
"time"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/authorization"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/gitops"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/transport"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
"github.com/golang-jwt/jwt"
log "github.com/sirupsen/logrus"
ssh2 "golang.org/x/crypto/ssh"
)
// GitConfig structure for the GitOps settings
type GitConfig struct {
ProjectID string
RepositoryURL string
LocalPath string
RemoteName string
Branch string
LatestCommit string
UserName *string
Password *string
AuthType model.AuthType
Token *string
SSHPrivateKey *string
}
type GitUser struct {
username string
email string
}
const (
DefaultPath = "/tmp/gitops/"
ProjectDataPath = "litmus"
)
func GitUserFromContext(ctx context.Context) GitUser {
defaultUser := GitUser{
username: "gitops@litmus-chaos",
email: "gitops@litmus.chaos",
}
if ctx == nil {
return defaultUser
}
userClaims := ctx.Value(authorization.UserClaim)
if userClaims == nil {
return defaultUser
}
if _, ok := userClaims.(jwt.MapClaims)["email"]; !ok {
return GitUser{
username: userClaims.(jwt.MapClaims)["username"].(string) + "@litmus-chaos",
email: userClaims.(jwt.MapClaims)["username"].(string) + "@litmus-chaos",
}
}
return GitUser{
username: userClaims.(jwt.MapClaims)["username"].(string) + "@litmus-chaos",
email: userClaims.(jwt.MapClaims)["email"].(string),
}
}
// GetGitOpsConfig is used for constructing the GitConfig from dbSchemaGitOps.GitConfigDB
func GetGitOpsConfig(repoData gitops.GitConfigDB) GitConfig {
gitConfig := GitConfig{
ProjectID: repoData.ProjectID,
RepositoryURL: repoData.RepositoryURL,
RemoteName: "origin",
Branch: repoData.Branch,
LocalPath: DefaultPath + repoData.ProjectID,
LatestCommit: repoData.LatestCommit,
UserName: repoData.UserName,
Password: repoData.Password,
AuthType: model.AuthType(repoData.AuthType),
Token: repoData.Token,
SSHPrivateKey: repoData.SSHPrivateKey,
}
return gitConfig
}
// setupGitRepo helps clones and sets up the repo for gitops
func (c GitConfig) setupGitRepo(user GitUser) error {
projectPath := c.LocalPath + "/" + ProjectDataPath + "/" + c.ProjectID
// clone repo
_, err := c.GitClone()
if err != nil {
return err
}
// check if project dir already present in repo
exists, err := PathExists(projectPath + "/.info")
if err != nil {
return err
}
gitInfo := map[string]string{"projectID": c.ProjectID, "revision": "1"}
if exists {
data, err := ioutil.ReadFile(projectPath + "/.info")
if err != nil {
return errors.New("can't read existing git info file " + err.Error())
}
err = json.Unmarshal(data, &gitInfo)
if err != nil {
return errors.New("can't read existing git info file " + err.Error())
}
newRev, err := strconv.Atoi(gitInfo["revision"])
if err != nil {
return errors.New("can't read existing git info file[failed to parse revision] " + err.Error())
}
gitInfo["revision"] = strconv.Itoa(newRev + 1)
} else {
// create project dir and add config if not already present
err = os.MkdirAll(projectPath, 0755)
if err != nil {
return err
}
}
data, err := json.Marshal(gitInfo)
if err != nil {
return err
}
err = ioutil.WriteFile(projectPath+"/.info", data, 0644)
if err != nil {
return err
}
// commit and push
_, err = c.GitCommit(user, "Setup Litmus Chaos GitOps for Project :"+c.ProjectID, nil)
if err != nil {
return err
}
return c.GitPush()
}
// GitClone clones the repo
func (c GitConfig) GitClone() (*git.Repository, error) {
// clean the local path
err := os.RemoveAll(c.LocalPath)
if err != nil {
return nil, err
}
auth, err := c.getAuthMethod()
if err != nil {
return nil, err
}
return git.PlainClone(c.LocalPath, false, &git.CloneOptions{
Auth: auth,
URL: c.RepositoryURL,
Progress: nil,
ReferenceName: plumbing.NewBranchReferenceName(c.Branch),
SingleBranch: true,
})
}
// getAuthMethod returns the AuthMethod instance required for the current repo access [read/writes]
func (c GitConfig) getAuthMethod() (transport.AuthMethod, error) {
switch c.AuthType {
case model.AuthTypeToken:
return &http.BasicAuth{
Username: "litmus", // this can be anything except an empty string
Password: *c.Token,
}, nil
case model.AuthTypeBasic:
return &http.BasicAuth{
Username: *c.UserName,
Password: *c.Password,
}, nil
case model.AuthTypeSSH:
publicKey, err := ssh.NewPublicKeys("git", []byte(*c.SSHPrivateKey), "")
if err != nil {
return nil, err
}
publicKey.HostKeyCallback = ssh2.InsecureIgnoreHostKey()
return publicKey, nil
case model.AuthTypeNone:
return nil, nil
default:
return nil, errors.New("no Matching Auth Type Found")
}
}
// UnsafeGitPull executes git pull after a hard reset when uncommited changes are present in repo. Not safe.
func (c GitConfig) UnsafeGitPull() error {
cleanStatus, err := c.GitGetStatus()
if err != nil {
return err
}
log.WithFields(log.Fields{"cleanStatus": cleanStatus}).Info("executed GitGetStatus()... ")
if !cleanStatus {
log.Info("resetting Repo...: " + c.ProjectID)
return c.handlerForDirtyStatus()
}
return c.GitPull()
}
// GitGetStatus executes "git get status --porcelain" for the provided Repository Path,
// returns true if the repository is clean
// and false if the repository is dirty
func (c GitConfig) GitGetStatus() (bool, error) {
_, workTree, err := c.getRepositoryWorktreeReference()
if err != nil {
return true, err
}
status, err := workTree.Status()
if err != nil {
return false, err
}
return status.IsClean(), nil
}
// getRepositoryWorktreeReference returns the git.Repository and git.Worktree instances for the repo
func (c GitConfig) getRepositoryWorktreeReference() (*git.Repository, *git.Worktree, error) {
repo, err := git.PlainOpen(c.LocalPath)
if err != nil {
return nil, nil, err
}
workTree, err := repo.Worktree()
if err != nil {
return nil, nil, err
}
return repo, workTree, nil
}
// handlerForDirtyStatus calls relative functions if the GitGetStatus gives a clean status as a result
func (c GitConfig) handlerForDirtyStatus() error {
if err := c.GitHardReset(); err != nil {
return err
}
return c.GitPull()
}
// GitHardReset executes "git reset --hard HEAD" in provided Repository Path
func (c GitConfig) GitHardReset() error {
_, workTree, err := c.getRepositoryWorktreeReference()
if err != nil {
return err
}
if workTree.Reset(&git.ResetOptions{Mode: git.HardReset}) != nil {
return fmt.Errorf("error in executing Reset: %s", err)
}
return nil
}
// GitPull updates the repository in provided Path
func (c GitConfig) GitPull() error {
_, workTree, err := c.getRepositoryWorktreeReference()
if err != nil {
return err
}
auth, err := c.getAuthMethod()
if err != nil {
return err
}
err = workTree.Pull(&git.PullOptions{
Auth: auth,
RemoteName: c.RemoteName,
ReferenceName: plumbing.NewBranchReferenceName(c.Branch),
SingleBranch: true,
})
if err != nil && err != git.NoErrAlreadyUpToDate {
return err
}
return nil
}
// GitCheckout changes the current active branch to specified branch in GitConfig
func (c GitConfig) GitCheckout() error {
r, w, err := c.getRepositoryWorktreeReference()
if err != nil {
return err
}
_, err = r.Storer.Reference(plumbing.NewBranchReferenceName(c.Branch))
create := true
log.Error(err)
if err == nil {
create = false
}
return w.Checkout(&git.CheckoutOptions{
Branch: plumbing.NewBranchReferenceName(c.Branch),
Create: create,
})
}
// GitPush pushes the current changes to remote set in GitConfig, always needs auth credentials
func (c GitConfig) GitPush() error {
if c.AuthType == model.AuthTypeNone {
return errors.New("cannot write/push without credentials, auth type = none")
}
r, _, err := c.getRepositoryWorktreeReference()
if err != nil {
return err
}
auth, err := c.getAuthMethod()
if err != nil {
return err
}
err = r.Push(&git.PushOptions{
RemoteName: c.RemoteName,
Auth: auth,
Progress: nil,
})
if err == git.NoErrAlreadyUpToDate {
return nil
}
return err
}
// GitCommit saves the changes in the repo and commits them with the message provided
func (c GitConfig) GitCommit(user GitUser, message string, deleteFile *string) (string, error) {
_, w, err := c.getRepositoryWorktreeReference()
if err != nil {
return "", err
}
if deleteFile == nil {
_, err = w.Add("./litmus/" + c.ProjectID + "/")
if err != nil {
return "", err
}
} else {
_, err := w.Remove(*deleteFile)
if err != nil {
return "", err
}
}
hash, err := w.Commit(message, &git.CommitOptions{
Author: &object.Signature{
Name: user.username,
Email: user.email,
When: time.Now(),
},
})
if err != nil {
return "", err
}
return hash.String(), nil
}
// GetChanges returns the LatestCommit and list of files changed(since previous LatestCommit) in the project directory mentioned in GitConfig
func (c GitConfig) GetChanges() (string, map[string]int, error) {
path := ProjectDataPath + "/" + c.ProjectID + "/"
r, _, err := c.getRepositoryWorktreeReference()
if err != nil {
return "", nil, err
}
var knownCommit *object.Commit = nil
if c.LatestCommit != "" {
knownCommit, err = r.CommitObject(plumbing.NewHash(c.LatestCommit))
if err != nil {
return "", nil, err
}
}
visited := map[string]int{}
commitIter, err := r.Log(&git.LogOptions{
PathFilter: func(file string) bool {
if (strings.HasSuffix(path, "/") && strings.HasPrefix(file, path)) || (path == file) {
visited[file] += 1
return true
}
return false
},
Order: git.LogOrderCommitterTime,
})
if err != nil {
return "", nil, errors.New("failed to get commit Iterator :" + err.Error())
}
commit, err := commitIter.Next()
if err != nil && err != io.EOF {
return "", nil, err
}
if commit != nil {
c.LatestCommit = commit.Hash.String()
}
for err != io.EOF && commit != nil {
if knownCommit != nil {
ancestor, er := commit.IsAncestor(knownCommit)
if er != nil {
return "", nil, er
}
if knownCommit.Hash == commit.Hash || ancestor {
break
}
}
commit, err = commitIter.Next()
if err != nil && err != io.EOF {
return "", nil, err
}
}
return c.LatestCommit, visited, nil
}
// GetLatestCommitHash returns the latest commit hash in the local repo for the project directory
func (c GitConfig) GetLatestCommitHash() (string, error) {
path := ProjectDataPath + "/" + c.ProjectID + "/"
r, _, err := c.getRepositoryWorktreeReference()
if err != nil {
return "", err
}
commitIter, err := r.Log(&git.LogOptions{
PathFilter: func(file string) bool {
return (strings.HasSuffix(path, "/") && strings.HasPrefix(file, path)) || (path == file)
},
Order: git.LogOrderCommitterTime,
})
if err != nil {
return "", errors.New("failed to get latest commit hash :" + err.Error())
}
commit, err := commitIter.Next()
if err != nil {
return "", errors.New("failed to get latest commit hash:" + err.Error())
}
return commit.Hash.String(), nil
}
// SetupGitOps clones and sets up the repo for gitops and returns the LatestCommit
func SetupGitOps(user GitUser, gitConfig GitConfig) (string, error) {
err := gitConfig.setupGitRepo(user)
if err != nil {
return "", err
}
commitHash, err := gitConfig.GetLatestCommitHash()
if err != nil {
return "", err
}
return commitHash, err
}