Class: Terraspace::CLI::Plan

Inherits:
Base
  • Object
show all
Includes:
Concerns::PlanPath, TfcConcern, Terraspace::Cloud::Streamer, Terraspace::Cloud::Vcs::Commenter
Defined in:
lib/terraspace/cli/plan.rb

Instance Method Summary collapse

Methods included from TfcConcern

#backend, #tfc?

Methods included from Terraspace::Cloud::Vcs::Commenter

#able_to_comment?, #cloud_comment, #finalize_vars, #kind, #pr_comment, #vcs, #vcs_vars

Methods included from Terraspace::Cloud::Streamer

#cloud_stream

Methods included from Concerns::PlanPath

#plan_path

Methods inherited from Base

#initialize

Methods included from Util::Pretty

#pretty_path, #pretty_time

Methods included from Util::Sure

#sure?

Methods included from Util::Logging

#logger

Constructor Details

This class inherits a constructor from Terraspace::CLI::Base

Instance Method Details

#cloud_costObject



72
73
74
# File 'lib/terraspace/cli/plan.rb', line 72

def cloud_cost
  Terraspace::Cloud::Cost.new(@options.merge(stack: @mod.name, kind: kind, vcs_vars: vcs_vars))
end

#cloud_planObject



67
68
69
# File 'lib/terraspace/cli/plan.rb', line 67

def cloud_plan
  Terraspace::Cloud::Plan.new(@options.merge(stack: @mod.name, kind: kind, vcs_vars: vcs_vars))
end

#commanderObject



48
49
50
# File 'lib/terraspace/cli/plan.rb', line 48

def commander
  Commander.new("plan", @options)
end

#copy_out_file_to_rootObject



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/terraspace/cli/plan.rb', line 53

def copy_out_file_to_root
  file = @mod.out_option
  return if !file || @options[:copy_to_root] == false
  return if file =~ %r{^/} # not need to copy absolute path

  name = file.sub("#{Terraspace.root}/",'')
  src = "#{@mod.cache_dir}/#{name}"
  dest = name
  return unless File.exist?(src) # plan wont exists if the plan errors
  FileUtils.mkdir_p(File.dirname(dest))
  FileUtils.cp(src, dest)
  !!dest
end

#performObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/terraspace/cli/plan.rb', line 16

def perform
  success = plan_only
  plan = cloud_plan.create(success, @stream)
  if success && plan # possible from no changes / recording is disabled
    resp = cloud_cost.cani?(exit_on_error: false)
    if resp['errors'] # info on why cannot create a plan from resp
      logger.info "WARN: Not creating a cost estimate."
      logger.info resp['errors'][0]['detail']
    else
      cost = cloud_cost.create(uid: plan['data']['id'], stream: @stream)
      pr_comment(plan, cost)
    end
    logger.info "Terraspace Cloud #{plan['data']['attributes']['url']}"
  end
  success
rescue Exception => e
  @exception = true
  logger.info "Exception #{e.class}: #{e.message}".color(:red)
  logger.info e.backtrace.join("\n")
  false
end

#plan_onlyObject



38
39
40
41
42
43
44
45
46
# File 'lib/terraspace/cli/plan.rb', line 38

def plan_only
  if Terraspace.cloud? && !@options[:out]
    @options[:out] = plan_path
  end
  cloud_plan.setup
  success = commander.run
  copy_out_file_to_root
  success
end

#runObject



8
9
10
11
12
13
14
# File 'lib/terraspace/cli/plan.rb', line 8

def run
  cloud_plan.cani?
  @stream = cloud_stream.open("plan")
  success = perform
  cloud_stream.close(success, @exception)
  exit 1 unless success
end