This repository has been archived by the owner on Jan 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
recipe_builder.go
138 lines (111 loc) · 3.66 KB
/
recipe_builder.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package recipebuilder
import (
"fmt"
"code.cloudfoundry.org/bbs/models"
"code.cloudfoundry.org/diego-ssh/keys"
"code.cloudfoundry.org/runtimeschema/cc_messages"
"code.cloudfoundry.org/urljoiner"
)
const (
MinCpuProxy = 128
MaxCpuProxy = 8192
DefaultFileDescriptorLimit = uint64(1024)
LRPLogSource = "CELL"
AppLogSource = "APP"
HealthLogSource = "HEALTH"
Router = "router"
DefaultPort = uint32(8080)
DefaultSSHPort = uint32(2222)
DefaultLANG = "en_US.UTF-8"
TrustedSystemCertificatesPath = "/etc/cf-system-certificates"
)
var (
ErrNoDockerImage = Error{Type: "ErrNoDockerImage", Message: "no docker image provided"}
ErrNoLifecycleDefined = Error{Type: "ErrNoLifecycleDefined", Message: "no lifecycle binary bundle defined for stack"}
ErrDropletSourceMissing = Error{Type: "ErrAppSourceMissing", Message: "desired app missing droplet_uri"}
ErrDockerImageMissing = Error{Type: "ErrDockerImageMissing", Message: "desired app missing docker_image"}
ErrMultipleAppSources = Error{Type: "ErrMultipleAppSources", Message: "desired app contains both droplet_uri and docker_image; exactly one is required."}
)
type Config struct {
Lifecycles map[string]string
FileServerURL string
KeyFactory keys.SSHKeyFactory
PrivilegedContainers bool
}
//go:generate counterfeiter -o ../bulk/fakes/fake_recipe_builder.go . RecipeBuilder
type RecipeBuilder interface {
Build(*cc_messages.DesireAppRequestFromCC) (*models.DesiredLRP, error)
BuildTask(*cc_messages.TaskRequestFromCC) (*models.TaskDefinition, error)
ExtractExposedPorts(*cc_messages.DesireAppRequestFromCC) ([]uint32, error)
}
type Error struct {
Type string `json:"name"`
Message string `json:"message"`
}
func (err Error) Error() string {
return err.Message
}
func lifecycleDownloadURL(lifecyclePath string, fileServerURL string) string {
return urljoiner.Join(fileServerURL, "/v1/static", lifecyclePath)
}
func cpuWeight(memoryMB int) uint32 {
cpuProxy := memoryMB
if cpuProxy > MaxCpuProxy {
return 100
}
if cpuProxy < MinCpuProxy {
cpuProxy = MinCpuProxy
}
return uint32((100 * cpuProxy) / MaxCpuProxy)
}
func createLrpEnv(env []*models.EnvironmentVariable, exposedPorts []uint32, includeDeprecated bool) []*models.EnvironmentVariable {
if len(exposedPorts) > 0 {
portValue := fmt.Sprintf("%d", exposedPorts[0])
env = append(env, &models.EnvironmentVariable{Name: "PORT", Value: portValue})
if includeDeprecated {
env = append(env, &models.EnvironmentVariable{Name: "VCAP_APP_PORT", Value: portValue})
}
}
if includeDeprecated {
env = append(env, &models.EnvironmentVariable{Name: "VCAP_APP_HOST", Value: "0.0.0.0"})
}
return env
}
func getParallelAction(ports []uint32, user string, uri string) *models.ParallelAction {
fileDescriptorLimit := DefaultFileDescriptorLimit
parallelAction := &models.ParallelAction{}
for _, port := range ports {
args := []string{fmt.Sprintf("-port=%d", port)}
if uri != "" {
args = append(args, fmt.Sprintf("-uri=%s", uri))
}
parallelAction.Actions = append(parallelAction.Actions,
&models.Action{
RunAction: &models.RunAction{
User: user,
Path: "/tmp/lifecycle/healthcheck",
Args: args,
LogSource: HealthLogSource,
ResourceLimits: &models.ResourceLimits{
Nofile: &fileDescriptorLimit,
},
SuppressLogOutput: true,
},
})
}
return parallelAction
}
func getDesiredAppPorts(ports []uint32) []uint32 {
desiredAppPorts := ports
if desiredAppPorts == nil {
desiredAppPorts = []uint32{DefaultPort}
}
return desiredAppPorts
}
func getAppLogSource(logSource string) string {
if logSource == "" {
return AppLogSource
} else {
return logSource
}
}