slim/pkg/app/master/commands/registry/handler.go
2022-01-09 19:42:33 -08:00

197 lines
5.3 KiB
Go

package registry
import (
"fmt"
"github.com/docker-slim/docker-slim/pkg/app"
"github.com/docker-slim/docker-slim/pkg/app/master/commands"
"github.com/docker-slim/docker-slim/pkg/app/master/docker/dockerclient"
"github.com/docker-slim/docker-slim/pkg/app/master/version"
"github.com/docker-slim/docker-slim/pkg/command"
"github.com/docker-slim/docker-slim/pkg/report"
"github.com/docker-slim/docker-slim/pkg/util/errutil"
"github.com/docker-slim/docker-slim/pkg/util/fsutil"
v "github.com/docker-slim/docker-slim/pkg/version"
log "github.com/sirupsen/logrus"
)
const appName = commands.AppName
type ovars = app.OutVars
// OnPullCommand implements the 'registry pull' docker-slim command
func OnPullCommand(
xc *app.ExecutionContext,
gparams *commands.GenericParams,
cparams *PullCommandParams) {
cmdName := fullCmdName(PullCmdName)
logger := log.WithFields(log.Fields{"app": appName, "command": cmdName})
prefix := fmt.Sprintf("cmd=%s", cmdName)
viChan := version.CheckAsync(gparams.CheckVersion, gparams.InContainer, gparams.IsDSImage)
cmdReport := report.NewRegistryCommand(gparams.ReportLocation, gparams.InContainer)
cmdReport.State = command.StateStarted
xc.Out.State("started")
client, err := dockerclient.New(gparams.ClientConfig)
if err == dockerclient.ErrNoDockerInfo {
exitMsg := "missing Docker connection info"
if gparams.InContainer && gparams.IsDSImage {
exitMsg = "make sure to pass the Docker connect parameters to the docker-slim container"
}
xc.Out.Info("docker.connect.error",
ovars{
"message": exitMsg,
})
exitCode := commands.ECTCommon | commands.ECNoDockerConnectInfo
xc.Out.State("exited",
ovars{
"exit.code": exitCode,
"version": v.Current(),
"location": fsutil.ExeDir(),
})
xc.Exit(exitCode)
}
errutil.FailOn(err)
if gparams.Debug {
version.Print(prefix, logger, client, false, gparams.InContainer, gparams.IsDSImage)
}
xc.Out.State("completed")
cmdReport.State = command.StateCompleted
xc.Out.State("done")
vinfo := <-viChan
version.PrintCheckVersion(xc, "", vinfo)
cmdReport.State = command.StateDone
if cmdReport.Save() {
xc.Out.Info("report",
ovars{
"file": cmdReport.ReportLocation(),
})
}
}
// OnPushCommand implements the 'registry push' docker-slim command
func OnPushCommand(
xc *app.ExecutionContext,
gparams *commands.GenericParams) {
cmdName := fullCmdName(PushCmdName)
logger := log.WithFields(log.Fields{"app": appName, "command": cmdName})
prefix := fmt.Sprintf("cmd=%s", cmdName)
viChan := version.CheckAsync(gparams.CheckVersion, gparams.InContainer, gparams.IsDSImage)
cmdReport := report.NewRegistryCommand(gparams.ReportLocation, gparams.InContainer)
cmdReport.State = command.StateStarted
xc.Out.State("started")
client, err := dockerclient.New(gparams.ClientConfig)
if err == dockerclient.ErrNoDockerInfo {
exitMsg := "missing Docker connection info"
if gparams.InContainer && gparams.IsDSImage {
exitMsg = "make sure to pass the Docker connect parameters to the docker-slim container"
}
xc.Out.Info("docker.connect.error",
ovars{
"message": exitMsg,
})
exitCode := commands.ECTCommon | commands.ECNoDockerConnectInfo
xc.Out.State("exited",
ovars{
"exit.code": exitCode,
"version": v.Current(),
"location": fsutil.ExeDir(),
})
xc.Exit(exitCode)
}
errutil.FailOn(err)
if gparams.Debug {
version.Print(prefix, logger, client, false, gparams.InContainer, gparams.IsDSImage)
}
xc.Out.State("completed")
cmdReport.State = command.StateCompleted
xc.Out.State("done")
vinfo := <-viChan
version.PrintCheckVersion(xc, "", vinfo)
cmdReport.State = command.StateDone
if cmdReport.Save() {
xc.Out.Info("report",
ovars{
"file": cmdReport.ReportLocation(),
})
}
}
// OnCopyCommand implements the 'registry copy' docker-slim command
func OnCopyCommand(
xc *app.ExecutionContext,
gparams *commands.GenericParams) {
cmdName := fullCmdName(CopyCmdName)
logger := log.WithFields(log.Fields{"app": appName, "command": cmdName})
prefix := fmt.Sprintf("cmd=%s", cmdName)
viChan := version.CheckAsync(gparams.CheckVersion, gparams.InContainer, gparams.IsDSImage)
cmdReport := report.NewRegistryCommand(gparams.ReportLocation, gparams.InContainer)
cmdReport.State = command.StateStarted
xc.Out.State("started")
client, err := dockerclient.New(gparams.ClientConfig)
if err == dockerclient.ErrNoDockerInfo {
exitMsg := "missing Docker connection info"
if gparams.InContainer && gparams.IsDSImage {
exitMsg = "make sure to pass the Docker connect parameters to the docker-slim container"
}
xc.Out.Info("docker.connect.error",
ovars{
"message": exitMsg,
})
exitCode := commands.ECTCommon | commands.ECNoDockerConnectInfo
xc.Out.State("exited",
ovars{
"exit.code": exitCode,
"version": v.Current(),
"location": fsutil.ExeDir(),
})
xc.Exit(exitCode)
}
errutil.FailOn(err)
if gparams.Debug {
version.Print(prefix, logger, client, false, gparams.InContainer, gparams.IsDSImage)
}
xc.Out.State("completed")
cmdReport.State = command.StateCompleted
xc.Out.State("done")
vinfo := <-viChan
version.PrintCheckVersion(xc, "", vinfo)
cmdReport.State = command.StateDone
if cmdReport.Save() {
xc.Out.Info("report",
ovars{
"file": cmdReport.ReportLocation(),
})
}
}