Class: Cult::Provider

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/cult/provider.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, path) ⇒ Provider

Returns a new instance of Provider.



13
14
15
16
# File 'lib/cult/provider.rb', line 13

def initialize(project, path)
  @project = project
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



10
11
12
# File 'lib/cult/provider.rb', line 10

def path
  @path
end

#projectObject (readonly)

Returns the value of attribute project.



9
10
11
# File 'lib/cult/provider.rb', line 9

def project
  @project
end

Class Method Details

.all(project) ⇒ Object



127
128
129
130
131
# 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) ⇒ Object



120
121
122
123
124
# 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) ⇒ Object

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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# 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

.path(project) ⇒ Object



115
116
117
# File 'lib/cult/provider.rb', line 115

def self.path(project)
  project.location_of("providers")
end

Instance Method Details

#definitionObject



40
41
42
# File 'lib/cult/provider.rb', line 40

def definition
  @definition ||= Definition.new(self)
end

#definition_parametersObject



52
53
54
# File 'lib/cult/provider.rb', line 52

def definition_parameters
  { project: self.project }
end

#definition_parentsObject



56
57
58
# File 'lib/cult/provider.rb', line 56

def definition_parents
  []
end

#definition_pathObject



45
46
47
48
49
# 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

#driverObject



32
33
34
35
36
37
# 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

#inspectObject



24
25
26
27
28
29
# 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

#nameObject



19
20
21
# File 'lib/cult/provider.rb', line 19

def name
  File.basename(path)
end