api/jobqueue: add route to upload images
This commit is contained in:
parent
08c5eaf6a6
commit
98aac91083
2 changed files with 76 additions and 0 deletions
|
|
@ -33,6 +33,7 @@ func New(logger *log.Logger, store *store.Store) *API {
|
|||
|
||||
api.router.POST("/job-queue/v1/jobs", api.addJobHandler)
|
||||
api.router.PATCH("/job-queue/v1/jobs/:job_id/builds/:build_id", api.updateJobHandler)
|
||||
api.router.POST("/job-queue/v1/jobs/:job_id/builds/:build_id/image", api.addJobImageHandler)
|
||||
|
||||
return api
|
||||
}
|
||||
|
|
@ -153,3 +154,34 @@ func (api *API) updateJobHandler(writer http.ResponseWriter, request *http.Reque
|
|||
}
|
||||
statusResponseOK(writer)
|
||||
}
|
||||
|
||||
func (api *API) addJobImageHandler(writer http.ResponseWriter, request *http.Request, params httprouter.Params) {
|
||||
id, err := uuid.Parse(params.ByName("job_id"))
|
||||
if err != nil {
|
||||
statusResponseError(writer, http.StatusBadRequest, "invalid compose id: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
imageBuildId, err := strconv.Atoi(params.ByName("build_id"))
|
||||
|
||||
if err != nil {
|
||||
statusResponseError(writer, http.StatusBadRequest, "invalid build id: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
err = api.store.AddImageToImageUpload(id, imageBuildId, request.Body)
|
||||
|
||||
if err != nil {
|
||||
switch err.(type) {
|
||||
case *store.NotFoundError:
|
||||
statusResponseError(writer, http.StatusNotFound, err.Error())
|
||||
case *store.NoLocalTargetError:
|
||||
statusResponseError(writer, http.StatusBadRequest, err.Error())
|
||||
default:
|
||||
statusResponseError(writer, http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
statusResponseOK(writer)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -100,6 +100,14 @@ func (e *InvalidRequestError) Error() string {
|
|||
return e.message
|
||||
}
|
||||
|
||||
type NoLocalTargetError struct {
|
||||
message string
|
||||
}
|
||||
|
||||
func (e *NoLocalTargetError) Error() string {
|
||||
return e.message
|
||||
}
|
||||
|
||||
func New(stateDir *string, distroArg distro.Distro, distroRegistryArg distro.Registry) *Store {
|
||||
var s Store
|
||||
|
||||
|
|
@ -734,6 +742,42 @@ func (s *Store) UpdateImageBuildInCompose(composeID uuid.UUID, imageBuildID int,
|
|||
})
|
||||
}
|
||||
|
||||
func (s *Store) AddImageToImageUpload(composeID uuid.UUID, imageBuildID int, reader io.Reader) error {
|
||||
currentCompose, exists := s.Composes[composeID]
|
||||
if !exists {
|
||||
return &NotFoundError{"compose does not exist"}
|
||||
}
|
||||
|
||||
imageBuild := currentCompose.ImageBuilds[imageBuildID]
|
||||
localTarget := imageBuild.GetLocalTarget()
|
||||
|
||||
if localTarget == nil {
|
||||
return &NoLocalTargetError{fmt.Sprintf("image upload requested for compse %s and image build %d but it has no local target", composeID.String(), imageBuildID)}
|
||||
}
|
||||
|
||||
imageType, _ := imageBuild.ImageType.ToCompatString()
|
||||
filename, _, err := s.distro.FilenameFromType(imageType)
|
||||
|
||||
if err != nil {
|
||||
return &InvalidRequestError{err.Error()}
|
||||
}
|
||||
|
||||
path := fmt.Sprintf("%s/%s", localTarget.Location, filename)
|
||||
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = io.Copy(f, reader)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Store) PushSource(source SourceConfig) {
|
||||
// FIXME: handle or comment this possible error
|
||||
_ = s.change(func() error {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue