Class: Gjallarhorn::Configuration

Inherits:
Object
  • Object
show all
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.

Examples:

Loading configuration

config = Gjallarhorn::Configuration.new('deploy.yml')
environments = config.environments
production_config = config.environment('production')

Accessing environment details

provider = config.provider_for('staging')
services = config.services_for('production')

Since:

  • 0.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file = "deploy.yml") ⇒ Configuration

Initialize a new Configuration instance

Raises:

Since:

  • 0.1.0



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_fileString (readonly)

Returns Path to the configuration file.

Since:

  • 0.1.0



24
25
26
# File 'lib/gjallarhorn/configuration.rb', line 24

def config_file
  @config_file
end

#dataHash (readonly)

Returns Loaded configuration data.

Since:

  • 0.1.0



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

Raises:

Since:

  • 0.1.0



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

#environmentsArray<String>

Get all available environment names

Since:

  • 0.1.0



53
54
55
# File 'lib/gjallarhorn/configuration.rb', line 53

def environments
  @data.keys
end

#load_configurationObject (private)

Since:

  • 0.1.0



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.message}"
end

#provider_for(environment) ⇒ String

Get the provider for a specific environment

Since:

  • 0.1.0



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

Since:

  • 0.1.0



70
71
72
73
# File 'lib/gjallarhorn/configuration.rb', line 70

def services_for(environment)
  env_config = environment(environment)
  env_config["services"] || []
end

#to_yamlString

Convert configuration to YAML string

Since:

  • 0.1.0



78
79
80
# File 'lib/gjallarhorn/configuration.rb', line 78

def to_yaml
  @data.to_yaml
end

#validate_configurationObject (private)

Raises:

Since:

  • 0.1.0



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)

Since:

  • 0.1.0



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)

Raises:

Since:

  • 0.1.0



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)

Raises:

Since:

  • 0.1.0



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)

Raises:

Since:

  • 0.1.0



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