Module: Smash::CloudPowers::Helper

Includes:
Auth
Included in:
Smash::CloudPowers, AwsResources, Delegator, SelfAwareness, Synapse, Synapse::Queue::Board
Defined in:
lib/cloud_powers/helper.rb

Instance Method Summary collapse

Methods included from Auth

creds

Instance Method Details

#attr_map!(keys) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/cloud_powers/helper.rb', line 13

def attr_map!(keys)
  keys.map do |attr|
    key = to_i_var(attr)
    value = yield key if block_given?
    instance_variable_set(key, value)
  end
end

#create_logger ⇒ Object



25
26
27
28
29
# File 'lib/cloud_powers/helper.rb', line 25

def create_logger
  logger = Logger.new(STDOUT)
  logger.datetime_format = '%Y-%m-%d %H:%M:%S'
  logger
end

#env(key) ⇒ Object



21
22
23
# File 'lib/cloud_powers/helper.rb', line 21

def env(key)
  ENV[to_snake(key).upcase]
end

#errors ⇒ Object



31
32
33
34
# File 'lib/cloud_powers/helper.rb', line 31

def errors
  # TODO: needs work
  $errors ||= SmashError.instance
end

#format_error_message(error) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/cloud_powers/helper.rb', line 36

def format_error_message(error)
  begin
    [error.message, error.backtrace.join("\n")].join("\n")
  rescue Exception => e
    # if the formatting won't work, return the original exception
    error
  end
end

#log_file ⇒ Object



45
46
47
# File 'lib/cloud_powers/helper.rb', line 45

def log_file
  @log_file ||= env('LOG_FILE')
end

#logger ⇒ Object



49
50
51
# File 'lib/cloud_powers/helper.rb', line 49

def logger
  @logger ||= create_logger
end

#retry(allowed_attempts = Float::Infinity, &test) ⇒ Object

Sample usage: check_stuff = lambda { |params| return true } retry(3, check_stuff(params)) { do_stuff_that_needs_to_be_checked }



56
57
58
59
60
61
62
63
# File 'lib/cloud_powers/helper.rb', line 56

def retry(allowed_attempts = Float::Infinity, &test)
  result = yield if block_given?
  tries = 1
  until test.call(result) || tries >= allowed_attempts
    result = yield if block_given?
    sleep 1
  end
end

#task_path(file = '') ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/cloud_powers/helper.rb', line 65

def task_path(file = '')
  # t_p = Pathname(__FILE__).parent.dirname + 'tasks'
  if file.empty?
    Pathname(__FILE__).parent.dirname + 'tasks'
  else
    Pathname(__FILE__).parent.dirname + "tasks/#{to_snake(file)}"
  end
end

#task_require_path(file_name) ⇒ Object



74
75
76
77
# File 'lib/cloud_powers/helper.rb', line 74

def task_require_path(file_name)
  file = File.basename(file_name, File.extname(file_name))
  Pathname(__FILE__).parent.dirname + 'tasks' + file
end

#to_camel(var) ⇒ Object



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

def to_camel(var)
  var = var.to_s unless var.kind_of? String
  step_one = to_snake(var)
  step_two = to_pascal(step_one)
  step_two[0, 1].downcase + step_two[1..-1]
end

#to_i_var(var) ⇒ Object



86
87
88
# File 'lib/cloud_powers/helper.rb', line 86

def to_i_var(var)
  "@#{to_snake(var)}"
end

#to_pascal(var) ⇒ Object



90
91
92
93
# File 'lib/cloud_powers/helper.rb', line 90

def to_pascal(var)
  var = var.to_s unless var.kind_of? String
  var.gsub(/^(.{1})|\W.{1}|\_.{1}/) { |s| s.gsub(/[^a-z0-9]+/i, '').capitalize }
end

#to_ruby_file_name(name) ⇒ Object



95
96
97
# File 'lib/cloud_powers/helper.rb', line 95

def to_ruby_file_name(name)
  "#{to_snake(name)}.rb"
end

#to_snake(var) ⇒ Object



99
100
101
102
# File 'lib/cloud_powers/helper.rb', line 99

def to_snake(var)
  file_ext = var.to_s[/\.{1}[a-z]+$/] || ''
  var.to_s.gsub(/\.\w+$/, '').gsub(/\W/, '_').downcase + file_ext
end

#update_message_body(opts = {}) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/cloud_powers/helper.rb', line 104

def update_message_body(opts = {})
  # TODO: find better implementation of merging nested hashes
  # this should be fixed with Job #sitrep_message
  # TODO: find a way to trim this method down and get rid
  # of a lof of the repitition with these messages
  # IDEA: throw events and have a separate thread listening. the separate
  # thread could be a communication or status update thread
  unless opts.kind_of? Hash
    update = opts.to_s
    opts[:extraInfo] = { message: update }
  end
  updated_extra_info = opts.delete(:extraInfo) || {}

  {
    instanceId:       @instance_id || 'none-aquired',
    type:             'status-update',
    content:          'running',
    extraInfo:        updated_extra_info
  }.merge(opts)
end