s3log 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 21b8adc04ba1abb0fa4de1cd1ba9d29b53a1a3c0
4
+ data.tar.gz: c2944d29a19ec7acd679bac7afb758db7eeaae52
5
+ SHA512:
6
+ metadata.gz: b4ea2df8459df2321f9ce7c3a449f8bd6ab2d8e33d3d2cba2176d364fefc9e7fc5b7908dd58cb44391a428856a9f11a68cad1def236aee4c48fb795aefa3715e
7
+ data.tar.gz: 7399046d15b3265d0e7b6a08b1368887a243b045071898b0c5ef0061a8a3b6bdf12084b8926e327446d1551776a56211bf013d207c43128edda9637969871fd0
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
19
+
20
+ .ruby-version
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ cache: bundler
3
+ rvm:
4
+ - 2.0.0
data/CHANGELOG.md ADDED
@@ -0,0 +1,6 @@
1
+ S3log Changelog
2
+ -------------------
3
+
4
+ ### v0.0.1 - wip
5
+
6
+ * setup base gem skeleton
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in s3log.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,57 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ s3log (0.0.1)
5
+ aws-sdk
6
+ thor
7
+ whenever
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ activesupport (4.0.2)
13
+ i18n (~> 0.6, >= 0.6.4)
14
+ minitest (~> 4.2)
15
+ multi_json (~> 1.3)
16
+ thread_safe (~> 0.1)
17
+ tzinfo (~> 0.3.37)
18
+ atomic (1.1.14)
19
+ aws-sdk (1.32.0)
20
+ json (~> 1.4)
21
+ nokogiri (>= 1.4.4)
22
+ uuidtools (~> 2.1)
23
+ chronic (0.10.2)
24
+ diff-lcs (1.2.5)
25
+ i18n (0.6.9)
26
+ json (1.8.1)
27
+ mini_portile (0.5.2)
28
+ minitest (4.7.5)
29
+ multi_json (1.8.4)
30
+ nokogiri (1.6.1)
31
+ mini_portile (~> 0.5.0)
32
+ rake (10.1.1)
33
+ rspec (2.14.1)
34
+ rspec-core (~> 2.14.0)
35
+ rspec-expectations (~> 2.14.0)
36
+ rspec-mocks (~> 2.14.0)
37
+ rspec-core (2.14.7)
38
+ rspec-expectations (2.14.4)
39
+ diff-lcs (>= 1.1.3, < 2.0)
40
+ rspec-mocks (2.14.4)
41
+ thor (0.18.1)
42
+ thread_safe (0.1.3)
43
+ atomic
44
+ tzinfo (0.3.38)
45
+ uuidtools (2.1.4)
46
+ whenever (0.9.0)
47
+ activesupport (>= 2.3.4)
48
+ chronic (>= 0.6.3)
49
+
50
+ PLATFORMS
51
+ ruby
52
+
53
+ DEPENDENCIES
54
+ bundler (~> 1.3)
55
+ rake
56
+ rspec
57
+ s3log!
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Faria Systems Ltd.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # S3log
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/s3log.png)](http://rubygems.org/gems/s3log)
4
+ [![Dependency Status](https://gemnasium.com/eduvo/s3log.png)](https://gemnasium.com/eduvo/s3log)
5
+
6
+ Downloader for aws S3 logs and agglomerator.
7
+
8
+ On S3 buckets you can activate logging, but the options are quite limited. You can point the logging to another bucket in the same region only and logging will generate a lot of small logging files, and will not delete them. It's not very convenient for debugging.
9
+
10
+ S3log script is intended to transform this logging in a unified logfile wherever you need it, removing the logfiles from the logging bucket when they are downloaded.
11
+
12
+ It's designed to run on debian/ubuntu servers but should work on any linux server, and it also work on mac osx.
13
+
14
+ (work in progress, not ready for use yet).
15
+
16
+ ## Installation
17
+
18
+ gem install s3log
19
+ s3log init # this will create a s3log/ dir at your current location
20
+
21
+ cd s3log/
22
+ vi config.yml # customize according to your need
23
+ s3log buckets # this will verify the list of buckets that your credential give access to
24
+ s3log list # this will list the logfiles that will be candidate for downloading
25
+
26
+ ## Usage
27
+
28
+ First you need to edit the config.yml file. The purpose is to have one config file per bucket you want to retrieve, so you can create as many config files as you need.
29
+
30
+ * jobname: the unique identifier for that job
31
+ * awspublic: the AWS access key
32
+ * awsprivate: the AWS secret key id
33
+ * bucket: the name of the bucket, with no s3:// prefix
34
+ * prefix: the path and prefix the same way you specified it in the logging setup
35
+ * logdir: the local dir where s3log logs are kept
36
+ * loglevel: the level of logging. use warn for only error display, info to record info on operations, debug to have much more details
37
+ * outputfile: the faile where the downloaded logs are going to be appended
38
+ * schedule: the cron formatted frequency of downloading the logs from bucket ie. `0/5 * * * *`
39
+
40
+ When your configuration is ready, veridy that it works with
41
+
42
+ s3log list
43
+ s3log -c another_config.yml schecule
44
+
45
+ Then you can update the user crontab for each config file.
46
+
47
+ s3log schedule
48
+ s3log -c another_config.yml schedule
49
+
50
+ The `schedule` command will update the user crontab, that you can verify with `crontab -l`.
51
+ In all s3log commands if you don't specify the config file with `-c`, it will use `config.yml`.
52
+
53
+ ## Contributing
54
+
55
+ 1. Fork it
56
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
57
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
58
+ 4. Push to the branch (`git push origin my-new-feature`)
59
+ 5. Create new Pull Request
60
+
61
+ ## author
62
+
63
+ * mose
64
+
65
+ ## License
66
+
67
+ Copyright (c) 2014 Faria Systems Inc. distributed under MIT license
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/s3log ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler"
4
+ require "s3log"
5
+
6
+ S3log::Cli.start
data/lib/s3log/cli.rb ADDED
@@ -0,0 +1,52 @@
1
+ require "thor"
2
+
3
+ module S3log
4
+
5
+ class Cli < Thor
6
+ include Thor::Actions
7
+
8
+ def self.source_root
9
+ File.expand_path("../../../templates", __FILE__)
10
+ end
11
+
12
+ default_task :help
13
+ class_option :configfile,
14
+ aliases: "-c",
15
+ banner: "PATH",
16
+ default: File.expand_path("config.yml", Dir.pwd),
17
+ desc: "Path to the configuration file to use"
18
+
19
+ desc "init", "Creates a s3log dir with default config files."
20
+ def init(name="s3log")
21
+ directory "s3log", name
22
+ end
23
+
24
+ desc "list", "Lists log files that are waiting on the S3 bucket."
25
+ def list
26
+ @s3log = S3log::Runner.new(options[:configfile])
27
+ @s3log.items.each do |o|
28
+ puts o
29
+ end
30
+ end
31
+
32
+ desc "buckets", "List available buckets."
33
+ def buckets
34
+ @s3log = S3log::Runner.new(options[:configfile])
35
+ @s3log.buckets
36
+ end
37
+
38
+ desc "download", "Downloads and delete logs from the logging S3 bucket."
39
+ def download
40
+ @s3log = S3log::Runner.new(options[:configfile])
41
+ @s3log.download
42
+ end
43
+
44
+ desc "schedule", "Install cronjob for S3 logs download and agglomeration."
45
+ def schedule
46
+ @cron = S3log::Cron.new(options[:configfile])
47
+ @cron.update
48
+ end
49
+
50
+ end
51
+
52
+ end
data/lib/s3log/cron.rb ADDED
@@ -0,0 +1,48 @@
1
+ module S3log
2
+ class Cron
3
+
4
+ def initialize(configfile)
5
+ @configfile = configfile
6
+ @config = YAML::load_file(configfile)
7
+ @path = File.dirname(configfile)
8
+ @jobname = @config['jobname']
9
+ @schedule = @config['schedule']
10
+ @logdir = @config['logdir']
11
+ FileUtils.mkdir(@logdir) unless Dir.exists? @logdir
12
+ S3log::Log.set_logger(File.join(@logdir, 's3log.log'), @config['loglevel'])
13
+ end
14
+
15
+ def update
16
+ line = "#{@schedule} cd #{@path} && bash -l -c 'bundle exec s3log -c #{@configfile} download >> /dev/null 2>&1' # s3log_#{@jobname}\n"
17
+ tmp_cron_file = Tempfile.open('tmp_cron')
18
+ included = false
19
+ existing.each_line do |l|
20
+ if l =~ Regexp.new("# s3log_#{@jobname}")
21
+ tmp_cron_file << line
22
+ included = true
23
+ else
24
+ tmp_cron_file << l
25
+ end
26
+ end
27
+ tmp_cron_file << line unless included
28
+ tmp_cron_file.fsync
29
+ if system("crontab #{tmp_cron_file.path}")
30
+ S3log::Log.info "[update] crontab updated."
31
+ else
32
+ S3log::Log.warn "[fail] Couldn't write crontab."
33
+ tmp_cron_file.close!
34
+ exit(1)
35
+ end
36
+ end
37
+
38
+ def existing
39
+ existing = %x(crontab -l 2> /dev/null)
40
+ if $?.exitstatus.zero?
41
+ existing
42
+ else
43
+ ""
44
+ end
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,55 @@
1
+ require "logger"
2
+
3
+ module S3log
4
+ module Log
5
+ extend self
6
+
7
+ def set_logger(logfile, level="info")
8
+ if logfile.is_a?(String) && !Dir.exists?(File.dirname(logfile))
9
+ FileUtils.mkdir_p(File.dirname(logfile))
10
+ end
11
+ @logger = ::Logger.new(logfile)
12
+ @logger.level = get_level_constant(level)
13
+ @logger.formatter = proc do |severity, datetime, progname, msg|
14
+ "[#{datetime.utc}] #{severity}: #{msg}\n"
15
+ end
16
+ end
17
+
18
+ def logger
19
+ @logger ||= ::Logger.new(STDOUT)
20
+ end
21
+
22
+ def debug(msg)
23
+ logger.debug(msg)
24
+ end
25
+
26
+ def info(msg)
27
+ logger.info(msg)
28
+ end
29
+
30
+ def warn(msg)
31
+ logger.warn(msg)
32
+ end
33
+
34
+ def error(msg)
35
+ logger.error(msg)
36
+ end
37
+
38
+ def fatal(msg)
39
+ logger.fatal(msg)
40
+ end
41
+
42
+ def get_level_constant(level)
43
+ if level
44
+ begin
45
+ ::Logger.const_get(level.to_s.upcase)
46
+ rescue NameError
47
+ return ::Logger::INFO
48
+ end
49
+ else
50
+ ::Logger::INFO
51
+ end
52
+ end
53
+
54
+ end
55
+ end
@@ -0,0 +1,49 @@
1
+ require 'aws-sdk'
2
+
3
+ module S3log
4
+
5
+ class Runner
6
+
7
+ def initialize(configfile)
8
+ @config = YAML::load_file(configfile)
9
+ @s3 = AWS::S3.new(
10
+ access_key_id: @config['awspublic'],
11
+ secret_access_key: @config['awsprivate']
12
+ )
13
+ @bucket = @s3.buckets[@config['bucket']]
14
+ @prefix = @config['prefix']
15
+ @logdir = @config['logdir']
16
+ FileUtils.mkdir(@logdir) unless Dir.exists? @logdir
17
+ S3log::Log.set_logger(File.join(@logdir, 's3log.log'), @config['loglevel'])
18
+ end
19
+
20
+ def items
21
+ @_items ||= @bucket.objects.with_prefix(@prefix).collect(&:key)
22
+ end
23
+
24
+ def buckets
25
+ @s3.buckets.each do |bucket|
26
+ puts bucket.name
27
+ end
28
+ end
29
+
30
+ def download
31
+ if items.size > 0
32
+ time = Time.now
33
+ S3log::Log.info "Downloading #{items.size} file."
34
+ File.open(@config['outputfile'], 'a+') do |f|
35
+ items.each do |i|
36
+ f.puts @bucket.objects[i].read
37
+ S3log::Log.debug " #{i} added."
38
+ @bucket.objects[i].delete
39
+ end
40
+ end
41
+ S3log::Log.info "... done in #{Time.now - time}s."
42
+ else
43
+ S3log::Log.debug "No file to download."
44
+ end
45
+ end
46
+
47
+ end
48
+
49
+ end
@@ -0,0 +1,3 @@
1
+ module S3log
2
+ VERSION = "0.0.1"
3
+ end
data/lib/s3log.rb ADDED
@@ -0,0 +1,8 @@
1
+ require "s3log/version"
2
+ require "s3log/cli"
3
+ require "s3log/runner"
4
+ require "s3log/logger"
5
+ require "s3log/cron"
6
+
7
+ module S3log
8
+ end
data/s3log.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 's3log/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "s3log"
8
+ spec.version = S3log::VERSION
9
+ spec.authors = ["mose"]
10
+ spec.email = ["mose@mose.com"]
11
+ spec.description = %q{Downloader for aws S3 logs.}
12
+ spec.summary = %q{Downloader for aws S3 logs, scheduler agglomerator.}
13
+ spec.homepage = "https://github.com/eduvo/s3log"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^spec/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "thor"
22
+ spec.add_dependency "aws-sdk"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rspec"
26
+ spec.add_development_dependency "rake"
27
+ end
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+ require 's3log/cli'
5
+
6
+ describe S3log::Cli do
7
+
8
+ before :each do
9
+ @cli = S3log::Cli.new([], { 'configfile' => 'config.yml' })
10
+ @testdir = File.join('spec','files','init')
11
+ @cli.shell.mute do
12
+ @cli.init(@testdir)
13
+ end
14
+ @oldpwd = Dir.pwd
15
+ Dir.chdir @testdir
16
+ end
17
+
18
+ after :each do
19
+ Dir.chdir @oldpwd
20
+ FileUtils.rm_rf @testdir if Dir.exists? @testdir
21
+ end
22
+
23
+ it "init creates a working directory" do
24
+ expect(File.file? 'config.yml').to be_true
25
+ end
26
+
27
+ end
@@ -0,0 +1,10 @@
1
+ $LOAD_PATH << File.expand_path('../../lib', __FILE__)
2
+ require 'rubygems'
3
+ require 'bundler'
4
+
5
+ RSpec.configure do |config|
6
+ config.mock_with :rspec
7
+ config.expect_with :rspec do |c|
8
+ c.syntax = :expect
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "s3log"
@@ -0,0 +1,10 @@
1
+ ---
2
+ jobname: job
3
+ awspublic: xxx
4
+ awsprivate: xxx
5
+ bucket: bucketname # no s3:// prefix, just the name
6
+ prefix: "somedir/prefix_" # the same as prefix stated in logging interface for the logged bucket
7
+ logdir: logs
8
+ loglevel: info
9
+ outputfile: s3.log
10
+ schedule: "0/5 * * * *"
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: s3log
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - mose
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: aws-sdk
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Downloader for aws S3 logs.
84
+ email:
85
+ - mose@mose.com
86
+ executables:
87
+ - s3log
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".travis.yml"
93
+ - CHANGELOG.md
94
+ - Gemfile
95
+ - Gemfile.lock
96
+ - LICENSE
97
+ - README.md
98
+ - Rakefile
99
+ - bin/s3log
100
+ - lib/s3log.rb
101
+ - lib/s3log/cli.rb
102
+ - lib/s3log/cron.rb
103
+ - lib/s3log/logger.rb
104
+ - lib/s3log/runner.rb
105
+ - lib/s3log/version.rb
106
+ - s3log.gemspec
107
+ - spec/lib/s3log/cli_spec.rb
108
+ - spec/spec_helper.rb
109
+ - templates/s3log/Gemfile
110
+ - templates/s3log/config.yml
111
+ homepage: https://github.com/eduvo/s3log
112
+ licenses:
113
+ - MIT
114
+ metadata: {}
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 2.0.14
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: Downloader for aws S3 logs, scheduler agglomerator.
135
+ test_files:
136
+ - spec/lib/s3log/cli_spec.rb
137
+ - spec/spec_helper.rb