github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/modules/deploy/instance_context.go (about)

     1  package deploy
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strings"
     7  
     8  	confid "github.com/machinefi/w3bstream/pkg/depends/conf/id"
     9  	"github.com/machinefi/w3bstream/pkg/depends/x/contextx"
    10  	"github.com/machinefi/w3bstream/pkg/errors/status"
    11  	"github.com/machinefi/w3bstream/pkg/models"
    12  	"github.com/machinefi/w3bstream/pkg/modules/config"
    13  	"github.com/machinefi/w3bstream/pkg/modules/metrics"
    14  	"github.com/machinefi/w3bstream/pkg/modules/operator"
    15  	"github.com/machinefi/w3bstream/pkg/modules/projectoperator"
    16  	"github.com/machinefi/w3bstream/pkg/types"
    17  	"github.com/machinefi/w3bstream/pkg/types/wasm"
    18  )
    19  
    20  func WithInstanceRuntimeContext(parent context.Context) (context.Context, error) {
    21  	d := types.MustMgrDBExecutorFromContext(parent)
    22  	ins := types.MustInstanceFromContext(parent)
    23  	app := types.MustAppletFromContext(parent)
    24  
    25  	var (
    26  		prj    *models.Project
    27  		exists bool
    28  	)
    29  
    30  	// completing parent context
    31  	if prj, exists = types.ProjectFromContext(parent); !exists {
    32  		prj = &models.Project{RelProject: models.RelProject{ProjectID: app.ProjectID}}
    33  		if err := prj.FetchByProjectID(d); err != nil {
    34  			return nil, err
    35  		}
    36  		parent = types.WithProject(parent, prj)
    37  	}
    38  	{
    39  		op, err := projectoperator.GetByProject(parent, prj.ProjectID)
    40  		if err != nil && err != status.ProjectOperatorNotFound {
    41  			return nil, err
    42  		}
    43  		if op != nil {
    44  			parent = types.WithProjectOperator(parent, op)
    45  		}
    46  		ops, err := operator.ListByCond(parent, &operator.CondArgs{AccountID: prj.RelAccount.AccountID})
    47  		if err != nil {
    48  			return nil, err
    49  		}
    50  		parent = types.WithOperators(parent, ops)
    51  	}
    52  	apisrv := types.MustWasmApiServerFromContext(parent)
    53  	account := prj.AccountID.String()
    54  	if strings.HasPrefix(prj.Name, "eth_") {
    55  		parts := strings.Split(prj.Name, "_")
    56  		if len(parts) >= 3 {
    57  			account = strings.Join(parts[0:2], "_")
    58  		}
    59  	}
    60  	metric := metrics.NewCustomMetric(account, prj.Name)
    61  	logger := types.MustLoggerFromContext(parent)
    62  	sfid := confid.MustSFIDGeneratorFromContext(parent)
    63  
    64  	// with user defined contexts
    65  	configs, err := config.List(parent, &config.CondArgs{
    66  		RelIDs: []types.SFID{prj.ProjectID, app.AppletID, ins.InstanceID},
    67  	})
    68  
    69  	if err != nil {
    70  		return nil, err
    71  	}
    72  	for _, c := range configs {
    73  		if err = wasm.InitConfiguration(parent, c.Configuration); err != nil {
    74  			return nil, status.ConfigInitFailed.StatusErr().WithDesc(
    75  				fmt.Sprintf("config init failed: [type] %s [err] %v", c.ConfigType(), err),
    76  			)
    77  		}
    78  		parent = c.WithContext(parent)
    79  	}
    80  
    81  	// with context from global configurations
    82  	for _, t := range wasm.ConfigTypes {
    83  		c, _ := wasm.NewGlobalConfigurationByType(t)
    84  		if c == nil {
    85  			continue
    86  		}
    87  		if err = wasm.InitGlobalConfiguration(parent, c); err != nil {
    88  			return nil, status.ConfigInitFailed.StatusErr().WithDesc(
    89  				fmt.Sprintf("global config init failed: [type] %s [err] %v", t, err),
    90  			)
    91  		}
    92  		parent = c.WithContext(parent)
    93  	}
    94  	chainconf := types.MustChainConfigFromContext(parent)
    95  	operators := types.MustOperatorPoolFromContext(parent)
    96  
    97  	return contextx.WithContextCompose(
    98  		types.WithMgrDBExecutorContext(d),
    99  		types.WithWasmApiServerContext(apisrv),
   100  		types.WithLoggerContext(logger),
   101  		wasm.WithCustomMetricsContext(metric),
   102  		confid.WithSFIDGeneratorContext(sfid),
   103  		types.WithProjectContext(prj),
   104  		types.WithAppletContext(app),
   105  		types.WithInstanceContext(ins),
   106  		types.WithChainConfigContext(chainconf),
   107  		types.WithOperatorPoolContext(operators),
   108  	)(parent), nil
   109  }