get.porter.sh/porter@v1.3.0/pkg/porter/run.go (about) 1 package porter 2 3 import ( 4 "context" 5 "fmt" 6 "strconv" 7 8 "get.porter.sh/porter/pkg/config" 9 "get.porter.sh/porter/pkg/manifest" 10 "get.porter.sh/porter/pkg/runtime" 11 "get.porter.sh/porter/pkg/schema" 12 "get.porter.sh/porter/pkg/tracing" 13 ) 14 15 type RunOptions struct { 16 config *config.Config 17 18 // Debug specifies if the bundle should be run in debug mode 19 DebugMode bool 20 21 // File is the path to the porter manifest. 22 File string 23 24 // Action name to run in the bundle, such as install. 25 Action string 26 } 27 28 func NewRunOptions(c *config.Config) RunOptions { 29 return RunOptions{ 30 config: c, 31 } 32 } 33 34 func (o *RunOptions) Validate() error { 35 err := o.defaultDebug() 36 if err != nil { 37 return err 38 } 39 40 err = o.validateAction() 41 if err != nil { 42 return err 43 } 44 45 return nil 46 } 47 48 func (o *RunOptions) validateAction() error { 49 if o.Action == "" { 50 o.Action = o.config.Getenv(config.EnvACTION) 51 } 52 53 return nil 54 } 55 56 func (o *RunOptions) defaultDebug() error { 57 // if debug was manually set, leave it 58 if o.DebugMode { 59 return nil 60 } 61 62 rawDebug, set := o.config.LookupEnv(config.EnvDEBUG) 63 if !set { 64 return nil 65 } 66 67 debug, err := strconv.ParseBool(rawDebug) 68 if err != nil { 69 return fmt.Errorf("invalid PORTER_DEBUG, expected a bool (true/false) but got %s: %w", rawDebug, err) 70 } 71 72 if debug { 73 o.DebugMode = debug 74 } 75 76 return nil 77 } 78 79 func (p *Porter) Run(ctx context.Context, opts RunOptions) error { 80 ctx, span := tracing.StartSpan(ctx) 81 defer span.EndSpan() 82 83 // Once the bundle has been built, we shouldn't check the schemaVersion again when running it. 84 // If the author built it with the rules loosened, then it should execute regardless of the version matching. 85 // A warning is printed if it doesn't match. 86 p.Config.Data.SchemaCheck = string(schema.CheckStrategyNone) 87 88 m, err := manifest.LoadManifestFrom(ctx, p.Config, opts.File) 89 if err != nil { 90 return span.Error(err) 91 } 92 93 runtimeCfg := runtime.NewConfigFor(p.Config) 94 runtimeCfg.DebugMode = opts.DebugMode 95 r := runtime.NewPorterRuntime(runtimeCfg, p.Mixins) 96 runtimeManifest := r.NewRuntimeManifest(opts.Action, m) 97 err = r.Execute(ctx, runtimeManifest) 98 if err != nil { 99 return span.Error(err) 100 } 101 102 span.Info("execution completed successfully!") 103 return nil 104 }