cloud_powers 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.test.env.example +27 -0
- data/.travis.yml +1 -2
- data/Gemfile.lock +1 -1
- data/Rakefile +4 -0
- data/cloud_powers.gemspec +1 -1
- data/lib/cloud_powers/aws_resources.rb +5 -0
- data/lib/cloud_powers/helper.rb +9 -0
- data/lib/cloud_powers/self_awareness.rb +7 -4
- data/lib/cloud_powers/synapse/queue.rb +67 -37
- data/lib/cloud_powers/version.rb +1 -1
- data/lib/cloud_powers/zenv.rb +59 -40
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 70615a0db0f98f47026a8d05b715553490dfeb00
|
4
|
+
data.tar.gz: 7e2aca01ee8b38955586ae0d3fb42029d5bc9457
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b1da98d073d2d3fe8af5200fecd185ef8af1cba65672aba687780dd3f1d0b5fe52110b5470467a0d8f01bc40d6fcbf2b4883780e6e60b0d7d69c6a2062edee86
|
7
|
+
data.tar.gz: 6850a5a5e8c354599cd6ed18d437ff28961140ebacbede6047ab9728f544f8cac0219119c31e511424491acaad8e63d3d8c818a2345cd1747091f4e0da0d75fc
|
data/.test.env.example
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# Project information:
|
2
|
+
PROJECT_ROOT=""
|
3
|
+
|
4
|
+
# Aws access:
|
5
|
+
AWS_ACCESS_KEY_ID=""
|
6
|
+
AWS_SECRET_ACCESS_KEY=""
|
7
|
+
|
8
|
+
# Aws areas and auth-related locations:
|
9
|
+
AWS_REGION=""
|
10
|
+
|
11
|
+
# Aws Build info:
|
12
|
+
AMI_NAME="for Cerebrums to create a node"
|
13
|
+
|
14
|
+
# Aws kinesis stream names
|
15
|
+
STATUS_STREAM="e.g. kinesis stream name"
|
16
|
+
|
17
|
+
# Aws s3 buckets etc
|
18
|
+
TASK_STORAGE="e.g. s3 object name"
|
19
|
+
|
20
|
+
# Aws sqs queue addresses
|
21
|
+
BACKLOG_QUEUE_ADDRESS=""
|
22
|
+
WIP_QUEUE_ADDRESS=""
|
23
|
+
FINISHED_QUEUE_ADDRESS=""
|
24
|
+
|
25
|
+
|
26
|
+
# --THIS SHOULD ALWAYS BE THE LAST LINE-- < delete everything below this line for production
|
27
|
+
TESTING="true"
|
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
data/Rakefile
CHANGED
data/cloud_powers.gemspec
CHANGED
@@ -14,7 +14,7 @@ Gem::Specification.new do |spec|
|
|
14
14
|
CloudPowers is a wrapper around AWS and other cloud services.
|
15
15
|
It was developed specifically for the Brain project
|
16
16
|
but hopefully can be used in any other ruby project that needs to use cloud
|
17
|
-
service providers' resources. Version 0.
|
17
|
+
service providers' resources. Version 0.2.3 has a little EC2, S3, SQS and
|
18
18
|
Kinesis. This project is actively being developed, so more additions, specs
|
19
19
|
and docs will be updated frequently. I always welcome input.
|
20
20
|
Enjoy!
|
data/lib/cloud_powers/helper.rb
CHANGED
@@ -12,8 +12,8 @@ module Smash
|
|
12
12
|
extend Smash::CloudPowers::Helper
|
13
13
|
extend Smash::CloudPowers::Synapse::Pipe
|
14
14
|
extend Smash::CloudPowers::Synapse::Queue
|
15
|
+
extend Smash::CloudPowers::Zenv
|
15
16
|
include Smash::CloudPowers::AwsResources
|
16
|
-
include Smash::CloudPowers::Zenv
|
17
17
|
|
18
18
|
# Gets the instance time or the time it was called and as seconds from
|
19
19
|
# epoch
|
@@ -23,7 +23,8 @@ module Smash
|
|
23
23
|
@boot_time ||=
|
24
24
|
ec2.describe_instances(dry_run: zfind('testing'), instance_ids:[@instance_id]).
|
25
25
|
reservations[0].instances[0].launch_time.to_i
|
26
|
-
rescue Aws::EC2::Errors::DryRunOperation => e
|
26
|
+
# rescue Aws::EC2::Errors::DryRunOperation => e
|
27
|
+
rescue Exception => e
|
27
28
|
logger.info "dry run for testing: #{e}"
|
28
29
|
@boot_time ||= Time.now.to_i # comment the code below for development mode
|
29
30
|
end
|
@@ -74,8 +75,10 @@ module Smash
|
|
74
75
|
# get @task_name
|
75
76
|
return @task_name unless @task_name.nil?
|
76
77
|
# set @task_name
|
77
|
-
|
78
|
-
|
78
|
+
# TODO: get all tasks instead of just the first
|
79
|
+
resp = ec2.describe_instances(instance_ids: [id].flatten).reservations.first
|
80
|
+
return @task_name = nil if resp.nil?
|
81
|
+
@task_name = resp.instances[0].tags.select do |t|
|
79
82
|
t.value if t.key == 'taskType'
|
80
83
|
end.first
|
81
84
|
end
|
@@ -1,22 +1,38 @@
|
|
1
1
|
require 'uri'
|
2
|
-
require_relative '../helper'
|
3
2
|
|
4
3
|
module Smash
|
5
4
|
module CloudPowers
|
6
5
|
module Synapse
|
7
|
-
include Smash::CloudPowers::Helper
|
8
|
-
|
9
6
|
module Queue
|
10
|
-
|
7
|
+
include Smash::CloudPowers::Helper
|
8
|
+
include Smash::CloudPowers::AwsResources
|
9
|
+
# Board <Struct>
|
10
|
+
# This groups common functionality for a queue
|
11
|
+
Board = Struct.new(:sqs, :set_name, :set_address, :workflow) do
|
12
|
+
include Smash::CloudPowers::Synapse::Queue
|
11
13
|
include Smash::CloudPowers::Helper
|
14
|
+
include Smash::CloudPowers::Zenv
|
15
|
+
|
12
16
|
def i_var
|
13
17
|
"@#{name}"
|
14
18
|
end
|
15
19
|
|
16
20
|
def address
|
17
|
-
set_address ||
|
18
|
-
|
19
|
-
|
21
|
+
set_address || zfind(set_name) ||
|
22
|
+
"https://sqs.us-west-2.amazonaws.com/#{zfind(:account_number)}/#{name}"
|
23
|
+
end
|
24
|
+
|
25
|
+
def create_queue!
|
26
|
+
sqs.create_queue(queue_name: to_camel(name))
|
27
|
+
self
|
28
|
+
end
|
29
|
+
|
30
|
+
def destroy!
|
31
|
+
sqs.delete_queue(queue_url: address)
|
32
|
+
end
|
33
|
+
|
34
|
+
def message_count
|
35
|
+
get_queue_message_count(address)
|
20
36
|
end
|
21
37
|
|
22
38
|
def name
|
@@ -26,6 +42,20 @@ module Smash
|
|
26
42
|
def next_board
|
27
43
|
workflow.next
|
28
44
|
end
|
45
|
+
|
46
|
+
def pluck_message(board_name = name)
|
47
|
+
pluck_queue_message(board_name)
|
48
|
+
end
|
49
|
+
|
50
|
+
def real?
|
51
|
+
queue_exists?(name)
|
52
|
+
end
|
53
|
+
|
54
|
+
def send_message(message)
|
55
|
+
send_queue_message(
|
56
|
+
address, (valid_json?(message) ? message : message.to_json)
|
57
|
+
)
|
58
|
+
end
|
29
59
|
end # end Board
|
30
60
|
#############################################
|
31
61
|
|
@@ -33,19 +63,17 @@ module Smash
|
|
33
63
|
url.to_s.split('/').last
|
34
64
|
end
|
35
65
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
def create_queue(name)
|
48
|
-
sqs.create_queue(queue_name: to_camel(name))
|
66
|
+
def create_queue!(name)
|
67
|
+
begin
|
68
|
+
Board.new(sqs, to_camel(name)).create_queue!
|
69
|
+
rescue Aws::SQS::Errors::QueueDeletedRecently => e
|
70
|
+
sleep 5
|
71
|
+
retry
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def build_queue(name)
|
76
|
+
Board.new(sqs, to_camel(name))
|
49
77
|
end
|
50
78
|
|
51
79
|
def delete_queue_message(queue, opts = {})
|
@@ -55,31 +83,35 @@ module Smash
|
|
55
83
|
end
|
56
84
|
end
|
57
85
|
|
58
|
-
def
|
86
|
+
def get_queue_message_count(board_url)
|
59
87
|
sqs.get_queue_attributes(
|
60
|
-
queue_url:
|
88
|
+
queue_url: board_url,
|
61
89
|
attribute_names: ['ApproximateNumberOfMessages']
|
62
90
|
).attributes['ApproximateNumberOfMessages'].to_f
|
63
91
|
end
|
64
92
|
|
65
|
-
#
|
66
|
-
# returns a message and deletes it from its origin
|
67
|
-
def
|
93
|
+
# @params: board<String|symbol>: The name of the board
|
94
|
+
# @returns a message and deletes it from its origin
|
95
|
+
def pluck_queue_message(board)
|
68
96
|
poll(board) do |msg, poller|
|
69
97
|
poller.delete_message(msg)
|
70
|
-
return msg
|
98
|
+
return valid_json?(msg.body) ? JSON.parse(msg.body) : msg.body
|
71
99
|
end
|
72
100
|
end
|
73
101
|
|
74
102
|
def poll(board, opts = {})
|
75
103
|
this_poller = poller(board)
|
104
|
+
results = nil
|
76
105
|
this_poller.poll(opts) do |msg|
|
77
|
-
yield msg, this_poller if block_given?
|
106
|
+
results = yield msg, this_poller if block_given?
|
107
|
+
this_poller.delete_message(msg)
|
108
|
+
throw :stop_polling
|
78
109
|
end
|
110
|
+
results
|
79
111
|
end
|
80
112
|
|
81
113
|
def poller(board_name)
|
82
|
-
board = Board.new(board_name)
|
114
|
+
board = Board.new(sqs, board_name)
|
83
115
|
|
84
116
|
unless instance_variable_defined?(board.i_var)
|
85
117
|
instance_variable_set(
|
@@ -91,21 +123,19 @@ module Smash
|
|
91
123
|
end
|
92
124
|
|
93
125
|
def queue_exists?(name)
|
94
|
-
sqs.list_queues(queue_name_prefix: name)
|
126
|
+
!sqs.list_queues(queue_name_prefix: name).queue_urls.empty?
|
127
|
+
end
|
128
|
+
|
129
|
+
def queue_search(name)
|
130
|
+
sqs.list_queues(queue_name_prefix: name).queue_urls
|
95
131
|
end
|
96
132
|
|
97
|
-
def send_queue_message(
|
98
|
-
board = board_info.first.kind_of?(Board) ? board_info.first : Board.new(*board_info)
|
99
|
-
message = message.to_json unless message.kind_of? String
|
133
|
+
def send_queue_message(address, message)
|
100
134
|
sqs.send_message(
|
101
|
-
queue_url:
|
135
|
+
queue_url: address,
|
102
136
|
message_body: message
|
103
137
|
)
|
104
138
|
end
|
105
|
-
|
106
|
-
def sqs
|
107
|
-
@sqs ||= Aws::SQS::Client.new(credentials: Auth.creds)
|
108
|
-
end
|
109
139
|
end
|
110
140
|
end
|
111
141
|
end
|
data/lib/cloud_powers/version.rb
CHANGED
data/lib/cloud_powers/zenv.rb
CHANGED
@@ -7,21 +7,28 @@ module Smash
|
|
7
7
|
# will also use elasticache/redis...some other stuff too, in the coming weeks
|
8
8
|
module Zenv
|
9
9
|
include Smash::CloudPowers::Helper
|
10
|
-
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
10
|
+
|
11
|
+
# Attempts to find a file by searching the current directory for the file
|
12
|
+
# then walking up the file tree and searching at each stop
|
13
|
+
# @param: name <String>: name of the file or directory to find
|
14
|
+
# @return: Pathname: path to the file or directory given as the `name` param
|
15
|
+
def file_tree_search(name)
|
16
|
+
next_dir = Pathname.new(`pwd`.strip).parent
|
17
|
+
current_dir = Pathname.new(`pwd`.strip)
|
18
|
+
until(next_dir == current_dir) do
|
19
|
+
path = Dir.glob("#{current_dir}/#{name}").first
|
20
|
+
return current_dir unless path.nil?
|
21
|
+
current_dir = next_dir
|
22
|
+
next_dir = next_dir.parent
|
23
|
+
end
|
24
|
+
return nil
|
25
|
+
end
|
26
|
+
|
27
|
+
# Search through the .env variables for a key or if no key is given,
|
28
|
+
# return all the .env-vars and their values
|
29
|
+
def env_vars(key = '')
|
30
|
+
return ENV if key.empty?
|
31
|
+
ENV[to_snake(key).upcase]
|
25
32
|
end
|
26
33
|
|
27
34
|
# Search through the instance variables for a key or if no key is given,
|
@@ -37,11 +44,27 @@ module Smash
|
|
37
44
|
self.instance_variable_get(to_i_var(key))
|
38
45
|
end
|
39
46
|
|
40
|
-
#
|
41
|
-
#
|
42
|
-
|
43
|
-
|
44
|
-
|
47
|
+
# PROJECT_ROOT should be set as early as possible in this Node's initilize
|
48
|
+
# method. This method tries to search for it, using #zfind() and if a `nil`
|
49
|
+
# result is returned from that search, `pwd` is used as the PROJECT_ROOT.
|
50
|
+
# @return: Path to the project root or where ever `pwd` resolves to <Pathname>
|
51
|
+
# TODO: improve this...it needs to find the gem's method's caller's project
|
52
|
+
# root or at least the gem's method's caller's file's location.
|
53
|
+
def project_root
|
54
|
+
if @project_root.nil?
|
55
|
+
file_home = Pathname.new(
|
56
|
+
caller_locations.first.path.strip.split(/\//).last).realdirpath.parent
|
57
|
+
# path = (zfind('PROJECT_ROOT') or file_home)
|
58
|
+
@project_root = Pathname.new(file_home)
|
59
|
+
end
|
60
|
+
@project_root
|
61
|
+
end
|
62
|
+
|
63
|
+
# Manually set the `@project_root` i-var as a `Pathname` object.
|
64
|
+
# @param: New path to the project root <String|Pathname>
|
65
|
+
# @return: @project_root <Pathname>
|
66
|
+
def project_root=(var)
|
67
|
+
@project_root = Pathname.new(var)
|
45
68
|
end
|
46
69
|
|
47
70
|
# Search through the system environment variables for a key or if no key
|
@@ -68,26 +91,22 @@ module Smash
|
|
68
91
|
end
|
69
92
|
end
|
70
93
|
|
71
|
-
#
|
72
|
-
#
|
73
|
-
#
|
74
|
-
#
|
75
|
-
#
|
76
|
-
#
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
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)
|
94
|
+
# ZFind looks for the key in a preditermined order of importance:
|
95
|
+
# * i-vars are considered first becuase they might be tracking different
|
96
|
+
# locations for multiple tasks or something like that.
|
97
|
+
# * dotenv files are second because they were manually set, so for sure
|
98
|
+
# it's important
|
99
|
+
# * System Env[@] variables are up next. Hopefully by this time we've found
|
100
|
+
# our information but if not, it should "search" through the system env too.
|
101
|
+
# @params: key <String>: The key to search for
|
102
|
+
# @return: <String>
|
103
|
+
# TODO: implement a search for all 3 that can find close matches
|
104
|
+
def zfind(key)
|
105
|
+
project_root if @project_root.nil?
|
106
|
+
res = (i_vars[to_snake(key).upcase] or
|
107
|
+
env_vars[to_snake(key).upcase] unless @project_root.nil?) or
|
108
|
+
system_vars[to_snake(key).upcase]
|
109
|
+
(res.nil? or res.empty?) ? nil : res
|
91
110
|
end
|
92
111
|
end
|
93
112
|
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.2.
|
4
|
+
version: 0.2.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-
|
11
|
+
date: 2016-09-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport-core-ext
|
@@ -154,7 +154,7 @@ description: |2
|
|
154
154
|
CloudPowers is a wrapper around AWS and other cloud services.
|
155
155
|
It was developed specifically for the Brain project
|
156
156
|
but hopefully can be used in any other ruby project that needs to use cloud
|
157
|
-
service providers' resources. Version 0.
|
157
|
+
service providers' resources. Version 0.2.3 has a little EC2, S3, SQS and
|
158
158
|
Kinesis. This project is actively being developed, so more additions, specs
|
159
159
|
and docs will be updated frequently. I always welcome input.
|
160
160
|
Enjoy!
|
@@ -166,6 +166,7 @@ files:
|
|
166
166
|
- ".gitignore"
|
167
167
|
- ".rspec"
|
168
168
|
- ".ruby-version"
|
169
|
+
- ".test.env.example"
|
169
170
|
- ".travis.yml"
|
170
171
|
- Gemfile
|
171
172
|
- Gemfile.lock
|