github.com/naphatkrit/deis@v1.12.3/client/cmd/ps.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  	"strconv"
     7  	"strings"
     8  	"time"
     9  
    10  	"github.com/deis/deis/client/controller/api"
    11  	"github.com/deis/deis/client/controller/models/ps"
    12  )
    13  
    14  // PsList lists an app's processes.
    15  func PsList(appID string, results int) error {
    16  	c, appID, err := load(appID)
    17  
    18  	if err != nil {
    19  		return err
    20  	}
    21  
    22  	if results == defaultLimit {
    23  		results = c.ResponseLimit
    24  	}
    25  
    26  	processes, count, err := ps.List(c, appID, results)
    27  
    28  	if err != nil {
    29  		return err
    30  	}
    31  
    32  	printProcesses(appID, processes, count)
    33  
    34  	return nil
    35  }
    36  
    37  // PsScale scales an app's processes.
    38  func PsScale(appID string, targets []string) error {
    39  	c, appID, err := load(appID)
    40  
    41  	if err != nil {
    42  		return err
    43  	}
    44  
    45  	targetMap := make(map[string]int)
    46  	regex := regexp.MustCompile("^([A-z]+)=([0-9]+)$")
    47  
    48  	for _, target := range targets {
    49  		if regex.MatchString(target) {
    50  			captures := regex.FindStringSubmatch(target)
    51  			targetMap[captures[1]], err = strconv.Atoi(captures[2])
    52  
    53  			if err != nil {
    54  				return err
    55  			}
    56  		} else {
    57  			fmt.Printf("'%s' does not match the pattern 'type=num', ex: web=2\n", target)
    58  		}
    59  	}
    60  
    61  	fmt.Printf("Scaling processes... but first, %s!\n", drinkOfChoice())
    62  	startTime := time.Now()
    63  	quit := progress()
    64  
    65  	err = ps.Scale(c, appID, targetMap)
    66  
    67  	quit <- true
    68  	<-quit
    69  
    70  	if err != nil {
    71  		return err
    72  	}
    73  
    74  	fmt.Printf("done in %ds\n", int(time.Since(startTime).Seconds()))
    75  
    76  	processes, count, err := ps.List(c, appID, c.ResponseLimit)
    77  
    78  	if err != nil {
    79  		return err
    80  	}
    81  
    82  	printProcesses(appID, processes, count)
    83  	return nil
    84  }
    85  
    86  // PsRestart restarts an app's processes.
    87  func PsRestart(appID, target string) error {
    88  	c, appID, err := load(appID)
    89  
    90  	if err != nil {
    91  		return err
    92  	}
    93  
    94  	psType := ""
    95  	psNum := -1
    96  
    97  	if target != "" {
    98  		if strings.Contains(target, ".") {
    99  			parts := strings.Split(target, ".")
   100  			psType = parts[0]
   101  			psNum, err = strconv.Atoi(parts[1])
   102  
   103  			if err != nil {
   104  				return err
   105  			}
   106  		} else {
   107  			psType = target
   108  		}
   109  	}
   110  
   111  	fmt.Printf("Restarting processes... but first, %s!\n", drinkOfChoice())
   112  	startTime := time.Now()
   113  	quit := progress()
   114  
   115  	_, err = ps.Restart(c, appID, psType, psNum)
   116  
   117  	quit <- true
   118  	<-quit
   119  
   120  	if err != nil {
   121  		return err
   122  	}
   123  
   124  	fmt.Printf("done in %ds\n", int(time.Since(startTime).Seconds()))
   125  
   126  	processes, count, err := ps.List(c, appID, c.ResponseLimit)
   127  
   128  	if err != nil {
   129  		return err
   130  	}
   131  
   132  	printProcesses(appID, processes, count)
   133  	return nil
   134  }
   135  
   136  func printProcesses(appID string, processes []api.Process, count int) {
   137  	psMap := ps.ByType(processes)
   138  
   139  	fmt.Printf("=== %s Processes%s", appID, limitCount(len(processes), count))
   140  
   141  	for psType, procs := range psMap {
   142  		fmt.Printf("--- %s:\n", psType)
   143  
   144  		for _, proc := range procs {
   145  			fmt.Printf("%s.%d %s (%s)\n", proc.Type, proc.Num, proc.State, proc.Release)
   146  		}
   147  	}
   148  }