Skip to content

Commit e5c3ee0

Browse files
authored
feat(deploy): warn when volume changes will replace machine (#4726)
1 parent 7f0ff1f commit e5c3ee0

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

internal/command/deploy/machines.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/superfly/flyctl/internal/flapsutil"
2424
"github.com/superfly/flyctl/internal/flyutil"
2525
"github.com/superfly/flyctl/internal/machine"
26+
"github.com/superfly/flyctl/internal/prompt"
2627
"github.com/superfly/flyctl/internal/tracing"
2728
"github.com/superfly/flyctl/internal/uiex"
2829
"github.com/superfly/flyctl/internal/uiexutil"
@@ -311,7 +312,7 @@ func NewMachineDeployment(ctx context.Context, args MachineDeploymentArgs) (_ Ma
311312
}
312313

313314
// validations must happen after every else
314-
if err := md.validateVolumeConfig(); err != nil {
315+
if err := md.validateVolumeConfig(ctx); err != nil {
315316
tracing.RecordError(span, err, "failed to validate volume config")
316317
return nil, err
317318
}
@@ -465,7 +466,7 @@ func (md *machineDeployment) popVolumeFor(name, region string) *fly.Volume {
465466
return nil
466467
}
467468

468-
func (md *machineDeployment) validateVolumeConfig() error {
469+
func (md *machineDeployment) validateVolumeConfig(ctx context.Context) error {
469470
machineGroups := lo.GroupBy(
470471
lo.Map(md.machineSet.GetMachines(), func(lm machine.LeasableMachine, _ int) *fly.Machine {
471472
return lm.Machine()
@@ -494,13 +495,23 @@ func (md *machineDeployment) validateVolumeConfig() error {
494495
for _, m := range ms {
495496
mConfig := m.GetConfig()
496497
if mntDst == "" && len(mConfig.Mounts) != 0 {
497-
// TODO: Detaching a volume from a machine is possible, but it usually means a missconfiguration.
498-
// We should show a warning and ask the user for confirmation and let it happen instead of failing here.
499-
return fmt.Errorf(
500-
"machine %s [%s] has a volume mounted but app config does not specify a volume; "+
501-
"remove the volume from the machine or add a [mounts] section to fly.toml",
502-
m.ID, groupName,
503-
)
498+
msg := fmt.Sprintf("Warning! machine %s [%s] has a volume mounted but app config does not specify a volume.\nThis usually indicates a misconfiguration.", m.ID, groupName)
499+
fmt.Fprintln(md.io.ErrOut, md.colorize.Red(msg))
500+
501+
switch confirmed, err := prompt.Confirm(ctx, "Do you still want to continue and detach the volume? This will replace the machine."); {
502+
case err == nil:
503+
if !confirmed {
504+
return fmt.Errorf(
505+
"deployment cancelled: machine %s [%s] has a volume mounted but app config does not specify a volume; "+
506+
"remove the volume from the machine or add a [mounts] section to fly.toml",
507+
m.ID, groupName,
508+
)
509+
}
510+
case prompt.IsNonInteractive(err):
511+
return prompt.NonInteractiveError("yes flag must be specified when not running interactively")
512+
default:
513+
return err
514+
}
504515
}
505516

506517
if mntDst != "" && len(mConfig.Mounts) == 0 {

internal/command/deploy/machines_launchinput.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ func (md *machineDeployment) launchInputForUpdate(origMachineRaw *fly.Machine) (
152152
case len(mMounts) == 0:
153153
// The mounts section was removed from fly.toml
154154
machineShouldBeReplaced = true
155-
terminal.Warnf("Machine %s has a volume attached but fly.toml doesn't have a [mounts] section\n", mID)
156155
case oMounts[0].Name == "":
157156
// It's rare but can happen, we don't know the mounted volume name
158157
// so can't be sure it matches the mounts defined in fly.toml, in this

0 commit comments

Comments
 (0)