github.com/solo-io/unik@v0.0.0-20190717152701-a58d3e8e33b7/pkg/providers/ukvm/list_instances.go (about) 1 package ukvm 2 3 import ( 4 "fmt" 5 "github.com/emc-advanced-dev/pkg/errors" 6 "github.com/solo-io/unik/pkg/types" 7 "os" 8 "strconv" 9 "syscall" 10 ) 11 12 func (p *UkvmProvider) ListInstances() ([]*types.Instance, error) { 13 if len(p.state.GetInstances()) < 1 { 14 return []*types.Instance{}, nil 15 } 16 17 var instances []*types.Instance 18 for _, instance := range p.state.GetInstances() { 19 pid, err := strconv.Atoi(instance.Id) 20 if err != nil { 21 return nil, errors.New("invalid id (is not a pid)", err) 22 } 23 if err := detectInstance(pid); err != nil { 24 p.state.RemoveInstance(instance) 25 } 26 instances = append(instances, instance) 27 } 28 29 return instances, nil 30 } 31 32 func detectInstance(pid int) error { 33 process, err := os.FindProcess(pid) 34 if err != nil { 35 return errors.New("Failed to find process", err) 36 } 37 if err := process.Signal(syscall.Signal(0)); err != nil { 38 return errors.New(fmt.Sprintf("process.Signal on pid %d returned", pid), err) 39 } 40 return nil 41 }