github.com/juju/juju@v0.0.0-20240327075706-a90865de2538/worker/meterstatus/context.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package meterstatus
     5  
     6  import (
     7  	"fmt"
     8  	"math/rand"
     9  	"path"
    10  
    11  	"github.com/juju/loggo"
    12  
    13  	"github.com/juju/juju/caas"
    14  	"github.com/juju/juju/core/model"
    15  	"github.com/juju/juju/worker/uniter/runner/context"
    16  	"github.com/juju/juju/worker/uniter/runner/jujuc"
    17  )
    18  
    19  type limitedContext struct {
    20  	jujuc.RestrictedContext
    21  
    22  	env map[string]string
    23  
    24  	id     string
    25  	config hookConfig
    26  }
    27  
    28  type hookConfig struct {
    29  	unitName string
    30  	clock    Clock
    31  	logger   Logger
    32  }
    33  
    34  // newLimitedContext creates a new context that implements just the bare minimum
    35  // of the hooks.Context interface.
    36  func newLimitedContext(config hookConfig) *limitedContext {
    37  	now := config.clock.Now().Unix()
    38  	id := fmt.Sprintf("%s-%s-%d", config.unitName, "meter-status", rand.New(rand.NewSource(now)).Int63())
    39  	return &limitedContext{id: id, config: config}
    40  }
    41  
    42  // HookVars implements runner.Context.
    43  func (ctx *limitedContext) HookVars(
    44  	paths context.Paths,
    45  	remote bool,
    46  	envVars context.Environmenter,
    47  ) ([]string, error) {
    48  	vars := []string{
    49  		"CHARM_DIR=" + paths.GetCharmDir(), // legacy
    50  		"JUJU_CHARM_DIR=" + paths.GetCharmDir(),
    51  		"JUJU_CONTEXT_ID=" + ctx.id,
    52  		"JUJU_AGENT_SOCKET_ADDRESS=" + paths.GetJujucClientSocket(remote).Address,
    53  		"JUJU_AGENT_SOCKET_NETWORK=" + paths.GetJujucClientSocket(remote).Network,
    54  		"JUJU_UNIT_NAME=" + ctx.config.unitName,
    55  	}
    56  	if remote {
    57  		vars = append(vars,
    58  			"JUJU_AGENT_CA_CERT="+path.Join(paths.GetBaseDir(), caas.CACertFile),
    59  		)
    60  	}
    61  	for key, val := range ctx.env {
    62  		vars = append(vars, fmt.Sprintf("%s=%s", key, val))
    63  	}
    64  	return append(vars, context.OSDependentEnvVars(paths, envVars)...), nil
    65  }
    66  
    67  // GetLogger returns the logger for the specified module.
    68  func (ctx *limitedContext) GetLogger(module string) loggo.Logger {
    69  	return ctx.config.logger.Root().Child(module)
    70  }
    71  
    72  // SetEnvVars sets additional environment variables to be exported by the context.
    73  func (ctx *limitedContext) SetEnvVars(vars map[string]string) {
    74  	if ctx.env == nil {
    75  		ctx.env = vars
    76  		return
    77  	}
    78  	for key, val := range vars {
    79  		ctx.env[key] = val
    80  	}
    81  }
    82  
    83  // UnitName implements runner.Context.
    84  func (ctx *limitedContext) UnitName() string {
    85  	return ctx.config.unitName
    86  }
    87  
    88  // ModelType implements runner.Context
    89  func (ctx *limitedContext) ModelType() model.ModelType {
    90  	// Can return IAAS constant because meter status is only used in Uniter.
    91  	// TODO(caas): Required for CAAS support.
    92  	return model.IAAS
    93  }
    94  
    95  // SetProcess implements runner.Context.
    96  func (ctx *limitedContext) SetProcess(process context.HookProcess) {}
    97  
    98  // ActionData implements runner.Context.
    99  func (ctx *limitedContext) ActionData() (*context.ActionData, error) {
   100  	return nil, jujuc.ErrRestrictedContext
   101  }
   102  
   103  // Flush implements runner.Context.
   104  func (ctx *limitedContext) Flush(_ string, err error) error {
   105  	return err
   106  }
   107  
   108  // HasExecutionSetUnitStatus implements runner.Context.
   109  func (ctx *limitedContext) HasExecutionSetUnitStatus() bool { return false }
   110  
   111  // ResetExecutionSetUnitStatus implements runner.Context.
   112  func (ctx *limitedContext) ResetExecutionSetUnitStatus() {}
   113  
   114  // Id implements runner.Context.
   115  func (ctx *limitedContext) Id() string { return ctx.id }
   116  
   117  // Prepare implements runner.Context.
   118  func (ctx *limitedContext) Prepare() error {
   119  	return jujuc.ErrRestrictedContext
   120  }