backup-baidu 0.1.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: d35dcf3395690cead523fc1e159c1a618196a302
4
+ data.tar.gz: badbe163ddf969886d04e5b27b488ea118e157fe
5
+ SHA512:
6
+ metadata.gz: 7c45b82015918acc36289788f62854b5585b71a8a1044acb7e0a0fe0090d19f84a2494145f2cd1409b5893c9abf60bafdc28d2fcc1555409c32a3eb97b6b67c4
7
+ data.tar.gz: 2cc87037cf29b87c148953a94cf0e5d8af18982a057d0c9f1c8c3ca0eefe07de5a207cb1fe8d7b7af23d5e24b941c94fd69af8ffad4ac8ba2f2b90cefc388e4a
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --format documentation --color spec --drb
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+ # source 'http://ruby.taobao.org'
3
+
4
+ # Specify your gem's dependencies in backup-baidu.gemspec
5
+ gemspec
6
+ gem 'rspec'
7
+ gem 'mocha'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 DylanDeng, George Lee
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # Backup::Baidu
2
+
3
+ This Gem is aimed to provide a plugin for [Backup](https://github.com/meskyanichi/backup), using Baidu's [PCS](http://developer.baidu.com/wiki/index.php?title=docs/pcs/rest/overview) as storage.
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'backup-baidu'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install backup-baidu
19
+
20
+ ## Usage
21
+
22
+ include this in your backup model file, like:
23
+
24
+ ```ruby
25
+ require "backup-baidu"
26
+
27
+ Backup::Model.new(:foo, 'Description for foo') do
28
+
29
+ store_with "Baidu" do |config|
30
+ config.access_key_id = 'my_access_id'
31
+ config.access_key_secret = 'my_access_key'
32
+ config.path = '/apps/<my_path_value>' # IMPORTANT - set this to your API's path value when signing up for baidu PCS
33
+ config.keep = 10
34
+ end
35
+ end
36
+ ```
37
+
38
+ ## Contributing
39
+
40
+ 1. Fork it
41
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
42
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
43
+ 4. Push to the branch (`git push origin my-new-feature`)
44
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "backup-baidu"
7
+ spec.version = "0.1.1"
8
+ spec.authors = ["DylanDeng", "George Lee"]
9
+ spec.email = ["dylan@beansmile.com", "mclee@foolme.net"]
10
+ spec.description = %q{pan.baidu.com Storage support for Backup}
11
+ spec.summary = %q{pan.baidu.com Storage support for Backup}
12
+ spec.homepage = "https://github.com/mclee/backup-baidu"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "rspec", "~> 2.6"
21
+
22
+ spec.add_dependency "backup", ">= 3.7.0"
23
+ spec.add_dependency "api4baidu", ">= 1.0.1"
24
+ spec.add_dependency "oauth2", ["~> 0.9.2"]
25
+ spec.add_dependency "json", ["~> 1.8.0"]
26
+ end
@@ -0,0 +1,6 @@
1
+ require "backup"
2
+ require "json"
3
+ require "api4baidu"
4
+ require "oauth2"
5
+
6
+ Backup::Storage.send(:autoload, :Baidu, File.join(File.dirname(__FILE__),"backup/storage/baidu"))
@@ -0,0 +1,82 @@
1
+ module Backup
2
+ module Storage
3
+ class Baidu < Base
4
+ attr_accessor :api_key, :api_secret, :path
5
+
6
+ def initialize(model, storage_id = nil, &block)
7
+ super(model, storage_id)
8
+
9
+ @path ||= 'backups'
10
+
11
+ instance_eval(&block) if block_given?
12
+ end
13
+
14
+ private
15
+
16
+ def connection
17
+ return @connection if @connection
18
+ @api_key = self.api_key
19
+ @api_secret = self.api_secret
20
+ @connection = ::Baidu::Client.new { }
21
+ @connection.api_key = self.api_key
22
+ @connection.api_secret = self.api_secret
23
+
24
+ if session
25
+ @connection.access_token = session
26
+ else
27
+ puts "You'll need to authorize from Baidu for the first time:\n\n"
28
+ puts @connection.authorize_url
29
+ print "Type in 'code' from callback url: "
30
+ auth_code = $stdin.gets.chomp.split("\n").first
31
+ @connection.token!(auth_code)
32
+ puts "Auth successful!"
33
+ save_session(@connection.access_token)
34
+ end
35
+ @connection
36
+ end
37
+
38
+ def save_session(token)
39
+ @access_token = token
40
+ FileUtils.mkdir_p(Config.cache_path) unless File.directory?(Config.cache_path)
41
+ File.open(cached_file,"w") do |f|
42
+ f.puts @access_token.to_hash.to_json
43
+ end
44
+ end
45
+
46
+ def session
47
+ return @session if @session
48
+ if File.exist?(cached_file)
49
+ stored_data = File.open(cached_file).read
50
+ @session = OAuth2::AccessToken.from_hash(@connection.oauth_client, JSON.parse(stored_data))
51
+ if @session.expired?
52
+ Logger.info "Access Token has expired, not refresh a new token..."
53
+ @session.refresh!
54
+ Logger.info "Refresh successed. #{@session.token}"
55
+ save_session(@session)
56
+ end
57
+ end
58
+ @session
59
+ end
60
+
61
+ def cached_file
62
+ File.join(Config.cache_path, "baidu-" + self.api_key + "-" + self.api_secret)
63
+ end
64
+
65
+ def transfer!
66
+ remote_path = remote_path_for(package)
67
+ package.filenames.each do |filename|
68
+ src = File.join(Config.tmp_path, filename)
69
+ dest = File.join(remote_path, filename)
70
+ Logger.info "Storing '#{ dest }'..."
71
+ connection.upload_single_file(dest, src)
72
+ end
73
+ end
74
+
75
+ def remove!(package)
76
+ remote_path = remote_path_for(package)
77
+ Logger.info "Removeing '#{remote_path}' from Baidu..."
78
+ connection.delete(remote_path)
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,108 @@
1
+ require File.expand_path('../spec_helper.rb', __FILE__)
2
+
3
+ describe Backup::Storage::Baidu do
4
+ let(:model) { Backup::Model.new(:test_trigger, 'test label') }
5
+ let(:storage) do
6
+ Backup::Storage::Baidu.new(model) do |db|
7
+ db.api_key = 'lZUsGbfnXOkwa2tvtZVI1Sn7'
8
+ db.api_secret = 'qjhCau3p8EIPmZKAHyEArKn19H74FtEj'
9
+ db.keep = 5
10
+ end
11
+ end
12
+
13
+ it 'should be a subclass of Storage::Base' do
14
+ Backup::Storage::Baidu.
15
+ superclass.should == Backup::Storage::Base
16
+ end
17
+
18
+ describe '#initialize' do
19
+ after { Backup::Storage::Baidu.clear_defaults! }
20
+
21
+ it 'should load pre-configured defaults through Base' do
22
+ Backup::Storage::Baidu.any_instance.expects(:load_defaults!)
23
+ storage
24
+ end
25
+
26
+ it 'should pass the model reference to Base' do
27
+ storage.instance_variable_get(:@model).should == model
28
+ end
29
+
30
+ it 'should pass the storage_id to Base' do
31
+ storage = Backup::Storage::Baidu.new(model, 'my_storage_id')
32
+ storage.storage_id.should == 'my_storage_id'
33
+ end
34
+
35
+ end # describe '#initialize'
36
+
37
+ describe '#connection' do
38
+ let (:connection) { mock }
39
+ let (:session) { mock }
40
+
41
+ it "should return old connction if it exist" do
42
+ storage.instance_variable_set(:@connection,connection)
43
+ storage.send(:connection).should == connection
44
+ end
45
+ end
46
+
47
+ describe '#transfer!' do
48
+ let(:connection) { mock }
49
+ let(:package) { mock }
50
+ let(:file) { mock }
51
+ let(:s) { sequence '' }
52
+
53
+ before do
54
+ storage.instance_variable_set(:@package, package)
55
+ Backup::Config.stubs(:tmp_path).returns('/local/path')
56
+ storage.stubs(:connection).returns(connection)
57
+ file.stubs(:read).returns("foo")
58
+ end
59
+
60
+ it 'should transfer the package files' do
61
+ pending
62
+ # storage.expects(:remote_path_for).in_sequence(s).with(package).
63
+ # returns('remote/path')
64
+ # package.stubs(:filenames).returns(['backup.tar.enc-aa','backup.tar.enc-ab'])
65
+ # # first yield
66
+ # Backup::Logger.expects(:info).in_sequence(s).with(
67
+ # "Storing 'remote/path/backup.tar.enc-aa'..."
68
+ # )
69
+ # connection.expects(:put).in_sequence(s).with(
70
+ # File.join('remote/path', 'backup.tar.enc-aa'), File.join('/local/path', 'backup.tar.enc-aa')
71
+ # )
72
+ # # second yield
73
+ # Backup::Logger.expects(:info).in_sequence(s).with(
74
+ # "Storing 'remote/path/backup.tar.enc-ab'..."
75
+ # )
76
+ # connection.expects(:put).in_sequence(s).with(
77
+ # File.join('remote/path', 'backup.tar.enc-ab'), File.join('/local/path', 'backup.tar.enc-ab')
78
+ # )
79
+
80
+ # storage.send(:transfer!)
81
+ end
82
+ end # describe '#transfer!'
83
+
84
+ describe '#remove!' do
85
+ let(:package) { mock }
86
+ let(:connection) { mock }
87
+ let(:s) { sequence '' }
88
+
89
+ before do
90
+ storage.stubs(:storage_name).returns('Storage::Baidu')
91
+ storage.stubs(:connection).returns(connection)
92
+ end
93
+
94
+ it 'should remove the package files' do
95
+ storage.expects(:remote_path_for).in_sequence(s).with(package).
96
+ returns('remote/path')
97
+ # after both yields
98
+ Backup::Logger.expects(:info).in_sequence(s).with(
99
+ "Removeing 'remote/path' from Baidu..."
100
+ )
101
+ connection.expects(:delete).in_sequence(s).with('remote/path')
102
+
103
+ storage.send(:remove!, package)
104
+ end
105
+ end # describe '#remove!'
106
+
107
+
108
+ end
@@ -0,0 +1,36 @@
1
+ require 'rubygems' if RUBY_VERSION < '1.9'
2
+ require 'bundler/setup'
3
+ require 'backup'
4
+ require 'backup-baidu'
5
+
6
+ require 'rspec/autorun'
7
+
8
+ RSpec.configure do |config|
9
+ ##
10
+ # Use Mocha to mock with RSpec
11
+ config.mock_with :mocha
12
+
13
+ ##
14
+ # Example Helpers
15
+ # config.include Backup::ExampleHelpers
16
+
17
+ config.filter_run :focus => true
18
+ config.run_all_when_everything_filtered = true
19
+ config.treat_symbols_as_metadata_keys_with_true_values = true
20
+
21
+ config.before(:each) do
22
+ # ::FileUtils will always be either SandboxFileUtils or FileUtils::NoWrite.
23
+ # SandboxFileUtils.deactivate!(:noop)
24
+
25
+ # prevent system calls
26
+ Backup::Utilities.stubs(:gnu_tar?).returns(true)
27
+ Backup::Utilities.stubs(:utility)
28
+ Backup::Utilities.stubs(:run)
29
+ Backup::Pipeline.any_instance.stubs(:run)
30
+
31
+ Backup::Utilities.send(:reset!)
32
+ Backup::Config.send(:reset!)
33
+ # Logger only queues messages received until Logger.start! is called.
34
+ Backup::Logger.send(:reset!)
35
+ end
36
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: backup-baidu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - DylanDeng
8
+ - George Lee
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-11-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ~>
19
+ - !ruby/object:Gem::Version
20
+ version: '2.6'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ version: '2.6'
28
+ - !ruby/object:Gem::Dependency
29
+ name: backup
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - '>='
33
+ - !ruby/object:Gem::Version
34
+ version: 3.7.0
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - '>='
40
+ - !ruby/object:Gem::Version
41
+ version: 3.7.0
42
+ - !ruby/object:Gem::Dependency
43
+ name: api4baidu
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - '>='
47
+ - !ruby/object:Gem::Version
48
+ version: 1.0.1
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - '>='
54
+ - !ruby/object:Gem::Version
55
+ version: 1.0.1
56
+ - !ruby/object:Gem::Dependency
57
+ name: oauth2
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ~>
61
+ - !ruby/object:Gem::Version
62
+ version: 0.9.2
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 0.9.2
70
+ - !ruby/object:Gem::Dependency
71
+ name: json
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: 1.8.0
77
+ type: :runtime
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ~>
82
+ - !ruby/object:Gem::Version
83
+ version: 1.8.0
84
+ description: pan.baidu.com Storage support for Backup
85
+ email:
86
+ - dylan@beansmile.com
87
+ - mclee@foolme.net
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - .gitignore
93
+ - .rspec
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - backup-baidu.gemspec
99
+ - lib/backup-baidu.rb
100
+ - lib/backup/storage/baidu.rb
101
+ - spec/baidu_spec.rb
102
+ - spec/spec_helper.rb
103
+ homepage: https://github.com/mclee/backup-baidu
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.1.9
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: pan.baidu.com Storage support for Backup
127
+ test_files:
128
+ - spec/baidu_spec.rb
129
+ - spec/spec_helper.rb