|
| 1 | +// Copyright Axis Communications AB. |
| 2 | +// |
| 3 | +// For a full list of individual contributors, please see the commit history. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | +package config |
| 17 | + |
| 18 | +import ( |
| 19 | + "flag" |
| 20 | + "os" |
| 21 | + "time" |
| 22 | + |
| 23 | + "github.com/sirupsen/logrus" |
| 24 | +) |
| 25 | + |
| 26 | +type ExecutionSpaceConfig interface { |
| 27 | + Config |
| 28 | + Hostname() string |
| 29 | + Timeout() time.Duration |
| 30 | + ExecutionSpaceWaitTimeout() time.Duration |
| 31 | + RabbitMQHookURL() string |
| 32 | + RabbitMQHookExchangeName() string |
| 33 | + EiffelGoerURL() string |
| 34 | +} |
| 35 | + |
| 36 | +// executionSpaceCfg implements the ExecutionSpaceConfig interface. |
| 37 | +type executionSpaceCfg struct { |
| 38 | + Config |
| 39 | + stripPrefix string |
| 40 | + hostname string |
| 41 | + timeout time.Duration |
| 42 | + executionSpaceWaitTimeout time.Duration |
| 43 | + rabbitmqHookURL string |
| 44 | + rabbitmqHookExchange string |
| 45 | + eiffelGoerURL string |
| 46 | +} |
| 47 | + |
| 48 | +// NewExecutionSpaceConfig creates an executio nspace config interface based on input parameters or environment variables. |
| 49 | +func NewExecutionSpaceConfig() ExecutionSpaceConfig { |
| 50 | + var conf executionSpaceCfg |
| 51 | + |
| 52 | + defaultTimeout, err := time.ParseDuration(EnvOrDefault("REQUEST_TIMEOUT", "1m")) |
| 53 | + if err != nil { |
| 54 | + logrus.Panic(err) |
| 55 | + } |
| 56 | + |
| 57 | + executionSpaceWaitTimeout, err := time.ParseDuration(EnvOrDefault("EXECUTION_SPACE_WAIT_TIMEOUT", "1h")) |
| 58 | + if err != nil { |
| 59 | + logrus.Panic(err) |
| 60 | + } |
| 61 | + |
| 62 | + flag.StringVar(&conf.hostname, "hostname", EnvOrDefault("PROVIDER_HOSTNAME", "http://localhost"), "Host to supply to ESR for starting executors") |
| 63 | + flag.DurationVar(&conf.timeout, "timeout", defaultTimeout, "Maximum timeout for requests to Execution space provider Service.") |
| 64 | + flag.DurationVar(&conf.executionSpaceWaitTimeout, "executionSpaceWaitTimeout", executionSpaceWaitTimeout, "Timeout duration to wait when trying to checkout execution space(s)") |
| 65 | + flag.StringVar(&conf.rabbitmqHookURL, "rabbitmq_hook_url", os.Getenv("ETOS_RABBITMQ_URL"), "URL to the ETOS rabbitmq for logs") |
| 66 | + flag.StringVar(&conf.rabbitmqHookExchange, "rabbitmq_hook_exchange", os.Getenv("ETOS_RABBITMQ_EXCHANGE"), "Exchange to use for the ETOS rabbitmq for logs") |
| 67 | + flag.StringVar(&conf.eiffelGoerURL, "event_repository_host", os.Getenv("EIFFEL_GOER_URL"), "Event repository URL used for Eiffel event lookup") |
| 68 | + base := load() |
| 69 | + flag.Parse() |
| 70 | + conf.Config = base |
| 71 | + |
| 72 | + return &conf |
| 73 | +} |
| 74 | + |
| 75 | +// Hostname returns the hostname to use for executors. |
| 76 | +func (c *executionSpaceCfg) Hostname() string { |
| 77 | + return c.hostname |
| 78 | +} |
| 79 | + |
| 80 | +// Timeout returns the request timeout for Execution space provider Service API. |
| 81 | +func (c *executionSpaceCfg) Timeout() time.Duration { |
| 82 | + return c.timeout |
| 83 | +} |
| 84 | + |
| 85 | +// ExecutionSpaceWaitTimeout returns the timeout for checking out execution spaces. |
| 86 | +func (c *executionSpaceCfg) ExecutionSpaceWaitTimeout() time.Duration { |
| 87 | + return c.executionSpaceWaitTimeout |
| 88 | +} |
| 89 | + |
| 90 | +// RabbitMQHookURL returns the rabbitmq url for ETOS logs |
| 91 | +func (c *executionSpaceCfg) RabbitMQHookURL() string { |
| 92 | + return c.rabbitmqHookURL |
| 93 | +} |
| 94 | + |
| 95 | +// EiffelGoerURL returns the Eiffel event repository used for event lookups |
| 96 | +func (c *executionSpaceCfg) EiffelGoerURL() string { |
| 97 | + return c.eiffelGoerURL |
| 98 | +} |
| 99 | + |
| 100 | +// RabbitMQHookExchangeName returns the rabbitmq exchange name used for ETOS logs |
| 101 | +func (c *executionSpaceCfg) RabbitMQHookExchangeName() string { |
| 102 | + return c.rabbitmqHookExchange |
| 103 | +} |
0 commit comments