github.com/mccv1r0/cni@v0.7.0-alpha1/pkg/invoke/raw_exec.go (about) 1 // Copyright 2016 CNI 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 invoke 16 17 import ( 18 "bytes" 19 "encoding/json" 20 "fmt" 21 "io" 22 "os/exec" 23 24 "github.com/containernetworking/cni/pkg/types" 25 ) 26 27 type RawExec struct { 28 Stderr io.Writer 29 } 30 31 func (e *RawExec) ExecPlugin(pluginPath string, stdinData []byte, environ []string) ([]byte, error) { 32 stdout := &bytes.Buffer{} 33 34 c := exec.Cmd{ 35 Env: environ, 36 Path: pluginPath, 37 Args: []string{pluginPath}, 38 Stdin: bytes.NewBuffer(stdinData), 39 Stdout: stdout, 40 Stderr: e.Stderr, 41 } 42 if err := c.Run(); err != nil { 43 return nil, pluginErr(err, stdout.Bytes()) 44 } 45 46 return stdout.Bytes(), nil 47 } 48 49 func pluginErr(err error, output []byte) error { 50 if _, ok := err.(*exec.ExitError); ok { 51 emsg := types.Error{} 52 if perr := json.Unmarshal(output, &emsg); perr != nil { 53 emsg.Msg = fmt.Sprintf("netplugin failed but error parsing its diagnostic message %q: %v", string(output), perr) 54 } 55 return &emsg 56 } 57 58 return err 59 } 60 61 func (e *RawExec) FindInPath(plugin string, paths []string) (string, error) { 62 return FindInPath(plugin, paths) 63 }