Skip to content
Merged
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
11 changes: 6 additions & 5 deletions pkg/serviceapi/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ func Error(w http.ResponseWriter, apiMessage string, code int, err error) {
// Log detailed message of what happened to machine running podman service
log.Errorf(err.Error())

w.(ServiceWriter).WriteJSON(code, struct {
message string
}{
apiMessage,
})
w.(ServiceWriter).WriteJSON(code,
struct {
Message string `json:"message"`
}{
apiMessage,
})
}

func noSuchContainerError(w http.ResponseWriter, nameOrId string, err error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/serviceapi/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ func (w ServiceWriter) WriteJSON(code int, value interface{}) error {
w.WriteHeader(code)

coder := json.NewEncoder(w)
coder.SetEscapeHTML(false)
coder.SetEscapeHTML(true)
return coder.Encode(value)
}
27 changes: 23 additions & 4 deletions pkg/serviceapi/handler_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"strconv"
"strings"

"github.com/containers/libpod/libpod"
image2 "github.com/containers/libpod/libpod/image"
Expand Down Expand Up @@ -32,21 +33,39 @@ func createImage(w http.ResponseWriter, r *http.Request, runtime *libpod.Runtime
fromImage := r.Form.Get("fromImage")
// TODO
// We are eating the output right now because we haven't talked about how to deal with multiple responses yet
_, err := runtime.ImageRuntime().New(ctx, fromImage, "", "", nil, &image2.DockerRegistryOptions{}, image2.SigningOptions{}, nil, util.PullImageAlways)
img, err := runtime.ImageRuntime().New(ctx, fromImage, "", "", nil, &image2.DockerRegistryOptions{}, image2.SigningOptions{}, nil, util.PullImageAlways)
if err != nil {
Error(w, "Something went wrong.", http.StatusInternalServerError, err)
return
}

name := fromImage
tag := "latest"
idx := strings.LastIndexByte(fromImage, ':')
if idx != -1 {
name = fromImage[:idx]
tag = fromImage[idx:]
}
// Success
w.(ServiceWriter).WriteJSON(http.StatusOK, "")
w.(ServiceWriter).WriteJSON(http.StatusOK, struct {
Status string `json:"status"`
Error string `json:"error"`
Progress string `json:"progress"`
ProgressDetail map[string]string `json:"progressDetail"`
Id string `json:"id"`
}{
Status: fmt.Sprintf("Pulling image (%s) from %s", tag, name),
ProgressDetail: map[string]string{},
Id: img.ID(),
})
}

func tagImage(w http.ResponseWriter, r *http.Request, runtime *libpod.Runtime) {
// /v1.xx/images/(name)/tag
name := mux.Vars(r)["name"]
newImage, err := runtime.ImageRuntime().NewFromLocal(name)
if err != nil {
Error(w, "Something went wrong.", http.StatusNotFound, err)
noSuchImageError(w, name, errors.Wrapf(err, "Failed to find image %s", name))
return
}
tag := "latest"
Expand All @@ -70,7 +89,7 @@ func image(w http.ResponseWriter, r *http.Request, runtime *libpod.Runtime) {
name := mux.Vars(r)["name"]
newImage, err := runtime.ImageRuntime().NewFromLocal(name)
if err != nil {
Error(w, "Something went wrong.", http.StatusNotFound, errors.Wrapf(err, "Failed to find image %s", name))
noSuchImageError(w, name, errors.Wrapf(err, "Failed to find image %s", name))
return
}
ctx := context.Background()
Expand Down
7 changes: 3 additions & 4 deletions pkg/serviceapi/handler_notfound.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package serviceapi

import (
"fmt"
"net/http"

"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
)

func registerNotFoundHandlers(r *mux.Router) error {
Expand All @@ -13,7 +13,6 @@ func registerNotFoundHandlers(r *mux.Router) error {
}

func notFound(w http.ResponseWriter, r *http.Request) {
http.Error(w,
fmt.Sprintf("%d %s for '%s'", http.StatusNotFound, http.StatusText(http.StatusNotFound), r.URL.String()),
http.StatusNotFound)
log.Errorf("%d %s for %s:'%s'", http.StatusNotFound, http.StatusText(http.StatusNotFound), r.Method, r.URL.String())
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
}
18 changes: 11 additions & 7 deletions pkg/serviceapi/handler_swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@ import (
"net/http"

"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
)

func registerSwarmHandlers(r *mux.Router) error {
r.HandleFunc(unversionedPath("/swarm"), noSwarm)
r.HandleFunc(unversionedPath("/services"), noSwarm)
r.HandleFunc(unversionedPath("/nodes"), noSwarm)
r.HandleFunc(unversionedPath("/tasks"), noSwarm)
r.HandleFunc(unversionedPath("/secrets"), noSwarm)
r.HandleFunc(unversionedPath("/configs"), noSwarm)
r.PathPrefix("/v{version:[0-9.]+}/swarm/").HandlerFunc(noSwarm)
r.PathPrefix("/v{version:[0-9.]+}/services/").HandlerFunc(noSwarm)
r.PathPrefix("/v{version:[0-9.]+}/nodes/").HandlerFunc(noSwarm)
r.PathPrefix("/v{version:[0-9.]+}/tasks/").HandlerFunc(noSwarm)
r.PathPrefix("/v{version:[0-9.]+}/secrets/").HandlerFunc(noSwarm)
r.PathPrefix("/v{version:[0-9.]+}/configs/").HandlerFunc(noSwarm)
return nil
}

// noSwarm returns http.StatusServiceUnavailable rather than something like http.StatusInternalServerError,
// this allows the client to decide if they still can talk to us
func noSwarm(w http.ResponseWriter, r *http.Request) {
Error(w, "node is not part of a swarm", http.StatusServiceUnavailable, errors.New("swarm is not supported by podman"))
logrus.Errorf("%s is not a podman supported service", r.URL.String())
Error(ServiceWriter{w}, "node is not part of a swarm", http.StatusServiceUnavailable, errors.New("Podman does not support service: "+r.URL.String()))
}
37 changes: 27 additions & 10 deletions pkg/serviceapi/handler_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

func registerVersionHandlers(r *mux.Router) error {
r.Handle(unversionedPath("/version"), serviceHandler(versionHandler))
r.Handle("/version", serviceHandler(versionHandler))
return nil
}

Expand All @@ -32,22 +33,38 @@ func versionHandler(w http.ResponseWriter, r *http.Request, runtime *libpod.Runt
}
hostInfo := infoData[0].Data

components := []docker.ComponentVersion{{
Name: "Engine",
Version: versionInfo.Version,
Details: map[string]string{
"APIVersion": DefaultApiVersion,
"Arch": goRuntime.GOARCH,
"BuildTime": time.Unix(versionInfo.Built, 0).Format(time.RFC3339),
"Experimental": "true",
"GitCommit": versionInfo.GitCommit,
"GoVersion": versionInfo.GoVersion,
"KernelVersion": hostInfo["kernel"].(string),
"MinAPIVersion": MinimalApiVersion,
"Os": goRuntime.GOOS,
},
}}

w.(ServiceWriter).WriteJSON(http.StatusOK, Version{docker.Version{
Platform: struct {
Name string
}{
Name: fmt.Sprintf("%s/%s/%s", goRuntime.GOOS, goRuntime.GOARCH, hostInfo["Distribution"].(map[string]interface{})["distribution"].(string)),
},
Components: nil,
Version: versionInfo.Version,
APIVersion: DefaultApiVersion,
MinAPIVersion: MinimalApiVersion,
GitCommit: versionInfo.GitCommit,
GoVersion: versionInfo.GoVersion,
Os: goRuntime.GOOS,
Arch: goRuntime.GOARCH,
KernelVersion: hostInfo["kernel"].(string),
APIVersion: components[0].Details["APIVersion"],
Arch: components[0].Details["Arch"],
BuildTime: components[0].Details["BuildTime"],
Components: components,
Experimental: true,
BuildTime: time.Unix(versionInfo.Built, 0).Format(time.RFC3339),
GitCommit: components[0].Details["GitCommit"],
GoVersion: components[0].Details["GoVersion"],
KernelVersion: components[0].Details["KernelVersion"],
MinAPIVersion: components[0].Details["MinAPIVersion"],
Os: components[0].Details["Os"],
Version: components[0].Version,
}})
}
2 changes: 1 addition & 1 deletion pkg/serviceapi/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,5 @@ func (s *HttpServer) Close() error {
// unversionedPath prepends the version parsing code
// any handler may override this default when registering URL(s)
func unversionedPath(p string) string {
return "/v{version:[0-9.]*}" + p
return "/v{version:[0-9][0-9.]*}" + p
}