Class: Terraspace::Cloud::Base

Inherits:
Terraspace::CLI::Base show all
Extended by:
Memoist
Includes:
Api::Concern, Context, Util
Defined in:
lib/terraspace/cloud/base.rb

Direct Known Subclasses

Comment, Cost, Plan, Stream, Update, Upload, Upload::Base

Instance Method Summary collapse

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?

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



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

def initialize(options={})
  super
  @cani = options[:cani]
  @kind = options[:kind]
  @vcs_vars = options[:vcs_vars]
  setup_context(options)
end

Instance Method Details

#cancelled?Boolean

Returns:

  • (Boolean)


71
72
73
74
75
# File 'lib/terraspace/cloud/base.rb', line 71

def cancelled?
  !!Terraspace::Logger.buffer.detect do |line|
    line.include?(' cancelled')
  end
end

#changes?Boolean

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
69
# File 'lib/terraspace/cloud/base.rb', line 61

def changes?
  no_changes = Terraspace::Logger.buffer.detect do |line|
    line.include?('No changes')
  end
  zero_destroyed = Terraspace::Logger.buffer.detect do |line|
    line.include?('Destroy complete! Resources: 0 destroyed')
  end
  !no_changes && !zero_destroyed
end

#clean_cache2_stageObject



48
49
50
51
52
53
54
55
# File 'lib/terraspace/cloud/base.rb', line 48

def clean_cache2_stage
  # terraform plan can be a kind of apply or destroy
  # terraform apply can be a kind of apply or destroy
  kind = self.class.name.to_s.split('::').last.underscore # IE: apply or destroy
  dir = "#{@mod.cache_dir}/.terraspace-cache/.cache2/#{kind}"
  FileUtils.rm_rf(dir)
  FileUtils.mkdir_p(dir)
end

#cloud_uploadObject



26
27
28
# File 'lib/terraspace/cloud/base.rb', line 26

def cloud_upload
  Upload.new(@options)
end

#record?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/terraspace/cloud/base.rb', line 57

def record?
  changes? && !cancelled? || Terraspace.config.cloud.record == "all"
end

#sh(command, exit_on_fail: true) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/terraspace/cloud/base.rb', line 39

def sh(command, exit_on_fail: true)
  logger.debug "=> #{command}"
  system command
  if $?.exitstatus != 0 && exit_on_fail
    logger.info "ERROR RUNNING: #{command}"
    exit $?.exitstatus
  end
end

#stage_attrs(success) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/terraspace/cloud/base.rb', line 16

def stage_attrs(success)
  status = success_status(success)
  attrs = {
    status: status,
    kind: @kind,
  }
  attrs.merge!(@vcs_vars)
  attrs
end

#success_status(success) ⇒ Object



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

def success_status(success)
  case success
  when true then "success"
  when false then "fail"
  when nil then "started"
  end
end