Class: Terraspace::Cloud::Stream

Inherits:
Base show all
Defined in:
lib/terraspace/cloud/stream.rb

Instance Method Summary collapse

Methods inherited from Base

#cancelled?, #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

#changes?Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
# File 'lib/terraspace/cloud/stream.rb', line 38

def changes?
  buffer = Terraspace::Logger.buffer.map { |s| s.force_encoding('UTF-8') }
  will_found = buffer.detect { |s| s.include?("Terraform will perform") }
  saved_found = buffer.detect { |s| s.include?("Saved the plan to:") }
  !!(will_found && saved_found)
end

#close(success, exception) ⇒ Object



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

def close(success, exception)
  return unless @stream
  @end_streaming = true
  @stream_thread.join
  status = success ? "success" : "fail"
  api.complete_stream(id: @stream['data']['id'], status: status, exception: !!exception)
end

#create_cloud_recordObject



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

def create_cloud_record
  return if @created
  # Dont create records unless changes are detected or user configure to always create recrods
  return unless record_all? || yes? || changes?

  case @command
  when "plan"
    create_plan
  when "up", "down"
    create_update
  end
  @created = true
end

#create_planObject



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

def create_plan
  api.create_plan(
    stream_id: @stream['data']['id'],
    plan: stage_attrs(nil),
  )
end

#create_stream(params = {}) ⇒ Object



100
101
102
103
104
105
# File 'lib/terraspace/cloud/stream.rb', line 100

def create_stream(params={})
  api.create_stream(params.merge(command: @command))
rescue Terraspace::NetworkError => e
  logger.warn "WARN: Error calling Terraspace cloud"
  logger.warn "WARN: #{e.class} #{e.message}"
end

#create_updateObject



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

def create_update
  api.create_update(
    stream_id: @stream['data']['id'],
    update: stage_attrs(nil),
  )
end

#log_errors(body, level = :debug) ⇒ Object



107
108
109
110
# File 'lib/terraspace/cloud/stream.rb', line 107

def log_errors(body, level=:debug)
  logger.send(level, "ERROR: Stream uploading logs")
  logger.send(level, "resp body #{body}")
end

#open(command) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/terraspace/cloud/stream.rb', line 3

def open(command)
  return unless Terraspace.cloud?
  @command = command # down, up, plan
  @stream = create_stream
  logger.info "Live Stream: #{@stream['data']['attributes']['url']}"
  @stream_thread ||= Thread.new do
    loop do
      upload_stream
      create_cloud_record
      break if @end_streaming
      sleep 1
    end
  end
  @stream
end

#record_all?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/terraspace/cloud/stream.rb', line 33

def record_all?
  Terraspace.config.cloud.record == "all"
end

#upload_stream(retries = 0) ⇒ Object



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
# File 'lib/terraspace/cloud/stream.rb', line 71

def upload_stream(retries=0)
  stream_url = @stream['data']['attributes']['upload_url']
  url = stream_url
  uri = URI.parse(url)
  object_content = Terraspace::Logger.logs
  resp = Net::HTTP.start(uri.host) do |http|
    http.send_request(
      'PUT',
      uri.request_uri,
      object_content,
      'content-type' => ''
    )
  end

  return if resp.code =~ /^20/

  log_errors(resp.body, :debug)
  if resp.body.include?('expired')
    if retries >= 10 # 15m * 10 = 150m = 2.5h TODO: CHANGE
      log_errors(resp.body, :info)
      # raise "RetriesExceeded"
      exit 1
    end
    logger.debug("Retrying upload")
    @stream = create_stream(stream_id: @stream['data']['id'])
    upload_stream(retries+1)
  end
end

#yes?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/terraspace/cloud/stream.rb', line 45

def yes?
  Terraspace::Logger.stdin_capture == 'yes'
end