Skip to content

Commit

Permalink
examples
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelmota committed Feb 16, 2019
1 parent d805278 commit fe48024
Show file tree
Hide file tree
Showing 12 changed files with 377 additions and 120 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -11,6 +11,7 @@
# Output of the go coverage tool, specifically when used with LiteIDE
*.out

bin
build
dist
*.tar
Expand Down
117 changes: 115 additions & 2 deletions README.md
@@ -1,3 +1,5 @@
WIP

<h3 align="center">
<br />
<img src="https://user-images.githubusercontent.com/168240/52895983-7330f100-3176-11e9-855c-246eaabd3adc.png" alt="logo" width="600" />
Expand All @@ -14,8 +16,16 @@
[![Build Status](https://travis-ci.org/miguelmota/ipdr.svg?branch=master)](https://travis-ci.org/miguelmota/ipdr)
[![Go Report Card](https://goreportcard.com/badge/github.com/miguelmota/ipdr?)](https://goreportcard.com/report/github.com/miguelmota/ipdr)
[![GoDoc](https://godoc.org/github.com/miguelmota/ipdr?status.svg)](https://godoc.org/github.com/miguelmota/ipdr)
[![stability-experimental](https://img.shields.io/badge/stability-experimental-orange.svg)](https://github.com/emersion/stability-badges#experimental)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](#contributing)

## Contents

**WIP**
- [Install](#install)
- [Getting started](#getting-started)
- [Test](#test)
- [Contributing](#contributing)
- [License](#license)

## Install

Expand All @@ -25,14 +35,117 @@ go install github.com/miguelmota/ipdr/cmd/ipdr

## Getting started

TODO
Create `Dockerfile`:

```dockerfile
FROM busybox:latest

CMD echo 'hello world'
```

Build Docker image:

```bash
docker build -t example/helloworld .
```

Test run:

```bash
$ docker run example/helloworld:latest
hello world
```

Start IPFS daemon ([Install instructions](https://docs.ipfs.io/introduction/install/)):

```bash
$ ipfs daemon
Initializing daemon...
Swarm listening on /ip4/127.0.0.1/tcp/4001
Swarm listening on /ip4/192.168.86.90/tcp/4001
Swarm listening on /ip6/::1/tcp/4001
Swarm listening on /p2p-circuit/ipfs/QmR29wrbNv3WrMuodwuLiDwvskuZKKeTtcYDw7SwNffzCH
Swarm announcing /ip4/127.0.0.1/tcp/4001
Swarm announcing /ip4/192.168.0.21/tcp/43042
Swarm announcing /ip4/192.168.86.90/tcp/4001
Swarm announcing /ip6/::1/tcp/4001
API server listening on /ip4/0.0.0.0/tcp/5001
Gateway (readonly) server listening on /ip4/0.0.0.0/tcp/8080
Daemon is ready
```

Add `docker.localhost` to `/etc/hosts`:

```hosts
127.0.0.1 docker.localhost
```

Flush DNS cache:

on macOS:

```bash
dscacheutil -flushcache; sudo killall -HUP mDNSResponder
```

Use IPDR CLI to push to IPFS:

```bash
$ ipdr push example/helloworld

INFO[0000] [registry] temp: /var/folders/k1/m2rmftgd48q97pj0xf9csdb00000gn/T/205139235
INFO[0000] [registry] preparing image in: /var/folders/k1/m2rmftgd48q97pj0xf9csdb00000gn/T/657143846
INFO[0000]
[registry] dist: /var/folders/k1/m2rmftgd48q97pj0xf9csdb00000gn/T/657143846/default/blobs/sha256:305510b2c684403553fd8f383e8d109b147df2cfde60e40a85564532c383c8b8
INFO[0000] [registry] compressing layer: /var/folders/k1/m2rmftgd48q97pj0xf9csdb00000gn/T/205139235/886f4bdfa483cc176e947c63d069579785c051793a9634f571fded7b9026cd3c/layer.tar
INFO[0000] [registry] root dir: /var/folders/k1/m2rmftgd48q97pj0xf9csdb00000gn/T/657143846
INFO[0000] [registry] upload hash QmRxZ5Wffj6b1j8ckJLcr7yFrbHUhBYXsAMbj7Krwu1pp8
INFO[0000]
[registry] uploaded to /ipfs/Qmc2ot2NQadXmbvPbsidyjYDvPfPwKZmovzNpfRPKxXUrL
INFO[0000] [registry] docker image ciqmw4mig2uwaygddjlutoywq43udutvdmuxkcxvetsjp2mjdde27wi

Successfully pushed Docker image to IPFS:
/ipfs/Qmc2ot2NQadXmbvPbsidyjYDvPfPwKZmovzNpfRPKxXUrL
```

Use IPDR CLI to pull from IPFS:

```bash
$ ipdr pull /ipfs/QmagW4H1uE5rkm8A6iVS8WuiyjcWQzqXRHbM3KuUfzrCup

INFO[0000] [registry/server] port 5000
INFO[0000] [registry] attempting to pull docker.localhost:5000/ciqmw4mig2uwaygddjlutoywq43udutvdmuxkcxvetsjp2mjdde27wi
INFO[0000] [registry/server] /v2/
INFO[0000] [registry/server] /v2/ciqmw4mig2uwaygddjlutoywq43udutvdmuxkcxvetsjp2mjdde27wi/manifests/latest
INFO[0000] [registry/server] location http://127.0.0.1:8080/ipfs/Qmc2ot2NQadXmbvPbsidyjYDvPfPwKZmovzNpfRPKxXUrL/manifests/latest-v2
{"status":"Pulling from ciqmw4mig2uwaygddjlutoywq43udutvdmuxkcxvetsjp2mjdde27wi","id":"latest"}
{"status":"Digest: sha256:1fb36e4704d6ebad5becdcfe996807de5f8db687da396330f112157c888c165b"}
{"status":"Status: Downloaded newer image for docker.localhost:5000/ciqmw4mig2uwaygddjlutoywq43udutvdmuxkcxvetsjp2mjdde27wi:latest"}

Successfully pulled Docker image from IPFS:
docker.localhost:5000/ciqmw4mig2uwaygddjlutoywq43udutvdmuxkcxvetsjp2mjdde27wi
```

Run image pulled from IPFS:

```bash
$ docker run docker.localhost:5000/ciqmw4mig2uwaygddjlutoywq43udutvdmuxkcxvetsjp2mjdde27wi

hello world
```

## Test

```bash
make test
```

## Contributing

Pull requests are welcome!

For contributions please create a new branch and submit a pull request for review.

## License

[MIT](LICENSE)
46 changes: 43 additions & 3 deletions cmd/ipdr/main.go
Expand Up @@ -7,6 +7,8 @@ import (

color "github.com/fatih/color"
registry "github.com/miguelmota/ipdr/registry"
"github.com/miguelmota/ipdr/server"
log "github.com/sirupsen/logrus"
cobra "github.com/spf13/cobra"
)

Expand All @@ -20,7 +22,12 @@ var (
)

func main() {
if os.Getenv("DEBUG") != "" {
log.SetReportCaller(true)
}

var ipfsHost string
var silent bool

rootCmd := &cobra.Command{
Use: "ipdr",
Expand Down Expand Up @@ -50,6 +57,7 @@ More info: https://github.com/miguelmota/ipdr`,
reg := registry.NewRegistry(&registry.Config{
DockerLocalRegistryHost: "5000",
IPFSHost: ipfsHost,
Debug: !silent,
})

imageID := args[0]
Expand All @@ -59,12 +67,17 @@ More info: https://github.com/miguelmota/ipdr`,
return err
}

fmt.Println(green.Sprintf("\nSuccessfully pushed Docker image to IPFS:\n/ipfs/%s", hash))
if silent {
fmt.Println(hash)
} else {
fmt.Println(green.Sprintf("\nSuccessfully pushed Docker image to IPFS:\n/ipfs/%s", hash))
}
return nil
},
}

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().BoolVarP(&silent, "silent", "s", false, "Silent flag suppresses logs and outputs only IPFS hash")

pullCmd := &cobra.Command{
Use: "pull",
Expand All @@ -83,18 +96,45 @@ More info: https://github.com/miguelmota/ipdr`,
RunE: func(cmd *cobra.Command, args []string) error {
reg := registry.NewRegistry(&registry.Config{
DockerLocalRegistryHost: "docker.localhost",
Debug: !silent,
})

imageHash := args[0]
_, err := reg.PullImage(imageHash)
tag, err := reg.PullImage(imageHash)
if err != nil {
return err
}

if silent {
fmt.Println(tag)
} else {
fmt.Println(green.Sprintf("\nSuccessfully pulled Docker image from IPFS:\n%s", tag))
}
return nil
},
}

pullCmd.Flags().BoolVarP(&silent, "silent", "s", false, "Silent flag suppresses logs and outputs only Docker repo tag")

return err
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{
Debug: !silent,
})

return srv.Start()
},
}

serverCmd.Flags().BoolVarP(&silent, "silent", "s", false, "Silent flag suppresses logs")

rootCmd.AddCommand(
pushCmd,
pullCmd,
serverCmd,
)

if err := rootCmd.Execute(); err != nil {
Expand Down

0 comments on commit fe48024

Please sign in to comment.