Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor chief submitpackage #111

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion cmd/chief/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,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 @@ -71,9 +72,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), server))

app = cli.NewApp()
app.Name = "irgsh-go"
Expand Down Expand Up @@ -115,6 +119,8 @@ func serve() {
http.HandleFunc("/api/v1/build-iso", BuildISOHandler)
http.HandleFunc("/api/v1/version", VersionHandler)

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

// Pages
http.HandleFunc("/maintainers", MaintainersHandler)
// Static file routes
Expand Down
32 changes: 26 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,45 @@ module github.com/blankon/irgsh-go
require (
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/RichardKnop/machinery v1.5.5
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7 // indirect
github.com/alecthomas/units v0.0.0-20201120081800-1786d5ef83d4 // indirect
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 // indirect
github.com/emirpasic/gods v1.9.0 // indirect
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/ghodss/yaml v1.0.0
github.com/gliderlabs/ssh v0.1.1 // indirect
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/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf
github.com/jinzhu/configor v1.0.0
github.com/julienschmidt/httprouter v1.3.0
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v0.0.0-20180830205328-81db2a75821e // indirect
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/mitchellh/go-homedir v1.0.0 // indirect
github.com/nicksnyder/go-i18n v1.10.1 // indirect
github.com/pelletier/go-buffruneio v0.2.0 // indirect
github.com/pkg/errors v0.8.0 // indirect
github.com/sergi/go-diff v1.0.0 // indirect
github.com/src-d/gcfg v1.4.0 // indirect
github.com/stretchr/testify v1.4.0
github.com/urfave/cli v1.20.0
github.com/xanzy/ssh-agent v0.2.0 // indirect
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad // indirect
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208 // indirect
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd // 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/src-d/go-billy.v4 v4.2.1 // indirect
gopkg.in/src-d/go-git-fixtures.v3 v3.1.1 // indirect
gopkg.in/src-d/go-git.v4 v4.7.0
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
gopkg.in/warnings.v0 v0.1.2 // indirect
)

go 1.13
68 changes: 57 additions & 11 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