|
| 1 | +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 2 | +// or more contributor license agreements. Licensed under the Elastic License; |
| 3 | +// you may not use this file except in compliance with the Elastic License. |
| 4 | + |
| 5 | +package install |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "io/ioutil" |
| 10 | + "path/filepath" |
| 11 | + |
| 12 | + "github.com/pkg/errors" |
| 13 | + "gopkg.in/yaml.v3" |
| 14 | +) |
| 15 | + |
| 16 | +// ApplicationConfiguration represents the configuration of the elastic-package. |
| 17 | +type ApplicationConfiguration struct { |
| 18 | + c configFile |
| 19 | +} |
| 20 | + |
| 21 | +type configFile struct { |
| 22 | + Stack stack `yaml:"stack"` |
| 23 | +} |
| 24 | + |
| 25 | +type stack struct { |
| 26 | + ImageRefOverrides map[string]ImageRefs `yaml:"image_ref_overrides"` |
| 27 | +} |
| 28 | + |
| 29 | +func (s stack) ImageRefOverridesForVersion(version string) ImageRefs { |
| 30 | + refs, ok := s.ImageRefOverrides[version] |
| 31 | + if !ok { |
| 32 | + return ImageRefs{} |
| 33 | + } |
| 34 | + return refs |
| 35 | +} |
| 36 | + |
| 37 | +// ImageRefs stores Docker image references used to create the Elastic stack containers. |
| 38 | +type ImageRefs struct { |
| 39 | + ElasticAgent string `yaml:"elastic-agent"` |
| 40 | + Elasticsearch string `yaml:"elasticsearch"` |
| 41 | + Kibana string `yaml:"kibana"` |
| 42 | +} |
| 43 | + |
| 44 | +// AsEnv method returns key=value representation of image refs. |
| 45 | +func (ir ImageRefs) AsEnv() []string { |
| 46 | + var vars []string |
| 47 | + vars = append(vars, "ELASTIC_AGENT_IMAGE_REF="+ir.ElasticAgent) |
| 48 | + vars = append(vars, "ELASTICSEARCH_IMAGE_REF="+ir.Elasticsearch) |
| 49 | + vars = append(vars, "KIBANA_IMAGE_REF="+ir.Kibana) |
| 50 | + return vars |
| 51 | +} |
| 52 | + |
| 53 | +// DefaultStackImageRefs function selects the appropriate set of Docker image references for the default stack version. |
| 54 | +func (ac *ApplicationConfiguration) DefaultStackImageRefs() ImageRefs { |
| 55 | + return ac.StackImageRefs(DefaultStackVersion) |
| 56 | +} |
| 57 | + |
| 58 | +// StackImageRefs function selects the appropriate set of Docker image references for the given stack version. |
| 59 | +func (ac *ApplicationConfiguration) StackImageRefs(version string) ImageRefs { |
| 60 | + refs := ac.c.Stack.ImageRefOverridesForVersion(version) |
| 61 | + refs.ElasticAgent = stringOrDefault(refs.ElasticAgent, fmt.Sprintf("%s:%s", elasticAgentImageName, version)) |
| 62 | + refs.Elasticsearch = stringOrDefault(refs.Elasticsearch, fmt.Sprintf("%s:%s", elasticsearchImageName, version)) |
| 63 | + refs.Kibana = stringOrDefault(refs.Kibana, fmt.Sprintf("%s:%s", kibanaImageName, version)) |
| 64 | + return refs |
| 65 | +} |
| 66 | + |
| 67 | +// Configuration function returns the elastic-package configuration. |
| 68 | +func Configuration() (*ApplicationConfiguration, error) { |
| 69 | + configPath, err := configurationDir() |
| 70 | + if err != nil { |
| 71 | + return nil, errors.Wrap(err, "can't read configuration directory") |
| 72 | + } |
| 73 | + |
| 74 | + cfg, err := ioutil.ReadFile(filepath.Join(configPath, applicationConfigurationYmlFile)) |
| 75 | + if err != nil { |
| 76 | + return nil, errors.Wrap(err, "can't read configuration file") |
| 77 | + } |
| 78 | + |
| 79 | + var c configFile |
| 80 | + err = yaml.Unmarshal(cfg, &c) |
| 81 | + if err != nil { |
| 82 | + return nil, errors.Wrap(err, "can't unmarshal configuration file") |
| 83 | + } |
| 84 | + |
| 85 | + return &ApplicationConfiguration{ |
| 86 | + c: c, |
| 87 | + }, nil |
| 88 | +} |
| 89 | + |
| 90 | +func stringOrDefault(value string, defaultValue string) string { |
| 91 | + if value == "" { |
| 92 | + return defaultValue |
| 93 | + } |
| 94 | + return value |
| 95 | +} |
0 commit comments