huginn_s3_file_agent 0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/huginn_s3_file_agent/s3_file_agent.rb +99 -0
- data/lib/huginn_s3_file_agent.rb +4 -0
- data/spec/s3_file_agent_spec.rb +11 -0
- metadata +89 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 503b9d14836803e3313021cfd8bbca9f536f5a7e
|
4
|
+
data.tar.gz: 78b971b20a571ff35ab8ee9c069d804790c32cfb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 44b0033da7ba3d086999b9bfdd3d04a5f66016f802603e33c62229d3b99a45a323c6ca0157ce95c92248373c223e92c2e1292a2827d9fc259cf92fae08ed61d3
|
7
|
+
data.tar.gz: d1ded59b85ae5ab7f0dac6a10fe58ca054666d8b581d332f265315cfd9104bb230e0a384adc32c389aee3af9e9906b1c9666ed9a0169fdc69ca0555957b18c43
|
@@ -0,0 +1,99 @@
|
|
1
|
+
module Agents
|
2
|
+
class S3FileAgent < Agent
|
3
|
+
include FormConfigurable
|
4
|
+
|
5
|
+
cannot_be_scheduled!
|
6
|
+
|
7
|
+
description <<-MD
|
8
|
+
Uploading files to S3.
|
9
|
+
MD
|
10
|
+
|
11
|
+
def default_options
|
12
|
+
{
|
13
|
+
'access_key_id': '',
|
14
|
+
'access_key_secret': '',
|
15
|
+
'bucket': '',
|
16
|
+
'local_path': '',
|
17
|
+
'remote_path': '',
|
18
|
+
'acl': 'Public'
|
19
|
+
}
|
20
|
+
end
|
21
|
+
|
22
|
+
form_configurable :access_key_id, roles: :validatable
|
23
|
+
form_configurable :access_key_secret, roles: :validatable
|
24
|
+
form_configurable :bucket, roles: :completable
|
25
|
+
form_configurable :local_path
|
26
|
+
form_configurable :remote_path
|
27
|
+
form_configurable :acl, type: :array, values: ['Public', 'Private']
|
28
|
+
form_configurable :region, type: :array, values: %w(us-east-1 us-west-1 us-west-2 eu-west-1 eu-central-1 ap-southeast-1 ap-southeast-2 ap-northeast-1 ap-northeast-2 sa-east-1)
|
29
|
+
|
30
|
+
def validate_options
|
31
|
+
errors.add(:base, '\'local_path\' needs to be present') if options['local_path'].blank?
|
32
|
+
errors.add(:base, '\'remote_path\' needs to be present') if options['remote_path'].blank?
|
33
|
+
errors.add(:base, '\'region\' needs to be present') if options['region'].blank?
|
34
|
+
errors.add(:base, '\'acl\' needs to be present') if options['acl'].blank?
|
35
|
+
end
|
36
|
+
|
37
|
+
def validate_access_key_id
|
38
|
+
!!buckets
|
39
|
+
end
|
40
|
+
|
41
|
+
def validate_access_key_secret
|
42
|
+
!!buckets
|
43
|
+
end
|
44
|
+
|
45
|
+
def complete_bucket
|
46
|
+
(buckets || []).collect { |room| {text: room.name, id: room.name} }
|
47
|
+
end
|
48
|
+
|
49
|
+
def working?
|
50
|
+
received_event_without_error?
|
51
|
+
end
|
52
|
+
|
53
|
+
def receive(incoming_events)
|
54
|
+
log('S3 File Agent - Received event')
|
55
|
+
|
56
|
+
incoming_events.each do |event|
|
57
|
+
interpolate_with(event) do
|
58
|
+
log("Uploading \"#{interpolated['local_path']}\" to \"#{interpolated['bucket']}##{interpolated['remote_path']}\"")
|
59
|
+
|
60
|
+
event_data = interpolated(event)
|
61
|
+
|
62
|
+
safely do
|
63
|
+
client.put_object(
|
64
|
+
acl: 'private',
|
65
|
+
bucket: interpolated['bucket'],
|
66
|
+
key: event_data['remote_path'],
|
67
|
+
body: File.read(event_data['local_path'])
|
68
|
+
)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def client
|
75
|
+
@client ||= Aws::S3::Client.new(
|
76
|
+
credentials: aws_credentials,
|
77
|
+
region: interpolated['region']
|
78
|
+
)
|
79
|
+
end
|
80
|
+
|
81
|
+
def aws_credentials
|
82
|
+
Aws::Credentials.new(interpolated['access_key_id'], interpolated['access_key_secret'])
|
83
|
+
end
|
84
|
+
|
85
|
+
def safely
|
86
|
+
yield
|
87
|
+
rescue Aws::S3::Errors::AccessDenied => e
|
88
|
+
error("Could not access '#{interpolated['bucket']}' #{e.class} #{e.message}")
|
89
|
+
rescue Aws::S3::Errors::ServiceError =>e
|
90
|
+
error("#{e.class}: #{e.message}")
|
91
|
+
end
|
92
|
+
|
93
|
+
def buckets(log = false)
|
94
|
+
@buckets ||= client.list_buckets.buckets
|
95
|
+
rescue Aws::S3::Errors::ServiceError => e
|
96
|
+
false
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'rails_helper'
|
2
|
+
require 'huginn_agent/spec_helper'
|
3
|
+
|
4
|
+
describe Agents::S3FileAgent do
|
5
|
+
before(:each) do
|
6
|
+
@valid_options = Agents::S3FileAgent.new.default_options
|
7
|
+
@checker = Agents::S3FileAgent.new(name: 'S3FileAgent', options: @valid_options)
|
8
|
+
@checker.user = users(:bob)
|
9
|
+
@checker.save!
|
10
|
+
end
|
11
|
+
end
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: huginn_s3_file_agent
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Samuel Garneau
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-11-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: huginn_agent
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Write a longer description or delete this line.
|
56
|
+
email:
|
57
|
+
- sam@garno.me
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- lib/huginn_s3_file_agent.rb
|
63
|
+
- lib/huginn_s3_file_agent/s3_file_agent.rb
|
64
|
+
- spec/s3_file_agent_spec.rb
|
65
|
+
homepage:
|
66
|
+
licenses: []
|
67
|
+
metadata: {}
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options: []
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
requirements: []
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 2.5.1
|
85
|
+
signing_key:
|
86
|
+
specification_version: 4
|
87
|
+
summary: Write a short summary, because Rubygems requires one.
|
88
|
+
test_files:
|
89
|
+
- spec/s3_file_agent_spec.rb
|