-
Notifications
You must be signed in to change notification settings - Fork 234
/
config.go
193 lines (166 loc) · 6.83 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
// Package app implements functionality related to reading and writing app
// configuration files.
package appconfig
import (
"fmt"
"reflect"
"github.com/superfly/flyctl/api"
)
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,
}
}
// 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"`
KillSignal *string `toml:"kill_signal,omitempty" json:"kill_signal,omitempty"`
KillTimeout *int `toml:"kill_timeout,omitempty" json:"kill_timeout,omitempty"`
PrimaryRegion string `toml:"primary_region,omitempty" json:"primary_region,omitempty"`
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"`
HTTPService *HTTPService `toml:"http_service,omitempty" json:"http_service,omitempty"`
Metrics *api.MachineMetrics `toml:"metrics,omitempty" json:"metrics,omitempty"`
Statics []Static `toml:"statics,omitempty" json:"statics,omitempty"`
Mounts []Mount `toml:"mounts,omitempty" json:"mounts,omitempty"`
Processes map[string]string `toml:"processes,omitempty" json:"processes,omitempty"`
Checks map[string]*ToplevelCheck `toml:"checks,omitempty" json:"checks,omitempty"`
Services []Service `toml:"services,omitempty" json:"services,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 struct {
Source string `toml:"source,omitempty" json:"source,omitempty"`
Destination string `toml:"destination" json:"destination,omitempty"`
Processes []string `json:"processes,omitempty" toml:"processes,omitempty"`
}
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) 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
}