class Cult::Provider

Attributes

path[R]
project[R]

Public Class Methods

all(project) click to toggle source
# File lib/cult/provider.rb, line 127
def self.all(project)
  all_files(project).map do |filename|
    new(project, filename)
  end.to_named_array
end
all_files(project) click to toggle source
# File lib/cult/provider.rb, line 120
def self.all_files(project)
  Dir.glob(File.join(path(project), "*")).select do |file|
    Dir.exist?(file)
  end
end
generate_defaults(definition) click to toggle source

Chooses the smallest size setup with Ubuntu > Debian > Redhat, and a random zone.

# File lib/cult/provider.rb, line 63
def self.generate_defaults(definition)
  definition = JSON.parse(definition.to_json)
  text_to_mb = ->(text) {
    multipliers = {
      mb: 1 ** 1,
      gb: 1 ** 2,
      tb: 1 ** 3,
      pb: 1 ** 4
    }
    if (m = text.match(/(\d+)([mgtp]b)/))
      base = m[1].to_i
      mul = multipliers.fetch(m[2].to_sym)
      base * mul
    else
      nil
    end
  }

  conf = definition['configurations']

  # select the smallest size
  size = conf['sizes'].map do |size|
    if mb = text_to_mb.(size)
      [mb, size]
    else
      nil
    end
  end.compact.sort_by(&:first).last.last

  image = conf['images'].sort_by do |i|
    case i
      when /ubuntu-(\d+)-(\d+)/;
        10000 + ($1.to_i * 100) + ($2.to_i * 10)
      when /debian-(\d+)/
        9000 + ($1.to_i * 100)
      when /(redhat|centos|fedora)-(\d+)/
        8000 + ($2.to_i * 100)
      else
        1
    end
  end.last

  zone = conf['zones'].sample

  {
    default_size:  size,
    default_zone:  zone,
    default_image: image
  }
end
new(project, path) click to toggle source
# File lib/cult/provider.rb, line 13
def initialize(project, path)
  @project = project
  @path = path
end
path(project) click to toggle source
# File lib/cult/provider.rb, line 115
def self.path(project)
  project.location_of("providers")
end

Public Instance Methods

definition() click to toggle source
# File lib/cult/provider.rb, line 40
def definition
  @definition ||= Definition.new(self)
end
definition_parameters() click to toggle source
# File lib/cult/provider.rb, line 52
def definition_parameters
  { project: self.project }
end
definition_parents() click to toggle source
# File lib/cult/provider.rb, line 56
def definition_parents
  []
end
definition_path() click to toggle source
# File lib/cult/provider.rb, line 45
def definition_path
  [ File.join(path, "extra.json"),
    File.join(path, "defaults.json"),
    File.join(path, "provider.json") ]
end
driver() click to toggle source
# File lib/cult/provider.rb, line 32
def driver
  @driver ||= begin
    cls = project.drivers[definition['driver']]
    cls.new(api_key: definition['api_key'])
  end
end
inspect() click to toggle source
# File lib/cult/provider.rb, line 24
def inspect
  prelude = "#{self.class.name} \"#{name}\""
  driver_name = driver.class.driver_name
  driver_string = (driver_name == name) ? '' : " driver=\"#{driver_name}\""
  "\#<#{prelude}#{driver_string}>"
end
name() click to toggle source
# File lib/cult/provider.rb, line 19
def name
  File.basename(path)
end