kojiapi: add a server/client implementation of the OpenAPI spec
This just translates between the OpenAPI spec and our internal API. This still lacks tests, but a follow-up commit adds integration tests. `internal/kojiapi/openapi.gen.go` was automatically generated from `internal/kojiapi/openapi.yml`. To regenerate use `go generate ./...`. Signed-off-by: Tom Gundersen <teg@jklm.no>
This commit is contained in:
parent
7109f49692
commit
67e4182ba4
17 changed files with 3062 additions and 5 deletions
490
internal/kojiapi/openapi.gen.go
Normal file
490
internal/kojiapi/openapi.gen.go
Normal file
|
|
@ -0,0 +1,490 @@
|
|||
// Package kojiapi provides primitives to interact the openapi HTTP API.
|
||||
//
|
||||
// Code generated by github.com/deepmap/oapi-codegen DO NOT EDIT.
|
||||
package kojiapi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/deepmap/oapi-codegen/pkg/runtime"
|
||||
"github.com/go-chi/chi"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ComposeRequest defines model for ComposeRequest.
|
||||
type ComposeRequest struct {
|
||||
Distribution string `json:"distribution"`
|
||||
ImageRequests []ImageRequest `json:"image_requests"`
|
||||
Koji Koji `json:"koji"`
|
||||
Name string `json:"name"`
|
||||
Release string `json:"release"`
|
||||
Version string `json:"version"`
|
||||
}
|
||||
|
||||
// ComposeResponse defines model for ComposeResponse.
|
||||
type ComposeResponse struct {
|
||||
Id string `json:"id"`
|
||||
KojiBuildId int `json:"koji_build_id"`
|
||||
}
|
||||
|
||||
// ComposeStatus defines model for ComposeStatus.
|
||||
type ComposeStatus struct {
|
||||
ImageStatuses []ImageStatus `json:"image_statuses"`
|
||||
KojiTaskId int `json:"koji_task_id"`
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
// ImageRequest defines model for ImageRequest.
|
||||
type ImageRequest struct {
|
||||
Architecture string `json:"architecture"`
|
||||
ImageType string `json:"image_type"`
|
||||
Repositories []Repository `json:"repositories"`
|
||||
}
|
||||
|
||||
// ImageStatus defines model for ImageStatus.
|
||||
type ImageStatus struct {
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
// Koji defines model for Koji.
|
||||
type Koji struct {
|
||||
Server string `json:"server"`
|
||||
TaskId int `json:"task_id"`
|
||||
}
|
||||
|
||||
// Repository defines model for Repository.
|
||||
type Repository struct {
|
||||
Baseurl string `json:"baseurl"`
|
||||
Gpgkey string `json:"gpgkey"`
|
||||
}
|
||||
|
||||
// PostComposeJSONBody defines parameters for PostCompose.
|
||||
type PostComposeJSONBody ComposeRequest
|
||||
|
||||
// PostComposeRequestBody defines body for PostCompose for application/json ContentType.
|
||||
type PostComposeJSONRequestBody PostComposeJSONBody
|
||||
|
||||
// RequestEditorFn is the function signature for the RequestEditor callback function
|
||||
type RequestEditorFn func(ctx context.Context, req *http.Request) error
|
||||
|
||||
// Doer performs HTTP requests.
|
||||
//
|
||||
// The standard http.Client implements this interface.
|
||||
type HttpRequestDoer interface {
|
||||
Do(req *http.Request) (*http.Response, error)
|
||||
}
|
||||
|
||||
// Client which conforms to the OpenAPI3 specification for this service.
|
||||
type Client struct {
|
||||
// The endpoint of the server conforming to this interface, with scheme,
|
||||
// https://api.deepmap.com for example.
|
||||
Server string
|
||||
|
||||
// Doer for performing requests, typically a *http.Client with any
|
||||
// customized settings, such as certificate chains.
|
||||
Client HttpRequestDoer
|
||||
|
||||
// A callback for modifying requests which are generated before sending over
|
||||
// the network.
|
||||
RequestEditor RequestEditorFn
|
||||
}
|
||||
|
||||
// ClientOption allows setting custom parameters during construction
|
||||
type ClientOption func(*Client) error
|
||||
|
||||
// Creates a new Client, with reasonable defaults
|
||||
func NewClient(server string, opts ...ClientOption) (*Client, error) {
|
||||
// create a client with sane default values
|
||||
client := Client{
|
||||
Server: server,
|
||||
}
|
||||
// mutate client and add all optional params
|
||||
for _, o := range opts {
|
||||
if err := o(&client); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
// ensure the server URL always has a trailing slash
|
||||
if !strings.HasSuffix(client.Server, "/") {
|
||||
client.Server += "/"
|
||||
}
|
||||
// create httpClient, if not already present
|
||||
if client.Client == nil {
|
||||
client.Client = http.DefaultClient
|
||||
}
|
||||
return &client, nil
|
||||
}
|
||||
|
||||
// WithHTTPClient allows overriding the default Doer, which is
|
||||
// automatically created using http.Client. This is useful for tests.
|
||||
func WithHTTPClient(doer HttpRequestDoer) ClientOption {
|
||||
return func(c *Client) error {
|
||||
c.Client = doer
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithRequestEditorFn allows setting up a callback function, which will be
|
||||
// called right before sending the request. This can be used to mutate the request.
|
||||
func WithRequestEditorFn(fn RequestEditorFn) ClientOption {
|
||||
return func(c *Client) error {
|
||||
c.RequestEditor = fn
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// The interface specification for the client above.
|
||||
type ClientInterface interface {
|
||||
// PostCompose request with any body
|
||||
PostComposeWithBody(ctx context.Context, contentType string, body io.Reader) (*http.Response, error)
|
||||
|
||||
PostCompose(ctx context.Context, body PostComposeJSONRequestBody) (*http.Response, error)
|
||||
|
||||
// GetComposeId request
|
||||
GetComposeId(ctx context.Context, id string) (*http.Response, error)
|
||||
}
|
||||
|
||||
func (c *Client) PostComposeWithBody(ctx context.Context, contentType string, body io.Reader) (*http.Response, error) {
|
||||
req, err := NewPostComposeRequestWithBody(c.Server, contentType, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req = req.WithContext(ctx)
|
||||
if c.RequestEditor != nil {
|
||||
err = c.RequestEditor(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return c.Client.Do(req)
|
||||
}
|
||||
|
||||
func (c *Client) PostCompose(ctx context.Context, body PostComposeJSONRequestBody) (*http.Response, error) {
|
||||
req, err := NewPostComposeRequest(c.Server, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req = req.WithContext(ctx)
|
||||
if c.RequestEditor != nil {
|
||||
err = c.RequestEditor(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return c.Client.Do(req)
|
||||
}
|
||||
|
||||
func (c *Client) GetComposeId(ctx context.Context, id string) (*http.Response, error) {
|
||||
req, err := NewGetComposeIdRequest(c.Server, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req = req.WithContext(ctx)
|
||||
if c.RequestEditor != nil {
|
||||
err = c.RequestEditor(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return c.Client.Do(req)
|
||||
}
|
||||
|
||||
// NewPostComposeRequest calls the generic PostCompose builder with application/json body
|
||||
func NewPostComposeRequest(server string, body PostComposeJSONRequestBody) (*http.Request, error) {
|
||||
var bodyReader io.Reader
|
||||
buf, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bodyReader = bytes.NewReader(buf)
|
||||
return NewPostComposeRequestWithBody(server, "application/json", bodyReader)
|
||||
}
|
||||
|
||||
// NewPostComposeRequestWithBody generates requests for PostCompose with any type of body
|
||||
func NewPostComposeRequestWithBody(server string, contentType string, body io.Reader) (*http.Request, error) {
|
||||
var err error
|
||||
|
||||
queryUrl, err := url.Parse(server)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
basePath := fmt.Sprintf("/compose")
|
||||
if basePath[0] == '/' {
|
||||
basePath = basePath[1:]
|
||||
}
|
||||
|
||||
queryUrl, err = queryUrl.Parse(basePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", queryUrl.String(), body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Add("Content-Type", contentType)
|
||||
return req, nil
|
||||
}
|
||||
|
||||
// NewGetComposeIdRequest generates requests for GetComposeId
|
||||
func NewGetComposeIdRequest(server string, id string) (*http.Request, error) {
|
||||
var err error
|
||||
|
||||
var pathParam0 string
|
||||
|
||||
pathParam0, err = runtime.StyleParam("simple", false, "id", id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
queryUrl, err := url.Parse(server)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
basePath := fmt.Sprintf("/compose/%s", pathParam0)
|
||||
if basePath[0] == '/' {
|
||||
basePath = basePath[1:]
|
||||
}
|
||||
|
||||
queryUrl, err = queryUrl.Parse(basePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("GET", queryUrl.String(), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return req, nil
|
||||
}
|
||||
|
||||
// ClientWithResponses builds on ClientInterface to offer response payloads
|
||||
type ClientWithResponses struct {
|
||||
ClientInterface
|
||||
}
|
||||
|
||||
// NewClientWithResponses creates a new ClientWithResponses, which wraps
|
||||
// Client with return type handling
|
||||
func NewClientWithResponses(server string, opts ...ClientOption) (*ClientWithResponses, error) {
|
||||
client, err := NewClient(server, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &ClientWithResponses{client}, nil
|
||||
}
|
||||
|
||||
// WithBaseURL overrides the baseURL.
|
||||
func WithBaseURL(baseURL string) ClientOption {
|
||||
return func(c *Client) error {
|
||||
newBaseURL, err := url.Parse(baseURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.Server = newBaseURL.String()
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// ClientWithResponsesInterface is the interface specification for the client with responses above.
|
||||
type ClientWithResponsesInterface interface {
|
||||
// PostCompose request with any body
|
||||
PostComposeWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader) (*PostComposeResponse, error)
|
||||
|
||||
PostComposeWithResponse(ctx context.Context, body PostComposeJSONRequestBody) (*PostComposeResponse, error)
|
||||
|
||||
// GetComposeId request
|
||||
GetComposeIdWithResponse(ctx context.Context, id string) (*GetComposeIdResponse, error)
|
||||
}
|
||||
|
||||
type PostComposeResponse struct {
|
||||
Body []byte
|
||||
HTTPResponse *http.Response
|
||||
JSON201 *ComposeResponse
|
||||
}
|
||||
|
||||
// Status returns HTTPResponse.Status
|
||||
func (r PostComposeResponse) Status() string {
|
||||
if r.HTTPResponse != nil {
|
||||
return r.HTTPResponse.Status
|
||||
}
|
||||
return http.StatusText(0)
|
||||
}
|
||||
|
||||
// StatusCode returns HTTPResponse.StatusCode
|
||||
func (r PostComposeResponse) StatusCode() int {
|
||||
if r.HTTPResponse != nil {
|
||||
return r.HTTPResponse.StatusCode
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
type GetComposeIdResponse struct {
|
||||
Body []byte
|
||||
HTTPResponse *http.Response
|
||||
JSON200 *ComposeStatus
|
||||
}
|
||||
|
||||
// Status returns HTTPResponse.Status
|
||||
func (r GetComposeIdResponse) Status() string {
|
||||
if r.HTTPResponse != nil {
|
||||
return r.HTTPResponse.Status
|
||||
}
|
||||
return http.StatusText(0)
|
||||
}
|
||||
|
||||
// StatusCode returns HTTPResponse.StatusCode
|
||||
func (r GetComposeIdResponse) StatusCode() int {
|
||||
if r.HTTPResponse != nil {
|
||||
return r.HTTPResponse.StatusCode
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// PostComposeWithBodyWithResponse request with arbitrary body returning *PostComposeResponse
|
||||
func (c *ClientWithResponses) PostComposeWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader) (*PostComposeResponse, error) {
|
||||
rsp, err := c.PostComposeWithBody(ctx, contentType, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ParsePostComposeResponse(rsp)
|
||||
}
|
||||
|
||||
func (c *ClientWithResponses) PostComposeWithResponse(ctx context.Context, body PostComposeJSONRequestBody) (*PostComposeResponse, error) {
|
||||
rsp, err := c.PostCompose(ctx, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ParsePostComposeResponse(rsp)
|
||||
}
|
||||
|
||||
// GetComposeIdWithResponse request returning *GetComposeIdResponse
|
||||
func (c *ClientWithResponses) GetComposeIdWithResponse(ctx context.Context, id string) (*GetComposeIdResponse, error) {
|
||||
rsp, err := c.GetComposeId(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ParseGetComposeIdResponse(rsp)
|
||||
}
|
||||
|
||||
// ParsePostComposeResponse parses an HTTP response from a PostComposeWithResponse call
|
||||
func ParsePostComposeResponse(rsp *http.Response) (*PostComposeResponse, error) {
|
||||
bodyBytes, err := ioutil.ReadAll(rsp.Body)
|
||||
defer rsp.Body.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
response := &PostComposeResponse{
|
||||
Body: bodyBytes,
|
||||
HTTPResponse: rsp,
|
||||
}
|
||||
|
||||
switch {
|
||||
case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 201:
|
||||
var dest ComposeResponse
|
||||
if err := json.Unmarshal(bodyBytes, &dest); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
response.JSON201 = &dest
|
||||
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
// ParseGetComposeIdResponse parses an HTTP response from a GetComposeIdWithResponse call
|
||||
func ParseGetComposeIdResponse(rsp *http.Response) (*GetComposeIdResponse, error) {
|
||||
bodyBytes, err := ioutil.ReadAll(rsp.Body)
|
||||
defer rsp.Body.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
response := &GetComposeIdResponse{
|
||||
Body: bodyBytes,
|
||||
HTTPResponse: rsp,
|
||||
}
|
||||
|
||||
switch {
|
||||
case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 200:
|
||||
var dest ComposeStatus
|
||||
if err := json.Unmarshal(bodyBytes, &dest); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
response.JSON200 = &dest
|
||||
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
// ServerInterface represents all server handlers.
|
||||
type ServerInterface interface {
|
||||
// Create compose
|
||||
// (POST /compose)
|
||||
PostCompose(w http.ResponseWriter, r *http.Request)
|
||||
// The status of a compose
|
||||
// (GET /compose/{id})
|
||||
GetComposeId(w http.ResponseWriter, r *http.Request, id string)
|
||||
}
|
||||
|
||||
// ServerInterfaceWrapper converts contexts to parameters.
|
||||
type ServerInterfaceWrapper struct {
|
||||
Handler ServerInterface
|
||||
}
|
||||
|
||||
// PostCompose operation middleware
|
||||
func (siw *ServerInterfaceWrapper) PostCompose(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
siw.Handler.PostCompose(w, r.WithContext(ctx))
|
||||
}
|
||||
|
||||
// GetComposeId operation middleware
|
||||
func (siw *ServerInterfaceWrapper) GetComposeId(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
var err error
|
||||
|
||||
// ------------- Path parameter "id" -------------
|
||||
var id string
|
||||
|
||||
err = runtime.BindStyledParameter("simple", false, "id", chi.URLParam(r, "id"), &id)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("Invalid format for parameter id: %s", err), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
siw.Handler.GetComposeId(w, r.WithContext(ctx), id)
|
||||
}
|
||||
|
||||
// Handler creates http.Handler with routing matching OpenAPI spec.
|
||||
func Handler(si ServerInterface) http.Handler {
|
||||
return HandlerFromMux(si, chi.NewRouter())
|
||||
}
|
||||
|
||||
// HandlerFromMux creates http.Handler with routing matching OpenAPI spec based on the provided mux.
|
||||
func HandlerFromMux(si ServerInterface, r chi.Router) http.Handler {
|
||||
wrapper := ServerInterfaceWrapper{
|
||||
Handler: si,
|
||||
}
|
||||
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Post("/compose", wrapper.PostCompose)
|
||||
})
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Get("/compose/{id}", wrapper.GetComposeId)
|
||||
})
|
||||
|
||||
return r
|
||||
}
|
||||
199
internal/kojiapi/server.go
Normal file
199
internal/kojiapi/server.go
Normal file
|
|
@ -0,0 +1,199 @@
|
|||
//go:generate go run github.com/deepmap/oapi-codegen/cmd/oapi-codegen --package=kojiapi --generate types,chi-server,client -o openapi.gen.go openapi.yml
|
||||
|
||||
// Package kojiapi provides a REST API to build and push images to Koji
|
||||
package kojiapi
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/osbuild/osbuild-composer/internal/blueprint"
|
||||
"github.com/osbuild/osbuild-composer/internal/common"
|
||||
"github.com/osbuild/osbuild-composer/internal/distro"
|
||||
"github.com/osbuild/osbuild-composer/internal/rpmmd"
|
||||
"github.com/osbuild/osbuild-composer/internal/worker"
|
||||
)
|
||||
|
||||
// Server represents the state of the koji Server
|
||||
type Server struct {
|
||||
workers *worker.Server
|
||||
rpmMetadata rpmmd.RPMMD
|
||||
distros *distro.Registry
|
||||
}
|
||||
|
||||
// NewServer creates a new koji server
|
||||
func NewServer(workers *worker.Server, rpmMetadata rpmmd.RPMMD, distros *distro.Registry) *Server {
|
||||
server := &Server{
|
||||
workers: workers,
|
||||
rpmMetadata: rpmMetadata,
|
||||
distros: distros,
|
||||
}
|
||||
return server
|
||||
}
|
||||
|
||||
// Serve serves the koji API over the provided listener socket
|
||||
func (server *Server) Serve(listener net.Listener) error {
|
||||
s := http.Server{Handler: Handler(server)}
|
||||
|
||||
err := s.Serve(listener)
|
||||
if err != nil && err != http.ErrServerClosed {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// PostCompose handles a new /compose POST request
|
||||
func (server *Server) PostCompose(w http.ResponseWriter, r *http.Request) {
|
||||
contentType := r.Header["Content-Type"]
|
||||
if len(contentType) != 1 || contentType[0] != "application/json" {
|
||||
http.Error(w, "Only 'application/json' content type is supported", http.StatusUnsupportedMediaType)
|
||||
return
|
||||
}
|
||||
|
||||
var request ComposeRequest
|
||||
err := json.NewDecoder(r.Body).Decode(&request)
|
||||
if err != nil {
|
||||
http.Error(w, "Could not parse JSON body", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
d := server.distros.GetDistro(request.Distribution)
|
||||
if d == nil {
|
||||
http.Error(w, fmt.Sprintf("Unsupported distribution: %s", request.Distribution), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
type imageRequest struct {
|
||||
manifest distro.Manifest
|
||||
}
|
||||
imageRequests := make([]imageRequest, len(request.ImageRequests))
|
||||
|
||||
for i, ir := range request.ImageRequests {
|
||||
arch, err := d.GetArch(ir.Architecture)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("Unsupported architecture '%s' for distribution '%s'", ir.Architecture, request.Distribution), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
imageType, err := arch.GetImageType(ir.ImageType)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("Unsupported image type '%s' for %s/%s", ir.ImageType, ir.Architecture, request.Distribution), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
repositories := make([]rpmmd.RepoConfig, len(ir.Repositories))
|
||||
for j, repo := range ir.Repositories {
|
||||
repositories[j].BaseURL = repo.Baseurl
|
||||
repositories[j].GPGKey = repo.Gpgkey
|
||||
}
|
||||
bp := &blueprint.Blueprint{}
|
||||
err = bp.Initialize()
|
||||
if err != nil {
|
||||
panic("Could not initialize empty blueprint.")
|
||||
}
|
||||
packageSpecs, _ := imageType.Packages(*bp)
|
||||
packages, _, err := server.rpmMetadata.Depsolve(packageSpecs, nil, repositories, d.ModulePlatformID(), arch.Name())
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("Failed to depsolve base base packages for %s/%s/%s: %s", ir.ImageType, ir.Architecture, request.Distribution, err), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
buildPackageSpecs := imageType.BuildPackages()
|
||||
buildPackages, _, err := server.rpmMetadata.Depsolve(buildPackageSpecs, nil, repositories, d.ModulePlatformID(), arch.Name())
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("Failed to depsolve build packages for %s/%s/%s: %s", ir.ImageType, ir.Architecture, request.Distribution, err), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
manifest, err := imageType.Manifest(nil, distro.ImageOptions{Size: imageType.Size(0)}, repositories, packages, buildPackages)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("Failed to get manifest for for %s/%s/%s: %s", ir.ImageType, ir.Architecture, request.Distribution, err), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
imageRequests[i].manifest = manifest
|
||||
}
|
||||
|
||||
var ir imageRequest
|
||||
if len(imageRequests) == 1 {
|
||||
// NOTE: the store currently does not support multi-image composes
|
||||
ir = imageRequests[0]
|
||||
} else {
|
||||
http.Error(w, "Only single-image composes are currently supported", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
id, err := server.workers.Enqueue(ir.manifest, nil)
|
||||
if err != nil {
|
||||
// This is a programming errror.
|
||||
panic(err)
|
||||
}
|
||||
|
||||
var response ComposeResponse
|
||||
response.Id = id.String()
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
err = json.NewEncoder(w).Encode(response)
|
||||
if err != nil {
|
||||
panic("Failed to write response")
|
||||
}
|
||||
}
|
||||
|
||||
func composeStateToStatus(state common.ComposeState) string {
|
||||
switch state {
|
||||
case common.CFailed:
|
||||
return "failure"
|
||||
case common.CFinished:
|
||||
return "success"
|
||||
case common.CRunning:
|
||||
return "pending"
|
||||
case common.CWaiting:
|
||||
return "pending"
|
||||
default:
|
||||
panic("invalid compose state")
|
||||
}
|
||||
}
|
||||
|
||||
func composeStateToImageStatus(state common.ComposeState) string {
|
||||
switch state {
|
||||
case common.CFailed:
|
||||
return "failure"
|
||||
case common.CFinished:
|
||||
return "success"
|
||||
case common.CRunning:
|
||||
return "building"
|
||||
case common.CWaiting:
|
||||
return "pending"
|
||||
default:
|
||||
panic("invalid compose state")
|
||||
}
|
||||
}
|
||||
|
||||
// GetComposeId handles a /compose/{id} GET request
|
||||
func (server *Server) GetComposeId(w http.ResponseWriter, r *http.Request, id string) {
|
||||
parsedID, err := uuid.Parse(id)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("Invalid format for parameter id: %s", err), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
status, err := server.workers.JobStatus(parsedID)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("Job %s not found: %s", id, err), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
response := ComposeStatus{
|
||||
Status: composeStateToStatus(status.State),
|
||||
ImageStatuses: []ImageStatus{
|
||||
{
|
||||
Status: composeStateToImageStatus(status.State),
|
||||
},
|
||||
},
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
err = json.NewEncoder(w).Encode(response)
|
||||
if err != nil {
|
||||
panic("Failed to write response")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue