with '#' will be ignored, and an empty message aborts the commit. Author: Ethan Alicea <64653625+Tech-Gamer@users.noreply.github.com> On branch develop Your branch is up to date with 'origin/develop'. Changes to be committed: modified: .github/workflows/push.yaml modified: .github/workflows/release.yaml modified: CHANGELOG.md modified: Dockerfile modified: Makefile modified: README.md modified: cmd/configure.go modified: cmd/diagnostics.go modified: cmd/root.go modified: config/config.go modified: environment/allocations.go modified: environment/docker.go modified: environment/docker/api.go modified: environment/docker/container.go modified: environment/docker/environment.go modified: environment/docker/power.go modified: environment/docker/stats.go modified: environment/environment.go modified: environment/settings.go modified: events/events.go modified: go.mod modified: internal/cron/activity_cron.go modified: internal/cron/cron.go modified: internal/cron/sftp_cron.go modified: internal/database/database.go modified: internal/progress/progress.go modified: internal/progress/progress_test.go modified: loggers/cli/cli.go new file: oryxBuildBinary modified: parser/parser.go modified: remote/http.go modified: remote/servers.go modified: remote/types.go modified: router/downloader/downloader.go modified: router/middleware.go modified: router/middleware/middleware.go modified: router/middleware/request_error.go modified: router/router.go modified: router/router_download.go modified: router/router_server.go modified: router/router_server_backup.go modified: router/router_server_files.go modified: router/router_server_transfer.go modified: router/router_server_ws.go modified: router/router_system.go modified: router/router_transfer.go modified: router/tokens/parser.go modified: router/websocket/listeners.go modified: router/websocket/websocket.go modified: server/activity.go modified: server/backup.go modified: server/backup/backup.go modified: server/backup/backup_local.go modified: server/backup/backup_s3.go modified: server/configuration.go modified: server/console.go modified: server/crash.go modified: server/events.go modified: server/filesystem/archive.go modified: server/filesystem/filesystem.go modified: server/filesystem/filesystem_test.go modified: server/install.go modified: server/installer/installer.go modified: server/listeners.go modified: server/manager.go modified: server/mounts.go modified: server/power.go modified: server/power_test.go modified: server/resources.go modified: server/server.go modified: server/transfer/archive.go modified: server/transfer/source.go modified: server/transfer/transfer.go modified: server/update.go modified: sftp/event.go modified: sftp/handler.go modified: sftp/server.go modified: wings.go
121 lines
3.1 KiB
Go
121 lines
3.1 KiB
Go
package docker
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
"reflect"
|
|
"strings"
|
|
"sync"
|
|
|
|
"emperror.dev/errors"
|
|
"github.com/docker/docker/api/types"
|
|
"github.com/docker/docker/api/types/versions"
|
|
"github.com/docker/docker/client"
|
|
"github.com/docker/docker/errdefs"
|
|
"github.com/goccy/go-json"
|
|
|
|
"github.com/Tech-Gamer/nwy-wings/config"
|
|
)
|
|
|
|
var (
|
|
o sync.Once
|
|
cli cliSettings
|
|
fastEnabled bool
|
|
)
|
|
|
|
type cliSettings struct {
|
|
enabled bool
|
|
proto string
|
|
host string
|
|
scheme string
|
|
version string
|
|
}
|
|
|
|
func configure(c *client.Client) {
|
|
o.Do(func() {
|
|
fastEnabled = config.Get().Docker.UsePerformantInspect
|
|
|
|
r := reflect.ValueOf(c).Elem()
|
|
cli.proto = r.FieldByName("proto").String()
|
|
cli.host = r.FieldByName("addr").String()
|
|
cli.scheme = r.FieldByName("scheme").String()
|
|
cli.version = r.FieldByName("version").String()
|
|
})
|
|
}
|
|
|
|
// ContainerInspect is a rough equivalent of Docker's client.ContainerInspect()
|
|
// but re-written to use a more performant JSON decoder. This is important since
|
|
// a large number of requests to this endpoint are spawned by Wings, and the
|
|
// standard "encoding/json" shows its performance woes badly even with single
|
|
// containers running.
|
|
func (e *Environment) ContainerInspect(ctx context.Context) (types.ContainerJSON, error) {
|
|
configure(e.client)
|
|
|
|
// Support feature flagging of this functionality so that if something goes
|
|
// wrong for now it is easy enough for people to switch back to the older method
|
|
// of fetching stats.
|
|
if !fastEnabled {
|
|
return e.client.ContainerInspect(ctx, e.Id)
|
|
}
|
|
|
|
var st types.ContainerJSON
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "/containers/"+e.Id+"/json", nil)
|
|
if err != nil {
|
|
return st, errors.WithStack(err)
|
|
}
|
|
|
|
if cli.proto == "unix" || cli.proto == "npipe" {
|
|
req.Host = "docker"
|
|
}
|
|
|
|
req.URL.Host = cli.host
|
|
req.URL.Scheme = cli.scheme
|
|
|
|
res, err := e.client.HTTPClient().Do(req)
|
|
if err != nil {
|
|
if res == nil {
|
|
return st, errdefs.Unknown(err)
|
|
}
|
|
return st, errdefs.FromStatusCode(err, res.StatusCode)
|
|
}
|
|
|
|
body, err := io.ReadAll(res.Body)
|
|
if err != nil {
|
|
return st, errors.Wrap(err, "failed to read response body from Docker")
|
|
}
|
|
if err := parseErrorFromResponse(res, body); err != nil {
|
|
return st, errdefs.FromStatusCode(err, res.StatusCode)
|
|
}
|
|
if err := json.Unmarshal(body, &st); err != nil {
|
|
return st, errors.WithStack(err)
|
|
}
|
|
return st, nil
|
|
}
|
|
|
|
// parseErrorFromResponse is a re-implementation of Docker's
|
|
// client.checkResponseErr() function.
|
|
func parseErrorFromResponse(res *http.Response, body []byte) error {
|
|
if res.StatusCode >= 200 && res.StatusCode < 400 {
|
|
return nil
|
|
}
|
|
|
|
var ct string
|
|
if res.Header != nil {
|
|
ct = res.Header.Get("Content-Type")
|
|
}
|
|
|
|
var emsg string
|
|
if (cli.version == "" || versions.GreaterThan(cli.version, "1.23")) && ct == "application/json" {
|
|
var errResp types.ErrorResponse
|
|
if err := json.Unmarshal(body, &errResp); err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
emsg = strings.TrimSpace(errResp.Message)
|
|
} else {
|
|
emsg = strings.TrimSpace(string(body))
|
|
}
|
|
|
|
return errors.Wrap(errors.New(emsg), "Error response from daemon")
|
|
}
|