Handle abs path for rootfs in oci hook

Fixes #2412

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2018-06-25 14:10:17 -04:00
parent f15c3be348
commit e239f65590

View File

@ -37,8 +37,12 @@ var ociHook = cli.Command{
if err != nil { if err != nil {
return err return err
} }
spec, err := loadSpec(state.Bundle)
if err != nil {
return err
}
var ( var (
ctx = newTemplateContext(state) ctx = newTemplateContext(state, spec)
args = []string(context.Args()) args = []string(context.Args())
env = os.Environ() env = os.Environ()
) )
@ -52,6 +56,25 @@ var ociHook = cli.Command{
}, },
} }
type hookSpec struct {
Root struct {
Path string `json:"path"`
} `json:"root"`
}
func loadSpec(bundle string) (*hookSpec, error) {
f, err := os.Open(filepath.Join(bundle, "config.json"))
if err != nil {
return nil, err
}
defer f.Close()
var s hookSpec
if err := json.NewDecoder(f).Decode(&s); err != nil {
return nil, err
}
return &s, nil
}
func loadHookState(r io.Reader) (*specs.State, error) { func loadHookState(r io.Reader) (*specs.State, error) {
var s specs.State var s specs.State
if err := json.NewDecoder(r).Decode(&s); err != nil { if err := json.NewDecoder(r).Decode(&s); err != nil {
@ -60,9 +83,10 @@ func loadHookState(r io.Reader) (*specs.State, error) {
return &s, nil return &s, nil
} }
func newTemplateContext(state *specs.State) *templateContext { func newTemplateContext(state *specs.State, spec *hookSpec) *templateContext {
t := &templateContext{ t := &templateContext{
state: state, state: state,
root: spec.Root.Path,
} }
t.funcs = template.FuncMap{ t.funcs = template.FuncMap{
"id": t.id, "id": t.id,
@ -77,6 +101,7 @@ func newTemplateContext(state *specs.State) *templateContext {
type templateContext struct { type templateContext struct {
state *specs.State state *specs.State
root string
funcs template.FuncMap funcs template.FuncMap
} }
@ -89,7 +114,10 @@ func (t *templateContext) bundle() string {
} }
func (t *templateContext) rootfs() string { func (t *templateContext) rootfs() string {
return filepath.Join(t.state.Bundle, "rootfs") if filepath.IsAbs(t.root) {
return t.root
}
return filepath.Join(t.state.Bundle, t.root)
} }
func (t *templateContext) pid() int { func (t *templateContext) pid() int {