Class: KubeDeployTools::Generate

Inherits:
Object
  • Object
show all
Defined in:
lib/kube_deploy_tools/generate.rb

Defined Under Namespace

Classes: Optparser

Instance Method Summary collapse

Constructor Details

#initialize(manifest, input_dir, output_dir, file_filters: [], print_flags_only: false, literals: {}) ⇒ Generate

Returns a new instance of Generate.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/kube_deploy_tools/generate.rb', line 22

def initialize(manifest, input_dir, output_dir, file_filters: [], print_flags_only: false, literals: {})
  @project = KubeDeployTools::PROJECT
  @build_number = KubeDeployTools::BUILD_NUMBER

  @config = DeployConfigFile.new(manifest)
  @literals = literals

  @input_dir = input_dir
  @output_dir = output_dir
  FileUtils.mkdir_p @output_dir

  @file_filters = file_filters

  @print_flags_only = print_flags_only
end

Instance Method Details

#generateObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/kube_deploy_tools/generate.rb', line 48

def generate
  permutations = {}
  @config.artifacts.each do |c|
    artifact = c.fetch('name')

    # Get metadata for this target/environment pair from manifest
    cluster_flags = DEFAULT_FLAGS.dup

    cluster_flags['image_registry'] = c.fetch('image_registry')

    # Merge in configured default flags
    cluster_flags.merge!(@config.default_flags)

    # Update and merge deploy flags for rendering
    cluster_flags.merge!(generate_erb_flags(c.fetch('flags', {})))

    cluster_flags['image_registry'] = @config.valid_image_registries[cluster_flags['image_registry']]
    # Allow deploy.yaml to gate certain flavors to certain targets.
    cluster_flavors = @config.flavors.select { |key, value| c['flavors'].nil? || c['flavors'].include?(key) }
    cluster_flavors.each do |flavor, flavor_flags|
      full_flags = cluster_flags.clone
      full_flags.merge!(generate_erb_flags(flavor_flags)) if flavor_flags
      # Project information used to identify source of various manifests
      full_flags.merge!({
        'git_commit' => git_commit,
        'git_project' => git_project
      })

      full_flags.merge!(@literals)

      # Print all flags for each artifact-flavor
      puts "artifact '#{artifact}', flavor '#{flavor}'"
      full_flags.
        sort_by { |k, v| k }.
        each do |k, v|
          puts "config['#{k}'] = #{v}"
        end
      puts ""
      # Skip rendering ERB templates and generating artifacts
      # if printing flags only
      next if @print_flags_only

      file_filters = FileFilter.filters_from_hash(c) + @file_filters

      # Call individual templating hook with the rendered configuration
      # and a prefix to place all the files. Run many hooks in the
      # background.
      flavor_dir = File.join(@output_dir, "#{artifact}_#{flavor}")
      FileUtils.rm_rf flavor_dir
      FileUtils.mkdir_p flavor_dir
      pid = fork do
        # Save rendered release configuration to a temp file.
        rendered = Tempfile.new('deploy_config')
        rendered << YAML.dump(full_flags)
        rendered.flush

        # Run every hook sequentially. 'default' hook is special.
        @config.hooks.each do |hook|
          if hook == DEFAULT_HOOK_SCRIPT_LABEL
            # TODO(joshk): render_deploys method should take a hash for testability
            KubeDeployTools::RenderDeploysHook.render_deploys(rendered.path, @input_dir, flavor_dir, file_filters)
          else
            Shellrunner.check_call(hook, rendered.path, @input_dir, flavor_dir)
          end
        end
      end

      permutations[pid] = "#{artifact}_#{flavor}"
    end
  end

  failure = false
  Process.waitall.each do |pid, status|
    if status.exitstatus != 0
      Logger.error "Rendering #{permutations[pid]} failed: exit status #{status.exitstatus}"
      failure = true
    end
  end

  raise 'rendering deploy configurations failed' if failure
end

#generate_erb_flags(flags) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/kube_deploy_tools/generate.rb', line 130

def generate_erb_flags(flags)
  result = Hash.new

  flags.each do |key, template|
    if template.is_a?(String)
      templater = ERB.new(template)
      result[key] = templater.result
    else
      result[key] = template
    end
  end

  result
end

#git_commitObject



38
39
40
41
# File 'lib/kube_deploy_tools/generate.rb', line 38

def git_commit
  commit_sha = Shellrunner.check_call(*%w(git rev-parse HEAD))
  commit_sha ? commit_sha.strip : ''
end

#git_projectObject



43
44
45
46
# File 'lib/kube_deploy_tools/generate.rb', line 43

def git_project
  project_url = Shellrunner.check_call(*%w(git config --get remote.origin.url))
  project_url ? project_url.strip : ''
end