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

     1  package ps
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"strconv"
     7  
     8  	"github.com/deis/deis/client/controller/api"
     9  	"github.com/deis/deis/client/controller/client"
    10  )
    11  
    12  // List an app's processes.
    13  func List(c *client.Client, appID string, results int) ([]api.Process, int, error) {
    14  	u := fmt.Sprintf("/v1/apps/%s/containers/", appID)
    15  	body, count, err := c.LimitedRequest(u, results)
    16  
    17  	if err != nil {
    18  		return []api.Process{}, -1, err
    19  	}
    20  
    21  	var procs []api.Process
    22  	if err = json.Unmarshal([]byte(body), &procs); err != nil {
    23  		return []api.Process{}, -1, err
    24  	}
    25  
    26  	return procs, count, nil
    27  }
    28  
    29  // Scale an app's processes.
    30  func Scale(c *client.Client, appID string, targets map[string]int) error {
    31  	u := fmt.Sprintf("/v1/apps/%s/scale/", appID)
    32  
    33  	body, err := json.Marshal(targets)
    34  
    35  	if err != nil {
    36  		return err
    37  	}
    38  
    39  	_, err = c.BasicRequest("POST", u, body)
    40  	return err
    41  }
    42  
    43  // Restart an app's processes.
    44  func Restart(c *client.Client, appID string, procType string, num int) ([]api.Process, error) {
    45  	u := fmt.Sprintf("/v1/apps/%s/containers/", appID)
    46  
    47  	if procType == "" {
    48  		u += "restart/"
    49  	} else {
    50  		if num == -1 {
    51  			u += procType + "/restart/"
    52  		} else {
    53  			u += procType + "/" + strconv.Itoa(num) + "/restart/"
    54  		}
    55  	}
    56  
    57  	body, err := c.BasicRequest("POST", u, nil)
    58  
    59  	if err != nil {
    60  		return []api.Process{}, err
    61  	}
    62  
    63  	procs := []api.Process{}
    64  	if err = json.Unmarshal([]byte(body), &procs); err != nil {
    65  		return []api.Process{}, err
    66  	}
    67  
    68  	return procs, nil
    69  }
    70  
    71  // ByType organizes processes of an app by process type.
    72  func ByType(processes []api.Process) map[string][]api.Process {
    73  	psMap := make(map[string][]api.Process)
    74  
    75  	for _, ps := range processes {
    76  		psMap[ps.Type] = append(psMap[ps.Type], ps)
    77  	}
    78  
    79  	return psMap
    80  }