Class: Cfer::Auster::Repo

Inherits:
Object
  • Object
show all
Includes:
Logging::Mixin
Defined in:
lib/cfer/auster/repo.rb

Constant Summary collapse

REPO_FILE =
".auster.yaml"
STEP_REGEX =
/(?<count>[0-9]{2})\.(?<tag>[a-z0-9][a-z0-9\-]*[a-z0-9])/
INT_REGEX =
/[0-9]+/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging::Mixin

#logger

Constructor Details

#initialize(root) ⇒ Repo

Returns a new instance of Repo.



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
# File 'lib/cfer/auster/repo.rb', line 19

def initialize(root)
  raise "root must be a String." unless root.is_a?(String)
  @root = File.expand_path(root).freeze

  logger.debug "Repo location: #{@root}"

  options_file = File.expand_path(REPO_FILE, @root)
  logger.debug "Loading options file..."
  raise "#{options_file} does not exist." unless File.file?(options_file)
  @options = YAML.load_file(options_file)

  @cfg_root = File.join(@root, "config").freeze
  raise "#{@cfg_root} does not exist." unless File.directory?(@cfg_root)
  @step_root = File.join(@root, "steps").freeze
  raise "#{@step_root} does not exist." unless File.directory?(@step_root)

  logger.debug "Enumerating steps..."
  steps = build_steps
  step_tags = steps.map(&:tag)
  step_counts = steps.map(&:count)

  raise "Multiple steps with the same tag found. Can't continue." \
    if step_tags.uniq.length != steps.length
  raise "Multiple steps with the same count found. Can't continue." \
    if step_counts.uniq.length != steps.length

  @steps = steps.map { |step| [step.count, step] }.to_h.freeze
  @steps_by_tag = steps.map { |step| [step.tag, step.count] }.to_h.freeze

  logger.debug "Enumerating config sets..."
  @config_sets = find_config_sets

  logger.debug "Loading param validator..."
  @param_validator = load_param_validator
end

Instance Attribute Details

#config_setsObject (readonly)

Returns the value of attribute config_sets.



15
16
17
# File 'lib/cfer/auster/repo.rb', line 15

def config_sets
  @config_sets
end

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/cfer/auster/repo.rb', line 12

def options
  @options
end

#param_validatorObject (readonly)

Returns the value of attribute param_validator.



17
18
19
# File 'lib/cfer/auster/repo.rb', line 17

def param_validator
  @param_validator
end

#rootObject (readonly)

Returns the value of attribute root.



11
12
13
# File 'lib/cfer/auster/repo.rb', line 11

def root
  @root
end

#stepsObject (readonly)

Returns the value of attribute steps.



14
15
16
# File 'lib/cfer/auster/repo.rb', line 14

def steps
  @steps
end

Class Method Details

.discover_from_cwdObject



146
147
148
149
150
151
152
153
154
# File 'lib/cfer/auster/repo.rb', line 146

def discover_from_cwd
  require "search_up"

  repo_file = SearchUp.search(Dir.pwd, REPO_FILE) { |f| File.file?(f) }.first

  raise "No repo found (signaled by #{REPO_FILE}) from pwd to root." if repo_file.nil?

  Cfer::Auster::Repo.new File.dirname(repo_file)
end

Instance Method Details

#config_set(id) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/cfer/auster/repo.rb', line 76

def config_set(id)
  raise "Config set not found with id '#{id}'." unless @config_sets.include?(id)

  tokens = id.split("/")

  name = tokens[1]
  region = tokens[0]

  filename = File.join(@cfg_root, "#{id}.yaml")

  schema_file = File.join(@cfg_root, "schema.yaml")
  schema_file = nil unless File.file?(schema_file)

  Cfer::Auster::Config.from_file(name: name, aws_region: region,
                                 schema_file: schema_file, data_file: filename)
end

#nuke(config_set) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/cfer/auster/repo.rb', line 93

def nuke(config_set)
  raise "config_set must be a Cfer::Auster::Config." unless config_set.is_a?(Cfer::Auster::Config)

  logger.info "Nuking '#{config_set.full_name}'."

  ordered_steps.reverse.each { |step| step.destroy(config_set) }
end

#ordered_stepsObject



72
73
74
# File 'lib/cfer/auster/repo.rb', line 72

def ordered_steps
  @steps.sort.map(&:last)
end

#step_by_count(count) ⇒ Object



60
61
62
# File 'lib/cfer/auster/repo.rb', line 60

def step_by_count(count)
  @steps[count.to_i]
end

#step_by_count_or_tag(value) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/cfer/auster/repo.rb', line 64

def step_by_count_or_tag(value)
  if INT_REGEX.match(value)
    step_by_count(value)
  else
    step_by_tag(value)
  end
end

#step_by_tag(tag) ⇒ Object



55
56
57
58
# File 'lib/cfer/auster/repo.rb', line 55

def step_by_tag(tag)
  raise "Couldn't find a step with tag '#{tag}'." unless @steps_by_tag.key?(tag)
  @steps[@steps_by_tag[tag]]
end