scccp 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
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/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "http://rubygems.org"
2
+ gemspec
3
+
4
+ group :test do
5
+ gem "test-unit","2.5.2"
6
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,20 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ scccp (0.0.1)
5
+ net-scp
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ net-scp (1.0.4)
11
+ net-ssh (>= 1.99.1)
12
+ net-ssh (2.6.1)
13
+ test-unit (2.5.2)
14
+
15
+ PLATFORMS
16
+ ruby
17
+
18
+ DEPENDENCIES
19
+ scccp!
20
+ test-unit (= 2.5.2)
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ scccp
2
+ =====
3
+
4
+ automatically scp files in a specified folder
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/example/sample.rb ADDED
@@ -0,0 +1,39 @@
1
+ # https://github.com/pacojp/batchbase
2
+ require 'batchbase'
3
+ require 'scccp'
4
+
5
+ include Batchbase::Core
6
+ #create_logger('/tmp/batchbase_test_sample1.log')
7
+ create_logger(STDERR)
8
+
9
+ REMOTE_HOST = 'localhost'
10
+ REMOTE_USER_NAME = 'paco'
11
+ REMOTE_USER_PASSWORD = nil
12
+ WORK_SPACE = '/tmp/test/scccp_sample/'
13
+ REMOTE_PATH = WORK_SPACE + 'remote/'
14
+ QUEUE_FOLDER = WORK_SPACE + 'from/'
15
+ OK_FOLDER = WORK_SPACE + 'ok'
16
+ NG_FOLDER = WORK_SPACE + 'ng'
17
+
18
+ <<`MKDIR`
19
+ mkdir -p /tmp/test/scccp_sample/from
20
+ mkdir -p /tmp/test/scccp_sample/ok
21
+ mkdir -p /tmp/test/scccp_sample/ng
22
+ mkdir -p /tmp/test/scccp_sample/remote
23
+ touch /tmp/test/scccp_sample/from/file1
24
+ touch /tmp/test/scccp_sample/from/file2.tmp
25
+ touch /tmp/test/scccp_sample/from/file3.ok
26
+ MKDIR
27
+
28
+ execute do
29
+ scccp = Scccp::Scp.new()
30
+ scccp.logger = logger
31
+ scccp.remote_host = REMOTE_HOST
32
+ scccp.remote_user_name = REMOTE_USER_NAME
33
+ scccp.remote_user_password = REMOTE_USER_PASSWORD
34
+ scccp.remote_path = REMOTE_PATH
35
+ scccp.queue_folder = QUEUE_FOLDER
36
+ scccp.ok_folder = OK_FOLDER
37
+ scccp.ng_folder = NG_FOLDER
38
+ scccp.proceed
39
+ end
data/lib/scccp.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "scccp/version"
2
+ require "scccp/scp"
3
+
4
+ module Scccp
5
+ end
data/lib/scccp/scp.rb ADDED
@@ -0,0 +1,109 @@
1
+ require 'net/scp'
2
+ require 'fileutils'
3
+
4
+ module Scccp
5
+ class Scp
6
+ ATTRS = [
7
+ :remote_host,
8
+ :remote_user_name,
9
+ :remote_user_password,
10
+ :remote_path,
11
+ :queue_folder,
12
+ :ok_folder,
13
+ :ng_folder,
14
+ :timeout,
15
+ :logger2ssh,
16
+ :logger
17
+ ]
18
+ ATTRS.each do |_attr|
19
+ attr_accessor _attr
20
+ end
21
+
22
+ def initialize(opts={})
23
+ opts.each do |k, v|
24
+ send("#{k}=", v)
25
+ end
26
+ end
27
+
28
+ def attrs_ok?
29
+ ATTRS.each do |attr|
30
+ #v = instance_variable_get("@#{attr.to_s}")
31
+ v = send(attr)
32
+ case attr
33
+ when :remote_user_name,:remote_user_password,:timeout,:logger2ssh
34
+ next
35
+ when :queue_folder,:ok_folder,:ng_folder
36
+ unless File.directory?(v)
37
+ raise "#{attr}:#{v} is not folder"
38
+ end
39
+ else
40
+ unless v
41
+ raise %|must set "#{attr}" at least|
42
+ end
43
+ end
44
+ end
45
+ true
46
+ end
47
+
48
+ def proceed
49
+ attrs_ok?
50
+ opt = {}
51
+ if remote_user_password
52
+ opt[:password] = remote_user_password
53
+ end
54
+ if timeout
55
+ opt[:timeout] = timeout
56
+ end
57
+ if logger2ssh
58
+ opt[:logger] = logger
59
+ end
60
+
61
+ files = Dir::entries(queue_folder).map{|o|"#{queue_folder}/#{o}"}
62
+ files = files.select do |o|
63
+ File::ftype(o) == "file" &&
64
+ !(o =~ /\.(tmp|ok)$/)
65
+ end
66
+
67
+ logger.info("queue_folder is #{queue_folder}")
68
+ logger.info("target is #{remote_host}:#{remote_path}")
69
+ #logger.info(files.inspect)
70
+
71
+ begin
72
+ # ブロックで使うと途中で失敗した場合にscpインスタンスを
73
+ # 使いまわせない(というか固まる)のでこんな感じの使い方で
74
+ scp = Net::SCP.start(remote_host, remote_user_name, opt)
75
+ files.each do |file|
76
+ ok_file = file + '.ok'
77
+ begin
78
+ logger.info "uploading_start:#{file}"
79
+ scp.upload! file, remote_path
80
+ logger.info "uploading_finish:#{file}"
81
+ FileUtils.touch ok_file
82
+ logger.info "uploading_start:#{ok_file}"
83
+ scp.upload! ok_file, remote_path
84
+ logger.info "uploading_finish:#{ok_file}"
85
+ FileUtils.mv(file,ok_folder)
86
+ FileUtils.mv(ok_file,ok_folder)
87
+ true
88
+ rescue => e
89
+ logger.error e
90
+ File.delete(ok_file) if File.exists?(ok_file)
91
+ FileUtils.mv(file,ng_folder)
92
+ begin
93
+ scp.session.close if scp
94
+ rescue
95
+ end
96
+ scp = Net::SCP.start(remote_host, remote_user_name, opt)
97
+ false
98
+ end
99
+ end
100
+ rescue SocketError => socket_error
101
+ logger.error socket_error
102
+ ensure
103
+ if scp
104
+ scp.session.close
105
+ end
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,3 @@
1
+ module Scccp
2
+ VERSION = "0.0.1"
3
+ end
data/scccp.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "scccp/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "scccp"
7
+ s.version = Scccp::VERSION
8
+ s.authors = ["pacojp"]
9
+ s.email = ["paco.jp@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{automatically scp files in a specified folder}
12
+ s.description = %q{automatically scp files in a specified folder}
13
+ s.rubyforge_project = "scccp"
14
+
15
+ #s.add_dependency "batchbase",["0.0.4"]
16
+ s.add_dependency "net-scp"
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_paths = ["lib"]
22
+
23
+ # specify any dependencies here; for example:
24
+ # s.add_development_dependency "rspec"
25
+ # s.add_runtime_dependency "rest-client"
26
+ end
data/test/test.rb ADDED
@@ -0,0 +1,115 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ $: << File.dirname(__FILE__)
4
+ require 'test_helper'
5
+ require 'test/unit'
6
+ require 'Fileutils'
7
+
8
+ require 'scccp'
9
+
10
+ class TestScccp < Test::Unit::TestCase
11
+
12
+ SCCCP_WORKING_DIR = '/tmp/test/scccp/'
13
+ QUEUE_FOLDER = "#{SCCCP_WORKING_DIR}from/"
14
+ OK_FOLDER = "#{SCCCP_WORKING_DIR}done/"
15
+ NG_FOLDER = "#{SCCCP_WORKING_DIR}error/"
16
+ REMOTE_PATH = "#{SCCCP_WORKING_DIR}remote/"
17
+ REMOTE_USER_NAME = 'paco'
18
+ REMOTE_USER_PASSWORD = nil
19
+ REMOTE_HOST = "localhost"
20
+
21
+ def setup
22
+ FileUtils.mkdir_p(QUEUE_FOLDER)
23
+ FileUtils.mkdir_p(OK_FOLDER)
24
+ FileUtils.mkdir_p(REMOTE_PATH)
25
+ FileUtils.mkdir_p(NG_FOLDER)
26
+ delete_files(QUEUE_FOLDER)
27
+ delete_files(OK_FOLDER)
28
+ delete_files(NG_FOLDER)
29
+ delete_files(REMOTE_PATH)
30
+ files = [
31
+ @l_file = QUEUE_FOLDER + 'testfile',
32
+ @l_file1 = QUEUE_FOLDER + 'testfile1',
33
+ @l_file2 = QUEUE_FOLDER + 'testfile2',
34
+ @l_file_tmp = QUEUE_FOLDER + 'test.tmp',
35
+ @l_file_ok = QUEUE_FOLDER + 'test.ok'
36
+ ]
37
+ files.each do |file|
38
+ File.write(file,'test_data')
39
+ end
40
+ end
41
+
42
+ def delete_files(dir)
43
+ raise 'safety net orz' unless dir =~ %r|/scccp/|
44
+ Dir::entries(dir).each do |file|
45
+ file = "#{dir}/#{file}"
46
+ next unless File::ftype(file) == 'file'
47
+ File.delete(file) if File.exist?(file)
48
+ end
49
+ end
50
+
51
+ def attr
52
+ {
53
+ :logger => Logger.new('/dev/null'),
54
+ :remote_host => REMOTE_HOST,
55
+ :remote_user_name => REMOTE_USER_NAME,
56
+ :remote_user_password => REMOTE_USER_PASSWORD,
57
+ :remote_path => REMOTE_PATH,
58
+ :queue_folder => QUEUE_FOLDER,
59
+ :ok_folder => OK_FOLDER,
60
+ :ng_folder => NG_FOLDER
61
+ }
62
+ end
63
+
64
+ def test_error
65
+ scccp = Scccp::Scp.new(attr)
66
+ scccp.remote_path = '/tmp/slefijseflislfjsliefjsief/'
67
+ scccp.timeout = 3
68
+ #scccp.logger2ssh = true
69
+ scccp.proceed
70
+
71
+ assert_true File.exists?(NG_FOLDER + "testfile2")
72
+ assert_false File.exists?(NG_FOLDER + "testfile2.ok")
73
+ end
74
+
75
+ def test_connection_error
76
+ scccp = Scccp::Scp.new(attr)
77
+ scccp.remote_host = 'sefisfejisfe.sefijsefij.esfij'
78
+ scccp.timeout = 3
79
+ #scccp.logger2ssh = true
80
+ scccp.proceed
81
+
82
+ # コネクションエラーだとキューフォルダーをいじらない
83
+ assert_true File.exists?(QUEUE_FOLDER + "testfile2")
84
+ end
85
+
86
+
87
+ def test_scccp
88
+ assert_true File.exists?(@l_file)
89
+ assert_true File.exists?(@l_file1)
90
+ scccp = Scccp::Scp.new()
91
+ assert_raise do
92
+ scccp.proceed
93
+ end
94
+ scccp.logger = Logger.new('/dev/null')
95
+ scccp.remote_host = REMOTE_HOST
96
+ scccp.remote_user_name = REMOTE_USER_NAME
97
+ scccp.remote_user_password = REMOTE_USER_PASSWORD
98
+ scccp.remote_path = REMOTE_PATH
99
+ scccp.queue_folder = 'hohogege'
100
+ scccp.ok_folder = OK_FOLDER
101
+ scccp.ng_folder = NG_FOLDER
102
+
103
+ assert_raise do
104
+ scccp.proceed
105
+ end
106
+ scccp.queue_folder = QUEUE_FOLDER
107
+ scccp.proceed
108
+
109
+ assert_true File.exists?(REMOTE_PATH + "testfile2")
110
+ assert_true File.exists?(REMOTE_PATH + "testfile2.ok")
111
+
112
+ assert_true File.exists?(@l_file_tmp)
113
+ assert_true File.exists?(@l_file_ok)
114
+ end
115
+ end
@@ -0,0 +1,5 @@
1
+ $: << File.dirname(__FILE__) + '/../lib'
2
+
3
+ require 'scccp'
4
+ require 'scccp/version'
5
+
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: scccp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - pacojp
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: net-scp
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: automatically scp files in a specified folder
31
+ email:
32
+ - paco.jp@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - Gemfile.lock
40
+ - README.md
41
+ - Rakefile
42
+ - example/sample.rb
43
+ - lib/scccp.rb
44
+ - lib/scccp/scp.rb
45
+ - lib/scccp/version.rb
46
+ - scccp.gemspec
47
+ - test/test.rb
48
+ - test/test_helper.rb
49
+ homepage: ''
50
+ licenses: []
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project: scccp
69
+ rubygems_version: 1.8.23
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: automatically scp files in a specified folder
73
+ test_files:
74
+ - test/test.rb
75
+ - test/test_helper.rb
76
+ has_rdoc: