cloud_powers 0.1.5 → 0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5276c7479f1dfcdc4d59aaffa8841d5ab4839180
4
- data.tar.gz: 9dcab0fc49c1c0bd9e13fd259719ef697f209429
3
+ metadata.gz: f7b6c2d878907d4b7d3d61633e5cbb656aaf60e4
4
+ data.tar.gz: d2719ee9fc973f91e847e19958db96f2bc0daf81
5
5
  SHA512:
6
- metadata.gz: b5c3f428b50af4dd03c40521d461ed4bc7c23c28b598557b3495bfe85ee63a1571d6f863c0d1203ba9a309214344222b073a232cd340e67c6841ed30863ae65c
7
- data.tar.gz: 6bbac08f878d73345f023fde38ffbde8ab92c53a134c828fe4721d176363126ad99e028672e4c3383837afe935da5b32b2f4bffa73a06506710f6e58fa22f29c
6
+ metadata.gz: c44f2215f31bba48d510f1ac0f780c50d79e85911cf0c1dffd83135d35d19087319718f4446cc9cb22d4c54846dc80eb28f3c12eae5067f4b90bf51758f212ba
7
+ data.tar.gz: 27f3870cd67688ce00c0c021e49c0f6517609d31c26bf453466963fbeef3c16469e23cd6a47ea194d2b4411e5fbc1c799062d70df5f2cb57af9192c8b02364da
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cloud_powers (0.1.5)
4
+ cloud_powers (0.2)
5
5
  activesupport-core-ext (~> 4)
6
6
  aws-sdk (~> 2)
7
7
  dotenv (~> 2.1)
data/cloud_powers.gemspec CHANGED
@@ -12,8 +12,12 @@ Gem::Specification.new do |spec|
12
12
  spec.summary = %q{Cloud providers wrapper. Currently only AWS is supported.}
13
13
  spec.description = <<-EOF
14
14
  CloudPowers is a wrapper around AWS and other cloud services.
15
- This wrapper was developed specifically for the Brain project
16
- but can be used in any other ruby project.
15
+ It was developed specifically for the Brain project
16
+ but hopefully can be used in any other ruby project that needs to use cloud
17
+ service providers' resources. Version 0.1.5 has a little EC2, S3, SQS and
18
+ Kinesis. This project is actively being developed, so more additions, specs
19
+ and docs will be updated frequently. I always welcome input.
20
+ Enjoy!
17
21
  EOF
18
22
  spec.homepage = 'https://github.com/adam-phillipps/cloud_powers'
19
23
  spec.license = 'MIT'
@@ -1,14 +1,16 @@
1
1
  require_relative 'auth'
2
2
  require_relative 'helper'
3
+ require_relative 'zenv'
3
4
 
4
5
  module Smash
5
6
  module CloudPowers
6
7
  module AwsResources
7
8
  include Smash::CloudPowers::Auth
8
9
  include Smash::CloudPowers::Helper
10
+ include Smash::CloudPowers::Zenv
9
11
 
10
12
  def region
11
- env('Aws Region')
13
+ zfind('Aws Region')
12
14
  end
13
15
 
14
16
  def ec2
@@ -14,12 +14,12 @@ module Smash
14
14
  keys.map do |attr|
15
15
  key = to_i_var(attr)
16
16
  value = yield key if block_given?
17
- instance_variable_set(key, value)
17
+ instance_variable_set(key, value) unless instance_variable_get(to_i_var(key))
18
18
  end
19
19
  end
20
20
 
21
- def env(key)
22
- ENV[to_snake(key).upcase]
21
+ def called_from
22
+ File.expand_path(File.dirname($0))
23
23
  end
24
24
 
25
25
  def create_logger
@@ -45,7 +45,7 @@ module Smash
45
45
  # Gets the path from the environment and sets @log_file using the path
46
46
  # @returns @log_file path <String>
47
47
  def log_file
48
- @log_file ||= env('LOG_FILE')
48
+ @log_file ||= zfind('LOG_FILE')
49
49
  end
50
50
 
51
51
  # @returns: An instance of Logger, cached as @logger
@@ -107,7 +107,8 @@ module Smash
107
107
  end
108
108
 
109
109
  def to_i_var(var)
110
- "@#{to_snake(var)}"
110
+ var = var.to_s unless var.kind_of? String
111
+ /^\W*@\w+/ =~ var ? to_snake(var) : "@#{to_snake(var)}"
111
112
  end
112
113
 
113
114
  def to_pascal(var)
@@ -122,7 +123,6 @@ module Smash
122
123
  def to_snake(var)
123
124
  var = var.to_s unless var.kind_of? String
124
125
 
125
- # var.gsub(/\W/, '_').downcase
126
126
  var.gsub(/:{2}|\//, '_').
127
127
  gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
128
128
  gsub(/([a-z\d])([A-Z])/,'\1_\2').
@@ -4,6 +4,7 @@ require 'httparty'
4
4
  require_relative 'aws_resources'
5
5
  require_relative 'helper'
6
6
  require_relative './synapse/synapse'
7
+ require_relative 'zenv'
7
8
 
8
9
  module Smash
9
10
  module CloudPowers
@@ -12,14 +13,15 @@ module Smash
12
13
  extend Smash::CloudPowers::Synapse::Pipe
13
14
  extend Smash::CloudPowers::Synapse::Queue
14
15
  include Smash::CloudPowers::AwsResources
16
+ include Smash::CloudPowers::Zenv
15
17
 
16
- # gets the instance time or the time it was called and as seconds from
18
+ # Gets the instance time or the time it was called and as seconds from
17
19
  # epoch
18
20
  # TODO: use time codes
19
21
  def boot_time
20
22
  begin
21
23
  @boot_time ||=
22
- ec2.describe_instances(dry_run: env('testing'), instance_ids:[@instance_id]).
24
+ ec2.describe_instances(dry_run: zfind('testing'), instance_ids:[@instance_id]).
23
25
  reservations[0].instances[0].launch_time.to_i
24
26
  rescue Aws::EC2::Errors::DryRunOperation => e
25
27
  logger.info "dry run for testing: #{e}"
@@ -47,7 +49,7 @@ module Smash
47
49
  end
48
50
  send_logs_to_s3
49
51
  begin
50
- ec2.terminate_instances(dry_run: env('testing'), ids: [@instance_id])
52
+ ec2.terminate_instances(dry_run: zfind('testing'), ids: [@instance_id])
51
53
  rescue Aws::EC2::Error::DryRunOperation => e
52
54
  logger.info "dry run testing in die! #{format_error_message(e)}"
53
55
  @instance_id
@@ -80,7 +82,7 @@ module Smash
80
82
 
81
83
  # Gets and sets the public hostname of the instance
82
84
  def instance_url
83
- @instance_url ||= if env('TESTING')
85
+ @instance_url ||= if zfind('TESTING')
84
86
  'https://test-url.com'
85
87
  else
86
88
  hostname_uri = 'http://169.254.169.254/latest/meta-data/public-hostname'
@@ -93,10 +95,11 @@ module Smash
93
95
  # particular key from the metadata
94
96
  # @param: [key <String>]
95
97
  def metadata_request(key = '')
96
- unless env('TESTING')
98
+ unless zfind('TESTING')
97
99
  metadata_uri = "http://169.254.169.254/latest/meta-data/#{key}"
98
100
  HTTParty.get(metadata_uri).parsed_response.split("\n")
99
101
  else
102
+
100
103
  @z ||= ['i-9254d106', 'ami-id', 'ami-launch-index', 'ami-manifest-path', 'network/thing']
101
104
  if key == ''
102
105
  @boogs = ['instance-id', 'ami-id', 'ami-launch-index', 'ami-manifest-path', 'network/interfaces/macs/mac/device-number']
@@ -125,10 +128,10 @@ module Smash
125
128
 
126
129
  def status(id = @instance_id)
127
130
  begin
128
- ec2.describe_instances(dry_run: env('testing'), instance_ids: [id]).
131
+ ec2.describe_instances(dry_run: zfind('TESTING'), instance_ids: [id]).
129
132
  reservations[0].instances[0].state.name
130
133
  rescue Aws::EC2::Errors::DryRunOperation => e
131
- logger.info "Dry run flag set for testing: #{format_error_message(e)}"
134
+ logger.info "Dry run flag set for testing: #{e}"
132
135
  'testing'
133
136
  end
134
137
  end
@@ -8,7 +8,7 @@ module Smash
8
8
 
9
9
  def source_task(file)
10
10
  # TODO: better path management
11
- bucket = env('task storage')
11
+ bucket = zfind('task storage')
12
12
  unless task_path(file).exist?
13
13
  objects = s3.list_objects(bucket: bucket).contents.select do |f|
14
14
  /#{Regexp.escape file}/i =~ f.key
@@ -1,3 +1,3 @@
1
1
  module CloudPowers
2
- VERSION = "0.1.5"
2
+ VERSION = "0.2"
3
3
  end
@@ -0,0 +1,94 @@
1
+ require 'dotenv'
2
+ require_relative 'helper'
3
+ module Smash
4
+ module CloudPowers
5
+ # This module provides some environment variable management and functionality
6
+ # System ENV, dotenv ENV and instance variables are considered for now but this
7
+ # will also use elasticache/redis...some other stuff too, in the coming weeks
8
+ module Zenv
9
+ include Smash::CloudPowers::Helper
10
+ # ZFind looks for the key in a preditermined order of importance:
11
+ # * i-vars are considered first becuase they might be tracking different
12
+ # locations for multiple tasks or something like that.
13
+ # * dotenv files are second because they were manually set, so for sure
14
+ # it's important
15
+ # * System Env[@] variables are up next. Hopefully by this time we've found
16
+ # our information but if not, it should "search" through the system env too.
17
+ # @params: key <String>: The key to search for
18
+ # @return: <String>
19
+ # TODO: implement a search for all 3 that can find close matches
20
+ def zfind(key)
21
+ res = (i_vars[to_snake(key).upcase] or
22
+ env_vars[to_snake(key).upcase] unless project_root.nil?) or
23
+ system_vars[to_snake(key).upcase]
24
+ (res.nil? or res.empty?) ? nil : res
25
+ end
26
+
27
+ # Search through the instance variables for a key or if no key is given,
28
+ # return all the i-vars and their values
29
+ # @params: [key <String]: The key to search for
30
+ # @return:
31
+ def i_vars(key = '')
32
+ if key.empty?
33
+ return self.instance_variables.inject({}) do |r,v|
34
+ r.tap { |h| h[to_snake(v)] = self.instance_variable_get(to_i_var(v)) }
35
+ end
36
+ end
37
+ self.instance_variable_get(to_i_var(key))
38
+ end
39
+
40
+ # Search through the .env variables for a key or if no key is given,
41
+ # return all the .env-vars and their values
42
+ def env_vars(key = '')
43
+ return ENV if key.empty?
44
+ ENV[to_snake(key).upcase]
45
+ end
46
+
47
+ # Search through the system environment variables for a key or if no key
48
+ # is given, return all the system-env-vars and their values
49
+ # @params: [key <String>]: The key to search for
50
+ # @return: Value <String> for the given key or if no key was given, a
51
+ # Hash with { key => value, ... } is returned for all keys with a value.
52
+ # Keys with no value are ommitted from the result.
53
+ def system_vars(key = '')
54
+ if key.empty?
55
+ # Separate key-value pairs from the large string received by `ENV`
56
+ separate_pairs = `ENV`.split(/\n/).map do |string_pair|
57
+ string_pair.split('=')
58
+ end
59
+ # Separate key-value pairs from each other into a hash of
60
+ # { key => value, other_key => other_value }
61
+ # * keys with no value are removed
62
+ separate_pairs.inject({}) do |res, pair|
63
+ res.tap { |h_res| h_res[pair.first] = pair.last unless (pair.first == pair.last) }
64
+ end
65
+ else
66
+ res = `printf "#{to_snake(key).upcase}"`
67
+ return res.empty? ? nil : res
68
+ end
69
+ end
70
+
71
+ # PROJECT_ROOT should be set as early as possible in this Node's initilize
72
+ # method. This method tries to search for it, using #zfind() and if a `nil`
73
+ # result is returned from that search, `pwd` is used as the PROJECT_ROOT.
74
+ # @return: Path to the project root or where ever `pwd` resolves to <Pathname>
75
+ # TODO: improve this...it needs to find the gem's method's caller's project
76
+ # root or at least the gem's method's caller's file's location.
77
+ def project_root
78
+ if @project_root.nil?
79
+ pr = (PROJECT_ROOT rescue nil) or zfind('PROJECT_ROOT') or system('pwd')
80
+ @project_root = Pathname.new(pr)
81
+ else
82
+ @project_root
83
+ end
84
+ end
85
+
86
+ # Manually set the `@project_root` i-var as a `Pathname` object.
87
+ # @param: New path to the project root <String|Pathname>
88
+ # @return: @project_root <Pathname>
89
+ def project_root=(var)
90
+ @project_root = Pathname.new(var)
91
+ end
92
+ end
93
+ end
94
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cloud_powers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: '0.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Phillipps
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-09-24 00:00:00.000000000 Z
11
+ date: 2016-09-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport-core-ext
@@ -152,8 +152,12 @@ dependencies:
152
152
  version: '3.0'
153
153
  description: |2
154
154
  CloudPowers is a wrapper around AWS and other cloud services.
155
- This wrapper was developed specifically for the Brain project
156
- but can be used in any other ruby project.
155
+ It was developed specifically for the Brain project
156
+ but hopefully can be used in any other ruby project that needs to use cloud
157
+ service providers' resources. Version 0.1.5 has a little EC2, S3, SQS and
158
+ Kinesis. This project is actively being developed, so more additions, specs
159
+ and docs will be updated frequently. I always welcome input.
160
+ Enjoy!
157
161
  email: adam.phillipps@gmail.com
158
162
  executables: []
159
163
  extensions: []
@@ -184,6 +188,7 @@ files:
184
188
  - lib/cloud_powers/synapse/synapse.rb
185
189
  - lib/cloud_powers/version.rb
186
190
  - lib/cloud_powers/workflow.rb
191
+ - lib/cloud_powers/zenv.rb
187
192
  homepage: https://github.com/adam-phillipps/cloud_powers
188
193
  licenses:
189
194
  - MIT