Class: Putpaws::Provision::Preset

Inherits:
Object
  • Object
show all
Defined in:
lib/putpaws/provision/preset.rb

Constant Summary collapse

BUILTIN_DIR =
File.expand_path('presets', __dir__)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, dir:) ⇒ Preset



61
62
63
64
# File 'lib/putpaws/provision/preset.rb', line 61

def initialize(name:, dir:)
  @name = name
  @dir = dir
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



60
61
62
# File 'lib/putpaws/provision/preset.rb', line 60

def dir
  @dir
end

#nameObject (readonly)

Returns the value of attribute name.



60
61
62
# File 'lib/putpaws/provision/preset.rb', line 60

def name
  @name
end

Class Method Details

.available(path_prefix: '.putpaws') ⇒ Object



39
40
41
# File 'lib/putpaws/provision/preset.rb', line 39

def self.available(path_prefix: '.putpaws')
  catalog(path_prefix: path_prefix).map{|e| e[:name]}
end

.catalog(path_prefix: '.putpaws') ⇒ Object

[dir:, source:] with name collisions resolved by search order.



28
29
30
31
32
33
34
35
36
37
# File 'lib/putpaws/provision/preset.rb', line 28

def self.catalog(path_prefix: '.putpaws')
  seen = {}
  search_paths(path_prefix: path_prefix).each do |dir, source|
    Dir.glob(File.join(dir, '*/preset.json')).sort.each do |p|
      name = File.basename(File.dirname(p))
      seen[name] ||= {name: name, dir: File.dirname(p), source: source}
    end
  end
  seen.values.sort_by{|e| e[:name]}
end

.find(name, path_prefix: '.putpaws') ⇒ Object



43
44
45
46
# File 'lib/putpaws/provision/preset.rb', line 43

def self.find(name, path_prefix: '.putpaws')
  entry = catalog(path_prefix: path_prefix).detect{|e| e[:name] == name}
  entry && new(name: entry[:name], dir: entry[:dir])
end

.install(name, path_prefix: '.putpaws') ⇒ Object

Copy a preset into .putpaws so that the project owns its snapshot and users can edit it. Does nothing when the local preset already exists.



50
51
52
53
54
55
56
57
58
# File 'lib/putpaws/provision/preset.rb', line 50

def self.install(name, path_prefix: '.putpaws')
  entry = catalog(path_prefix: path_prefix).detect{|e| e[:name] == name}
  raise "Unknown preset: #{name}" unless entry
  return new(name: entry[:name], dir: entry[:dir]) if entry[:source] == 'local'
  local = local_dir(path_prefix: path_prefix).join(name)
  FileUtils.mkdir_p(local.dirname)
  FileUtils.cp_r(entry[:dir], local)
  find(name, path_prefix: path_prefix)
end

.local_dir(path_prefix: '.putpaws') ⇒ Object



10
11
12
# File 'lib/putpaws/provision/preset.rb', line 10

def self.local_dir(path_prefix: '.putpaws')
  Pathname.new(path_prefix).join('provisioning', 'presets')
end

.search_paths(path_prefix: '.putpaws') ⇒ Object

Search order: project local -> user global (PUTPAWS_PRESETS_PATH dirs, then ~/.putpaws/presets) -> builtin (bundled in the gem).



16
17
18
19
20
21
22
23
24
25
# File 'lib/putpaws/provision/preset.rb', line 16

def self.search_paths(path_prefix: '.putpaws')
  paths = [[local_dir(path_prefix: path_prefix).to_s, 'local']]
  ENV['PUTPAWS_PRESETS_PATH'].to_s.split(File::PATH_SEPARATOR).reject(&:empty?).each do |dir|
    paths << [dir, 'global']
  end
  home = ENV['HOME'].to_s
  paths << [File.join(home, '.putpaws', 'presets'), 'global'] unless home.empty?
  paths << [BUILTIN_DIR, 'builtin']
  paths
end

Instance Method Details

#dataObject



66
67
68
# File 'lib/putpaws/provision/preset.rb', line 66

def data
  @data ||= JSON.parse(File.read(File.join(dir, 'preset.json')), symbolize_names: true)
end

#defaultsObject



70
71
72
# File 'lib/putpaws/provision/preset.rb', line 70

def defaults
  data[:defaults] || {}
end

#devops_defaultsObject



82
83
84
# File 'lib/putpaws/provision/preset.rb', line 82

def devops_defaults
  data[:devops_defaults] || {}
end

#service_task_definitionObject



78
79
80
# File 'lib/putpaws/provision/preset.rb', line 78

def service_task_definition
  data[:service_task_definition]
end

#task_definitionsObject



74
75
76
# File 'lib/putpaws/provision/preset.rb', line 74

def task_definitions
  data[:task_definitions] || []
end

#template_path(file) ⇒ Object



86
87
88
# File 'lib/putpaws/provision/preset.rb', line 86

def template_path(file)
  File.join(dir, 'templates', file)
end