support docker 1.2 in GetFromDocker

This commit is contained in:
Vishvananda Ishaya 2014-09-14 23:56:34 -07:00
parent be82404eeb
commit e14a2d4e90
1 changed files with 16 additions and 12 deletions

View File

@ -135,6 +135,7 @@ func getThisCgroup(cgroupType string) (string, error) {
// borrowed from docker/utils/utils.go // borrowed from docker/utils/utils.go
// modified to only return the first pid // modified to only return the first pid
// modified to glob with id // modified to glob with id
// modified to search for newer docker containers
func getPidForContainer(id string) (int, error) { func getPidForContainer(id string) (int, error) {
pid := 0 pid := 0
@ -153,26 +154,29 @@ func getPidForContainer(id string) (int, error) {
id += "*" id += "*"
filename := filepath.Join(cgroupRoot, cgroupThis, id, "tasks") attempts := []string{
filenames, _ := filepath.Glob(filename) filepath.Join(cgroupRoot, cgroupThis, id, "tasks"),
if len(filenames) > 1 {
return pid, fmt.Errorf("Ambiguous id supplied: %v", filenames)
} else if len(filenames) == 1 {
filename = filenames[0]
}
if _, err := os.Stat(filename); os.IsNotExist(err) {
// With more recent lxc versions use, cgroup will be in lxc/ // With more recent lxc versions use, cgroup will be in lxc/
filename = filepath.Join(cgroupRoot, cgroupThis, "lxc", id, "tasks") filepath.Join(cgroupRoot, cgroupThis, "lxc", id, "tasks"),
filenames, _ = filepath.Glob(filename) // With more recent dockee, cgroup will be in docker/
filepath.Join(cgroupRoot, cgroupThis, "docker", id, "tasks"),
}
var filename string
for _, attempt := range attempts {
filenames, _ := filepath.Glob(attempt)
if len(filenames) > 1 { if len(filenames) > 1 {
return pid, fmt.Errorf("Ambiguous id supplied: %v", filenames) return pid, fmt.Errorf("Ambiguous id supplied: %v", filenames)
} else if len(filenames) == 1 { } else if len(filenames) == 1 {
filename = filenames[0] filename = filenames[0]
} else { break;
return pid, fmt.Errorf("Unable to find container: %v", id[:len(id)-1])
} }
} }
if filename == "" {
return pid, fmt.Errorf("Unable to find container: %v", id[:len(id)-1])
}
output, err := ioutil.ReadFile(filename) output, err := ioutil.ReadFile(filename)
if err != nil { if err != nil {
return pid, err return pid, err