with pull into storage

This commit is contained in:
2022-09-11 22:56:54 +02:00
parent 3643d4a467
commit 226e47e5f4
15 changed files with 485 additions and 53 deletions

View File

@@ -1,2 +1,4 @@
package action
package actions
type Action struct {
}

View File

@@ -0,0 +1,4 @@
package jobs
type JobId struct {
}

View File

@@ -0,0 +1,118 @@
package providers
import (
"context"
"git.front.kjuulh.io/kjuulh/kraken/internal/services/storage"
"github.com/go-git/go-git/v5"
"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"
"go.uber.org/zap"
"go.uber.org/zap/zapio"
)
// Git is a native git provider, it can clone, pull
// , push and as in abstraction on native git operations
type Git struct {
logger *zap.Logger
gitConfig *GitConfig
}
type GitRepo struct {
repo *git.Repository
}
type GitAuth string
const (
GIT_AUTH_SSH GitAuth = "ssh"
GIT_AUTH_USERNAME_PASSWORD GitAuth = "username_password"
GIT_AUTH_ACCESS_TOKEN GitAuth = "access_token"
GIT_AUTH_ANONYMOUS GitAuth = "anonymous"
GIT_AUTH_SSH_AGENT GitAuth = "ssh_agent"
)
type GitConfig struct {
AuthOption GitAuth
User string
Password string
AccessToken string
SshPublicKeyFilePath string
SshPrivateKeyPassword string
}
func NewGit(logger *zap.Logger, gitConfig *GitConfig) *Git {
return &Git{logger: logger, gitConfig: gitConfig}
}
func (g *Git) Clone(ctx context.Context, storageArea *storage.Area, repoUrl string) (*GitRepo, error) {
g.logger.Debug(
"cloning repository",
zap.String("repoUrl", repoUrl),
zap.String("path", storageArea.Path),
)
auth, err := g.GetAuth()
if err != nil {
return nil, err
}
cloneOptions := git.CloneOptions{
URL: repoUrl,
Auth: auth,
RemoteName: "origin",
ReferenceName: "refs/heads/main",
SingleBranch: true,
NoCheckout: false,
Depth: 1,
RecurseSubmodules: 1,
Progress: &zapio.Writer{
Log: g.logger.With(zap.String("process", "go-git")),
Level: zap.DebugLevel,
},
Tags: 0,
InsecureSkipTLS: false,
CABundle: []byte{},
}
repo, err := git.PlainCloneContext(ctx, storageArea.Path, false, &cloneOptions)
if err != nil {
return nil, err
}
g.logger.Debug("done cloning repo")
return &GitRepo{repo: repo}, nil
}
func (g *Git) GetAuth() (transport.AuthMethod, error) {
switch g.gitConfig.AuthOption {
case GIT_AUTH_SSH:
sshKey, err := ssh.NewPublicKeysFromFile(
g.gitConfig.User,
g.gitConfig.SshPublicKeyFilePath,
g.gitConfig.SshPrivateKeyPassword,
)
if err != nil {
return nil, err
}
return sshKey, nil
case GIT_AUTH_USERNAME_PASSWORD:
return &http.BasicAuth{
Username: g.gitConfig.User,
Password: g.gitConfig.Password,
}, nil
case GIT_AUTH_ACCESS_TOKEN:
return &http.BasicAuth{
Username: "required-username",
Password: g.gitConfig.AccessToken,
}, nil
case GIT_AUTH_ANONYMOUS:
return nil, nil
case GIT_AUTH_SSH_AGENT:
return ssh.NewSSHAgentAuth(g.gitConfig.User)
default:
return nil, nil
}
}

View File

@@ -1,9 +1,11 @@
package storage
import (
"errors"
"os"
"path"
"go.uber.org/zap"
"golang.org/x/net/context"
)
@@ -25,11 +27,12 @@ func NewDefaultStorageConfig() (*StorageConfig, error) {
}
type Service struct {
cfg *StorageConfig
logger *zap.Logger
cfg *StorageConfig
}
func NewService(cfg *StorageConfig) *Service {
return &Service{cfg: cfg}
func NewService(logger *zap.Logger, cfg *StorageConfig) *Service {
return &Service{logger: logger, cfg: cfg}
}
func (s *Service) getStoragePath(ctx context.Context) string {
@@ -41,7 +44,21 @@ func (s *Service) InitializeStorage(ctx context.Context) error {
}
func (s *Service) CleanupStorage(ctx context.Context) error {
return os.RemoveAll(s.getStoragePath(ctx))
doneRemovingChan := make(chan struct{}, 1)
go func(ctx context.Context) {
s.logger.Debug("Removing all temp storage")
os.RemoveAll(s.getStoragePath(ctx))
doneRemovingChan <- struct{}{}
}(ctx)
select {
case <-ctx.Done():
return errors.New("could not cleanup storage aborting")
case <-doneRemovingChan:
return nil
}
return nil
}
func (s *Service) CreateArea(ctx context.Context) (*Area, error) {