Class: Kubes::Compiler

Inherits:
Object
  • Object
show all
Includes:
Hooks::Concern, Kubectl::Ordering, Logging, Util::Consider
Defined in:
lib/kubes/compiler.rb,
lib/kubes/compiler/layering.rb,
lib/kubes/compiler/strategy.rb

Defined Under Namespace

Modules: Decorator, Layering, Shared, Util Classes: Strategy

Instance Method Summary collapse

Methods included from Kubectl::Ordering

#config_skip?, #files, #filter_skip, #index_for, #process?, #search_expr, #sorted_files, #two_levels_deep?

Methods included from Util::Consider

#consider?

Methods included from Logging

#logger

Methods included from Hooks::Concern

#run_hooks

Constructor Details

#initialize(options = {}) ⇒ Compiler

Returns a new instance of Compiler.



8
9
10
# File 'lib/kubes/compiler.rb', line 8

def initialize(options={})
  @options = options
end

Instance Method Details

#pretty(path) ⇒ Object



72
73
74
# File 'lib/kubes/compiler.rb', line 72

def pretty(path)
  path.sub("#{Kubes.root}/",'')
end

#resourcesObject



30
31
32
33
34
35
36
37
38
# File 'lib/kubes/compiler.rb', line 30

def resources
  paths = []
  expr = "#{Kubes.root}/.kubes/resources/**/*"
  Dir.glob(expr).each do |path|
    next unless process?(path)
    paths << path
  end
  paths
end

#runObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/kubes/compiler.rb', line 12

def run
  Kubes.config # trigger config load. So can set ENV['VAR'] in config/envs/dev.rb etc
  run_hooks("kubes.rb", name: "compile") do
    results = resources.map do |path|
      strategy = Strategy.new(@options.merge(path: path))
      strategy.compile
    end.compact

    results.each do |result|
      write(result)
    end
  end

  write_full

  logger.info "Compiled .kubes/resources files to .kubes/output" if show_compiled_message?
end

#show_compiled_message?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/kubes/compiler.rb', line 68

def show_compiled_message?
  !%w[g ge get].include?(ARGV.first)
end

#write(result) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/kubes/compiler.rb', line 40

def write(result)
  return if config_skip?(result.filename)
  return if result.skip?
  result.decorate!(:post)
  filename, content = result.filename, result.content
  dest = "#{Kubes.root}/.kubes/output/#{filename}"

  if result.io?
    FileUtils.cp(filename, dest) # preserve permissions
  else
    FileUtils.mkdir_p(File.dirname(dest))
    IO.write(dest, content)
  end

  logger.debug "Written  #{pretty(dest)}"
end

#write_fullObject



57
58
59
60
61
62
63
64
65
66
# File 'lib/kubes/compiler.rb', line 57

def write_full
  full = sorted_files.inject([]) do |acc, file|
    acc << IO.read(file)
  end
  content = full.join("\n")
  path = "#{Kubes.root}/.kubes/tmp/full.yaml" # write to tmp instead of output so it doesnt interfere with kubes get
  FileUtils.mkdir_p(File.dirname(path))
  IO.write(path, content)
  logger.debug "Written  #{pretty(path)}"
end