github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/k8s.io/kubernetes/pkg/util/io/writer.go (about) 1 /* 2 Copyright 2015 The Kubernetes Authors All rights reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package io 18 19 import ( 20 "bytes" 21 "fmt" 22 "io/ioutil" 23 "os" 24 "os/exec" 25 26 "github.com/golang/glog" 27 ) 28 29 // Writer is an interface which allows to write data to a file. 30 type Writer interface { 31 WriteFile(filename string, data []byte, perm os.FileMode) error 32 } 33 34 // StdWriter implements Writer interface and uses standard libraries 35 // for writing data to files. 36 type StdWriter struct { 37 } 38 39 func (writer *StdWriter) WriteFile(filename string, data []byte, perm os.FileMode) error { 40 return ioutil.WriteFile(filename, data, perm) 41 } 42 43 // Alternative implementation of Writer interface that allows writing data to file 44 // using nsenter command. 45 // If a program (e.g. kubelet) runs in a container it may want to write data to 46 // a mounted device. Since in Docker, mount propagation mode is set to private, 47 // it will not see the mounted device in its own namespace. To work around this 48 // limitaion one has to first enter hosts namespace (by using 'nsenter') and only 49 // then write data. 50 type NsenterWriter struct { 51 } 52 53 // TODO: should take a writer, not []byte 54 func (writer *NsenterWriter) WriteFile(filename string, data []byte, perm os.FileMode) error { 55 cmd := "nsenter" 56 base_args := []string{ 57 "--mount=/rootfs/proc/1/ns/mnt", 58 "--", 59 } 60 61 echo_args := append(base_args, "sh", "-c", fmt.Sprintf("cat > %s", filename)) 62 glog.V(5).Infof("Command to write data to file: %v %v", cmd, echo_args) 63 command := exec.Command(cmd, echo_args...) 64 command.Stdin = bytes.NewBuffer(data) 65 outputBytes, err := command.CombinedOutput() 66 if err != nil { 67 glog.Errorf("Output from writing to %q: %v", filename, string(outputBytes)) 68 return err 69 } 70 71 chmod_args := append(base_args, "chmod", fmt.Sprintf("%o", perm), filename) 72 glog.V(5).Infof("Command to change permissions to file: %v %v", cmd, chmod_args) 73 outputBytes, err = exec.Command(cmd, chmod_args...).CombinedOutput() 74 if err != nil { 75 glog.Errorf("Output from chmod command: %v", string(outputBytes)) 76 return err 77 } 78 79 return nil 80 }