s3rsync 0.1.4 → 0.1.5

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,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MzYzODU3N2U1OGFmNzEwNzFlNzRkZWRkMmQ2YTZhY2ZlZTNkM2QyMw==
4
+ MDUyOTRiOWFhOWJjNmY4ZjVkZWI0NzFkY2IwNWFlM2EzNjU5Yzk2Zg==
5
5
  data.tar.gz: !binary |-
6
- YjhlMjJjZmFmNThhNzRkYjYwNWNmNzdjODc2N2Q2OWQwODY2NGE4OQ==
6
+ MTg4ZjgzMDA0YzgxMDM5NmQ4YjIzZDdhMmRhZmYzNzRmYjhjMGY5OA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NTAzOGRlMTc5NWVlMjg5ZGNkNWNhMDVhMzRlNmViYjNmMTIzZGQ3ZmM2MTRl
10
- ZjA5YzMyYzBiNjI2Mzk2OTY1ZjVlYzcwOTZiNzdkYTY2MzYzYmZiMGQ2YjU0
11
- ODdmZGFkNjY1NDM3NjhhOTU0NjFjODMzOTVhZTVjNzYxOTY0MzU=
9
+ ZTM3OGY2NWQzMTVmM2RjN2U2NGZhYmM3Njc1YjIwODM4ZGI4MmNlZDc4YTQw
10
+ YWVlMjBlOTcwYjdhYzI1NzY3ZDY4OGQ1YWRkZTRiMzI0NDYxNDQ2OWY2YmM4
11
+ NDEwMGZiNDQ2OGFjN2I5NTE2MTc4Mjg1ZWExYzg4NDVhY2QwMWM=
12
12
  data.tar.gz: !binary |-
13
- N2U1OGI0OWEyNDIwOWNlMDkwOTljMDVmMTRlMTBlMzM4ZTY4ZGI0ZDk3MTdi
14
- YWNkMzQ1ZmMwYmY5NGIxN2I0NmE3N2NjZmMyODIwZjE0MzQxMzVmMWIyZDY2
15
- MmEyNzhlMGIxMmEwYmQ0ODQ2MjU5YjY5OWYyMGZiZjFkYzBkZmE=
13
+ YmIzZGEwMzZiZmU1NmE4ZmMzNTBmMWE4ZWIxYjFjZWE3MTg3ZmQ1YTBhNjhk
14
+ ZGJlOGZkMjkxM2EzYzU5ZWM5YTNlNmMwNjBjNTNhMzJkNDU3N2RlNjA0NWRl
15
+ M2UyZmEzOTEwOTlmZjVlMmVhMTAyNjNlNGU2ZjcyZWY0YzZiZWU=
data/lib/s3rsync/base.rb CHANGED
@@ -4,8 +4,9 @@ require 'socket'
4
4
 
5
5
  module S3Rsync
6
6
  class Base
7
- def init_logger
8
- logger = Logger.new(STDOUT)
7
+ def init_logger(log_path = nil)
8
+ output_file = log_path.nil? ? STDOUT : log_path
9
+ logger = Logger.new(output_file)
9
10
  logger.level = (!ENV['DEBUG'].nil?) ? Logger::DEBUG : Logger::INFO
10
11
  logger.debug "logger.level: #{logger.level}"
11
12
  logger
data/lib/s3rsync/cli.rb CHANGED
@@ -2,29 +2,31 @@ require 'thor'
2
2
 
3
3
  module S3Rsync
4
4
  class CLI < Thor
5
- DEFAULT_SETTINGS = {
6
- :lock_file_path => "default",
7
- }
8
-
9
- desc "upload", "Upload"
10
- method_option :dir, :aliases => "-d", :required => true
11
- method_option :config_path, :aliases => "-c", :required => true
12
- method_option :s3_prefix, :aliases => "-p", :default => 's3rsync'
5
+
6
+ def self.common_opts
7
+ method_option :path, :aliases => "-p", :required => true
8
+ method_option :'config-path', :aliases => "-c", :required => true
9
+ method_option :'s3-prefix', :aliases => "-s", :default => 's3rsync'
10
+ method_option :'log-path', :aliases => "-l"
11
+ method_option :'dry-run', :aliases => "-d", :type => :boolean, :default => false
12
+ end
13
+
14
+ desc "upload", "sync from local to s3"
15
+ common_opts
13
16
  def upload
14
17
  require 's3rsync/sync'
15
18
  sync = Sync.new(options)
16
- sync.run(:upload)
19
+ exit sync.run(:upload)
17
20
  end
18
21
 
19
- desc "download", "Download"
20
- method_option :dir, :aliases => "-d", :required => true
21
- method_option :config_path, :aliases => "-c", :required => true
22
- method_option :s3_prefix, :aliases => "-p", :default => 's3rsync'
22
+ desc "download", "sync from s3 to local"
23
+ common_opts
23
24
  def download
24
25
  require 's3rsync/sync'
25
26
  sync = Sync.new(options)
26
- sync.run(:download)
27
+ exit sync.run(:download)
27
28
  end
28
-
29
+
29
30
  end
31
+
30
32
  end
data/lib/s3rsync/sync.rb CHANGED
@@ -1,19 +1,19 @@
1
1
  require "s3rsync"
2
2
  require "json"
3
3
  require "lockfile"
4
+ require "erb"
4
5
 
5
6
  module S3Rsync
6
7
  class Sync < Base
7
8
 
8
9
  def initialize(opt)
9
- @log = init_logger
10
+ @log = init_logger(opt['log-path'])
10
11
  @log.debug "opt: #{opt.inspect}"
11
12
  @params = {}
12
- @params[:dir] = opt['dir']
13
- @params[:s3_prefix] = opt['s3_prefix']
14
- @params[:config_path] = opt['config_path']
15
- @s3cmd = 's3cmd'
16
-
13
+ @params[:path] = opt['path'].chomp('/')
14
+ @params[:s3_prefix] = opt['s3-prefix']
15
+ @params[:config_path] = opt['config-path']
16
+ @params[:s3cmd_conf] = @params[:config_path].sub('.json', '_s3rsync.conf')
17
17
  @log.debug "@params[:config_path]: #{@params[:config_path]}"
18
18
  begin
19
19
  conf = JSON.parse File.read(@params[:config_path])
@@ -23,7 +23,7 @@ module S3Rsync
23
23
  raise mgs
24
24
  exit 1
25
25
  end
26
-
26
+
27
27
  @log.debug "conf: #{conf}"
28
28
  @params[:s3_bucket] = conf['env.static.s3.bucket']
29
29
  @params[:s3_access] = conf['env.static.s3.key.user']
@@ -33,19 +33,50 @@ module S3Rsync
33
33
  @log.debug "@params: #{@params.inspect}"
34
34
  @statsd = init_statsd @params[:statsd_host], @params[:statsd_port]
35
35
  @log.debug "@statsd: #{@statsd.inspect}"
36
+
37
+ init_s3cmd_conf
38
+ @s3cmd = "s3cmd -c #{@params[:s3cmd_conf]}"
36
39
  end
37
40
 
38
41
  def get_lock(name = 'common')
39
42
  lockfile = Lockfile.new(
40
43
  "/tmp/s3rsync_#{name}.lock",
41
- :timeout => 10,
44
+ :timeout => 3,
42
45
  :max_age => 3600,
43
46
  :debug => !ENV['DEBUG'].nil?
44
47
  )
45
48
  end
49
+
50
+ def init_s3cmd_conf
51
+ @log.debug "@params[:s3cmd_conf]: #{@params[:s3cmd_conf]}"
52
+ if !File.exists? @params[:s3cmd_conf]
53
+ @log.info "Generating dynamic s3cfg: #{@params[:s3cmd_conf]}"
54
+ begin
55
+ erb_path = File.join(File.dirname(__FILE__), 'templates', 's3cmd_conf.erb')
56
+ erb_template = IO.read erb_path
57
+ @log.debug "erb_template: #{erb_template.inspect}"
58
+ erb = ERB.new erb_template
59
+ rendered_template = erb.result(binding)
60
+ @log.debug "rendered_template: #{rendered_template}"
61
+ File.write @params[:s3cmd_conf], rendered_template
62
+ rescue Exception => e
63
+ @log.error "generating s3cmd config failed: #{e.inspect}"
64
+ exit 1
65
+ end
66
+ @log.info "Dynamic s3cfg generated: #{@params[:s3cmd_conf]}"
67
+ end
68
+ end
46
69
 
47
70
  def run(mode = :upload)
48
- sync_locations = "#{@params[:dir]} s3://#{@params[:s3_bucket]}/#{@params[:s3_prefix]}/"
71
+ init_s3cmd_conf
72
+
73
+ if !Dir.exists?(@params[:path])
74
+ @log.error "Local dir not exist on disk: #{@params[:path]}"
75
+ return 4
76
+ end
77
+
78
+ base_dir_name = @params[:path].split('/').last
79
+ sync_locations = "#{@params[:path]}/ s3://#{@params[:s3_bucket]}/#{@params[:s3_prefix]}/#{base_dir_name}/"
49
80
  @log.debug "original sync_locations: #{sync_locations}"
50
81
  lockfile = get_lock(mode)
51
82
  @log.debug "lockfile: #{lockfile.inspect}"
@@ -63,7 +94,9 @@ module S3Rsync
63
94
  sync_output = 1
64
95
  begin
65
96
  lockfile.lock
66
- sync_output = pass_cli "s3cmd sync #{sync_locations}"
97
+ cmd = "#{@s3cmd} sync #{sync_locations}"
98
+ @log.debug "cmd: #{cmd}"
99
+ sync_output = pass_cli cmd
67
100
  @log.debug "sync_output: #{sync_output}"
68
101
  rescue Exception => e
69
102
  sync_output = 1
@@ -0,0 +1,44 @@
1
+ [default]
2
+ access_key = <%= @params[:s3_access] %>
3
+ bucket_location = US
4
+ cloudfront_host = cloudfront.amazonaws.com
5
+ default_mime_type = binary/octet-stream
6
+ delete_removed = False
7
+ dry_run = False
8
+ enable_multipart = True
9
+ encoding = UTF-8
10
+ encrypt = False
11
+ follow_symlinks = False
12
+ force = False
13
+ get_continue = False
14
+ gpg_command = /usr/bin/gpg
15
+ gpg_decrypt = %(gpg_command)s -d --verbose --no-use-agent --batch --yes --passphrase-fd %(passphrase_fd)s -o %(output_file)s %(input_file)s
16
+ gpg_encrypt = %(gpg_command)s -c --verbose --no-use-agent --batch --yes --passphrase-fd %(passphrase_fd)s -o %(output_file)s %(input_file)s
17
+ gpg_passphrase = <%= @gpg_passphrase %>
18
+ guess_mime_type = True
19
+ host_base = s3.amazonaws.com
20
+ host_bucket = %(bucket)s.s3.amazonaws.com
21
+ human_readable_sizes = False
22
+ invalidate_on_cf = False
23
+ list_md5 = False
24
+ log_target_prefix =
25
+ mime_type =
26
+ multipart_chunk_size_mb = 15
27
+ preserve_attrs = True
28
+ progress_meter = True
29
+ proxy_host =
30
+ proxy_port = 0
31
+ recursive = False
32
+ recv_chunk = 4096
33
+ reduced_redundancy = False
34
+ secret_key = <%= @params[:s3_secret] %>
35
+ send_chunk = 4096
36
+ simpledb_host = sdb.amazonaws.com
37
+ skip_existing = False
38
+ socket_timeout = 300
39
+ urlencoding_mode = normal
40
+ use_https = False
41
+ verbosity = WARNING
42
+ website_endpoint = http://%(bucket)s.s3-website-%(location)s.amazonaws.com/
43
+ website_error =
44
+ website_index = index.html
@@ -1,3 +1,3 @@
1
1
  module S3rsync
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
data/spec/sync_spec.rb CHANGED
@@ -5,21 +5,30 @@ include Helpers
5
5
 
6
6
  describe S3Rsync::Sync do
7
7
  context 'sync is successfull' do
8
- let(:opt) { {
9
- 'dir' => '/test_dir',
10
- 's3_prefix' => 's3rsync',
11
- 'config_path' => 'test_json_conf_path',
12
- } }
8
+ let(:opt) {
9
+ {
10
+ 'path' => '/parent_test_dir/test_dir',
11
+ 's3-prefix' => 's3rsync',
12
+ 'config-path' => 'test_json_conf_path.json',
13
+ }
14
+ }
15
+
16
+ let(:json_conf) {
17
+ {
18
+ 'env.static.s3.bucket' => 'test-bucket',
19
+ 'env.static.s3.key.user' => 'test_access_key',
20
+ 'env.static.s3.key.secret' => 'test_secret_key',
21
+ }
22
+ }
13
23
 
14
- let(:json_conf) { {
15
- 'env.static.s3.bucket' => 'test-bucket',
16
- 'env.static.s3.key.user' => 'test_access_key',
17
- 'env.static.s3.key.secret' => 'test_secret_key',
18
- } }
24
+ let(:s3_conf_path) { opt['config-path'].sub('.json', '_s3rsync.conf') }
25
+ let(:base_sync_path) { opt['path'].split('/').last }
19
26
 
20
27
  before do
21
28
  allow(File).to receive(:read).and_return(nil)
22
- allow(File).to receive(:read).with(opt['config_path']).and_return(json_conf.to_json)
29
+ allow(File).to receive(:read).with(opt['config-path']).and_return(json_conf.to_json)
30
+ allow(Dir).to receive(:exists?).and_return(nil)
31
+ allow(Dir).to receive(:exists?).with(opt['path']).and_return(true)
23
32
  allow_any_instance_of(S3Rsync::Sync).to receive(:pass_cli).and_return(nil)
24
33
  end
25
34
 
@@ -31,16 +40,20 @@ describe S3Rsync::Sync do
31
40
  #Helpers::pre_run_helper
32
41
  end
33
42
 
34
- it "uploads the files" do
35
- cmd = "s3cmd sync #{opt['dir']} s3://#{json_conf['env.static.s3.bucket']}/#{opt['s3_prefix']}/"
43
+ after(:each) do
44
+ File.unlink s3_conf_path
45
+ end
46
+
47
+ it "uploads dir" do
48
+ cmd = "s3cmd -c #{s3_conf_path} sync #{opt['path']}/ s3://#{json_conf['env.static.s3.bucket']}/#{opt['s3-prefix']}/#{base_sync_path}/"
36
49
  allow_any_instance_of(S3Rsync::Sync).to receive(:pass_cli).with(cmd).and_return('Success')
37
50
 
38
51
  sync = S3Rsync::Sync.new(opt)
39
52
  expect(sync.run(:upload)).to eq 0
40
53
  end
41
54
 
42
- it "download the files" do
43
- cmd = "s3cmd sync s3://#{json_conf['env.static.s3.bucket']}/#{opt['s3_prefix']}/ #{opt['dir']}"
55
+ it "downloads dir" do
56
+ cmd = "s3cmd -c #{s3_conf_path} sync s3://#{json_conf['env.static.s3.bucket']}/#{opt['s3-prefix']}/#{base_sync_path}/ #{opt['path']}/"
44
57
  allow_any_instance_of(S3Rsync::Sync).to receive(:pass_cli).with(cmd).and_return('Success')
45
58
 
46
59
  sync = S3Rsync::Sync.new(opt)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: s3rsync
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - idevops
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-03 00:00:00.000000000 Z
11
+ date: 2014-08-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -200,6 +200,7 @@ files:
200
200
  - lib/s3rsync/base.rb
201
201
  - lib/s3rsync/cli.rb
202
202
  - lib/s3rsync/sync.rb
203
+ - lib/s3rsync/templates/s3cmd_conf.erb
203
204
  - lib/s3rsync/version.rb
204
205
  - s3rsync.gemspec
205
206
  - spec/spec_helper.rb