Skip to content

Commit

Permalink
registry host flag
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelmota committed Feb 16, 2019
1 parent 5ec59cb commit 660480f
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 10 deletions.
8 changes: 7 additions & 1 deletion README.md
Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions cmd/ipdr/main.go
Expand Up @@ -31,6 +31,8 @@ func main() {

var ipfsHost string
var format string
var dockerRegistryHost string
var port uint
var silent bool

rootCmd := &cobra.Command{
Expand Down Expand Up @@ -59,7 +61,7 @@ More info: https://github.com/miguelmota/ipdr`,
},
RunE: func(cmd *cobra.Command, args []string) error {
reg := registry.NewRegistry(&registry.Config{
DockerLocalRegistryHost: "5000",
DockerLocalRegistryHost: dockerRegistryHost,
IPFSHost: ipfsHost,
Debug: !silent,
})
Expand All @@ -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",
Expand All @@ -99,7 +102,7 @@ More info: https://github.com/miguelmota/ipdr`,
},
RunE: func(cmd *cobra.Command, args []string) error {
reg := registry.NewRegistry(&registry.Config{
DockerLocalRegistryHost: "docker.localhost",
DockerLocalRegistryHost: dockerRegistryHost,
Debug: !silent,
})

Expand All @@ -119,13 +122,15 @@ 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",
Short: "Start registry server",
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,
})

Expand All @@ -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",
Expand Down
17 changes: 17 additions & 0 deletions netutil/netutil.go
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
22 changes: 22 additions & 0 deletions netutil/netutil_test.go
@@ -1,6 +1,7 @@
package netutil

import (
"fmt"
"net"
"strconv"
"testing"
Expand Down Expand Up @@ -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)
}
})
}
}
5 changes: 3 additions & 2 deletions registry/registry.go
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion registry/registry_test.go
Expand Up @@ -36,7 +36,7 @@ func createClient() *docker.Client {

func createRegistry() *Registry {
registry := NewRegistry(&Config{
DockerLocalRegistryHost: "docker.localhost",
DockerLocalRegistryHost: "docker.localhost:5000",
IPFSHost: "localhost:8080",
})

Expand Down
11 changes: 8 additions & 3 deletions server/server.go
Expand Up @@ -22,6 +22,7 @@ type Server struct {
// Config is server config
type Config struct {
Debug bool
Port uint
}

// NewServer returns a new server instance
Expand All @@ -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,
}
}
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 660480f

Please sign in to comment.