-
Notifications
You must be signed in to change notification settings - Fork 224
/
config.go
255 lines (219 loc) · 8.1 KB
/
config.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Package appconfig implements functionality related to reading and writing app
// configuration files.
package appconfig
import (
"fmt"
"net/url"
"reflect"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/scanner"
"golang.org/x/exp/slices"
)
const (
// DefaultConfigFileName denotes the default application configuration file name.
DefaultConfigFileName = "fly.toml"
// Config is versioned, initially, to separate nomad from machine apps without having to consult
// the API
MachinesPlatform = "machines"
NomadPlatform = "nomad"
DetachedPlatform = "detached"
)
func NewConfig() *Config {
return &Config{
RawDefinition: map[string]any{},
defaultGroupName: api.MachineProcessGroupApp,
configFilePath: "--config path unset--",
}
}
// Config wraps the properties of app configuration.
// NOTE: If you any new setting here, please also add a value for it at testdata/rull-reference.toml
type Config struct {
AppName string `toml:"app,omitempty" json:"app,omitempty"`
PrimaryRegion string `toml:"primary_region,omitempty" json:"primary_region,omitempty"`
KillSignal *string `toml:"kill_signal,omitempty" json:"kill_signal,omitempty"`
KillTimeout *api.Duration `toml:"kill_timeout,omitempty" json:"kill_timeout,omitempty"`
ConsoleCommand string `toml:"console_command,omitempty" json:"console_command,omitempty"`
// Sections that are typically short and benefit from being on top
Experimental *Experimental `toml:"experimental,omitempty" json:"experimental,omitempty"`
Build *Build `toml:"build,omitempty" json:"build,omitempty"`
Deploy *Deploy `toml:"deploy, omitempty" json:"deploy,omitempty"`
Env map[string]string `toml:"env,omitempty" json:"env,omitempty"`
// Fields that are process group aware must come after Processes
Processes map[string]string `toml:"processes,omitempty" json:"processes,omitempty"`
Mounts []Mount `toml:"mounts,omitempty" json:"mounts,omitempty"`
HTTPService *HTTPService `toml:"http_service,omitempty" json:"http_service,omitempty"`
Services []Service `toml:"services,omitempty" json:"services,omitempty"`
Checks map[string]*ToplevelCheck `toml:"checks,omitempty" json:"checks,omitempty"`
// Others, less important.
Statics []Static `toml:"statics,omitempty" json:"statics,omitempty"`
Metrics *api.MachineMetrics `toml:"metrics,omitempty" json:"metrics,omitempty"`
// RawDefinition contains fly.toml parsed as-is
// If you add any config field that is v2 specific, be sure to remove it in SanitizeDefinition()
RawDefinition map[string]any `toml:"-" json:"-"`
// Path to application configuration file, usually fly.toml.
configFilePath string
// Indicates the intended platform to use: machines or nomad
platformVersion string
// Set when it fails to unmarshal fly.toml into Config
// Don't hard fail because RawDefinition still holds the app configuration for Nomad apps
v2UnmarshalError error
// The default group name to refer to (used with flatten configs)
defaultGroupName string
}
type Deploy struct {
ReleaseCommand string `toml:"release_command,omitempty" json:"release_command,omitempty"`
Strategy string `toml:"strategy,omitempty" json:"strategy,omitempty"`
}
type Static struct {
GuestPath string `toml:"guest_path" json:"guest_path,omitempty" validate:"required"`
UrlPrefix string `toml:"url_prefix" json:"url_prefix,omitempty" validate:"required"`
}
type Mount = scanner.Volume
type Build struct {
Builder string `toml:"builder,omitempty" json:"builder,omitempty"`
Args map[string]string `toml:"args,omitempty" json:"args,omitempty"`
Buildpacks []string `toml:"buildpacks,omitempty" json:"buildpacks,omitempty"`
Image string `toml:"image,omitempty" json:"image,omitempty"`
Settings map[string]any `toml:"settings,omitempty" json:"settings,omitempty"`
Builtin string `toml:"builtin,omitempty" json:"builtin,omitempty"`
Dockerfile string `toml:"dockerfile,omitempty" json:"dockerfile,omitempty"`
Ignorefile string `toml:"ignorefile,omitempty" json:"ignorefile,omitempty"`
DockerBuildTarget string `toml:"build-target,omitempty" json:"build-target,omitempty"`
}
type Experimental struct {
Cmd []string `toml:"cmd,omitempty" json:"cmd,omitempty"`
Entrypoint []string `toml:"entrypoint,omitempty" json:"entrypoint,omitempty"`
Exec []string `toml:"exec,omitempty" json:"exec,omitempty"`
AutoRollback bool `toml:"auto_rollback,omitempty" json:"auto_rollback,omitempty"`
EnableConsul bool `toml:"enable_consul,omitempty" json:"enable_consul,omitempty"`
EnableEtcd bool `toml:"enable_etcd,omitempty" json:"enable_etcd,omitempty"`
}
func (c *Config) ConfigFilePath() string {
return c.configFilePath
}
func (c *Config) SetConfigFilePath(configFilePath string) {
c.configFilePath = configFilePath
}
func (c *Config) HasNonHttpAndHttpsStandardServices() bool {
for _, service := range c.Services {
switch service.Protocol {
case "udp":
return true
case "tcp":
for _, p := range service.Ports {
if p.HasNonHttpPorts() {
return true
} else if p.ContainsPort(80) && !reflect.DeepEqual(p.Handlers, []string{"http"}) {
return true
} else if p.ContainsPort(443) && !(reflect.DeepEqual(p.Handlers, []string{"http", "tls"}) || reflect.DeepEqual(p.Handlers, []string{"tls", "http"})) {
return true
}
}
}
}
return false
}
func (c *Config) HasUdpService() bool {
for _, service := range c.Services {
if service.Protocol == "udp" {
return true
}
}
return false
}
func (c *Config) Dockerfile() string {
if c == nil || c.Build == nil {
return ""
}
return c.Build.Dockerfile
}
func (c *Config) Ignorefile() string {
if c == nil || c.Build == nil {
return ""
}
return c.Build.Ignorefile
}
func (c *Config) DockerBuildTarget() string {
if c == nil || c.Build == nil {
return ""
}
return c.Build.DockerBuildTarget
}
func (c *Config) InternalPort() int {
if c.HTTPService != nil {
return c.HTTPService.InternalPort
}
if len(c.Services) > 0 {
return c.Services[0].InternalPort
}
return 0
}
func (cfg *Config) BuildStrategies() []string {
strategies := []string{}
if cfg == nil || cfg.Build == nil {
return strategies
}
if cfg.Build.Image != "" {
strategies = append(strategies, fmt.Sprintf("the \"%s\" docker image", cfg.Build.Image))
}
if cfg.Build.Builder != "" || len(cfg.Build.Buildpacks) > 0 {
strategies = append(strategies, "a buildpack")
}
if cfg.Build.Dockerfile != "" || cfg.Build.DockerBuildTarget != "" {
if cfg.Build.Dockerfile != "" {
strategies = append(strategies, fmt.Sprintf("the \"%s\" dockerfile", cfg.Build.Dockerfile))
} else {
strategies = append(strategies, "a dockerfile")
}
}
if cfg.Build.Builtin != "" {
strategies = append(strategies, fmt.Sprintf("the \"%s\" builtin image", cfg.Build.Builtin))
}
return strategies
}
func (cfg *Config) URL() *url.URL {
u := &url.URL{
Scheme: "https",
Host: cfg.AppName + ".fly.dev",
Path: "/",
}
// HTTPService always listen on https, even if ForceHTTPS is false
if cfg.HTTPService != nil && cfg.HTTPService.InternalPort > 0 {
return u
}
var httpPorts []int
var httpsPorts []int
for _, service := range cfg.Services {
for _, port := range service.Ports {
if port.Port == nil || !slices.Contains(port.Handlers, "http") {
continue
}
if slices.Contains(port.Handlers, "tls") {
httpsPorts = append(httpsPorts, *port.Port)
} else {
httpPorts = append(httpPorts, *port.Port)
}
}
}
switch {
case slices.Contains(httpsPorts, 443):
return u
case slices.Contains(httpPorts, 80):
u.Scheme = "http"
return u
case len(httpsPorts) > 0:
slices.Sort(httpsPorts)
u.Host = fmt.Sprintf("%s:%d", u.Host, httpsPorts[0])
return u
case len(httpPorts) > 0:
slices.Sort(httpPorts)
u.Host = fmt.Sprintf("%s:%d", u.Host, httpPorts[0])
u.Scheme = "http"
return u
default:
return nil
}
}
func (cfg *Config) PlatformVersion() string {
return cfg.platformVersion
}