Skip to content

Commit

Permalink
Add oci-create-layer command to create filesystem changesets
Browse files Browse the repository at this point in the history
Signed-off-by: Lei Jitang <leijitang@huawei.com>
  • Loading branch information
coolljt0725 committed Sep 18, 2016
1 parent 721ba58 commit 664b97f
Show file tree
Hide file tree
Showing 281 changed files with 115,420 additions and 3 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ tools:
go build ./cmd/oci-create-runtime-bundle
go build ./cmd/oci-unpack
go build ./cmd/oci-image-validate
go build ./cmd/oci-create-layer

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

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

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

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

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

func newLayerCmd(stdout, 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
if len(args) == 1 {
err = image.CreateLayer(args[0], "", v.dest)
} else {
err = image.CreateLayer(args[0], args[1], v.dest)
}
if err != nil {
v.stderr.Printf("create layer failed: %v", err)
os.Exit(1)
}
os.Exit(0)
}
29 changes: 26 additions & 3 deletions glide.lock

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

2 changes: 2 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ import:
- package: github.com/pkg/errors
version: ~0.7.1
- package: github.com/spf13/cobra
- package: github.com/containers/storage

65 changes: 65 additions & 0 deletions image/layer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 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 (
"fmt"
"io"
"os"
"path/filepath"

"github.com/containers/storage/pkg/archive"
)

func CreateLayer(child, parent, dest string) error {
arch, err := Diff(child, parent)
if err != nil {
return err
}
defer arch.Close()
filename := fmt.Sprintf("%s.tar", filepath.Clean(child))
if dest != "" {
filename = filepath.Clean(dest)
}
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(f, arch)
return err
}

// Diff produces an archive of the changes between the specified
// layer and its parent layer which may be "".
func Diff(child, parent string) (arch archive.Archive, err error) {
if parent == "" {
archive, err := archive.Tar(child, archive.Uncompressed)
if err != nil {
return nil, err
}
return archive, nil
}

changes, err := archive.ChangesDirs(child, parent)
if err != nil {
return nil, err
}
archive, err := archive.ExportChanges(child, changes, nil, nil)
if err != nil {
return nil, err
}
return archive, nil
}
21 changes: 21 additions & 0 deletions vendor/github.com/Sirupsen/logrus/LICENSE

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

64 changes: 64 additions & 0 deletions vendor/github.com/Sirupsen/logrus/alt_exit.go

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

26 changes: 26 additions & 0 deletions vendor/github.com/Sirupsen/logrus/doc.go

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

Loading

0 comments on commit 664b97f

Please sign in to comment.