Skip to content

Commit

Permalink
image: Add image-layout directory based CAS and ref engines
Browse files Browse the repository at this point in the history
These are much nicer than the tar engines (hooray atomic renames :),
so switch the manifest tests tests back to using the directory-backed
engines.  I also switched the man-page examples over to
directory-backed layouts, now that they're what oci-image-init
generates by default.  And I added command-line wrappers for the
delete methods, now that we have a backend that implements it.

I do with there was a paginated, callback-based directory lister we
could use instead of ioutils.ReadDir.  On the other hand, by the time
directories get big enough for that to matter we may be sharding them
anyway.

Signed-off-by: W. Trevor King <wking@tremily.us>
  • Loading branch information
wking committed Oct 25, 2016
1 parent d324523 commit 512176d
Show file tree
Hide file tree
Showing 25 changed files with 732 additions and 51 deletions.
72 changes: 72 additions & 0 deletions cmd/oci-cas/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright 2016 The Linux Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"fmt"
"os"

"github.com/opencontainers/image-tools/image/cas/layout"
"github.com/spf13/cobra"
"golang.org/x/net/context"
)

type deleteCmd struct {
path string
digest string
}

func newDeleteCmd() *cobra.Command {
state := &deleteCmd{}

return &cobra.Command{
Use: "delete PATH DIGEST",
Short: "Remove a blob from from the store",
Run: state.Run,
}
}

func (state *deleteCmd) Run(cmd *cobra.Command, args []string) {
if len(args) != 2 {
fmt.Fprintln(os.Stderr, "both PATH and DIGEST must be provided")
if err := cmd.Usage(); err != nil {
fmt.Fprintln(os.Stderr, err)
}
os.Exit(1)
}

state.path = args[0]
state.digest = args[1]

err := state.run()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

os.Exit(0)
}

func (state *deleteCmd) run() (err error) {
ctx := context.Background()

engine, err := layout.NewEngine(ctx, state.path)
if err != nil {
return err
}
defer engine.Close()

return engine.Delete(ctx, state.digest)
}
1 change: 1 addition & 0 deletions cmd/oci-cas/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func main() {

cmd.AddCommand(newGetCmd())
cmd.AddCommand(newPutCmd())
cmd.AddCommand(newDeleteCmd())

err := cmd.Execute()
if err != nil {
Expand Down
27 changes: 27 additions & 0 deletions cmd/oci-cas/oci-cas-delete.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
% OCI(1) OCI-IMAGE-TOOL User Manuals
% OCI Community
% SEPTEMBER 2016
# NAME

oci-cas-get \- Remove a blob from the store

# SYNOPSIS

**oci-cas delete** [OPTIONS] PATH DIGEST

# DESCRIPTION

`oci-cas delete` removes the blob referenced by `DIGEST` from the store at `PATH`.

# OPTIONS

**--help**
Print usage statement

# SEE ALSO

**oci-cas**(1), **oci-cas-get**(1), **oci-cas-put**(1)

# HISTORY

September 2016, Originally compiled by W. Trevor King (wking at tremily dot us)
2 changes: 1 addition & 1 deletion cmd/oci-cas/oci-cas-get.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ oci-cas-get \- Retrieve a blob from the store

# SEE ALSO

**oci-cas**(1), **oci-cas-put**(1)
**oci-cas**(1), **oci-cas-put**(1), **oci-cas-delete**(1)

# HISTORY

Expand Down
2 changes: 1 addition & 1 deletion cmd/oci-cas/oci-cas-put.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ oci-cas-put \- Write a blob to the store

# SEE ALSO

**oci-cas**(1), **oci-cas-get**(1)
**oci-cas**(1), **oci-cas-get**(1), **oci-cas-delete**(1)

# HISTORY

Expand Down
13 changes: 9 additions & 4 deletions cmd/oci-cas/oci-cas.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,24 @@ oci-cas \- Content-addressable storage manipulation
Write a blob to the store.
See **oci-cas-put**(1) for full documentation on the **put** command.

**delete**
Remove a blob from the store.
See **oci-cas-delete**(1) for full documentation on the **delete** command.

# EXAMPLES

```
$ oci-image-init image-layout image.tar
$ echo hello | oci-cas put image.tar
$ oci-image-init image-layout image
$ echo hello | oci-cas put image
sha256:5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03
$ oci-cas get image.tar sha256:5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03
$ oci-cas get image sha256:5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03
hello
$ oci-cas delete image sha256:5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03
```

# SEE ALSO

**oci-image-tools**(7), **oci-cas-get**(1), **oci-cas-put**(1), **oci-image-init**(1)
**oci-image-tools**(7), **oci-cas-get**(1), **oci-cas-put**(1), **oci-cas-delete**(1), **oci-image-init**(1)

# HISTORY

Expand Down
29 changes: 24 additions & 5 deletions cmd/oci-image-init/image_layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,33 @@ import (
"golang.org/x/net/context"
)

type imageLayout struct{}
type imageLayout struct {
backend string
}

func newImageLayoutCmd() *cobra.Command {
state := &imageLayout{}
state := &imageLayout{
backend: "dir",
}

return &cobra.Command{
cmd := &cobra.Command{
Use: "image-layout PATH",
Short: "Initialize an OCI image-layout repository",
Run: state.Run,
}

cmd.Flags().StringVar(
&state.backend, "type", "dir",
"Select the image-backend. Choices: dir, tar. Defaults to dir.",
)

return cmd
}

func (state *imageLayout) Run(cmd *cobra.Command, args []string) {
var err error
if len(args) != 1 {
if err := cmd.Usage(); err != nil {
if err = cmd.Usage(); err != nil {
fmt.Fprintln(os.Stderr, err)
}
os.Exit(1)
Expand All @@ -47,7 +59,14 @@ func (state *imageLayout) Run(cmd *cobra.Command, args []string) {

ctx := context.Background()

err := layout.CreateTarFile(ctx, path)
switch state.backend {
case "dir":
err = layout.CreateDir(ctx, path)
case "tar":
err = layout.CreateTarFile(ctx, path)
default:
err = fmt.Errorf("unrecognized type: %q", state.backend)
}
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
Expand Down
5 changes: 5 additions & 0 deletions cmd/oci-image-init/oci-image-init-image-layout.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ oci-image-init-image-layout \- Initialize an OCI image-layout repository
**--help**
Print usage statement

**--type**
Select the image-layout backend.
Choices: dir, tar.
Defaults to dir.

# SEE ALSO

**oci-image-init**(1), **oci-cas**(1), ***oci-refs**(1)
Expand Down
72 changes: 72 additions & 0 deletions cmd/oci-refs/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright 2016 The Linux Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"fmt"
"os"

"github.com/opencontainers/image-tools/image/refs/layout"
"github.com/spf13/cobra"
"golang.org/x/net/context"
)

type deleteCmd struct {
path string
name string
}

func newDeleteCmd() *cobra.Command {
state := &deleteCmd{}

return &cobra.Command{
Use: "delete PATH NAME",
Short: "Remove a reference from the store",
Run: state.Run,
}
}

func (state *deleteCmd) Run(cmd *cobra.Command, args []string) {
if len(args) != 2 {
fmt.Fprintln(os.Stderr, "both PATH and NAME must be provided")
if err := cmd.Usage(); err != nil {
fmt.Fprintln(os.Stderr, err)
}
os.Exit(1)
}

state.path = args[0]
state.name = args[1]

err := state.run()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

os.Exit(0)
}

func (state *deleteCmd) run() (err error) {
ctx := context.Background()

engine, err := layout.NewEngine(ctx, state.path)
if err != nil {
return err
}
defer engine.Close()

return engine.Delete(ctx, state.name)
}
1 change: 1 addition & 0 deletions cmd/oci-refs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func main() {
cmd.AddCommand(newPutCmd())
cmd.AddCommand(newGetCmd())
cmd.AddCommand(newListCmd())
cmd.AddCommand(newDeleteCmd())

err := cmd.Execute()
if err != nil {
Expand Down
27 changes: 27 additions & 0 deletions cmd/oci-refs/oci-refs-delete.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
% OCI(1) OCI-IMAGE-TOOL User Manuals
% OCI Community
% SEPTEMBER 2016
# NAME

oci-refs-delete \- Remove a reference from the store

# SYNOPSIS

**oci-refs delete** [OPTIONS] PATH NAME

# DESCRIPTION

`oci-refs delete` removes reference `NAME` from the store at `PATH`.

# OPTIONS

**--help**
Print usage statement

# SEE ALSO

**oci-refs**(1), **oci-refs-get**(1), **oci-refs-list**(1), **oci-refs-put**(1)

# HISTORY

September 2016, Originally compiled by W. Trevor King (wking at tremily dot us)
2 changes: 1 addition & 1 deletion cmd/oci-refs/oci-refs-get.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ oci-refs-get \- Retrieve a reference from the store

# SEE ALSO

**oci-refs**(1), **oci-refs-list**(1), **oci-refs-put**(1)
**oci-refs**(1), **oci-refs-list**(1), **oci-refs-put**(1), **oci-refs-delete**(1)

# HISTORY

Expand Down
2 changes: 1 addition & 1 deletion cmd/oci-refs/oci-refs-list.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ oci-refs-list \- Return available names from the store

# SEE ALSO

**oci-refs**(1), **oci-refs-get**(1), **oci-refs-put**(1)
**oci-refs**(1), **oci-refs-get**(1), **oci-refs-put**(1), **oci-refs-delete**(1)

# HISTORY

Expand Down
2 changes: 1 addition & 1 deletion cmd/oci-refs/oci-refs-put.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ oci-refs-put \- Write a reference to the store

# SEE ALSO

**oci-refs**(1), **oci-refs-get**(1), **oci-refs-list**(1)
**oci-refs**(1), **oci-refs-get**(1), **oci-refs-list**(1), **oci-refs-delete**(1)

# HISTORY

Expand Down
17 changes: 11 additions & 6 deletions cmd/oci-refs/oci-refs.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,28 @@ oci-refs \- Name-based reference manipulation
Write a reference to the store.
See **oci-refs-put**(1) for full documentation on the **put** command.

**delete**
Remove a reference from the store.
See **oci-refs-delete**(1) for full documentation on the **delete** command.

# EXAMPLES

```
$ oci-image-init image-layout image.tar
$ DIGEST=$(echo hello | oci-cas put image.tar)
$ oci-image-init image-layout image
$ DIGEST=$(echo hello | oci-cas put image)
$ SIZE=$(echo hello | wc -c)
$ printf '{"mediaType": "text/plain", "digest": "%s", "size": %d}' "${DIGEST}" "${SIZE}" |
> oci-refs put image.tar greeting
$ oci-refs list image.tar
> oci-refs put image greeting
$ oci-refs list image
greeting
$ oci-refs get image.tar greeting
$ oci-refs get image greeting
{"mediaType":"text/plain","digest":"sha256:5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03","size":6}
$ oci-refs delete image greeting
```

# SEE ALSO

**oci-image-tools**(7), **oci-cas-put**(1), **oci-refs-get**(1), **oci-refs-list**(1), **oci-refs-put**(1)
**oci-image-tools**(7), **oci-cas-put**(1), **oci-refs-get**(1), **oci-refs-list**(1), **oci-refs-put**(1), **oci-refs-delete**(1)

# HISTORY

Expand Down
Loading

0 comments on commit 512176d

Please sign in to comment.