Files
Valentin Rothberg 8489dc4345 move go module to v2
With the advent of Podman 2.0.0 we crossed the magical barrier of go
modules.  While we were able to continue importing all packages inside
of the project, the project could not be vendored anymore from the
outside.

Move the go module to new major version and change all imports to
`github.com/containers/libpod/v2`.  The renaming of the imports
was done via `gomove` [1].

[1] https://github.com/KSubedi/gomove

Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
2020-07-06 15:50:12 +02:00

99 lines
2.4 KiB
Go

package registry
import (
"strings"
"github.com/containers/libpod/v2/utils"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
const (
imageKey = "PODMAN_REGISTRY_IMAGE"
userKey = "PODMAN_REGISTRY_USER"
passKey = "PODMAN_REGISTRY_PASS"
portKey = "PODMAN_REGISTRY_PORT"
)
var binary = "podman-registry"
// Registry is locally running registry.
type Registry struct {
// Image - container image of the registry.
Image string
// User - the user to authenticate against the registry.
User string
// Password - the accompanying password for the user.
Password string
// Port - the port the registry is listening to on the host.
Port string
// running indicates if the registry is running.
running bool
}
// Start a new registry and return it along with it's image, user, password, and port.
func Start() (*Registry, error) {
// Start a registry.
out, err := utils.ExecCmd(binary, "start")
if err != nil {
return nil, errors.Wrapf(err, "error running %q: %s", binary, out)
}
// Parse the output.
registry := Registry{}
for _, s := range strings.Split(out, "\n") {
if s == "" {
continue
}
spl := strings.Split(s, "=")
if len(spl) != 2 {
return nil, errors.Errorf("unexpected output format %q: want 'PODMAN_...=...'", s)
}
key := spl[0]
val := strings.TrimSuffix(strings.TrimPrefix(spl[1], "\""), "\"")
switch key {
case imageKey:
registry.Image = val
case userKey:
registry.User = val
case passKey:
registry.Password = val
case portKey:
registry.Port = val
default:
logrus.Errorf("unexpected podman-registry output: %q", s)
}
}
// Extra sanity check.
if registry.Image == "" {
return nil, errors.Errorf("unexpected output %q: %q missing", out, imageKey)
}
if registry.User == "" {
return nil, errors.Errorf("unexpected output %q: %q missing", out, userKey)
}
if registry.Password == "" {
return nil, errors.Errorf("unexpected output %q: %q missing", out, passKey)
}
if registry.Port == "" {
return nil, errors.Errorf("unexpected output %q: %q missing", out, portKey)
}
registry.running = true
return &registry, nil
}
// Stop the registry.
func (r *Registry) Stop() error {
// Stop a registry.
if !r.running {
return nil
}
if _, err := utils.ExecCmd(binary, "-P", r.Port, "stop"); err != nil {
return errors.Wrapf(err, "error stopping registry (%v) with %q", *r, binary)
}
r.running = false
return nil
}