Skip to content

Commit

Permalink
processes support for fly deploy, destroy release_command machines, a…
Browse files Browse the repository at this point in the history
…dd --process-group flag to fly m clone
  • Loading branch information
tvdfly authored and dangra committed Jan 25, 2023
1 parent 289d862 commit 9bd4fa7
Show file tree
Hide file tree
Showing 5 changed files with 287 additions and 66 deletions.
12 changes: 4 additions & 8 deletions api/machine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ const MachineConfigMetadataKeyFlyManagedPostgres = "fly-managed-postgres"
const MachineConfigMetadataKeyFlyPlatformVersion = "fly_platform_version"
const MachineConfigMetadataKeyFlyReleaseId = "fly_release_id"
const MachineConfigMetadataKeyFlyReleaseVersion = "fly_release_version"
const MachineConfigMetadataKeyProcessGroup = "process_group"
const MachineConfigMetadataKeyFlyProcessGroup = "fly_process_group"
const MachineFlyPlatformVersion2 = "v2"
const MachineProcessGroupApp = "app"
const MachineProcessGroupReleaseCommand = "release_command"
const MachineProcessGroupFlyAppReleaseCommand = "fly_app_release_command"
const MachineStateDestroyed = "destroyed"
const MachineStateStarted = "started"
const MachineStateStopped = "stopped"
Expand Down Expand Up @@ -54,20 +54,16 @@ func (m *Machine) IsFlyAppsPlatform() bool {
return m.Config != nil && m.Config.Metadata[MachineConfigMetadataKeyFlyPlatformVersion] == MachineFlyPlatformVersion2 && m.State != MachineStateDestroyed
}

func (m *Machine) IsFlyAppsInstance() bool {
return m.IsFlyAppsPlatform() && m.HasProcessGroup(MachineProcessGroupApp)
}

func (m *Machine) IsFlyAppsReleaseCommand() bool {
return m.IsFlyAppsPlatform() && m.HasProcessGroup(MachineProcessGroupReleaseCommand)
return m.IsFlyAppsPlatform() && m.HasProcessGroup(MachineProcessGroupFlyAppReleaseCommand)
}

func (m *Machine) IsActive() bool {
return m.State != MachineStateDestroyed
}

func (m *Machine) HasProcessGroup(desired string) bool {
return m.Config != nil && m.Config.Metadata[MachineConfigMetadataKeyProcessGroup] == desired
return m.Config != nil && m.Config.Metadata[MachineConfigMetadataKeyFlyProcessGroup] == desired
}

func (m Machine) ImageVersion() string {
Expand Down
4 changes: 2 additions & 2 deletions flaps/flaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func (f *Client) ListActive(ctx context.Context) ([]*api.Machine, error) {
}

machines = lo.Filter(machines, func(m *api.Machine, _ int) bool {
return !m.HasProcessGroup(api.MachineProcessGroupReleaseCommand) && m.IsActive()
return !m.HasProcessGroup(api.MachineProcessGroupFlyAppReleaseCommand) && m.IsActive()
})

return machines, nil
Expand All @@ -259,7 +259,7 @@ func (f *Client) ListFlyAppsMachines(ctx context.Context) ([]*api.Machine, *api.
var releaseCmdMachine *api.Machine
machines := make([]*api.Machine, 0)
for _, m := range allMachines {
if m.IsFlyAppsInstance() && m.IsActive() {
if m.IsFlyAppsPlatform() && m.IsActive() && !m.IsFlyAppsReleaseCommand() {
machines = append(machines, m)
} else if m.IsFlyAppsReleaseCommand() {
releaseCmdMachine = m
Expand Down
65 changes: 65 additions & 0 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ type Config struct {
PrimaryRegion string `toml:"primary_region,omitempty"`
Checks map[string]*Check `toml:"checks,omitempty"`
Mounts *scanner.Volume `toml:"mounts,omitempty"`
Processes map[string]string `toml:"processes,omitempty"`
platformVersion string
}

Expand All @@ -111,6 +112,7 @@ type Service struct {
Concurrency *api.MachineServiceConcurrency `json:"concurrency,omitempty" toml:"concurrency"`
TcpChecks []*TcpCheck `json:"tcp_checks,omitempty" toml:"tcp_checks,omitempty"`
HttpChecks []*HttpCheck `json:"http_checks,omitempty" toml:"http_checks,omitempty"`
Processes []string `json:"processes,omitempty" toml:"processes,omitempty"`
}

func (hs *HttpService) ToMachineService() *api.MachineService {
Expand Down Expand Up @@ -821,3 +823,66 @@ func (c *Config) SetStatics(statics []scanner.Static) {
func (c *Config) SetVolumes(volumes []scanner.Volume) {
c.Definition["mounts"] = volumes
}

type CmdAndMachineServices struct {
Cmd []string
MachineServices []api.MachineService
}

func (c *Config) GetProcessNamesToCmdAndService() (map[string]CmdAndMachineServices, error) {
res := make(map[string]CmdAndMachineServices)
processCount := 0
if c.Processes != nil {
processCount = len(c.Processes)
}
defaultProcessName := ""
firstProcessNameOrDefault := ""
if processCount == 1 {
for procName := range c.Processes {
firstProcessNameOrDefault = procName
break
}
}
if processCount > 0 {
for procName := range c.Processes {
res[procName] = CmdAndMachineServices{
Cmd: strings.Split(c.Processes[procName], " "),
MachineServices: make([]api.MachineService, 0),
}
}
} else {
res[defaultProcessName] = CmdAndMachineServices{
Cmd: strings.Split(c.Processes[defaultProcessName], " "),
MachineServices: make([]api.MachineService, 0),
}
}
if c.HttpService != nil {
if processCount > 1 {
return nil, fmt.Errorf("http_service is not supported when more than one processes are defined for an app, and this app has %d processes", processCount)
}
servicesToUpdate := res[firstProcessNameOrDefault]
servicesToUpdate.MachineServices = append(servicesToUpdate.MachineServices, *c.HttpService.ToMachineService())
res[firstProcessNameOrDefault] = servicesToUpdate
}
for _, service := range c.Services {
if len(service.Processes) == 0 && processCount > 1 {
return nil, fmt.Errorf("error service has no processes set and app has %d processes defined; update fly.toml to set processes for each service", processCount)
} else if len(service.Processes) > 1 && processCount == 0 {
return nil, fmt.Errorf("error services has %d processes defined, but no processes are defined in app config; add a [processes] section to fly.toml", processCount)
} else if len(service.Processes) == 0 {
servicesToUpdate := res[firstProcessNameOrDefault]
servicesToUpdate.MachineServices = append(servicesToUpdate.MachineServices, *service.ToMachineService())
res[firstProcessNameOrDefault] = servicesToUpdate
} else { // len(service.Processes) > 1
for _, processName := range service.Processes {
if _, present := res[processName]; !present {
return nil, fmt.Errorf("error service specifies '%s' as one of its processes, but no processes are defined with that name; update fly.toml [processes] to include a %s process", processName, processName)
}
servicesToUpdate := res[processName]
servicesToUpdate.MachineServices = append(servicesToUpdate.MachineServices, *service.ToMachineService())
res[processName] = servicesToUpdate
}
}
}
return res, nil
}

0 comments on commit 9bd4fa7

Please sign in to comment.