diff --git a/README.md b/README.md index de77544..b1905a4 100644 --- a/README.md +++ b/README.md @@ -138,8 +138,14 @@ go install github.com/miguelmota/ipdr/cmd/ipdr ``` - You can also pull the image using `docker pull`: + - First run the IPDR server in a seperate terminal: - - First convert the IPFS hash to a valid format docker allows: + ```bash + $ ipdr server -p 5000 + INFO[0000] [registry/server] listening on [::]:5000 + ``` + + - Then convert the IPFS hash to a valid format docker allows: ```bash $ ipdr convert QmYMg6WAuvF5i5yFmjT8KkqewZ5Ngh4U9Mp1bGfdjraFVk --format=docker diff --git a/cmd/ipdr/main.go b/cmd/ipdr/main.go index 8822c41..ef66594 100644 --- a/cmd/ipdr/main.go +++ b/cmd/ipdr/main.go @@ -31,6 +31,8 @@ func main() { var ipfsHost string var format string + var dockerRegistryHost string + var port uint var silent bool rootCmd := &cobra.Command{ @@ -59,7 +61,7 @@ More info: https://github.com/miguelmota/ipdr`, }, RunE: func(cmd *cobra.Command, args []string) error { reg := registry.NewRegistry(®istry.Config{ - DockerLocalRegistryHost: "5000", + DockerLocalRegistryHost: dockerRegistryHost, IPFSHost: ipfsHost, Debug: !silent, }) @@ -80,8 +82,9 @@ More info: https://github.com/miguelmota/ipdr`, }, } - pushCmd.Flags().StringVarP(&ipfsHost, "ipfs-host", "", "127.0.0.1:5001", "A remote IPFS API host to push the image to. Example: 127.0.0.1:5001") + pushCmd.Flags().StringVarP(&ipfsHost, "ipfs-host", "", "127.0.0.1:5001", "A remote IPFS API host to push the image to. Eg. 127.0.0.1:5001") pushCmd.Flags().BoolVarP(&silent, "silent", "s", false, "Silent flag suppresses logs and outputs only IPFS hash") + pushCmd.Flags().StringVarP(&dockerRegistryHost, "docker-registry-host", "", "docker.localhost:5000", "The Docker local registry host. Eg. 127.0.0.1:5000 Eg. docker.localhost:5000") pullCmd := &cobra.Command{ Use: "pull", @@ -99,7 +102,7 @@ More info: https://github.com/miguelmota/ipdr`, }, RunE: func(cmd *cobra.Command, args []string) error { reg := registry.NewRegistry(®istry.Config{ - DockerLocalRegistryHost: "docker.localhost", + DockerLocalRegistryHost: dockerRegistryHost, Debug: !silent, }) @@ -119,6 +122,7 @@ More info: https://github.com/miguelmota/ipdr`, } pullCmd.Flags().BoolVarP(&silent, "silent", "s", false, "Silent flag suppresses logs and outputs only Docker repo tag") + pullCmd.Flags().StringVarP(&dockerRegistryHost, "docker-registry-host", "", "docker.localhost:5000", "The Docker local registry host. Eg. 127.0.0.1:5000 Eg. docker.localhost:5000") serverCmd := &cobra.Command{ Use: "server", @@ -126,6 +130,7 @@ More info: https://github.com/miguelmota/ipdr`, Long: "Start the Docker registry server that images stored on IPFS to Docker registry format", RunE: func(cmd *cobra.Command, args []string) error { srv := server.NewServer(&server.Config{ + Port: port, Debug: !silent, }) @@ -134,6 +139,7 @@ More info: https://github.com/miguelmota/ipdr`, } serverCmd.Flags().BoolVarP(&silent, "silent", "s", false, "Silent flag suppresses logs") + serverCmd.Flags().UintVarP(&port, "port", "p", 5000, "The port for the Docker registry to listen on") convertCmd := &cobra.Command{ Use: "convert", diff --git a/netutil/netutil.go b/netutil/netutil.go index ace2fbe..ceab826 100644 --- a/netutil/netutil.go +++ b/netutil/netutil.go @@ -2,7 +2,10 @@ package netutil import ( "errors" + "fmt" "net" + "regexp" + "strconv" ) // GetFreePort asks the kernel for a free open port that is ready to use. @@ -78,3 +81,17 @@ func isPrivateIP(ip net.IP) bool { return false } + +// ExtractPort extracts the port from a host string +func ExtractPort(host string) uint { + re := regexp.MustCompile(`(.*:)?(\d+)`) + matches := re.FindStringSubmatch(host) + fmt.Println(matches) + if len(matches) == 0 { + return 0 + } + portStr := matches[len(matches)-1] + port, _ := strconv.ParseUint(portStr, 10, 64) + + return uint(port) +} diff --git a/netutil/netutil_test.go b/netutil/netutil_test.go index 597b77f..799982e 100644 --- a/netutil/netutil_test.go +++ b/netutil/netutil_test.go @@ -1,6 +1,7 @@ package netutil import ( + "fmt" "net" "strconv" "testing" @@ -42,3 +43,24 @@ func TestLocalIP(t *testing.T) { t.Log(ip) } + +func TestExtractPort(t *testing.T) { + for i, tt := range []struct { + in string + out uint + }{ + {"0.0.0.0:5000", 5000}, + {":5000", 5000}, + {"docker.localhost:5000", 5000}, + {"a123.com:5000", 5000}, + {"5000", 5000}, + {"", 0}, + } { + t.Run(fmt.Sprintf("%v", i), func(t *testing.T) { + got := ExtractPort(tt.in) + if got != tt.out { + t.Errorf("want %v, got %v", tt.out, got) + } + }) + } +} diff --git a/registry/registry.go b/registry/registry.go index 2d9270d..b8fd5d2 100644 --- a/registry/registry.go +++ b/registry/registry.go @@ -22,7 +22,7 @@ import ( ipfs "github.com/miguelmota/ipdr/ipfs" netutil "github.com/miguelmota/ipdr/netutil" regutil "github.com/miguelmota/ipdr/regutil" - "github.com/miguelmota/ipdr/server" + server "github.com/miguelmota/ipdr/server" log "github.com/sirupsen/logrus" ) @@ -164,7 +164,7 @@ func (r *Registry) DownloadImage(ipfsHash string) (string, error) { func (r *Registry) PullImage(ipfsHash string) (string, error) { r.runServer() dockerizedHash := regutil.DockerizeHash(ipfsHash) - dockerPullImageID := fmt.Sprintf("%s:%v/%s", r.dockerLocalRegistryHost, 5000, dockerizedHash) + dockerPullImageID := fmt.Sprintf("%s/%s", r.dockerLocalRegistryHost, dockerizedHash) r.Debugf("[registry] attempting to pull %s", dockerPullImageID) err := r.dockerClient.PullImage(dockerPullImageID) @@ -204,6 +204,7 @@ func (r *Registry) runServer() { resp, err := client.Get(url) if err != nil || resp.StatusCode != 200 { srv := server.NewServer(&server.Config{ + Port: netutil.ExtractPort(r.dockerLocalRegistryHost), Debug: r.debug, }) go srv.Start() diff --git a/registry/registry_test.go b/registry/registry_test.go index 01669f2..aea30dd 100644 --- a/registry/registry_test.go +++ b/registry/registry_test.go @@ -36,7 +36,7 @@ func createClient() *docker.Client { func createRegistry() *Registry { registry := NewRegistry(&Config{ - DockerLocalRegistryHost: "docker.localhost", + DockerLocalRegistryHost: "docker.localhost:5000", IPFSHost: "localhost:8080", }) diff --git a/server/server.go b/server/server.go index a72788d..67fae5d 100644 --- a/server/server.go +++ b/server/server.go @@ -22,6 +22,7 @@ type Server struct { // Config is server config type Config struct { Debug bool + Port uint } // NewServer returns a new server instance @@ -30,8 +31,13 @@ func NewServer(config *Config) *Server { config = &Config{} } + var port uint = 5000 + if config.Port != 0 { + port = config.Port + } + return &Server{ - host: fmt.Sprintf("0.0.0.0:%v", 5000), + host: fmt.Sprintf("0.0.0.0:%v", port), debug: config.Debug, } } @@ -138,8 +144,7 @@ func (s *Server) Start() error { return err } - port := s.listener.Addr().(*net.TCPAddr).Port - s.Debugf("[registry/server] port %v", port) + s.Debugf("[registry/server] listening on %s", s.listener.Addr()) return http.Serve(s.listener, nil) }