Class: Terraspace::Cloud::Cost::Infracost

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
Util::Popen
Defined in:
lib/terraspace/cloud/cost/infracost.rb

Constant Summary

Constants included from Util::Popen

Util::Popen::BLOCK_SIZE

Instance Method Summary collapse

Methods included from Util::Popen

#all_eof?, #handle_stderr, #handle_stdout, #handle_streams, #popen

Methods included from Util::Logging

#logger

Constructor Details

#initialize(options = {}) ⇒ Infracost

Returns a new instance of Infracost.



6
7
8
# File 'lib/terraspace/cloud/cost/infracost.rb', line 6

def initialize(options={})
  @cloud_stack_name = options[:cloud_stack_name]
end

Instance Method Details

#api_key?Boolean

Returns:

  • (Boolean)


75
76
77
78
# File 'lib/terraspace/cloud/cost/infracost.rb', line 75

def api_key?
  return true if ENV['INFRACOST_API_KEY']
  File.exist?("#{ENV['HOME']}/.config/infracost/credentials.yml")
end

#comment_formatObject



56
57
58
59
60
61
62
# File 'lib/terraspace/cloud/cost/infracost.rb', line 56

def comment_format
  name = Terraspace::Cloud::Vcs.detect_name # IE: github
  format = "#{name}-comment"
  # These are valid infracost comment format. Note: Not all have terraspace_vcs_* plugin support yet
  valid = %w[github-comment gitlab-comment azure-repos-comment bitbucket-comment]
  format if valid.include?(format)
end

#infracost_installed?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/terraspace/cloud/cost/infracost.rb', line 71

def infracost_installed?
  system "type infracost > /dev/null 2>&1"
end

#nameObject



10
11
12
# File 'lib/terraspace/cloud/cost/infracost.rb', line 10

def name
  "infracost"
end

#run(output_dir = ".terraspace-cache/.cache2/cost") ⇒ Object



14
15
16
17
18
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
54
# File 'lib/terraspace/cloud/cost/infracost.rb', line 14

def run(output_dir=".terraspace-cache/.cache2/cost")
  unless infracost_installed?
    logger.info "WARN: infracost not installed. Please install infracost first: https://www.infracost.io/docs/".color(:yellow)
    logger.info "Not running cost estimate"
    return false
  end
  unless api_key?
    logger.info "WARN: infracost api key not configured. Please set the environment variable INFRACOST_API_KEY".color(:yellow)
    logger.info "Not running cost estimate"
    return false
  end

  previous_cost_available = File.exist?("#{output_dir}/cost_prev.json")
  commands = [
    "infracost breakdown --path . --format json --out-file #{output_dir}/cost.json --project-name #{@cloud_stack_name}",
  ]
  if previous_cost_available
    commands << "infracost diff --path #{output_dir}/cost.json --compare-to=#{output_dir}/cost_prev.json --format json --out-file #{output_dir}/cost_diff.json"
  end

  cost_json_file = previous_cost_available ? "cost_diff.json" : "cost.json"
  commands += [
    "infracost output --path #{output_dir}/#{cost_json_file} --format html  --out-file #{output_dir}/cost.html",
    "infracost output --path #{output_dir}/#{cost_json_file} --format table --out-file #{output_dir}/cost.text",
  ]

  if comment_format
    path = previous_cost_available ? "cost_diff.json" : "cost.json"
    commands << "infracost output --path #{output_dir}/#{path} --format #{comment_format} --out-file #{output_dir}/cost.comment"
  end

  commands.each do |command|
    logger.debug "=> #{command}"

    popen(command, filter: "Output saved to ")
    if command.include?(".text")
      logger.info IO.read("#{output_dir}/cost.text")
      logger.info "\n"
    end
  end
end

#versionObject



64
65
66
67
68
# File 'lib/terraspace/cloud/cost/infracost.rb', line 64

def version
  out = `infracost --version`.strip # Infracost v0.10.6
  md = out.match(/ v(.*)/)
  md ? md[1] : out
end