Class: Gjallarhorn::Configuration
- Inherits:
-
Object
- Object
- Gjallarhorn::Configuration
- Defined in:
- lib/gjallarhorn/configuration.rb
Overview
Configuration management class for loading and validating deployment configurations
The Configuration class handles loading YAML configuration files that define deployment environments, their providers, and associated services. It provides validation to ensure all required fields are present and valid.
Instance Attribute Summary collapse
-
#config_file ⇒ String
readonly
Path to the configuration file.
-
#data ⇒ Hash
readonly
Loaded configuration data.
Instance Method Summary collapse
-
#environment(name) ⇒ Hash
Get configuration for a specific environment.
-
#environments ⇒ Array<String>
Get all available environment names.
-
#initialize(config_file = "deploy.yml") ⇒ Configuration
constructor
Initialize a new Configuration instance.
- #load_configuration ⇒ Object private
-
#provider_for(environment) ⇒ String
Get the provider for a specific environment.
-
#services_for(environment) ⇒ Array<Hash>
Get the services configuration for a specific environment.
-
#to_yaml ⇒ String
Convert configuration to YAML string.
- #validate_configuration ⇒ Object private
- #validate_environment(env_name, env_config) ⇒ Object private
- #validate_environment_structure(env_name, env_config) ⇒ Object private
- #validate_provider_field(env_name, env_config) ⇒ Object private
- #validate_provider_value(env_name, provider) ⇒ Object private
Constructor Details
#initialize(config_file = "deploy.yml") ⇒ Configuration
Initialize a new Configuration instance
33 34 35 36 |
# File 'lib/gjallarhorn/configuration.rb', line 33 def initialize(config_file = "deploy.yml") @config_file = config_file load_configuration end |
Instance Attribute Details
#config_file ⇒ String (readonly)
Returns Path to the configuration file.
24 25 26 |
# File 'lib/gjallarhorn/configuration.rb', line 24 def config_file @config_file end |
#data ⇒ Hash (readonly)
Returns Loaded configuration data.
27 28 29 |
# File 'lib/gjallarhorn/configuration.rb', line 27 def data @data end |
Instance Method Details
#environment(name) ⇒ Hash
Get configuration for a specific environment
43 44 45 46 47 48 |
# File 'lib/gjallarhorn/configuration.rb', line 43 def environment(name) env_config = @data[name.to_s] raise ConfigurationError, "Environment '#{name}' not found in #{config_file}" unless env_config env_config end |
#environments ⇒ Array<String>
Get all available environment names
53 54 55 |
# File 'lib/gjallarhorn/configuration.rb', line 53 def environments @data.keys end |
#load_configuration ⇒ Object (private)
84 85 86 87 88 89 90 91 |
# File 'lib/gjallarhorn/configuration.rb', line 84 def load_configuration raise ConfigurationError, "Configuration file '#{config_file}' not found" unless File.exist?(config_file) @data = YAML.load_file(config_file) validate_configuration rescue Psych::SyntaxError => e raise ConfigurationError, "Invalid YAML in #{config_file}: #{e.}" end |
#provider_for(environment) ⇒ String
Get the provider for a specific environment
61 62 63 64 |
# File 'lib/gjallarhorn/configuration.rb', line 61 def provider_for(environment) env_config = environment(environment) env_config["provider"] end |
#services_for(environment) ⇒ Array<Hash>
Get the services configuration for a specific environment
70 71 72 73 |
# File 'lib/gjallarhorn/configuration.rb', line 70 def services_for(environment) env_config = environment(environment) env_config["services"] || [] end |
#to_yaml ⇒ String
Convert configuration to YAML string
78 79 80 |
# File 'lib/gjallarhorn/configuration.rb', line 78 def to_yaml @data.to_yaml end |
#validate_configuration ⇒ Object (private)
93 94 95 96 97 98 99 |
# File 'lib/gjallarhorn/configuration.rb', line 93 def validate_configuration raise ConfigurationError, "Configuration file is empty" if @data.nil? || @data.empty? @data.each do |env_name, env_config| validate_environment(env_name, env_config) end end |
#validate_environment(env_name, env_config) ⇒ Object (private)
101 102 103 104 105 |
# File 'lib/gjallarhorn/configuration.rb', line 101 def validate_environment(env_name, env_config) validate_environment_structure(env_name, env_config) validate_provider_field(env_name, env_config) validate_provider_value(env_name, env_config["provider"]) end |
#validate_environment_structure(env_name, env_config) ⇒ Object (private)
107 108 109 110 111 |
# File 'lib/gjallarhorn/configuration.rb', line 107 def validate_environment_structure(env_name, env_config) return if env_config.is_a?(Hash) raise ConfigurationError, "Environment '#{env_name}' is not a hash" end |
#validate_provider_field(env_name, env_config) ⇒ Object (private)
113 114 115 116 117 118 |
# File 'lib/gjallarhorn/configuration.rb', line 113 def validate_provider_field(env_name, env_config) return if env_config["provider"] raise ConfigurationError, "Environment '#{env_name}' missing required 'provider' field" end |
#validate_provider_value(env_name, provider) ⇒ Object (private)
120 121 122 123 124 125 126 127 |
# File 'lib/gjallarhorn/configuration.rb', line 120 def validate_provider_value(env_name, provider) valid_providers = %w[aws gcp azure docker kubernetes] return if valid_providers.include?(provider) valid_list = valid_providers.join(", ") raise ConfigurationError, "Invalid provider '#{provider}' for environment '#{env_name}'. Must be one of: #{valid_list}" end |