diff --git a/.gitignore b/.gitignore index fe4e8e5..144c59d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ /oci-cas /oci-create-runtime-bundle +/oci-image-init /oci-image-validate /oci-refs /oci-unpack diff --git a/Makefile b/Makefile index a76ee9e..d7f6446 100644 --- a/Makefile +++ b/Makefile @@ -5,6 +5,7 @@ EPOCH_TEST_COMMIT ?= v0.2.0 TOOLS := \ oci-cas \ oci-create-runtime-bundle \ + oci-image-init \ oci-image-validate \ oci-refs \ oci-unpack diff --git a/cmd/oci-image-init/image_layout.go b/cmd/oci-image-init/image_layout.go new file mode 100644 index 0000000..10b8959 --- /dev/null +++ b/cmd/oci-image-init/image_layout.go @@ -0,0 +1,57 @@ +// 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/layout" + "github.com/spf13/cobra" + "golang.org/x/net/context" +) + +type imageLayout struct{} + +func newImageLayoutCmd() *cobra.Command { + state := &imageLayout{} + + return &cobra.Command{ + Use: "image-layout PATH", + Short: "Initialize an OCI image-layout repository", + Run: state.Run, + } +} + +func (state *imageLayout) Run(cmd *cobra.Command, args []string) { + if len(args) != 1 { + if err := cmd.Usage(); err != nil { + fmt.Fprintln(os.Stderr, err) + } + os.Exit(1) + } + + path := args[0] + + ctx := context.Background() + + err := layout.CreateTarFile(ctx, path) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + + os.Exit(0) +} diff --git a/cmd/oci-image-init/main.go b/cmd/oci-image-init/main.go new file mode 100644 index 0000000..49f4aab --- /dev/null +++ b/cmd/oci-image-init/main.go @@ -0,0 +1,37 @@ +// 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/spf13/cobra" +) + +func main() { + cmd := &cobra.Command{ + Use: "oci-image-init", + Short: "Initialize an OCI image", + } + + cmd.AddCommand(newImageLayoutCmd()) + + err := cmd.Execute() + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } +} diff --git a/image/layout/tar.go b/image/layout/tar.go index 1447a8e..2230335 100644 --- a/image/layout/tar.go +++ b/image/layout/tar.go @@ -218,3 +218,50 @@ func CheckTarVersion(ctx context.Context, reader io.ReadSeeker) (err error) { return nil } + +// CreateTarFile creates a new image-layout tar file at the given path. +func CreateTarFile(ctx context.Context, path string) (err error) { + file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666) + if err != nil { + return err + } + defer file.Close() + + tarWriter := tar.NewWriter(file) + defer tarWriter.Close() + + now := time.Now() + for _, name := range []string{"./blobs/", "./refs/"} { + header := &tar.Header{ + Name: name, + Mode: 0777, + ModTime: now, + Typeflag: tar.TypeDir, + } + err = tarWriter.WriteHeader(header) + if err != nil { + return err + } + } + + imageLayoutVersion := ImageLayoutVersion{ + Version: "1.0.0", + } + imageLayoutVersionBytes, err := json.Marshal(imageLayoutVersion) + if err != nil { + return err + } + header := &tar.Header{ + Name: "./oci-layout", + Mode: 0666, + Size: int64(len(imageLayoutVersionBytes)), + ModTime: now, + Typeflag: tar.TypeReg, + } + err = tarWriter.WriteHeader(header) + if err != nil { + return err + } + _, err = tarWriter.Write(imageLayoutVersionBytes) + return err +}