You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.6 KiB

  1. package config
  2. import (
  3. "gerrit.wikimedia.org/r/blubber/build"
  4. )
  5. // RunsConfig holds configuration fields related to the application's
  6. // runtime environment.
  7. //
  8. type RunsConfig struct {
  9. UserConfig `json:",inline"`
  10. Insecurely Flag `json:"insecurely"` // runs user owns application files
  11. Environment map[string]string `json:"environment" validate:"envvars"` // environment variables
  12. }
  13. // Merge takes another RunsConfig and overwrites this struct's fields. All
  14. // fields except Environment are overwritten if set. The latter is an additive
  15. // merge.
  16. //
  17. func (run *RunsConfig) Merge(run2 RunsConfig) {
  18. run.UserConfig.Merge(run2.UserConfig)
  19. run.Insecurely.Merge(run2.Insecurely)
  20. if run.Environment == nil {
  21. run.Environment = make(map[string]string)
  22. }
  23. for name, value := range run2.Environment {
  24. run.Environment[name] = value
  25. }
  26. }
  27. // InstructionsForPhase injects build instructions related to the runtime
  28. // configuration.
  29. //
  30. // PhasePrivileged
  31. //
  32. // Creates LocalLibPrefix directory and unprivileged user home directory,
  33. // creates the unprivileged user and its group, and sets up directory
  34. // permissions.
  35. //
  36. // PhasePrivilegeDropped
  37. //
  38. // Injects build.Env instructions for all names/values defined by
  39. // RunsConfig.Environment.
  40. //
  41. func (run RunsConfig) InstructionsForPhase(phase build.Phase) []build.Instruction {
  42. switch phase {
  43. case build.PhasePrivileged:
  44. return []build.Instruction{build.RunAll{
  45. build.CreateUser(run.As, run.UID, run.GID),
  46. }}
  47. case build.PhasePrivilegeDropped:
  48. if len(run.Environment) > 0 {
  49. return []build.Instruction{
  50. build.Env{run.Environment},
  51. }
  52. }
  53. }
  54. return []build.Instruction{}
  55. }