Add some support to add files in Yaml config

You can specify a file with contents.

Signed-off-by: Justin Cormack <justin.cormack@docker.com>
This commit is contained in:
Justin Cormack 2017-03-01 18:54:58 -08:00
parent ce417eab07
commit ec643ee8d3
3 changed files with 71 additions and 9 deletions

View File

@ -1,6 +1,10 @@
package main package main
import ( import (
"archive/tar"
"bytes"
"errors"
"path"
"strconv" "strconv"
"strings" "strings"
@ -8,12 +12,12 @@ import (
) )
type Moby struct { type Moby struct {
Kernel string Kernel string
Init string Init string
System []MobyImage System []MobyImage
Database []struct { Files []struct {
File string Path string
Value string Contents string
} }
} }
@ -67,3 +71,54 @@ func ConfigToRun(image *MobyImage) []string {
return args return args
} }
func Filesystem(m *Moby) (*bytes.Buffer, error) {
buf := new(bytes.Buffer)
tw := tar.NewWriter(buf)
defer tw.Close()
for _, f := range m.Files {
if f.Path == "" {
return buf, errors.New("Did not specify path for file")
}
if f.Contents == "" {
return buf, errors.New("Contents of file not specified")
}
// we need all the leading directories
parts := strings.Split(path.Dir(f.Path), "/")
root := ""
for _, p := range parts {
if p == "." || p == "/" {
continue
}
if root == "" {
root = p
} else {
root = root + "/" + p
}
hdr := &tar.Header{
Name: root,
Typeflag: tar.TypeDir,
Mode: 0700,
}
err := tw.WriteHeader(hdr)
if err != nil {
return buf, err
}
}
hdr := &tar.Header{
Name: f.Path,
Mode: 0600,
Size: int64(len(f.Contents)),
}
err := tw.WriteHeader(hdr)
if err != nil {
return buf, err
}
_, err = tw.Write([]byte(f.Contents))
if err != nil {
return buf, err
}
}
return buf, nil
}

View File

@ -131,6 +131,13 @@ func build() {
containers = append(containers, buffer) containers = append(containers, buffer)
} }
// add files
buffer, err = Filesystem(m)
if err != nil {
log.Fatalf("failed to add filesystem parts: %v", err)
}
containers = append(containers, buffer)
initrd, err := containersInitrd(containers) initrd, err := containersInitrd(containers)
if err != nil { if err != nil {
log.Fatalf("Failed to make initrd %v", err) log.Fatalf("Failed to make initrd %v", err)

View File

@ -20,6 +20,6 @@ system:
- CAP_SETUID - CAP_SETUID
- CAP_SETGID - CAP_SETGID
network_mode: host network_mode: host
database: files:
- file: etc/docker/daemon.json - path: etc/docker/daemon.json
value: '{"debug": true}' contents: '{"debug": true}'