Add a "metadata" file contents flag

Currently this supports "yaml" as the only option, which will output
the yaml config (as JSON) into the file specified in the image.

Fix #107

Signed-off-by: Justin Cormack <justin.cormack@docker.com>
This commit is contained in:
Justin Cormack 2017-07-17 15:11:05 +01:00
parent f035995b22
commit 389dd8c0fa
3 changed files with 44 additions and 14 deletions

View File

@ -3,6 +3,7 @@ package moby
import ( import (
"archive/tar" "archive/tar"
"bytes" "bytes"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@ -359,6 +360,16 @@ func tarAppend(iw *tar.Writer, tr *tar.Reader) error {
return nil return nil
} }
// this allows inserting metadata into a file in the image
func metadata(m Moby, md string) ([]byte, error) {
switch md {
case "yaml":
return json.MarshalIndent(m, "", " ")
default:
return []byte{}, fmt.Errorf("Unsupported metadata type: %s", md)
}
}
func filesystem(m Moby, tw *tar.Writer, idMap map[string]uint32) error { func filesystem(m Moby, tw *tar.Writer, idMap map[string]uint32) error {
// TODO also include the files added in other parts of the build // TODO also include the files added in other parts of the build
var addedFiles = map[string]bool{} var addedFiles = map[string]bool{}
@ -410,25 +421,42 @@ func filesystem(m Moby, tw *tar.Writer, idMap map[string]uint32) error {
if f.Contents != nil { if f.Contents != nil {
contents = []byte(*f.Contents) contents = []byte(*f.Contents)
} }
if !f.Directory && f.Contents == nil && f.Symlink == "" { if !f.Directory && f.Symlink == "" && f.Contents == nil {
if f.Source == "" { if f.Source == "" && f.Metadata == "" {
return errors.New("Contents of file not specified") return fmt.Errorf("Contents of file (%s) not specified", f.Path)
} }
if len(f.Source) > 2 && f.Source[:2] == "~/" { if f.Source != "" && f.Metadata != "" {
f.Source = homeDir() + f.Source[1:] return fmt.Errorf("Specified Source and Metadata for file: %s", f.Path)
} }
if f.Optional { if f.Source != "" {
_, err := os.Stat(f.Source) if len(f.Source) > 2 && f.Source[:2] == "~/" {
f.Source = homeDir() + f.Source[1:]
}
if f.Optional {
_, err := os.Stat(f.Source)
if err != nil {
// skip if not found or readable
log.Debugf("Skipping file [%s] as not readable and marked optional", f.Source)
continue
}
}
var err error
contents, err = ioutil.ReadFile(f.Source)
if err != nil { if err != nil {
// skip if not found or readable return err
log.Debugf("Skipping file [%s] as not readable and marked optional", f.Source) }
continue } else {
contents, err = metadata(m, f.Metadata)
if err != nil {
return err
} }
} }
var err error } else {
contents, err = ioutil.ReadFile(f.Source) if f.Metadata != "" {
if err != nil { return fmt.Errorf("Specified Contents and Metadata for file: %s", f.Path)
return err }
if f.Source != "" {
return fmt.Errorf("Specified Contents and Source for file: %s", f.Path)
} }
} }
// we need all the leading directories // we need all the leading directories

View File

@ -48,6 +48,7 @@ type File struct {
Symlink string Symlink string
Contents *string Contents *string
Source string Source string
Metadata string
Optional bool Optional bool
Mode string Mode string
UID interface{} `yaml:"uid" json:"uid"` UID interface{} `yaml:"uid" json:"uid"`

View File

@ -25,6 +25,7 @@ var schema = string(`
"symlink": {"type": "string"}, "symlink": {"type": "string"},
"contents": {"type": "string"}, "contents": {"type": "string"},
"source": {"type": "string"}, "source": {"type": "string"},
"metadata": {"type": "string"},
"optional": {"type": "boolean"}, "optional": {"type": "boolean"},
"mode": {"type": "string"}, "mode": {"type": "string"},
"uid": {"anyOf": [{"type": "string"}, {"type": "integer"}]}, "uid": {"anyOf": [{"type": "string"}, {"type": "integer"}]},