Module: Smash::CloudPowers::SelfAwareness

Extended by:
Helper, Smash::CloudPowers::Synapse::Pipe, Smash::CloudPowers::Synapse::Queue
Includes:
AwsResources
Included in:
Smash::CloudPowers
Defined in:
lib/cloud_powers/self_awareness.rb

Instance Method Summary collapse

Methods included from Helper

attr_map!, create_logger, env, errors, format_error_message, log_file, logger, retry, task_path, task_require_path, to_camel, to_i_var, to_pascal, to_ruby_file_name, to_snake, update_message_body

Methods included from Auth

creds

Methods included from Smash::CloudPowers::Synapse::Pipe

create_stream, flow_from_pipe, flow_to_pipe, from_pipe, message_body_collection, pipe_message_body, pipe_to, stream_config, stream_exists?

Methods included from Smash::CloudPowers::Synapse::Queue

board_name, create_queue, delete_queue_message, get_count, pluck_message, poll, poller, queue_exists?, send_queue_message, sqs

Methods included from AwsResources

#ec2, #image, #kinesis, #region, #s3

Instance Method Details

#boot_time ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/cloud_powers/self_awareness.rb', line 16

def boot_time
  begin
    @boot_time ||=
      ec2.describe_instances(dry_run: env('testing'), instance_ids:[@instance_id]).
        reservations[0].instances[0].launch_time.to_i
  rescue Aws::EC2::Errors::DryRunOperation => e
    logger.info "dry run for testing: #{e}"
    @boot_time ||= Time.now.to_i # comment the code below for development mode
  end
end

#die! ⇒ Object



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
# File 'lib/cloud_powers/self_awareness.rb', line 27

def die!
  Thread.kill(@status_thread) unless @status_thread.nil?
  # blame = errors.sort_by(&:reverse).last.first
  logger.info("The cause for the shutdown is TODO: fix SmashErrors")

  pipe_to(:status_stream) do
    {
      instanceID: @instance_id,
      type: 'status-update',
      content: 'dying'
      # extraInfo: blame
    }
  end

  [:count, :wip].each do |queue|
    delete_queue_message(queue, max_number_of_messages: 1)
  end
  send_logs_to_s3
  begin
    ec2.terminate_instances(dry_run: env('testing'), ids: [@instance_id])
  rescue Aws::EC2::Error::DryRunOperation => e
    logger.info "dry run testing in die! #{format_error_message(e)}"
    @instance_id
  end
end

#get_awareness! ⇒ Object

Get resource metadata, public host, boot time and task name and set them as instance variables



55
56
57
58
59
60
61
# File 'lib/cloud_powers/self_awareness.rb', line 55

def get_awareness!
  keys = 
  attr_map!(keys) { |key| (key) }
  boot_time # gets and sets @boot_time
  task_name # gets and sets @task_name
  instance_url # gets and sets @instance_url
end

#instance_url ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/cloud_powers/self_awareness.rb', line 77

def instance_url
  @instance_url ||= if env('TESTING')
    'https://test-url.com'
  else
    hostname_uri = 'http://169.254.169.254/latest/meta-data/public-hostname'
    HTTParty.get(hostname_uri).parsed_response
  end
end

#metadata_request(key = '') ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/cloud_powers/self_awareness.rb', line 86

def (key = '')
  unless env('TESTING')
     = "http://169.254.169.254/latest/meta-data/#{key}"
    HTTParty.get().parsed_response.split("\n")
  else
    @z ||= ['i-9254d106', 'ami-id', 'ami-launch-index', 'ami-manifest-path', 'network/thing']
    if key == ''
      @boogs = ['instance-id', 'ami-id', 'ami-launch-index', 'ami-manifest-path', 'network/interfaces/macs/mac/device-number']
    else
      @z.shift
    end
  end
end

#run_time ⇒ Object



100
101
102
103
104
# File 'lib/cloud_powers/self_awareness.rb', line 100

def run_time
  # TODO: refactor to use valid time stamps for better tracking.
  # reason -> separate regions or OSs etc.
  Time.now.to_i - boot_time
end

#send_frequent_status_updates(opts = {}) ⇒ Object



106
107
108
109
110
111
112
113
114
115
# File 'lib/cloud_powers/self_awareness.rb', line 106

def send_frequent_status_updates(opts = {})
  sleep_time = opts.delete(:interval) || 10
  stream = opts.delete(:stream_name)
  while true
    message = lambda { |o| update_message_body(o.merge(content: status)) }
    logger.info "Send update to status board #{message.call(opts)}"
    pipe_to(stream || :status_stream) { message.call(opts) }
    sleep sleep_time
  end
end

#status(id = @instance_id) ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'lib/cloud_powers/self_awareness.rb', line 117

def status(id = @instance_id)
  begin
    ec2.describe_instances(dry_run: env('testing'), instance_ids: [id]).
      reservations[0].instances[0].state.name
  rescue Aws::EC2::Errors::DryRunOperation => e
    logger.info "Dry run flag set for testing: #{format_error_message(e)}"
    'testing'
  end
end

#task_name(id = @instance_id) ⇒ Object

Check self-tags for 'task' and act as an attr_accessor. A different node's tag's can be checked for a task by passing the id param see also: SelfAwareness#task_names



67
68
69
70
71
72
73
74
75
# File 'lib/cloud_powers/self_awareness.rb', line 67

def task_name(id = @instance_id)
  # get @task_name
  return @task_name unless @task_name.nil?
  # set @task_name
  resp = ec2.describe_instances(instance_ids: [id].flatten)
  @task_name = resp.reservations[0].instances[0].tags.select do |t|
    t.value if t.key == 'taskType'
  end.first
end

#time_is_up? ⇒ Boolean

Returns:

  • (Boolean)


127
128
129
130
131
132
133
134
# File 'lib/cloud_powers/self_awareness.rb', line 127

def time_is_up?
  # returns true when the hour mark approaches
  an_hours_time = 60 * 60
  five_minutes_time = 60 * 5

  return false if run_time < five_minutes_time
  run_time % an_hours_time < five_minutes_time
end