summaryrefslogtreecommitdiff
path: root/hack/podman-registry-go/registry.go
blob: 30da3b6da80857905137d0e6c616e5872ee2ce3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package registry

import (
	"fmt"
	"strings"

	"github.com/containers/podman/v4/utils"
	"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
}

// Options allows for customizing a registry.
type Options struct {
	// Image - custom registry image.
	Image string
}

// Start a new registry and return it along with it's image, user, password, and port.
func Start() (*Registry, error) {
	return StartWithOptions(nil)
}

// StartWithOptions a new registry and return it along with it's image, user, password, and port.
func StartWithOptions(options *Options) (*Registry, error) {
	if options == nil {
		options = &Options{}
	}

	var args []string
	if options.Image != "" {
		args = append(args, "-i", options.Image)
	}
	args = append(args, "start")

	// Start a registry.
	out, err := utils.ExecCmd(binary, args...)
	if err != nil {
		return nil, fmt.Errorf("running %q: %s: %w", binary, out, err)
	}

	// 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, fmt.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, fmt.Errorf("unexpected output %q: %q missing", out, ImageKey)
	}
	if registry.User == "" {
		return nil, fmt.Errorf("unexpected output %q: %q missing", out, UserKey)
	}
	if registry.Password == "" {
		return nil, fmt.Errorf("unexpected output %q: %q missing", out, PassKey)
	}
	if registry.Port == "" {
		return nil, fmt.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 fmt.Errorf("stopping registry (%v) with %q: %w", *r, binary, err)
	}
	r.running = false
	return nil
}