Skip to content

Commit 2d2c337

Browse files
author
Allison Pierson
authored
migrate to v2 hacks: infer process names when possible (#2837)
* `migrate-to-v2` hacks: add process to services if none are specified * `migrate-to-v2` hacks: infer process name when specified by services
1 parent 5ec9cbf commit 2d2c337

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package migrate_to_v2
2+
3+
import (
4+
"context"
5+
6+
"github.com/samber/lo"
7+
"github.com/superfly/flyctl/api"
8+
)
9+
10+
// This all *should* work, based on the assumption that the existing app was validated and is running.
11+
// Essentially, we shouldn't make things up here, but extrapolate the *only* possible outcome.
12+
13+
func (m *v2PlatformMigrator) applyHacks(ctx context.Context) {
14+
15+
m.hackAddProcessToServices(ctx)
16+
m.hackInferProcessName(ctx)
17+
}
18+
19+
// If there is only one process to the app, we can assume that services are all tied to that one process
20+
// unless they specify another process name
21+
func (m *v2PlatformMigrator) hackAddProcessToServices(ctx context.Context) {
22+
appProcesses := m.appConfig.ProcessNames()
23+
24+
if len(appProcesses) != 1 {
25+
// Apps typically aren't hosting services on multiple process groups, so we can't really
26+
// make a good prediction here.
27+
return
28+
}
29+
30+
for idx := range m.appConfig.Services {
31+
if len(m.appConfig.Services[idx].Processes) == 0 {
32+
m.appConfig.Services[idx].Processes = appProcesses
33+
}
34+
}
35+
if m.appConfig.HTTPService != nil && len(m.appConfig.HTTPService.Processes) == 0 {
36+
m.appConfig.HTTPService.Processes = appProcesses
37+
}
38+
}
39+
40+
func (m *v2PlatformMigrator) hackInferProcessName(ctx context.Context) {
41+
42+
existingNames := m.appConfig.ProcessNames()
43+
if len(existingNames) > 1 || existingNames[0] != api.MachineProcessGroupApp {
44+
return
45+
}
46+
47+
// There's a single process, and it's unnamed. See if we can infer its name from services
48+
var listedNames []string
49+
for _, service := range m.appConfig.AllServices() {
50+
listedNames = append(listedNames, service.Processes...)
51+
}
52+
listedNames = lo.Uniq(listedNames)
53+
54+
if len(listedNames) == 1 {
55+
// There's a single name listed, so we can assume that that's the only process.
56+
m.appConfig.Processes = map[string]string{
57+
listedNames[0]: "", // an empty cmd will inherit the CMD from the Dockerfile, or from Experimental.Cmd
58+
}
59+
}
60+
}

internal/command/migrate_to_v2/migrate_to_v2.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,9 @@ func NewV2PlatformMigrator(ctx context.Context, appName string) (V2PlatformMigra
341341
}
342342
migrator.pgConsulUrl = consul.ConsulURL
343343
}
344+
345+
migrator.applyHacks(ctx)
346+
344347
err = migrator.validate(ctx)
345348
if err != nil {
346349
return nil, err

0 commit comments

Comments
 (0)