github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/hack/podman-registry-go/registry.go (about) 1 package registry 2 3 import ( 4 "strings" 5 6 "github.com/containers/podman/v2/utils" 7 "github.com/pkg/errors" 8 "github.com/sirupsen/logrus" 9 ) 10 11 const ( 12 imageKey = "PODMAN_REGISTRY_IMAGE" 13 userKey = "PODMAN_REGISTRY_USER" 14 passKey = "PODMAN_REGISTRY_PASS" 15 portKey = "PODMAN_REGISTRY_PORT" 16 ) 17 18 var binary = "podman-registry" 19 20 // Registry is locally running registry. 21 type Registry struct { 22 // Image - container image of the registry. 23 Image string 24 // User - the user to authenticate against the registry. 25 User string 26 // Password - the accompanying password for the user. 27 Password string 28 // Port - the port the registry is listening to on the host. 29 Port string 30 // running indicates if the registry is running. 31 running bool 32 } 33 34 // Start a new registry and return it along with it's image, user, password, and port. 35 func Start() (*Registry, error) { 36 // Start a registry. 37 out, err := utils.ExecCmd(binary, "start") 38 if err != nil { 39 return nil, errors.Wrapf(err, "error running %q: %s", binary, out) 40 } 41 42 // Parse the output. 43 registry := Registry{} 44 for _, s := range strings.Split(out, "\n") { 45 if s == "" { 46 continue 47 } 48 spl := strings.Split(s, "=") 49 if len(spl) != 2 { 50 return nil, errors.Errorf("unexpected output format %q: want 'PODMAN_...=...'", s) 51 } 52 key := spl[0] 53 val := strings.TrimSuffix(strings.TrimPrefix(spl[1], "\""), "\"") 54 switch key { 55 case imageKey: 56 registry.Image = val 57 case userKey: 58 registry.User = val 59 case passKey: 60 registry.Password = val 61 case portKey: 62 registry.Port = val 63 default: 64 logrus.Errorf("unexpected podman-registry output: %q", s) 65 } 66 } 67 68 // Extra sanity check. 69 if registry.Image == "" { 70 return nil, errors.Errorf("unexpected output %q: %q missing", out, imageKey) 71 } 72 if registry.User == "" { 73 return nil, errors.Errorf("unexpected output %q: %q missing", out, userKey) 74 } 75 if registry.Password == "" { 76 return nil, errors.Errorf("unexpected output %q: %q missing", out, passKey) 77 } 78 if registry.Port == "" { 79 return nil, errors.Errorf("unexpected output %q: %q missing", out, portKey) 80 } 81 82 registry.running = true 83 84 return ®istry, nil 85 } 86 87 // Stop the registry. 88 func (r *Registry) Stop() error { 89 // Stop a registry. 90 if !r.running { 91 return nil 92 } 93 if _, err := utils.ExecCmd(binary, "-P", r.Port, "stop"); err != nil { 94 return errors.Wrapf(err, "error stopping registry (%v) with %q", *r, binary) 95 } 96 r.running = false 97 return nil 98 }