-
Notifications
You must be signed in to change notification settings - Fork 12
/
update_desired_lrp.go
94 lines (80 loc) · 2.29 KB
/
update_desired_lrp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package commands
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"code.cloudfoundry.org/bbs"
"code.cloudfoundry.org/bbs/models"
"code.cloudfoundry.org/cfdot/commands/helpers"
"github.com/spf13/cobra"
)
var updateDesiredLRPCmd = &cobra.Command{
Use: "update-desired-lrp process-guid (SPEC|@FILE)",
Short: "Update a desired LRP",
Long: "Update a desired LRP for a process-guid with the given spec. Spec can either be json encoded update to a desired-lrp, e.g. '{\"instances\":\"4\"}', or a file containing json encoded update to a desired-lrp, e.g. @/path/to/spec/file",
RunE: updateDesiredLRP,
}
func init() {
AddBBSAndTimeoutFlags(updateDesiredLRPCmd)
RootCmd.AddCommand(updateDesiredLRPCmd)
}
func updateDesiredLRP(cmd *cobra.Command, args []string) error {
if len(args) != 2 {
return NewCFDotValidationError(cmd, fmt.Errorf("Missing arguments"))
}
processGuid, spec, err := ValidateUpdateDesiredLRPArguments(args)
if err != nil {
return NewCFDotValidationError(cmd, err)
}
bbsClient, err := helpers.NewBBSClient(cmd, Config)
if err != nil {
return NewCFDotError(cmd, err)
}
err = UpdateDesiredLRP(cmd.OutOrStdout(), cmd.OutOrStderr(), bbsClient, processGuid, spec)
if err != nil {
return NewCFDotError(cmd, err)
}
return nil
}
func ValidateUpdateDesiredLRPArguments(args []string) (string, []byte, error) {
var desiredLRP *models.DesiredLRPUpdate
var err error
var spec []byte
processGuid := args[0]
argValue := args[1]
if strings.HasPrefix(argValue, "@") {
_, err := os.Stat(argValue[1:])
if err != nil {
println(err.Error())
return "", nil, err
}
spec, err = ioutil.ReadFile(argValue[1:])
if err != nil {
return "", nil, err
}
} else {
spec = []byte(argValue)
}
err = json.Unmarshal([]byte(spec), &desiredLRP)
if err != nil {
return "", nil, errors.New(fmt.Sprintf("Invalid JSON: %s", err.Error()))
}
return processGuid, spec, nil
}
func UpdateDesiredLRP(stdout, stderr io.Writer, bbsClient bbs.Client, processGuid string, spec []byte) error {
logger := globalLogger.Session("update-desired-lrp")
var desiredLRP *models.DesiredLRPUpdate
err := json.Unmarshal(spec, &desiredLRP)
if err != nil {
return err
}
err = bbsClient.UpdateDesiredLRP(logger, processGuid, desiredLRP)
if err != nil {
return err
}
return nil
}