github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/hack/podman-registry-go/registry.go (about) 1 package registry 2 3 import ( 4 "strings" 5 6 "github.com/hanks177/podman/v4/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 // Options allows for customizing a registry. 35 type Options struct { 36 // Image - custom registry image. 37 Image string 38 } 39 40 // Start a new registry and return it along with it's image, user, password, and port. 41 func Start() (*Registry, error) { 42 return StartWithOptions(nil) 43 } 44 45 // StartWithOptions a new registry and return it along with it's image, user, password, and port. 46 func StartWithOptions(options *Options) (*Registry, error) { 47 if options == nil { 48 options = &Options{} 49 } 50 51 var args []string 52 if options.Image != "" { 53 args = append(args, "-i", options.Image) 54 } 55 args = append(args, "start") 56 57 // Start a registry. 58 out, err := utils.ExecCmd(binary, args...) 59 if err != nil { 60 return nil, errors.Wrapf(err, "error running %q: %s", binary, out) 61 } 62 63 // Parse the output. 64 registry := Registry{} 65 for _, s := range strings.Split(out, "\n") { 66 if s == "" { 67 continue 68 } 69 spl := strings.Split(s, "=") 70 if len(spl) != 2 { 71 return nil, errors.Errorf("unexpected output format %q: want 'PODMAN_...=...'", s) 72 } 73 key := spl[0] 74 val := strings.TrimSuffix(strings.TrimPrefix(spl[1], "\""), "\"") 75 switch key { 76 case ImageKey: 77 registry.Image = val 78 case UserKey: 79 registry.User = val 80 case PassKey: 81 registry.Password = val 82 case PortKey: 83 registry.Port = val 84 default: 85 logrus.Errorf("Unexpected podman-registry output: %q", s) 86 } 87 } 88 89 // Extra sanity check. 90 if registry.Image == "" { 91 return nil, errors.Errorf("unexpected output %q: %q missing", out, ImageKey) 92 } 93 if registry.User == "" { 94 return nil, errors.Errorf("unexpected output %q: %q missing", out, UserKey) 95 } 96 if registry.Password == "" { 97 return nil, errors.Errorf("unexpected output %q: %q missing", out, PassKey) 98 } 99 if registry.Port == "" { 100 return nil, errors.Errorf("unexpected output %q: %q missing", out, PortKey) 101 } 102 103 registry.running = true 104 105 return ®istry, nil 106 } 107 108 // Stop the registry. 109 func (r *Registry) Stop() error { 110 // Stop a registry. 111 if !r.running { 112 return nil 113 } 114 if _, err := utils.ExecCmd(binary, "-P", r.Port, "stop"); err != nil { 115 return errors.Wrapf(err, "error stopping registry (%v) with %q", *r, binary) 116 } 117 r.running = false 118 return nil 119 }