Class: Terraspace::Cloud::Plan

Inherits:
Base show all
Includes:
Terraspace::CLI::Concerns::PlanPath
Defined in:
lib/terraspace/cloud/plan.rb

Instance Method Summary collapse

Methods included from Terraspace::CLI::Concerns::PlanPath

#plan_path

Methods inherited from Base

#cancelled?, #changes?, #clean_cache2_stage, #cloud_upload, #initialize, #record?, #sh, #stage_attrs, #success_status

Methods included from Util::Pretty

#pretty_path, #pretty_time

Methods included from Util::Sure

#sure?

Methods included from Util::Logging

#logger

Methods included from Context

#setup_context

Methods included from Api::Validate

#validate

Methods included from Api::Concern

#api, #cloud_stack_name, #expander, #region

Methods included from Api::Concern::Errors

#error_message, #errors?

Methods inherited from Terraspace::CLI::Base

#initialize

Constructor Details

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

Instance Method Details

#build(success) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/terraspace/cloud/plan.rb', line 40

def build(success)
  clean_cache2_stage
  # .terraspace-cache/dev/stacks/demo
  Dir.chdir(@mod.cache_dir) do
    plan_dir = File.dirname(plan_path)

    IO.write("#{plan_dir}/plan.log", Terraspace::Logger.logs)

    return unless success
    return if File.empty?(plan_path)

    out_option_root_path = "#{Terraspace.root}/#{plan_path}"
    return unless File.exist?(out_option_root_path)
    FileUtils.cp(out_option_root_path, plan_path)

    json = plan_path.sub('.binary','.json')
    sh "#{Terraspace.terraform_bin} show -json #{plan_path} > #{json}"
  end
end

#cani?(exit_on_error: true) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
# File 'lib/terraspace/cloud/plan.rb', line 60

def cani?(exit_on_error: true)
  return true unless Terraspace.cloud?
  api.create_plan(cani: 1, exit_on_error: exit_on_error)
end

#create(success, stream) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/terraspace/cloud/plan.rb', line 17

def create(success, stream)
  return unless Terraspace.cloud?
  return unless record?
  build(success)
  upload = cloud_upload.create(type: "plan", stream_id: stream['data']['id'])
  api.create_plan(
    upload_id: upload['data']['id'],
    stack_uid: upload['data']['attributes']['stack_id'], # use stack_uid since stack_id is friendly url name
    plan: stage_attrs(success),
  )
rescue Terraspace::NetworkError => e
  logger.warn "WARN: #{e.class} #{e.message}"
  logger.warn "WARN: Unable to save data to Terraspace cloud"
end

#create_streamObject



32
33
34
35
36
37
38
# File 'lib/terraspace/cloud/plan.rb', line 32

def create_stream
  return unless Terraspace.cloud?
  api.create_stream("plan")
rescue Terraspace::NetworkError => e
  logger.warn "WARN: #{e.class} #{e.message}"
  logger.warn "WARN: Unable to save data to Terraspace cloud"
end

#setupObject



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/terraspace/cloud/plan.rb', line 5

def setup
  return unless Terraspace.cloud?
  cani?

  return unless @mod.out_option
  return if @mod.out_option =~ %r{^/} # not need to create parent dir for copy with absolute path

  name = @mod.out_option.sub("#{Terraspace.root}/",'')
  dest = "#{@mod.cache_dir}/#{name}"
  FileUtils.mkdir_p(File.dirname(dest))
end