Module: Smash::CloudPowers::SelfAwareness
- Includes:
- AwsResources
- Included in:
- Smash::CloudPowers
- Defined in:
- lib/cloud_powers/self_awareness.rb
Instance Method Summary collapse
-
#boot_time ⇒ Object
Gets the instance time or the time it was called and as seconds from epoch TODO: use time codes.
-
#die! ⇒ Object
Send a status message on the status Pipe then terminates the instance.
-
#get_awareness! ⇒ Object
Get resource metadata, public host, boot time and task name and set them as instance variables.
-
#instance_url ⇒ Object
Gets and sets the public hostname of the instance.
-
#metadata_request(key = '') ⇒ Object
Makes the http request to self/meta-data to get all the metadata keys or, if a key is given, the method makes the http request to get that particular key from the metadata @param: [key
]. - #run_time ⇒ Object
- #send_frequent_status_updates(opts = {}) ⇒ Object
- #status(id = @instance_id) ⇒ Object
-
#task_name(id = @instance_id) ⇒ Object
Check self-tags for 'task' and act as an attr_accessor.
- #time_is_up? ⇒ Boolean
Methods included from Helper
attr_map!, called_from, create_logger, errors, format_error_message, log_file, logger, smart_retry, task_path, task_require_path, to_camel, to_i_var, to_pascal, to_ruby_file_name, to_snake, update_message_body, valid_json?
Methods included from Auth
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, build_queue, create_queue!, delete_queue_message, get_queue_message_count, pluck_queue_message, poll, poller, queue_exists?, queue_search, send_queue_message
Methods included from AwsResources
#ec2, #image, #kinesis, #region, #s3, #sqs
Methods included from Zenv
env_vars, file_tree_search, i_vars, project_root, project_root=, system_vars, zfind
Instance Method Details
#boot_time ⇒ Object
Gets the instance time or the time it was called and as seconds from epoch TODO: use time codes
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/cloud_powers/self_awareness.rb', line 21 def boot_time begin @boot_time ||= ec2.describe_instances(dry_run: zfind('testing'), instance_ids:[@instance_id]). reservations[0].instances[0].launch_time.to_i # rescue Aws::EC2::Errors::DryRunOperation => e rescue Exception => 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
Send a status message on the status Pipe then terminates the instance.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/cloud_powers/self_awareness.rb', line 34 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| (queue, max_number_of_messages: 1) end send_logs_to_s3 begin ec2.terminate_instances(dry_run: zfind('testing'), ids: [@instance_id]) rescue Aws::EC2::Error::DryRunOperation => e logger.info "dry run testing in die! #{(e)}" @instance_id end end |
#get_awareness! ⇒ Object
Get resource metadata, public host, boot time and task name and set them as instance variables
62 63 64 65 66 67 68 |
# File 'lib/cloud_powers/self_awareness.rb', line 62 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
Gets and sets the public hostname of the instance
87 88 89 90 91 92 93 94 |
# File 'lib/cloud_powers/self_awareness.rb', line 87 def instance_url @instance_url ||= if zfind('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
Makes the http request to self/meta-data to get all the metadata keys or,
if a key is given, the method makes the http request to get that
particular key from the metadata
@param: [key
100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/cloud_powers/self_awareness.rb', line 100 def (key = '') unless zfind('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
115 116 117 118 119 |
# File 'lib/cloud_powers/self_awareness.rb', line 115 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
121 122 123 124 125 126 127 128 129 130 |
# File 'lib/cloud_powers/self_awareness.rb', line 121 def send_frequent_status_updates(opts = {}) sleep_time = opts.delete(:interval) || 10 stream = opts.delete(:stream_name) while true = lambda { |o| (o.merge(content: status)) } logger.info "Send update to status board #{.call(opts)}" pipe_to(stream || :status_stream) { .call(opts) } sleep sleep_time end end |
#status(id = @instance_id) ⇒ Object
132 133 134 135 136 137 138 139 140 |
# File 'lib/cloud_powers/self_awareness.rb', line 132 def status(id = @instance_id) begin ec2.describe_instances(dry_run: zfind('TESTING'), instance_ids: [id]). reservations[0].instances[0].state.name rescue Aws::EC2::Errors::DryRunOperation => e logger.info "Dry run flag set for testing: #{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
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/cloud_powers/self_awareness.rb', line 74 def task_name(id = @instance_id) # get @task_name return @task_name unless @task_name.nil? # set @task_name # TODO: get all tasks instead of just the first resp = ec2.describe_instances(instance_ids: [id].flatten).reservations.first return @task_name = nil if resp.nil? @task_name = resp.instances[0]..select do |t| t.value if t.key == 'taskType' end.first end |
#time_is_up? ⇒ Boolean
142 143 144 145 146 147 148 149 |
# File 'lib/cloud_powers/self_awareness.rb', line 142 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 |