Class: VagrantPlugins::StartCloud::ConfigBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-startcloud/config_builder.rb

Class Method Summary collapse

Class Method Details

.load_config(config_path) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/vagrant-startcloud/config_builder.rb', line 9

def load_config(config_path)
  require 'yaml'
  logger = Log4r::Logger.new('vagrant_startcloud::config_builder')
  config = Config.new

  if File.exist?(config_path)
    logger.debug("Loading configuration from #{config_path}")
    yaml_config = YAML.load_file(config_path)

    if yaml_config && yaml_config['hosts']&.first
      host = yaml_config['hosts'].first
      config.settings = host['settings'] || {}
      config.networks = host['networks'] || []
      config.disks = host['disks'] || {}
      config.provisioning = host['provisioning'] || {}
      config.folders = host['folders'] || []
      config.roles = host['roles'] || []
      config.vars = host['vars'] || {}
      config.plugins = host['plugins'] || {}
      config.zones = host['zones'] || {}
      config.config_path = config_path
      logger.info('Configuration loaded successfully')
    else
      logger.warn('No hosts defined in configuration')
      raise Errors::MissingHosts
    end
  else
    logger.error("Configuration file not found: #{config_path}")
    raise Errors::ConfigurationFileNotFound, path: config_path
  end

  config
rescue StandardError => e
  logger.error("Error loading configuration: #{e.message}")
  logger.error(e.backtrace.join("\n"))
  raise Errors::InvalidConfigurationFormat, error: e.message
end

.validate_config(config) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/vagrant-startcloud/config_builder.rb', line 47

def validate_config(config)
  logger = Log4r::Logger.new('vagrant_startcloud::config_builder')
  errors = []

  if config.settings.empty?
    logger.error('No settings defined in configuration')
    errors << I18n.t('vagrant_startcloud.config.no_settings')
  else
    required = %w[hostname domain server_id box]
    missing = required.reject { |key| config.settings.key?(key) && !config.settings[key].to_s.empty? }
    if missing.any?
      logger.error("Missing required settings: #{missing.join(', ')}")
      errors << I18n.t('vagrant_startcloud.config.missing_required', fields: missing.join(', '))
    end
  end

  if config.networks&.any?
    config.networks.each_with_index do |network, index|
      unless network['type']
        logger.error("Network type required for network at index #{index}")
        errors << I18n.t('vagrant_startcloud.config.network_type_required', index: index)
      end
    end
  end

  errors << I18n.t('vagrant_startcloud.config.boot_disk_size_required') if config.disks&.any? && config.disks['boot'] && !config.disks['boot']['size']

  if config.folders&.any?
    config.folders.each_with_index do |folder, index|
      unless folder['map'] && folder['to']
        logger.error("Both 'map' and 'to' paths are required for folder at index #{index}")
        errors << I18n.t('vagrant_startcloud.config.folder_paths_required', index: index)
      end
    end
  end

  errors
end