github.com/didip/deis@v1.4.1/deisctl/backend/fleet/destroy.go (about)

     1  package fleet
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"sync"
     7  	"time"
     8  )
     9  
    10  // Destroy units for a given target
    11  func (c *FleetClient) Destroy(targets []string, wg *sync.WaitGroup, outchan chan string, errchan chan error) {
    12  	// expand @* targets
    13  	expandedTargets, err := expandTargets(c, targets)
    14  	if err != nil {
    15  		errchan <- err
    16  		return
    17  	}
    18  
    19  	for _, target := range expandedTargets {
    20  		wg.Add(1)
    21  		go doDestroy(c, target, wg, outchan, errchan)
    22  	}
    23  	return
    24  }
    25  
    26  func doDestroy(c *FleetClient, target string, wg *sync.WaitGroup, outchan chan string, errchan chan error) {
    27  	defer wg.Done()
    28  
    29  	// prepare string representation
    30  	component, num, err := splitTarget(target)
    31  	if err != nil {
    32  		errchan <- err
    33  		return
    34  	}
    35  	name, err := formatUnitName(component, num)
    36  	if err != nil {
    37  		errchan <- err
    38  		return
    39  	}
    40  	destroyed := fmt.Sprintf("\033[0;33m%v:\033[0m destroyed                                 \r", name)
    41  
    42  	// tell fleet to destroy the unit
    43  	c.Fleet.DestroyUnit(name)
    44  
    45  	// loop until the unit is actually gone from unit states
    46  outerLoop:
    47  	for {
    48  		time.Sleep(250 * time.Millisecond)
    49  		unitStates, err := cAPI.UnitStates()
    50  		if err != nil {
    51  			errchan <- err
    52  		}
    53  		for _, us := range unitStates {
    54  			if strings.HasPrefix(us.Name, name) {
    55  				continue outerLoop
    56  			}
    57  		}
    58  		outchan <- destroyed
    59  		return
    60  	}
    61  }