github.com/chasestarr/deis@v1.13.5-0.20170519182049-1d9e59fbdbfc/client/cmd/ps.go (about)

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