github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/pkg/providers/ukvm/stop_instance_cgo.go (about)

     1  // +build cgo
     2  
     3  package ukvm
     4  
     5  import (
     6  	"os"
     7  	"strconv"
     8  	"syscall"
     9  
    10  	"github.com/sirupsen/logrus"
    11  	"github.com/emc-advanced-dev/pkg/errors"
    12  	"github.com/solo-io/unik/pkg/types"
    13  )
    14  
    15  func (p *UkvmProvider) StopInstance(id string) error {
    16  	instance, err := p.GetInstance(id)
    17  	if err != nil {
    18  		return errors.New("retrieving instance "+id, err)
    19  	}
    20  
    21  	// kill ukvm
    22  	pid, err := strconv.Atoi(instance.Id)
    23  	if err != nil {
    24  		return errors.New("invalid instance id (should be ukvm pid)", err)
    25  	}
    26  
    27  	process, err := os.FindProcess(pid)
    28  	if err != nil {
    29  		logrus.Warn("failed finding instance, assuming instance has externally terminated", err)
    30  	} else {
    31  		if err := process.Signal(syscall.SIGKILL); err != nil {
    32  			logrus.Warn("failed terminating instance, assuming instance has externally terminated", err)
    33  		}
    34  	}
    35  
    36  	volumesToDetach := []*types.Volume{}
    37  	volumes, err := p.ListVolumes()
    38  	if err != nil {
    39  		return errors.New("getting volume list", err)
    40  	}
    41  	for _, volume := range volumes {
    42  		if volume.Attachment == instance.Id {
    43  			volumesToDetach = append(volumesToDetach, volume)
    44  		}
    45  	}
    46  
    47  	return p.state.RemoveInstance(instance)
    48  }