Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add create-layer sub command to create filesystem changeset #8

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/oci-create-runtime-bundle
/oci-unpack
/oci-image-validate
/oci-create-layer
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ EPOCH_TEST_COMMIT ?= v0.2.0
TOOLS := \
oci-create-runtime-bundle \
oci-image-validate \
oci-unpack
oci-unpack \
oci-create-layer

default: help

Expand Down
104 changes: 104 additions & 0 deletions cmd/oci-create-layer/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// 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 (
"io"
"log"
"os"
"path/filepath"

"github.com/opencontainers/image-tools/image"
"github.com/spf13/cobra"
)

type layerCmd struct {
stdout io.Writer
stderr *log.Logger
dest string
}

func main() {
stderr := log.New(os.Stderr, "", 0)

cmd := newLayerCmd(os.Stdout, stderr)
if err := cmd.Execute(); err != nil {
stderr.Println(err)
os.Exit(1)
}
}

func newLayerCmd(stdout io.Writer, stderr *log.Logger) *cobra.Command {
v := &layerCmd{
stdout: stdout,
stderr: stderr,
}

cmd := &cobra.Command{
Use: "oci-create-layer [child] [parent]",
Short: "Create an OCI layer",
Long: `Create an OCI layer based on the changeset between filesystems.`,
Run: v.Run,
}
cmd.Flags().StringVar(
&v.dest, "dest", "",
`The dest specify a particular filename where the layer write to`,
)
return cmd
}

func (v *layerCmd) Run(cmd *cobra.Command, args []string) {
if len(args) != 1 && len(args) != 2 {
v.stderr.Print("One or two filesystems are required")
if err := cmd.Usage(); err != nil {
v.stderr.Println(err)
}
os.Exit(1)
}
var (
err error
out io.ReadCloser
)
if len(args) == 1 {
out, err = image.CreateLayer(args[0], "")
} else {
out, err = image.CreateLayer(args[0], args[1])
}
if err != nil {
v.stderr.Printf("create layer failed: %v", err)
os.Exit(1)
}
if v.dest == "" {
_, err := io.Copy(v.stdout, out)
if err != nil {
v.stderr.Printf("create layer failed: %v", err)
os.Exit(1)
}
} else {
filename := filepath.Clean(v.dest)
f, err := os.Create(filename)
if err != nil {
v.stderr.Printf("create layer failed: %v", err)
os.Exit(1)
}
defer f.Close()
_, err = io.Copy(f, out)
if err != nil {
v.stderr.Printf("create layer failed: %v", err)
os.Exit(1)
}
}
os.Exit(0)
}
29 changes: 29 additions & 0 deletions cmd/oci-create-layer/oci-create-layer.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
% OCI(1) OCI-CREATE-LAYER User Manuals
% OCI Community
% October 2016
# NAME
oci-create-layer \- Create filesystem changeset

# SYNOPSIS
**oci-create-layer** [child] [parent] [flags]

# DESCRIPTION
`oci-create-layer` creates a filesystem changeset from two layers. It compares child with parent and generates a filsystem diff, pack the diff into a uncompressed tar archive. The default output is stdout, use `--dest` to specify a custom one.

# OPTIONS
**--help**
Print usage statement

**--dest**
The dest specify a particular filename where the layer write to

# EXAMPLES
```
$ oci-create-layer --dest rootfs-1-s.tar rootfs-1-s rootfs-1
$ ls
rootfs-1 rootfs-1-s rootfs-1-s.tar

```

# HISTORY
Oct 2016, Originally compiled by Lei Jitang (coolljt0725 at huawei dot com)
8 changes: 5 additions & 3 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ import:
- package: github.com/pkg/errors
version: ~0.7.1
- package: github.com/spf13/cobra
- package: github.com/Sirupsen/logrus
34 changes: 34 additions & 0 deletions image/layer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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 image

import (
"io"

"github.com/opencontainers/image-tools/utils"
)

// CreateLayer cretes filesystem changset from child and parent
func CreateLayer(child, parent string) (io.ReadCloser, error) {
changes, err := utils.ChangesDirs(child, parent)
if err != nil {
return nil, err
}
archive, err := utils.ExportChanges(child, changes)
if err != nil {
return nil, err
}
return archive, nil
}
Loading