Class: KuberKit::Actions::ConfigurationLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/kuber_kit/actions/configuration_loader.rb

Constant Summary collapse

APP_CONFIG_FILENAME =
"config.rb".freeze

Instance Method Summary collapse

Instance Method Details

#call(options) ⇒ Object



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
46
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
# File 'lib/kuber_kit/actions/configuration_loader.rb', line 17

def call(options)
  root_path     = options[:path] || File.join(Dir.pwd, configs.kuber_kit_dirname)

  # require config file first, in case if other dirs are overriden in config
  config_file_path = File.join(root_path, APP_CONFIG_FILENAME)
  if File.exists?(config_file_path)
    require config_file_path
  end

  images_path   = options[:images_path] || File.join(root_path, configs.images_dirname)
  services_path = options[:services_path] || File.join(root_path, configs.services_dirname)
  infra_path    = options[:infra_path]  || File.join(root_path, configs.infra_dirname)
  configurations_path  = options[:configurations_path]  || File.join(root_path, configs.configurations_dirname)
  configuration_name   = ENV["KUBER_KIT_CONFIGURATION"] || options[:configuration]

  logger.info "Launching kuber_kit with:"
  logger.info "  Root path: #{root_path.to_s.yellow}"
  logger.info "  Images path: #{images_path.to_s.yellow}"
  logger.info "  Services path: #{services_path.to_s.yellow}"
  logger.info "  Infrastructure path: #{infra_path.to_s.yellow}"
  logger.info "  Configurations path: #{configurations_path.to_s.yellow}"
  logger.info "  Configuration name: #{configuration_name.to_s.yellow}"

  unless File.exists?(root_path)
    ui.print_warning "WARNING", "KuberKit root path #{root_path} doesn't exist. You may want to pass it --path parameter."
  end

  if Gem::Version.new(KuberKit::VERSION) < Gem::Version.new(configs.kuber_kit_min_version)
    raise KuberKit::Error, "The minimal required kuber_kit version is #{configs.kuber_kit_min_version}"
  end

  load_configurations(configurations_path, configuration_name)
  load_infrastructure(infra_path)

  ui.create_task("Updating artifacts") do |task|
    artifacts = KuberKit.current_configuration.artifacts.values
    artifacts_updater.update(local_shell, artifacts)
    task.update_title("Updated #{artifacts.count} artifacts")
  end

  ui.create_task("Loading image definitions") do |task|
    files = image_store.load_definitions(images_path)

    configs.additional_images_paths.each do |path|
      files += image_store.load_definitions(path)
    end

    task.update_title("Loaded #{files.count} image definitions")
  end

  ui.create_task("Loading service definitions") do |task|
    files = service_store.load_definitions(services_path)
    task.update_title("Loaded #{files.count} service definitions")
  end
  
  true
rescue KuberKit::Error => e
  ui.print_error("Error", e.message)
  false
end

#load_configurations(configurations_path, configuration_name) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/kuber_kit/actions/configuration_loader.rb', line 78

def load_configurations(configurations_path, configuration_name)
  configuration_store.load_definitions(configurations_path)

  if configuration_store.count.zero?
    configuration_store.define(:_default_)
    configuration_name ||= :_default_
  end

  all_configurations = configuration_store.all_definitions.values
  if configuration_store.count == 1 && configuration_name.nil?
    first_configurations = all_configurations.first
    configuration_name   = first_configurations.configuration_name
  end

  if configuration_store.count > 1 && configuration_name.nil?
    options = all_configurations.map(&:configuration_name).map(&:to_s)
    ui.prompt("Please select configuration name (or set it using -C option)", options) do |selection|
      configuration_name = selection
    end
  end

  KuberKit.set_configuration_name(configuration_name)
end

#load_infrastructure(infra_path) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/kuber_kit/actions/configuration_loader.rb', line 102

def load_infrastructure(infra_path)
  local_shell.recursive_list_files(infra_path).each do |path|
    require(path)
  end
rescue KuberKit::Shell::AbstractShell::DirNotFoundError
  logger.warn("Directory with infrastructure not found: #{infra_path}")
end