github.com/inflatablewoman/deis@v1.0.1-0.20141111034523-a4511c46a6ce/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  	for _, target := range targets {
    13  		wg.Add(1)
    14  		go doDestroy(c, target, wg, outchan, errchan)
    15  	}
    16  	return
    17  }
    18  
    19  func doDestroy(c *FleetClient, target string, wg *sync.WaitGroup, outchan chan string, errchan chan error) {
    20  	defer wg.Done()
    21  
    22  	// prepare string representation
    23  	component, num, err := splitTarget(target)
    24  	if err != nil {
    25  		errchan <- err
    26  		return
    27  	}
    28  	name, err := formatUnitName(component, num)
    29  	if err != nil {
    30  		errchan <- err
    31  		return
    32  	}
    33  	destroyed := fmt.Sprintf("\033[0;33m%v:\033[0m destroyed                                 \r", name)
    34  
    35  	// tell fleet to destroy the unit
    36  	c.Fleet.DestroyUnit(name)
    37  
    38  	// loop until the unit is actually gone from unit states
    39  outerLoop:
    40  	for {
    41  		time.Sleep(250 * time.Millisecond)
    42  		unitStates, err := cAPI.UnitStates()
    43  		if err != nil {
    44  			errchan <- err
    45  		}
    46  		for _, us := range unitStates {
    47  			if strings.HasPrefix(us.Name, name) {
    48  				continue outerLoop
    49  			}
    50  		}
    51  		outchan <- destroyed
    52  		return
    53  	}
    54  }