aws_pipes 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
+ ### Communicating
2
+
1
3
  Send messages between Amazon EC2 instances through Unix pipes.
2
4
 
3
- This gem is built on top of the Amazon [Simple Queue
5
+ Communication in aws_pipes is built on top of the Amazon [Simple Queue
4
6
  Service](http://aws.amazon.com/sqs/) (SQS) which lets you
5
7
 
6
8
  - Move data between distributed components of your application without
@@ -15,8 +17,21 @@ Text is the universal interface, and any application that can read and
15
17
  write text can use this gem – no knowledge of the Amazon API is
16
18
  required.
17
19
 
20
+ ### Logging
21
+
22
+ Consolidate logs between EC2 instances. Logging in aws_pipes is built on
23
+ top of Amazon [SimpleDB](http://aws.amazon.com/simpledb/).
24
+
25
+ - Get logs off individual servers to save disk space.
26
+ - Pool the log messages from related workers.
27
+ - Monitor and query logs from one place.
28
+ - Save as much log history as you want, the storage is virtually
29
+ unlimited.
30
+
18
31
  ## Usage
19
32
 
33
+ ### aws_queue
34
+
20
35
  # write data to an SQS queue named "foo"
21
36
  your_program | aws_queue write foo
22
37
 
@@ -26,6 +41,26 @@ required.
26
41
  To use this program you will need to [create a
27
42
  queue](https://console.aws.amazon.com/sqs/) in the Amazon Web Console.
28
43
 
44
+ ### aws_log
45
+
46
+ # write stderr to log named "bar"
47
+ your_program 2> >(aws_log record bar)
48
+
49
+ # delete all messages in log named "bar"
50
+ aws_log delete bar
51
+
52
+ # View log entries for "bar" within a date range
53
+ aws_log show bar --after "1970-01-01" --before "2020-02-02 13:42:12.123"
54
+
55
+ You can combine queuing and logging in
56
+ a single command using Bash [process
57
+ substitution](http://www.gnu.org/software/bash/manual/bashref.html#Proce
58
+ ss-Substitution):
59
+
60
+ # write stdout to an SQS queue named "foo"
61
+ # while logging stderr to a log named "bar"
62
+ your_program 1> >(aws_queue write foo) 2> >(aws_log record bar)
63
+
29
64
  ## Installation
30
65
 
31
66
  1. Sign up for an [AWS account](http://aws.amazon.com/).
@@ -34,9 +69,9 @@ queue](https://console.aws.amazon.com/sqs/) in the Amazon Web Console.
34
69
  AWS_ACCESS_KEY accordingly.
35
70
  1. Run `gem install aws_pipes` from the command line.
36
71
 
37
- This will install the `aws_queue` command to your path. If you haven't
38
- stored your Amazon credentials in environment variables, you can pass
39
- them in as command line options. For more info, run
72
+ This will install the `aws_queue` and `aws_log` commands to your path.
73
+ If you haven't stored your Amazon credentials in environment variables,
74
+ you can pass them in as command line options. For more info, run
40
75
 
41
76
  aws_queue --help
42
77
 
@@ -52,6 +87,7 @@ queue:
52
87
 
53
88
  Then have each worker pull from the `to_be_downloaded` queue and
54
89
  repeatedly run a command to download each url. The queue supports many
55
- simultaneous readers and prevents duplicate work.
90
+ simultaneous readers and prevents duplicate work. We save any errors to
91
+ a log named "downloader" which we can monitor remotely.
56
92
 
57
- aws_queue read to_be_downloaded | xargs -L1 wget
93
+ aws_queue read to_be_downloaded | xargs -L1 wget -nv 2> >(aws_log record downloader)
data/bin/aws_log ADDED
@@ -0,0 +1,74 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/aws_pipes'
4
+ require 'trollop'
5
+ require 'aws-sdk'
6
+ require 'open-uri'
7
+
8
+ opts = AwsPipes.common_aws_options(
9
+ "aws_log",
10
+ <<-EOS
11
+ Log messages to Amazom SimpleDB with timestamp and external ip.
12
+
13
+ Usage:
14
+ aws_log [options] record <log-name>
15
+ Log each line of STDIN as a message to Amazon SimpleDB
16
+
17
+ aws_log [options] show <log-name>
18
+ Display log
19
+
20
+ aws_log [options] delete <log-name>
21
+ Delete all messages in log
22
+ EOS
23
+ ) do
24
+ opt :'include-ip', "Include external ip in logs", :type => :boolean, :default => true
25
+ opt :count, "Only show how many messages are in log", :type => :boolean, :default => false
26
+ opt :before, "Show messages recorded before timestamp", :type => :string
27
+ opt :after, "Show messages recorded after timestamp", :type => :string
28
+ end
29
+
30
+ operation = ARGV.shift
31
+ unless %w(record show delete).include? operation
32
+ Trollop::die "Unknown operation \"#{operation}\" -- expecting \"record\", \"show\", or \"delete\""
33
+ end
34
+
35
+ if opts[:'include-ip']
36
+ match = /([\d\.]+)/.match open('http://checkip.dyndns.org').read
37
+ external_ip = match[1] if match
38
+ end
39
+
40
+ sdb = AWS::SimpleDB.new(
41
+ :access_key_id => AwsPipes.access_key_id(opts),
42
+ :secret_access_key => AwsPipes.secret_access_key(opts)
43
+ )
44
+
45
+ log_name = ARGV.shift
46
+ Trollop::die "Missing log (SimpleDB domain) name" unless log_name
47
+
48
+ messages = sdb.domains.create(log_name).items
49
+ messages = messages.where("itemName() <= \"#{opts[:before]}\"") if opts[:before]
50
+ messages = messages.where("itemName() >= \"#{opts[:after]}\"") if opts[:after]
51
+
52
+ begin
53
+ case operation
54
+ when 'record'
55
+ $stdin.sync = true
56
+ while msg = gets
57
+ messages.create Time.now.strftime("%Y-%m-%d %H:%M:%S.%L"),
58
+ { ip: external_ip || 'none', message: msg.delete("\n") }
59
+ end
60
+ when 'show'
61
+ if opts[:count]
62
+ puts messages.count
63
+ else
64
+ printf "%-26s%-18s%s\n", 'Timestamp', 'Public IP', 'Message'
65
+ messages.each do |item|
66
+ printf "%-26s%-18s%s\n", item.name, item.attributes[:ip].values[0], item.attributes[:message].values[0]
67
+ end
68
+ end
69
+ when 'delete'
70
+ sdb.domains[log_name].delete!
71
+ end
72
+ rescue Interrupt
73
+ exit 0
74
+ end
data/bin/aws_queue CHANGED
@@ -1,41 +1,26 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
+ require_relative '../lib/aws_pipes'
3
4
  require 'trollop'
4
5
  require 'aws-sdk'
5
- require_relative '../lib/aws_pipes/version'
6
6
 
7
- ## USAGE ###########################################################################
8
-
9
- opts = Trollop::options do
10
- version "aws_queue #{AwsPipes::VERSION} (c) 2013 Joe Nelson"
11
- banner <<-EOS
7
+ opts = AwsPipes.common_aws_options(
8
+ "aws_queue",
9
+ <<-EOS
12
10
  Read and write Amazon SQS through unix pipes.
13
11
 
14
- You may provide Amazon authentication through these environment variables:
15
- AWS_ACCESS_KEY_ID - AWS access key id
16
- AWS_ACCESS_KEY - AWS secret access key
17
-
18
12
  Usage:
19
13
  aws_queue [options] write <queue-name>
20
14
  Send STDIN to an Amazon SQS queue, one message per line.
21
15
 
22
16
  aws_queue [options] read <queue-name>
23
17
  Print messages from Amazon SQS queue to STDOUT, one message per line.
24
-
25
- where [options] are:
26
18
  EOS
19
+ )
27
20
 
28
- opt :keyid, "AWS access key id", :type => :string
29
- opt :key, "AWS secret access key", :type => :string
30
- end
31
-
32
- ## SETUP ###########################################################################
33
-
34
- access_key_id = ENV['AWS_ACCESS_KEY_ID'] || opts[:keyid] || Trollop::die("Missing access key id")
35
- secret_access_key = ENV['AWS_ACCESS_KEY'] || opts[:key] || Trollop::die("Missing secret access key")
36
21
  sqs = AWS::SQS.new(
37
- :access_key_id => access_key_id,
38
- :secret_access_key => secret_access_key
22
+ :access_key_id => AwsPipes.access_key_id(opts),
23
+ :secret_access_key => AwsPipes.secret_access_key(opts)
39
24
  )
40
25
 
41
26
  operation = ARGV.shift
@@ -46,8 +31,6 @@ end
46
31
  q_name = ARGV.shift
47
32
  Trollop::die "Missing queue name" unless q_name
48
33
 
49
- ## OPERATION #######################################################################
50
-
51
34
  begin
52
35
  q = sqs.queues.named q_name
53
36
  case operation
@@ -66,4 +49,6 @@ rescue AWS::SQS::Errors::NonExistentQueue
66
49
  warn "There is no queue named \"#{q_name}\" for your AWS account."
67
50
  warn "To create this and other queues, visit https://console.aws.amazon.com/sqs/"
68
51
  exit 1
52
+ rescue Interrupt
53
+ exit 0
69
54
  end
data/lib/aws_pipes.rb CHANGED
@@ -1,5 +1,34 @@
1
1
  require "aws_pipes/version"
2
+ require 'trollop'
2
3
 
3
4
  module AwsPipes
4
- # Your code goes here...
5
+ def AwsPipes.common_aws_options program_name, custom_banner, &more_opts
6
+ ::Trollop::options do
7
+ version "#{program_name} #{VERSION} (c) 2013 Joe Nelson"
8
+ banner <<-EOS
9
+ #{custom_banner}
10
+ You may provide Amazon authentication through these environment variables:
11
+ AWS_ACCESS_KEY_ID - AWS access key id
12
+ AWS_ACCESS_KEY - AWS secret access key
13
+
14
+ Additional options:
15
+ EOS
16
+ opt :keyid, "AWS access key id", :type => :string
17
+ opt :key, "AWS secret access key", :type => :string
18
+
19
+ self.instance_eval &more_opts if more_opts
20
+ end
21
+ end
22
+
23
+ def AwsPipes.access_key_id opts
24
+ ENV['AWS_ACCESS_KEY_ID'] ||
25
+ opts[:keyid] ||
26
+ ::Trollop::die("Missing access key id")
27
+ end
28
+
29
+ def AwsPipes.secret_access_key opts
30
+ ENV['AWS_ACCESS_KEY'] ||
31
+ opts[:key] ||
32
+ ::Trollop::die("Missing secret access key")
33
+ end
5
34
  end
@@ -1,3 +1,3 @@
1
1
  module AwsPipes
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aws_pipes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-23 00:00:00.000000000 Z
12
+ date: 2012-12-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: aws-sdk
@@ -47,6 +47,7 @@ description: Send messages between Amazon EC2 instances through Unix pipes.
47
47
  email:
48
48
  - cred+github@begriffs.com
49
49
  executables:
50
+ - aws_log
50
51
  - aws_queue
51
52
  extensions: []
52
53
  extra_rdoc_files: []
@@ -57,6 +58,7 @@ files:
57
58
  - README.md
58
59
  - Rakefile
59
60
  - aws_pipes.gemspec
61
+ - bin/aws_log
60
62
  - bin/aws_queue
61
63
  - lib/aws_pipes.rb
62
64
  - lib/aws_pipes/version.rb