Skip to content

Commit

Permalink
Refactoring chief, rewrite the submission API to use native golang fu…
Browse files Browse the repository at this point in the history
…nction and minimizing bash call

The only exec used is for gpg, after much considering and prototyping using the works one seems to be the wiser choice

machinery done, tarball deleted
  • Loading branch information
wejick committed Dec 27, 2020
1 parent e3d6e49 commit c39d071
Show file tree
Hide file tree
Showing 16 changed files with 797 additions and 139 deletions.
10 changes: 8 additions & 2 deletions cmd/chief/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/urfave/cli"

"github.com/blankon/irgsh-go/internal/config"
easypgp "github.com/blankon/irgsh-go/pkg/easygpg"

artifactEndpoint "github.com/blankon/irgsh-go/internal/artifact/endpoint"
artifactRepo "github.com/blankon/irgsh-go/internal/artifact/repo"
Expand Down Expand Up @@ -62,9 +63,12 @@ func main() {
}
log.Println(irgshConfig.Chief.Workdir)

// EasyGPG
egpg := easypgp.EasyPGP{}

artifactHTTPEndpoint = artifactEndpoint.NewArtifactHTTPEndpoint(
artifactService.NewArtifactService(
artifactRepo.NewFileRepo(irgshConfig.Chief.Workdir)))
artifactRepo.NewFileRepo(irgshConfig.Chief.Workdir, egpg)))

app = cli.NewApp()
app.Name = "irgsh-go"
Expand Down Expand Up @@ -96,12 +100,14 @@ func main() {
func serve() {
http.HandleFunc("/", indexHandler)
http.HandleFunc("/api/v1/artifacts", artifactHTTPEndpoint.GetArtifactListHandler)
http.HandleFunc("/api/v1/submit", PackageSubmitHandler)
http.HandleFunc("/api/v1/submit", artifactHTTPEndpoint.SubmitPackageHandler)
http.HandleFunc("/api/v1/status", BuildStatusHandler)
http.HandleFunc("/api/v1/artifact-upload", artifactUploadHandler())
http.HandleFunc("/api/v1/log-upload", logUploadHandler())
http.HandleFunc("/api/v1/build-iso", BuildISOHandler)

http.HandleFunc("/api/v2/submit", artifactHTTPEndpoint.SubmitPackageHandler)

artifactFs := http.FileServer(http.Dir(irgshConfig.Chief.Workdir + "/artifacts"))
http.Handle("/artifacts/", http.StripPrefix("/artifacts/", artifactFs))

Expand Down
13 changes: 8 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,27 @@ module github.com/blankon/irgsh-go
require (
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/RichardKnop/machinery v1.5.5
github.com/alecthomas/units v0.0.0-20201120081800-1786d5ef83d4 // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/ghodss/yaml v1.0.0
github.com/go-playground/locales v0.12.1 // indirect
github.com/go-playground/universal-translator v0.16.0 // indirect
github.com/google/uuid v1.1.0
github.com/hpcloud/tail v1.0.0
github.com/imroc/req v0.2.3
github.com/jinzhu/configor v1.0.0
github.com/julienschmidt/httprouter v1.3.0
github.com/leodido/go-urn v1.1.0 // indirect
github.com/manifoldco/promptui v0.3.2
github.com/stretchr/testify v1.2.2
github.com/nicksnyder/go-i18n v1.10.1 // indirect
github.com/stretchr/testify v1.4.0
github.com/urfave/cli v1.20.0
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
golang.org/x/tools v0.0.0-20200815165600-90abf76919f3 // indirect
gopkg.in/alecthomas/kingpin.v3-unstable v3.0.0-20191105091915-95d230a53780 // indirect
gopkg.in/fsnotify.v1 v1.4.7 // indirect
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
gopkg.in/go-playground/validator.v9 v9.27.0
gopkg.in/src-d/go-git.v4 v4.8.1
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v2 v2.2.1
mvdan.cc/sh v2.6.4+incompatible // indirect
)

go 1.13
72 changes: 66 additions & 6 deletions go.sum

Large diffs are not rendered by default.

32 changes: 7 additions & 25 deletions internal/artifact/endpoint/artifact.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,12 @@
package endpoint

import (
"net/http"

service "github.com/blankon/irgsh-go/internal/artifact/service"
httputil "github.com/blankon/irgsh-go/pkg/httputil"
)

// ArtifactHTTPEndpoint http endpoint for artifact
type ArtifactHTTPEndpoint struct {
service *service.ArtifactService
// SubmissionRequest request parameter
type SubmissionRequest struct {
Tarball string `json:"tarball"`
}

// NewArtifactHTTPEndpoint returns new artifact instance
func NewArtifactHTTPEndpoint(service *service.ArtifactService) *ArtifactHTTPEndpoint {
return &ArtifactHTTPEndpoint{
service: service,
}
}

// GetArtifactListHandler get artifact
func (A *ArtifactHTTPEndpoint) GetArtifactListHandler(w http.ResponseWriter, r *http.Request) {
artifactList, err := A.service.GetArtifactList(1, 1)
if err != nil {
httputil.ResponseError("Can't get artifact", 500, w)
}

httputil.ResponseJSON(artifactList, 200, w)
// SubmissionResponse response
type SubmissionResponse struct {
PipelineID string `json:"pipelineId"`
Jobs []string `json:"jobs"`
}
62 changes: 62 additions & 0 deletions internal/artifact/endpoint/artifact_http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package endpoint

import (
"encoding/json"
"io/ioutil"
"net/http"

service "github.com/blankon/irgsh-go/internal/artifact/service"
httputil "github.com/blankon/irgsh-go/pkg/httputil"
)

// ArtifactHTTPEndpoint http endpoint for artifact
type ArtifactHTTPEndpoint struct {
service *service.ArtifactService
}

// NewArtifactHTTPEndpoint returns new artifact instance
func NewArtifactHTTPEndpoint(service *service.ArtifactService) *ArtifactHTTPEndpoint {
return &ArtifactHTTPEndpoint{
service: service,
}
}

// GetArtifactListHandler get artifact
func (A *ArtifactHTTPEndpoint) GetArtifactListHandler(w http.ResponseWriter, r *http.Request) {
artifactList, err := A.service.GetArtifactList(1, 1)
if err != nil {
httputil.ResponseError("Can't get artifact", http.StatusInternalServerError, w)
}

httputil.ResponseJSON(artifactList, http.StatusOK, w)
}

// SubmitPackageHandler submit package
func (A *ArtifactHTTPEndpoint) SubmitPackageHandler(w http.ResponseWriter, r *http.Request) {
var requestParam SubmissionRequest

b, err := ioutil.ReadAll(r.Body)
if err != nil {
httputil.ResponseError("Can't read request body", http.StatusBadRequest, w)
}
defer r.Body.Close()

err = json.Unmarshal(b, &requestParam)
if err != nil {
httputil.ResponseError("Can't read request body", http.StatusBadRequest, w)
}

jobDetail, err := A.service.SubmitPackage(requestParam.Tarball)
if err != nil {
httputil.ResponseError("Can't complete the submission", http.StatusInternalServerError, w)
}

httputil.ResponseJSON(submissionToSubmissionResponse(jobDetail), http.StatusOK, w)
}

func submissionToSubmissionResponse(job service.Submission) SubmissionResponse {
return SubmissionResponse{
PipelineID: job.PipelineID,
Jobs: job.Jobs,
}
}
17 changes: 17 additions & 0 deletions internal/artifact/model/artifact.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package model

import "time"

// Artifact represent artifact data
type Artifact struct {
Name string
}

// Submission represent submission data
type Submission struct {
TaskUUID string `json:"taskUUID"`
Timestamp time.Time `json:"timestamp"`
SourceURL string `json:"sourceUrl"`
PackageURL string `json:"packageUrl"`
Tarball string `json:"tarball"`
}
14 changes: 8 additions & 6 deletions internal/artifact/repo/artifact.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package repo

//go:generate moq -out artifact_repo_moq.go . Repo
import (
model "github.com/blankon/irgsh-go/internal/artifact/model"
)

// ArtifactModel represent artifact data
type ArtifactModel struct {
Name string
}
//go:generate moq -out artifact_repo_moq.go . Repo

// ArtifactList list of artifacts
type ArtifactList struct {
TotalData int
Artifacts []ArtifactModel
Artifacts []model.Artifact
}

// Repo interface to operate with artifact
type Repo interface {
GetArtifactList(pageNum int64, rows int64) (ArtifactList, error)
PutTarballToFile(tarball *string, taskUUID string) error
ExtractSubmittedTarball(taskUUID string, deleteTarball bool) error
VerifyArtifact(taskUUID string) (bool, error)
}
Loading

0 comments on commit c39d071

Please sign in to comment.