|
| 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 | +} |
0 commit comments