Skip to content

Commit

Permalink
* Remove machine leasing for updates on deploy, since leases prevent …
Browse files Browse the repository at this point in the history
…direct updates

* Add Vm size configuration to fly.toml
* Rename 'version' to 'platform_version' in fly.toml to avoid confusion with release versions
  • Loading branch information
jsierles committed Jun 26, 2022
1 parent 5cabbf3 commit 98ce2d2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 35 deletions.
25 changes: 15 additions & 10 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,24 @@ func LoadConfig(path string) (cfg *Config, err error) {

// Config wraps the properties of app configuration.
type Config struct {
AppName string `toml:"app,omitempty"`
Build *Build `toml:"build,omitempty"`
Version int `toml:"version,omitempty"`
Count int `toml:"count,omitempty"`
PrimaryRegion string `toml:"primary_region,omitempty"`
HttpService *HttpService `toml:"http_service,omitempty"`
Definition map[string]interface{} `toml:"definition,omitempty"`
Path string `toml:"path,omitempty"`
AppName string `toml:"app,omitempty"`
Build *Build `toml:"build,omitempty"`
PlatformVersion int `toml:"platform_version,omitempty"`
PrimaryRegion string `toml:"primary_region,omitempty"`
HttpService *HttpService `toml:"http_service,omitempty"`
VM *VM `toml:"vm,omitempty"`
Definition map[string]interface{} `toml:"definition,omitempty"`
Path string `toml:"path,omitempty"`
}
type HttpService struct {
InternalPort int `json:"internal_port" toml:"internal_port" validate:"required,numeric"`
ForceHttps bool `toml:"force_https"`
}
type VM struct {
CpuCount int `toml:"cpu_count,omitempty"`
Memory int `toml:"memory,omitempty"`
}

type Build struct {
Builder string
Args map[string]string
Expand Down Expand Up @@ -127,7 +132,7 @@ func (c *Config) unmarshalTOML(file *os.File) (err error) {
// Config version 2 is for machines apps, with explicit structs for the whole config.
// Config version 1 is for nomad apps, for which most values are unmarshalled differently.
if _, err = toml.NewDecoder(file).Decode(&c); err == nil {
if c.Version < MachinesVersion {
if c.PlatformVersion < MachinesVersion {
file.Seek(0, io.SeekStart)
_, err = toml.NewDecoder(file).Decode(&data)
if err != nil {
Expand Down Expand Up @@ -218,7 +223,7 @@ func (c *Config) marshalTOML(w io.Writer) error {
fmt.Fprintf(w, "# fly.toml file generated for %s on %s\n\n", c.AppName, time.Now().Format(time.RFC3339))

// For machines apps, encode and write directly, bypassing custom marshalling
if c.Version > 1 {
if c.PlatformVersion > NomadVersion {
encoder.Encode(&c)
_, err := b.WriteTo(w)
return err
Expand Down
2 changes: 1 addition & 1 deletion internal/command/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func DeployWithConfig(ctx context.Context, appConfig *app.Config) (err error) {
var release *api.Release
var releaseCommand *api.ReleaseCommand

if appConfig.Version >= app.MachinesVersion {
if appConfig.PlatformVersion >= app.MachinesVersion {
return createMachinesRelease(ctx, appConfig, img)
} else {
release, releaseCommand, err = createRelease(ctx, appConfig, img)
Expand Down
41 changes: 19 additions & 22 deletions internal/command/deploy/machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ func createMachinesRelease(ctx context.Context, config *app.Config, img *imgsrc.
},
}
}
machineGuest := &api.MachineGuest{
CPUs: 1,
CPUKind: "shared",
MemoryMB: 256,
}

if config.VM != nil {
if config.VM.CpuCount > 0 {
machineGuest.CPUs = config.VM.CpuCount
}
if config.VM.Memory > 0 {
machineGuest.MemoryMB = config.VM.Memory
}
}

machineConfig.Guest = machineGuest

err = config.Validate()

Expand All @@ -81,35 +97,16 @@ func createMachinesRelease(ctx context.Context, config *app.Config, img *imgsrc.
}

if len(machines) > 0 {
ttl := api.IntPointer(40)

for _, machine := range machines {
fmt.Fprintf(io.Out, "Leasing VM %s with TTL %d\n", machine.ID, ttl)
lease, err := flapsClient.GetLease(ctx, machine.ID, ttl)

if err != nil {
return err
}

machine.LeaseNonce = lease.Data.Nonce
}

for _, machine := range machines {

fmt.Fprintf(io.Out, "Updating VM %s\n", machine.ID)
launchInput.ID = machine.ID
flapsClient.Update(ctx, launchInput)

}

for _, machine := range machines {
fmt.Fprintf(io.Out, "Releasing VM %s with nonce %s\n", machine.ID, machine.LeaseNonce)

err = flapsClient.ReleaseLease(ctx, machine.ID, machine.LeaseNonce)

_, err = flapsClient.Update(ctx, launchInput)
if err != nil {
fmt.Fprintf(io.Out, "Could not release lease %s on machine %s. Error: %s, Continuing.", machine.LeaseNonce, machine.ID, err)
return err
}

}

fmt.Fprintln(io.Out)
Expand Down
9 changes: 7 additions & 2 deletions internal/command/machine/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,19 @@ func run(ctx context.Context) (err error) {

// TODO: Handle imported fly.toml config

// Setup new fly.toml config file
// Setup new fly.toml config file with default values

appConfig := app.NewConfig()

// Config version 2 is for machine apps
appConfig.Version = app.MachinesVersion
appConfig.PlatformVersion = app.MachinesVersion
appConfig.AppName = createdApp.Name

appConfig.VM = &app.VM{
CpuCount: 1,
Memory: 256,
}

// Launch in the specified region, or when not specified, in the nearest region
regionCode := flag.GetString(ctx, "region")

Expand Down

0 comments on commit 98ce2d2

Please sign in to comment.