class Crowbar::Client::Config

General configuration for the Crowbar CLI

Attributes

config[W]
options[W]
values[W]

Protected Class Methods

config() click to toggle source

Define file config

@see config @return [Hashie::Mash] the file config

# File lib/crowbar/client/config.rb, line 374
def config
  instance.config
end
configure(options) click to toggle source

Define base configuration

@see configure @param options [Hash] the base configuration

# File lib/crowbar/client/config.rb, line 344
def configure(options)
  instance.configure(options)
end
defaults() click to toggle source

Define default config

@see defaults @return [Hashie::Mash] the default config

# File lib/crowbar/client/config.rb, line 354
def defaults
  instance.defaults
end
method_missing(method, *arguments, &block) click to toggle source

Magic to catch missing method calls

@param method [Symbol] the method that is missing @param arguments [Array] the list of attributes @yield

Calls superclass method
# File lib/crowbar/client/config.rb, line 395
def method_missing(method, *arguments, &block)
  case
  when method.to_s.ends_with?("=")
    key = method.to_s.gsub(/=\z/, "")

    if values.key?(key)
      values[key] = arguments.first
    else
      super
    end
  when values.key?(method)
    values[method]
  else
    super
  end
end
options() click to toggle source

Define parameter config

@see options @return [Hashie::Mash] the parameter config

# File lib/crowbar/client/config.rb, line 364
def options
  instance.options
end
respond_to?(method, include_private = false) click to toggle source

Magic to catch missing respond_to calls

@param method [Symbol] the method that is missing @param include_private [Bool] should include private methods @return [Bool] the class responds to it or not

Calls superclass method
# File lib/crowbar/client/config.rb, line 419
def respond_to?(method, include_private = false)
  case
  when method.to_s.ends_with?("=")
    key = method.to_s.gsub(/=\z/, "")

    if values.key?(key)
      true
    else
      super
    end
  when values.key?(method)
    true
  else
    super
  end
end
values() click to toggle source

Define merged config

@see values @return [Hashie::Mash] the merged config

# File lib/crowbar/client/config.rb, line 384
def values
  instance.values
end

Public Instance Methods

config() click to toggle source

Define file config

@return [Hashie::Mash] the file config

# File lib/crowbar/client/config.rb, line 83
def config
  @config ||= Hashie::Mash.new
end
configure(options) click to toggle source

Define base configuration

@param options [Hash] the base configuration

# File lib/crowbar/client/config.rb, line 38
def configure(options)
  self.options = Hashie::Mash.new(
    options
  )

  self.config = parser
  self.values = merge
end
defaults() click to toggle source

Define default config

@return [Hashie::Mash] the default config

# File lib/crowbar/client/config.rb, line 52
def defaults
  @defaults ||= Hashie::Mash.new(
    alias: default_alias,
    username: default_username,
    password: default_password,
    server: default_server,
    verify_ssl: default_verify_ssl,
    timeout: default_timeout,
    anonymous: default_anonymous,
    apiversion: default_apiversion,
    experimental: default_experimental,
    upgrade_versions: default_upgrade_versions,
    cloud_version: default_cloud_version,
    debug: default_debug
  )
end
options() click to toggle source

Define parameter config

@return [Hashie::Mash] the parameter config

# File lib/crowbar/client/config.rb, line 74
def options
  @options ||= defaults
end
values() click to toggle source

Define merged config

@return [Hashie::Mash] the merged config

# File lib/crowbar/client/config.rb, line 92
def values
  @values ||= Hashie::Mash.new
end

Protected Instance Methods

default_alias() click to toggle source

Define a default alias value

@return [String] the default alias value

# File lib/crowbar/client/config.rb, line 144
def default_alias
  ENV["CROWBAR_ALIAS"] || "default"
end
default_anonymous() click to toggle source

Define a default anonymous flag

@return [Bool] the default anonymous flag

# File lib/crowbar/client/config.rb, line 208
def default_anonymous
  if ENV["CROWBAR_ANONYMOUS"].present?
    [
      true, 1, "1", "t", "T", "true", "TRUE"
    ].include? ENV["CROWBAR_ANONYMOUS"]
  else
    false
  end
end
default_apiversion() click to toggle source

Define a default api version

@return [Float] the default crowbar api version

# File lib/crowbar/client/config.rb, line 223
def default_apiversion
  if ENV["CROWBAR_APIVERSION"].present?
    ENV["CROWBAR_APIVERSION"].to_f
  else
    Crowbar::Client::Util::ApiVersion.default
  end
end
default_cloud_version() click to toggle source

Define a default cloud version value It is detected based on OS release for local machine or set by user via ENV variable.

@return [String] the default cloud version value

# File lib/crowbar/client/config.rb, line 123
def default_cloud_version
  return ENV["CROWBAR_CLOUD_VERSION"] if ENV["CROWBAR_CLOUD_VERSION"].present?

  os_release = Crowbar::Client::Util::OsRelease.fields
  case os_release["VERSION_ID"]
  when "12.1"
    "6"
  when "12.2"
    "7"
  when "12.3"
    "8"
  else
    "9"
  end
end
default_debug() click to toggle source

Define a default debug flag

@return [String] the default alias flag

# File lib/crowbar/client/config.rb, line 251
def default_debug
  if ENV["CROWBAR_DEBUG"].present?
    [
      true, 1, "1", "t", "T", "true", "TRUE"
    ].include? ENV["CROWBAR_DEBUG"]
  else
    false
  end
end
default_experimental() click to toggle source

Define a experimental api version

@return [String] the default experimental flag

# File lib/crowbar/client/config.rb, line 236
def default_experimental
  if ENV["CROWBAR_EXPERIMENTAL"].present?
    [
      true, 1, "1", "t", "T", "true", "TRUE"
    ].include? ENV["CROWBAR_EXPERIMENTAL"]
  else
    false
  end
end
default_password() click to toggle source

Define a default password value

@return [String] the default password value

# File lib/crowbar/client/config.rb, line 162
def default_password
  ENV["CROWBAR_PASSWORD"] || "crowbar"
end
default_server() click to toggle source

Define a default server value

@return [String] the default server value

# File lib/crowbar/client/config.rb, line 171
def default_server
  ENV["CROWBAR_SERVER"] || "http://127.0.0.1:80"
end
default_timeout() click to toggle source

Define a default timeout value

@return [Integer] the default timeout value

# File lib/crowbar/client/config.rb, line 195
def default_timeout
  if ENV["CROWBAR_TIMEOUT"].present?
    ENV["CROWBAR_TIMEOUT"].to_i
  else
    3600
  end
end
default_upgrade_versions() click to toggle source

Define a default value for which upgrade is being executed User could provide values (“6-to-7” or “7-to-8”) via ENV variable or it's gonna be set based on the system state.

@return [String] the default value for upgrade versions

# File lib/crowbar/client/config.rb, line 105
def default_upgrade_versions
  return ENV["CROWBAR_UPGRADE_VERSIONS"] if ENV["CROWBAR_UPGRADE_VERSIONS"].present?
  # if an upgrade is running, we could check the file indication
  return "6-to-7" if File.exist?("/var/lib/crowbar/upgrade/6-to-7-upgrade-running")
  return "7-to-8" if File.exist?("/var/lib/crowbar/upgrade/7-to-8-upgrade-running")

  # if upgrade has not been started, check the system version
  return "6-to-7" if default_cloud_version == "6"

  "7-to-8"
end
default_username() click to toggle source

Define a default username value

@return [String] the default username value

# File lib/crowbar/client/config.rb, line 153
def default_username
  ENV["CROWBAR_USERNAME"] || "crowbar"
end
default_verify_ssl() click to toggle source

Define a default verify_ssl flag

@return [Bool] the default verify_ssl flag

# File lib/crowbar/client/config.rb, line 180
def default_verify_ssl
  if ENV["CROWBAR_VERIFY_SSL"].present?
    ![
      false, 0, "0", "f", "F", "false", "FALSE"
    ].include? ENV["CROWBAR_VERIFY_SSL"]
  else
    true
  end
end
finder() click to toggle source

Find the first config file

@return [String] the first config

# File lib/crowbar/client/config.rb, line 313
def finder
  paths.detect do |temp|
    File.exist? temp
  end
end
merge() click to toggle source

Merge the different configs together

@return [Hashie::Mash] the merged config

# File lib/crowbar/client/config.rb, line 266
def merge
  result = {}.tap do |overwrite|
    defaults.keys.each do |key|
      overwrite[key] = case
      when options[key] != defaults[key]
        options[key]
      when config[key].present?
        config[key]
      when options[key].present?
        options[key]
      else
        defaults[key]
      end
    end
  end

  Hashie::Mash.new(
    result
  )
end
parser() click to toggle source

Load and parse the config file

@return [Hashie::Mash] the config content

# File lib/crowbar/client/config.rb, line 292
def parser
  ini = Hashie::Mash.new(
    IniFile.load(
      finder
    ).to_h
  )

  if ini[options.alias]
    ini[options.alias]
  else
    Hashie::Mash.new
  end
rescue
  Hashie::Mash.new
end
paths() click to toggle source

Define the available config file paths

@return [Array] the available paths

# File lib/crowbar/client/config.rb, line 324
def paths
  [
    File.join(
      ENV["HOME"],
      ".crowbarrc"
    ),
    File.join(
      "/etc",
      "crowbarrc"
    )
  ]
end