github.com/stackdocker/rkt@v0.10.1-0.20151109095037-1aa827478248/rkt/image_export.go (about)

     1  // Copyright 2015 The rkt Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"io"
    19  	"os"
    20  
    21  	"github.com/coreos/rkt/store"
    22  
    23  	"github.com/coreos/rkt/Godeps/_workspace/src/github.com/spf13/cobra"
    24  )
    25  
    26  var (
    27  	cmdImageExport = &cobra.Command{
    28  		Use:   "export IMAGE OUTPUT_ACI_FILE",
    29  		Short: "Export a stored image to an ACI file",
    30  		Long:  `IMAGE should be a string referencing an image: either a hash or an image name.`,
    31  		Run:   runWrapper(runImageExport),
    32  	}
    33  	flagOverwriteACI bool
    34  )
    35  
    36  func init() {
    37  	cmdImage.AddCommand(cmdImageExport)
    38  	cmdImageExport.Flags().BoolVar(&flagOverwriteACI, "overwrite", false, "overwrite output ACI")
    39  }
    40  
    41  func runImageExport(cmd *cobra.Command, args []string) (exit int) {
    42  	if len(args) != 2 {
    43  		cmd.Usage()
    44  		return 1
    45  	}
    46  
    47  	s, err := store.NewStore(globalFlags.Dir)
    48  	if err != nil {
    49  		stderr("image export: cannot open store: %v", err)
    50  		return 1
    51  	}
    52  
    53  	key, err := getStoreKeyFromAppOrHash(s, args[0])
    54  	if err != nil {
    55  		stderr("image export: %v", err)
    56  		return 1
    57  	}
    58  
    59  	aci, err := s.ReadStream(key)
    60  	if err != nil {
    61  		stderr("image export: error reading image: %v", err)
    62  		return 1
    63  	}
    64  	defer aci.Close()
    65  
    66  	mode := os.O_CREATE | os.O_WRONLY
    67  	if flagOverwriteACI {
    68  		mode |= os.O_TRUNC
    69  	} else {
    70  		mode |= os.O_EXCL
    71  	}
    72  	f, err := os.OpenFile(args[1], mode, 0644)
    73  	if err != nil {
    74  		if os.IsExist(err) {
    75  			stderr("image export: output ACI file exists (try --overwrite)")
    76  		} else {
    77  			stderr("image export: unable to open output ACI file %s: %v", args[1], err)
    78  		}
    79  		return 1
    80  	}
    81  	defer func() {
    82  		err := f.Close()
    83  		if err != nil {
    84  			stderr("image export: error closing output ACI file: %v", err)
    85  			exit = 1
    86  		}
    87  	}()
    88  
    89  	_, err = io.Copy(f, aci)
    90  	if err != nil {
    91  		stderr("image export: error writing to output ACI file: %v", err)
    92  		return 1
    93  	}
    94  
    95  	return 0
    96  }