2021-11-11 01:57:22 +08:00
|
|
|
package artifacts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/fs"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2022-07-25 20:12:48 +08:00
|
|
|
"time"
|
2021-11-11 01:57:22 +08:00
|
|
|
|
|
|
|
"github.com/julienschmidt/httprouter"
|
2023-02-16 23:34:51 +08:00
|
|
|
|
2021-11-11 01:57:22 +08:00
|
|
|
"github.com/nektos/act/pkg/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FileContainerResourceURL struct {
|
|
|
|
FileContainerResourceURL string `json:"fileContainerResourceUrl"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type NamedFileContainerResourceURL struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
FileContainerResourceURL string `json:"fileContainerResourceUrl"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type NamedFileContainerResourceURLResponse struct {
|
|
|
|
Count int `json:"count"`
|
|
|
|
Value []NamedFileContainerResourceURL `json:"value"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ContainerItem struct {
|
|
|
|
Path string `json:"path"`
|
|
|
|
ItemType string `json:"itemType"`
|
|
|
|
ContentLocation string `json:"contentLocation"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ContainerItemResponse struct {
|
|
|
|
Value []ContainerItem `json:"value"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ResponseMessage struct {
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
|
|
|
|
2023-01-17 05:01:54 +08:00
|
|
|
type WritableFile interface {
|
|
|
|
io.WriteCloser
|
2021-11-11 01:57:22 +08:00
|
|
|
}
|
|
|
|
|
2023-01-17 05:01:54 +08:00
|
|
|
type WriteFS interface {
|
|
|
|
OpenWritable(name string) (WritableFile, error)
|
|
|
|
OpenAppendable(name string) (WritableFile, error)
|
2021-11-11 01:57:22 +08:00
|
|
|
}
|
|
|
|
|
2023-01-17 05:01:54 +08:00
|
|
|
type readWriteFSImpl struct {
|
2021-11-11 01:57:22 +08:00
|
|
|
}
|
|
|
|
|
2023-01-17 05:01:54 +08:00
|
|
|
func (fwfs readWriteFSImpl) Open(name string) (fs.File, error) {
|
|
|
|
return os.Open(name)
|
2022-06-21 06:06:55 +08:00
|
|
|
}
|
|
|
|
|
2023-01-17 05:01:54 +08:00
|
|
|
func (fwfs readWriteFSImpl) OpenWritable(name string) (WritableFile, error) {
|
|
|
|
if err := os.MkdirAll(filepath.Dir(name), os.ModePerm); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-02-16 23:34:51 +08:00
|
|
|
return os.OpenFile(name, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0o644)
|
2023-01-17 05:01:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fwfs readWriteFSImpl) OpenAppendable(name string) (WritableFile, error) {
|
|
|
|
if err := os.MkdirAll(filepath.Dir(name), os.ModePerm); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-02-16 23:34:51 +08:00
|
|
|
file, err := os.OpenFile(name, os.O_CREATE|os.O_RDWR, 0o644)
|
2022-06-21 06:06:55 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = file.Seek(0, os.SEEK_END)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return file, nil
|
2021-11-11 01:57:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var gzipExtension = ".gz__"
|
|
|
|
|
2023-01-17 05:01:54 +08:00
|
|
|
func safeResolve(baseDir string, relPath string) string {
|
|
|
|
return filepath.Join(baseDir, filepath.Clean(filepath.Join(string(os.PathSeparator), relPath)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func uploads(router *httprouter.Router, baseDir string, fsys WriteFS) {
|
2021-11-11 01:57:22 +08:00
|
|
|
router.POST("/_apis/pipelines/workflows/:runId/artifacts", func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
|
|
|
|
runID := params.ByName("runId")
|
|
|
|
|
|
|
|
json, err := json.Marshal(FileContainerResourceURL{
|
|
|
|
FileContainerResourceURL: fmt.Sprintf("http://%s/upload/%s", req.Host, runID),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = w.Write(json)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
router.PUT("/upload/:runId", func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
|
|
|
|
itemPath := req.URL.Query().Get("itemPath")
|
|
|
|
runID := params.ByName("runId")
|
|
|
|
|
|
|
|
if req.Header.Get("Content-Encoding") == "gzip" {
|
|
|
|
itemPath += gzipExtension
|
|
|
|
}
|
|
|
|
|
2023-01-17 05:01:54 +08:00
|
|
|
safeRunPath := safeResolve(baseDir, runID)
|
|
|
|
safePath := safeResolve(safeRunPath, itemPath)
|
2021-11-11 01:57:22 +08:00
|
|
|
|
2023-01-17 05:01:54 +08:00
|
|
|
file, err := func() (WritableFile, error) {
|
2022-06-21 06:06:55 +08:00
|
|
|
contentRange := req.Header.Get("Content-Range")
|
|
|
|
if contentRange != "" && !strings.HasPrefix(contentRange, "bytes 0-") {
|
2023-01-17 05:01:54 +08:00
|
|
|
return fsys.OpenAppendable(safePath)
|
2022-06-21 06:06:55 +08:00
|
|
|
}
|
2023-01-17 05:01:54 +08:00
|
|
|
return fsys.OpenWritable(safePath)
|
2022-06-21 06:06:55 +08:00
|
|
|
}()
|
|
|
|
|
2021-11-11 01:57:22 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
writer, ok := file.(io.Writer)
|
|
|
|
if !ok {
|
|
|
|
panic(errors.New("File is not writable"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.Body == nil {
|
|
|
|
panic(errors.New("No body given"))
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(writer, req.Body)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
json, err := json.Marshal(ResponseMessage{
|
|
|
|
Message: "success",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = w.Write(json)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
router.PATCH("/_apis/pipelines/workflows/:runId/artifacts", func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
|
|
|
|
json, err := json.Marshal(ResponseMessage{
|
|
|
|
Message: "success",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = w.Write(json)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-01-17 05:01:54 +08:00
|
|
|
func downloads(router *httprouter.Router, baseDir string, fsys fs.FS) {
|
2021-11-11 01:57:22 +08:00
|
|
|
router.GET("/_apis/pipelines/workflows/:runId/artifacts", func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
|
|
|
|
runID := params.ByName("runId")
|
|
|
|
|
2023-01-17 05:01:54 +08:00
|
|
|
safePath := safeResolve(baseDir, runID)
|
|
|
|
|
|
|
|
entries, err := fs.ReadDir(fsys, safePath)
|
2021-11-11 01:57:22 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var list []NamedFileContainerResourceURL
|
|
|
|
for _, entry := range entries {
|
|
|
|
list = append(list, NamedFileContainerResourceURL{
|
|
|
|
Name: entry.Name(),
|
|
|
|
FileContainerResourceURL: fmt.Sprintf("http://%s/download/%s", req.Host, runID),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
json, err := json.Marshal(NamedFileContainerResourceURLResponse{
|
|
|
|
Count: len(list),
|
|
|
|
Value: list,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = w.Write(json)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
router.GET("/download/:container", func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
|
|
|
|
container := params.ByName("container")
|
|
|
|
itemPath := req.URL.Query().Get("itemPath")
|
2023-01-17 05:01:54 +08:00
|
|
|
safePath := safeResolve(baseDir, filepath.Join(container, itemPath))
|
2021-11-11 01:57:22 +08:00
|
|
|
|
|
|
|
var files []ContainerItem
|
2023-01-17 05:01:54 +08:00
|
|
|
err := fs.WalkDir(fsys, safePath, func(path string, entry fs.DirEntry, err error) error {
|
2021-11-11 01:57:22 +08:00
|
|
|
if !entry.IsDir() {
|
2023-01-17 05:01:54 +08:00
|
|
|
rel, err := filepath.Rel(safePath, path)
|
2021-11-11 01:57:22 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// if it was upload as gzip
|
|
|
|
rel = strings.TrimSuffix(rel, gzipExtension)
|
|
|
|
|
|
|
|
files = append(files, ContainerItem{
|
2023-01-17 05:01:54 +08:00
|
|
|
Path: filepath.Join(itemPath, rel),
|
2021-11-11 01:57:22 +08:00
|
|
|
ItemType: "file",
|
|
|
|
ContentLocation: fmt.Sprintf("http://%s/artifact/%s/%s/%s", req.Host, container, itemPath, rel),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
json, err := json.Marshal(ContainerItemResponse{
|
|
|
|
Value: files,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = w.Write(json)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
router.GET("/artifact/*path", func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
|
|
|
|
path := params.ByName("path")[1:]
|
|
|
|
|
2023-01-17 05:01:54 +08:00
|
|
|
safePath := safeResolve(baseDir, path)
|
|
|
|
|
|
|
|
file, err := fsys.Open(safePath)
|
2021-11-11 01:57:22 +08:00
|
|
|
if err != nil {
|
|
|
|
// try gzip file
|
2023-01-17 05:01:54 +08:00
|
|
|
file, err = fsys.Open(safePath + gzipExtension)
|
2021-11-11 01:57:22 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
w.Header().Add("Content-Encoding", "gzip")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(w, file)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-01-16 22:12:20 +08:00
|
|
|
func Serve(ctx context.Context, artifactPath string, addr string, port string) context.CancelFunc {
|
2021-11-11 01:57:22 +08:00
|
|
|
serverContext, cancel := context.WithCancel(ctx)
|
2022-06-17 23:55:21 +08:00
|
|
|
logger := common.Logger(serverContext)
|
2021-11-11 01:57:22 +08:00
|
|
|
|
|
|
|
if artifactPath == "" {
|
|
|
|
return cancel
|
|
|
|
}
|
|
|
|
|
|
|
|
router := httprouter.New()
|
|
|
|
|
2022-06-17 23:55:21 +08:00
|
|
|
logger.Debugf("Artifacts base path '%s'", artifactPath)
|
2023-01-17 05:01:54 +08:00
|
|
|
fsys := readWriteFSImpl{}
|
|
|
|
uploads(router, artifactPath, fsys)
|
|
|
|
downloads(router, artifactPath, fsys)
|
2021-11-11 01:57:22 +08:00
|
|
|
|
2022-07-25 20:12:48 +08:00
|
|
|
server := &http.Server{
|
2023-01-16 22:12:20 +08:00
|
|
|
Addr: fmt.Sprintf("%s:%s", addr, port),
|
2022-07-25 20:12:48 +08:00
|
|
|
ReadHeaderTimeout: 2 * time.Second,
|
|
|
|
Handler: router,
|
|
|
|
}
|
2021-11-11 01:57:22 +08:00
|
|
|
|
|
|
|
// run server
|
|
|
|
go func() {
|
2023-01-16 22:12:20 +08:00
|
|
|
logger.Infof("Start server on http://%s:%s", addr, port)
|
2021-11-11 01:57:22 +08:00
|
|
|
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
2022-06-17 23:55:21 +08:00
|
|
|
logger.Fatal(err)
|
2021-11-11 01:57:22 +08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// wait for cancel to gracefully shutdown server
|
|
|
|
go func() {
|
|
|
|
<-serverContext.Done()
|
|
|
|
|
|
|
|
if err := server.Shutdown(ctx); err != nil {
|
2022-06-17 23:55:21 +08:00
|
|
|
logger.Errorf("Failed shutdown gracefully - force shutdown: %v", err)
|
2021-11-11 01:57:22 +08:00
|
|
|
server.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return cancel
|
|
|
|
}
|