Skip to content

Commit

Permalink
Merge pull request #534 from traPtitech/fix/artifact
Browse files Browse the repository at this point in the history
Fix/artifact
  • Loading branch information
motoki317 authored May 18, 2023
2 parents a0118e8 + 08ae2cc commit 1bc5f16
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 28 deletions.
31 changes: 10 additions & 21 deletions pkg/domain/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"path/filepath"

"github.com/friendsofgo/errors"

"github.com/traPtitech/neoshowcase/pkg/util/tarfs"
)

var (
Expand Down Expand Up @@ -51,9 +49,12 @@ func DeleteBuildLog(s Storage, buildID string) error {
return nil
}

func artifactPath(id string) string {
const artifactDirectory = "artifacts"
return filepath.Join(artifactDirectory, id+".tar")
func artifactPath(artifactID string) string {
return filepath.Join("artifacts", artifactFilename(artifactID))
}

func artifactFilename(artifactID string) string {
return artifactID + ".tar.gz"
}

// SaveArtifact Artifactをtar形式で保存する
Expand All @@ -64,13 +65,12 @@ func SaveArtifact(s Storage, artifactID string, src io.Reader) error {
return nil
}

func GetArtifact(s Storage, artifactID string) ([]byte, error) {
r, err := s.Open(artifactPath(artifactID))
func GetArtifact(s Storage, artifactID string) (filename string, r io.ReadCloser, err error) {
r, err = s.Open(artifactPath(artifactID))
if err != nil {
return nil, errors.Wrap(err, "failed to open artifact")
return "", nil, errors.Wrap(err, "failed to open artifact")
}
defer r.Close()
return io.ReadAll(r)
return artifactFilename(artifactID), r, nil
}

func DeleteArtifact(s Storage, artifactID string) error {
Expand All @@ -81,17 +81,6 @@ func DeleteArtifact(s Storage, artifactID string) error {
return nil
}

// ExtractTarToDir tarファイルをディレクトリに展開する
func ExtractTarToDir(s Storage, artifactID string, destPath string) error {
inputFile, err := s.Open(artifactPath(artifactID))
if err != nil {
return errors.Wrap(err, "couldn't open source file")
}
defer inputFile.Close()

return tarfs.Extract(inputFile, destPath)
}

type StorageConfig struct {
Type string `mapstructure:"type" yaml:"type"`
Local struct {
Expand Down
10 changes: 8 additions & 2 deletions pkg/infrastructure/grpc/api_app_build_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package grpc

import (
"context"
"io"

"github.com/bufbuild/connect-go"
"github.com/friendsofgo/errors"
Expand Down Expand Up @@ -78,12 +79,17 @@ func (s *APIService) GetBuildLogStream(ctx context.Context, req *connect.Request
}

func (s *APIService) GetBuildArtifact(ctx context.Context, req *connect.Request[pb.ArtifactIdRequest]) (*connect.Response[pb.ArtifactContent], error) {
content, err := s.svc.GetArtifact(ctx, req.Msg.ArtifactId)
filename, r, err := s.svc.GetArtifact(ctx, req.Msg.ArtifactId)
if err != nil {
return nil, handleUseCaseError(err)
}
defer r.Close()
content, err := io.ReadAll(r)
if err != nil {
return nil, err
}
res := connect.NewResponse(&pb.ArtifactContent{
Filename: req.Msg.ArtifactId + ".tar",
Filename: filename,
Content: content,
})
return res, nil
Expand Down
26 changes: 23 additions & 3 deletions pkg/infrastructure/staticserver/builtin.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package staticserver

import (
"compress/gzip"
"context"
"fmt"
"os"
Expand All @@ -14,6 +15,7 @@ import (

"github.com/traPtitech/neoshowcase/pkg/domain"
"github.com/traPtitech/neoshowcase/pkg/domain/web"
"github.com/traPtitech/neoshowcase/pkg/util/tarfs"
)

type BuiltIn struct {
Expand Down Expand Up @@ -121,10 +123,9 @@ func (b *BuiltIn) syncArtifacts(sites []*domain.StaticSite) error {
if _, ok := currentArtifacts[artifactID]; ok {
continue
}
artifactDir := filepath.Join(b.docsRoot, artifactID)
err = domain.ExtractTarToDir(b.storage, artifactID, artifactDir)
err = b.extractArtifact(artifactID)
if err != nil {
return errors.Wrap(err, "failed to extract artifact tar")
return err
}
}

Expand All @@ -141,3 +142,22 @@ func (b *BuiltIn) syncArtifacts(sites []*domain.StaticSite) error {

return nil
}

func (b *BuiltIn) extractArtifact(artifactID string) error {
destDir := filepath.Join(b.docsRoot, artifactID)
_, r, err := domain.GetArtifact(b.storage, artifactID)
if err != nil {
return errors.Wrap(err, "getting artifact")
}
defer r.Close()
tarReader, err := gzip.NewReader(r)
if err != nil {
return errors.Wrap(err, "preparing gzip reader")
}
defer tarReader.Close()
err = tarfs.Extract(tarReader, destDir)
if err != nil {
return errors.Wrap(err, "failed to extract artifact tar")
}
return nil
}
3 changes: 2 additions & 1 deletion pkg/usecase/api_app_build_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package usecase

import (
"context"
"io"

"github.com/friendsofgo/errors"

Expand Down Expand Up @@ -79,6 +80,6 @@ func (s *APIServerService) GetBuildLogStream(ctx context.Context, buildID string
return ch, nil
}

func (s *APIServerService) GetArtifact(_ context.Context, artifactID string) ([]byte, error) {
func (s *APIServerService) GetArtifact(_ context.Context, artifactID string) (filename string, r io.ReadCloser, err error) {
return domain.GetArtifact(s.storage, artifactID)
}
22 changes: 21 additions & 1 deletion pkg/usecase/builder_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package usecase

import (
"bytes"
"compress/gzip"
"context"
"fmt"
"io"
Expand Down Expand Up @@ -444,7 +445,26 @@ func (s *builderService) saveArtifact(ctx context.Context, st *state) error {
return errors.Wrap(err, "opening artifact")
}
defer file.Close()
err = domain.SaveArtifact(s.storage, filename, file)

pr, pw := io.Pipe()
gzipWriter := gzip.NewWriter(pw)
if err != nil {
return errors.Wrap(err, "creating gzip stream")
}
go func() {
defer pw.Close()
_, err := io.Copy(gzipWriter, file)
if err != nil {
_ = pw.CloseWithError(errors.Wrap(err, "copying file to pipe writer"))
return
}
err = gzipWriter.Close()
if err != nil {
_ = pw.CloseWithError(errors.Wrap(err, "flushing gzip writer"))
return
}
}()
err = domain.SaveArtifact(s.storage, artifact.ID, pr)
if err != nil {
return errors.Wrap(err, "saving artifact")
}
Expand Down

0 comments on commit 1bc5f16

Please sign in to comment.