Merge pull request #122 from justincormack/metadata

Add a "metadata" file contents flag
This commit is contained in:
Justin Cormack 2017-07-17 15:56:06 +01:00 committed by GitHub
commit 4105b7ea31
5 changed files with 77 additions and 36 deletions

View File

@ -96,6 +96,14 @@ Specifying the `mode` is optional, and will default to `0600`. Leading directori
created if not specified. You can use `~/path` in `source` to specify a path in the build created if not specified. You can use `~/path` in `source` to specify a path in the build
user's home directory. user's home directory.
In addition there is a `metadata` option that will generate the file. Currently the only value
supported here is `"yaml"` which will output the yaml used to generate the image into the specified
file:
```
- path: etc/linuxkit.yml
metadata:yaml
```
## `trust` ## `trust`
The `trust` section specifies which build components are to be cryptographically verified with The `trust` section specifies which build components are to be cryptographically verified with

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,43 @@ 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) source := f.Source
if len(source) > 2 && source[:2] == "~/" {
source = homeDir() + source[1:]
}
if f.Optional {
_, err := os.Stat(source)
if err != nil {
// skip if not found or readable
log.Debugf("Skipping file [%s] as not readable and marked optional", source)
continue
}
}
var err error
contents, err = ioutil.ReadFile(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

@ -29,29 +29,30 @@ type Moby struct {
// KernelConfig is the type of the config for a kernel // KernelConfig is the type of the config for a kernel
type KernelConfig struct { type KernelConfig struct {
Image string Image string `yaml:"image" json:"image"`
Cmdline string Cmdline string `yaml:"cmdline" json:"cmdline,omitempty"`
Binary string Binary string `yaml:"binary" json:"binary,omitempty"`
Tar *string Tar *string `yaml:"tar" json:"tar,omitempty"`
} }
// TrustConfig is the type of a content trust config // TrustConfig is the type of a content trust config
type TrustConfig struct { type TrustConfig struct {
Image []string Image []string `yaml:"image" json:"image,omitempty"`
Org []string Org []string `yaml:"org" json:"org,omitempty"`
} }
// File is the type of a file specification // File is the type of a file specification
type File struct { type File struct {
Path string Path string `yaml:"path" json:"path"`
Directory bool Directory bool `yaml:"directory" json:"directory"`
Symlink string Symlink string `yaml:"symlink" json:"symlink,omitempty"`
Contents *string Contents *string `yaml:"contents" json:"contents,omitempty"`
Source string Source string `yaml:"source" json:"source,omitempty"`
Optional bool Metadata string `yaml:"metadata" json:"metadata,omitempty"`
Mode string Optional bool `yaml:"optional" json:"optional"`
UID interface{} `yaml:"uid" json:"uid"` Mode string `yaml:"mode" json:"mode,omitempty"`
GID interface{} `yaml:"gid" json:"gid"` UID interface{} `yaml:"uid" json:"uid,omitempty"`
GID interface{} `yaml:"gid" json:"gid,omitempty"`
} }
// Image is the type of an image config // Image is the type of an image config
@ -65,13 +66,13 @@ type Image struct {
Tmpfs *[]string `yaml:"tmpfs" json:"tmpfs,omitempty"` Tmpfs *[]string `yaml:"tmpfs" json:"tmpfs,omitempty"`
Command *[]string `yaml:"command" json:"command,omitempty"` Command *[]string `yaml:"command" json:"command,omitempty"`
Env *[]string `yaml:"env" json:"env,omitempty"` Env *[]string `yaml:"env" json:"env,omitempty"`
Cwd string `yaml:"cwd" json:"cwd"` Cwd string `yaml:"cwd" json:"cwd,omitempty"`
Net string `yaml:"net" json:"net"` Net string `yaml:"net" json:"net,omitempty"`
Pid string `yaml:"pid" json:"pid"` Pid string `yaml:"pid" json:"pid,omitempty"`
Ipc string `yaml:"ipc" json:"ipc"` Ipc string `yaml:"ipc" json:"ipc,omitempty"`
Uts string `yaml:"uts" json:"uts"` Uts string `yaml:"uts" json:"uts,omitempty"`
Userns string `yaml:"userns" json:"userns"` Userns string `yaml:"userns" json:"userns,omitempty"`
Hostname string `yaml:"hostname" json:"hostname"` Hostname string `yaml:"hostname" json:"hostname,omitempty"`
Readonly *bool `yaml:"readonly" json:"readonly,omitempty"` Readonly *bool `yaml:"readonly" json:"readonly,omitempty"`
MaskedPaths *[]string `yaml:"maskedPaths" json:"maskedPaths,omitempty"` MaskedPaths *[]string `yaml:"maskedPaths" json:"maskedPaths,omitempty"`
ReadonlyPaths *[]string `yaml:"readonlyPaths" json:"readonlyPaths,omitempty"` ReadonlyPaths *[]string `yaml:"readonlyPaths" json:"readonlyPaths,omitempty"`

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"}]},

View File

@ -31,6 +31,8 @@ files:
contents: '{"debug": true}' contents: '{"debug": true}'
- path: /empty - path: /empty
contents: "" contents: ""
- path: etc/moby-config
metadata: yaml
trust: trust:
org: org:
- library - library