2024-03-18 14:41:58 +00:00
package deploy
import (
"context"
"os"
"github.com/databricks/cli/bundle"
2024-03-25 14:18:47 +00:00
"github.com/databricks/cli/libs/diag"
2024-03-18 14:41:58 +00:00
"github.com/databricks/cli/libs/filer"
"github.com/databricks/cli/libs/log"
)
2024-10-02 13:53:24 +00:00
const MaxStateFileSize = 10 * 1024 * 1024 // 10MB
2024-03-18 14:41:58 +00:00
type statePush struct {
filerFactory FilerFactory
}
func ( s * statePush ) Name ( ) string {
return "deploy:state-push"
}
2024-03-25 14:18:47 +00:00
func ( s * statePush ) Apply ( ctx context . Context , b * bundle . Bundle ) diag . Diagnostics {
2024-03-18 14:41:58 +00:00
f , err := s . filerFactory ( b )
if err != nil {
2024-03-25 14:18:47 +00:00
return diag . FromErr ( err )
2024-03-18 14:41:58 +00:00
}
statePath , err := getPathToStateFile ( ctx , b )
if err != nil {
2024-03-25 14:18:47 +00:00
return diag . FromErr ( err )
2024-03-18 14:41:58 +00:00
}
local , err := os . Open ( statePath )
if err != nil {
2024-03-25 14:18:47 +00:00
return diag . FromErr ( err )
2024-03-18 14:41:58 +00:00
}
defer local . Close ( )
2024-10-02 13:53:24 +00:00
if ! b . Config . Bundle . Force {
state , err := local . Stat ( )
if err != nil {
return diag . FromErr ( err )
}
if state . Size ( ) > MaxStateFileSize {
return diag . Errorf ( "Deployment state file size exceeds the maximum allowed size of %d bytes. Please reduce the number of resources in your bundle, split your bundle into multiple or re-run the command with --force flag." , MaxStateFileSize )
}
}
2024-03-18 14:41:58 +00:00
log . Infof ( ctx , "Writing local deployment state file to remote state directory" )
err = f . Write ( ctx , DeploymentStateFileName , local , filer . CreateParentDirectories , filer . OverwriteIfExists )
if err != nil {
2024-03-25 14:18:47 +00:00
return diag . FromErr ( err )
2024-03-18 14:41:58 +00:00
}
return nil
}
// StatePush returns a mutator that pushes the deployment state file to Databricks workspace.
func StatePush ( ) bundle . Mutator {
return & statePush { StateFiler }
}