autosftp 0.0.3

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 428468172ce0e302ef74b75f8f71d87ddb9ee188
4
+ data.tar.gz: c6f729fd3a0415b5c27afd998f32eb17aa7eff7c
5
+ SHA512:
6
+ metadata.gz: 2488fb0a523612b45008f914273ac691aac01d961eaf6ea26adff6dabb9341a8c8549b34f10dd121134adefb5be5d306aa957486dc353809d508819af9d4548d
7
+ data.tar.gz: c690bf0685fa41d879d4cb7d7f40d1699d7b73b62e1bfe53462cfe7262da06dba767a8b84dadca73092025152030458622edd011d38f513235681ad9e3720c62
data/.gitignore ADDED
@@ -0,0 +1,34 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .autosftp
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
19
+
20
+ # osx
21
+ .DS_Store
22
+ .AppleDouble
23
+ .LSOverride
24
+ Icon
25
+
26
+ # emacs
27
+ *~
28
+ \#*\#
29
+ /.emacs.desktop
30
+ /.emacs.desktop.lock
31
+ .elc
32
+ auto-save-list
33
+ tramp
34
+ .\#*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in autosftp.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,3 @@
1
+ Copyright (c) 2014 hirabaru
2
+
3
+ MIT License
data/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # Autosftp
2
+
3
+ ローカルファイルを監視し、自動でファイルをアップします。
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'autosftp'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install autosftp
18
+
19
+ ## Usage
20
+
21
+ ### 設定ファイルを準備します。プロジェクトの直下で作成したほうが良いです。
22
+
23
+ $ autosftp init
24
+
25
+ ### 設定ファイルに接続先の情報を追加します。[remote name]は自由な名称をつけて下さい。
26
+
27
+ $ autosftp set [remote name]
28
+
29
+ ### 自動監視をスタートします。
30
+
31
+ $ autosftp start [remote name]
32
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/autosftp.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'autosftp/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "autosftp"
8
+ spec.version = Autosftp::VERSION
9
+ spec.authors = ["hirabaru"]
10
+ spec.email = ["hirabaru@n-create.co.jp"]
11
+ spec.description = %q{"sftp automatically"}
12
+ spec.summary = %q{"autosftp"}
13
+ spec.homepage = "https://github.com/pikonori/auto_sftp"
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{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "net-ssh", "2.6.7"
22
+ spec.add_dependency "net-sftp", "2.1.2"
23
+ spec.add_dependency "fssm", "0.2.10"
24
+ spec.add_dependency "thor", "~> 0.19.1"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.3"
27
+ spec.add_development_dependency "rake"
28
+ spec.add_development_dependency "rspec"
29
+ end
data/bin/autosftp ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require 'autosftp'
3
+
4
+ Autosftp::CLI.start
data/lib/autosftp.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "autosftp/version"
2
+ require "autosftp/cli"
3
+
4
+ module Autosftp
5
+ end
@@ -0,0 +1,108 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'thor'
3
+ require 'autosftp/monitor'
4
+ require 'autosftp/file_access'
5
+ require 'autosftp/connection'
6
+
7
+ module Autosftp
8
+ class CLI < Thor
9
+
10
+ desc "start [remote name]", "Automatic monitoring start"
11
+ def start(word)
12
+ if false == Autosftp::FileAccess.exist?
13
+ puts "is not .autosftp \n $ autosftp init"
14
+ exit
15
+ end
16
+
17
+ conf = Autosftp::FileAccess.read
18
+ if !conf[word]
19
+ puts "is not setting \n $ autosftp set [remote name]"
20
+ exit
21
+ end
22
+
23
+ Autosftp::Monitor.start conf[word]
24
+ end
25
+
26
+ desc "set [remote name]", "add information to the '.autosftp'. Please put the name of any [remote name]"
27
+ def set(word)
28
+ ssh_hash = {}
29
+ begin
30
+ puts "[username@host:port]"
31
+ ssh_str = STDIN.gets.chomp
32
+ if false == Autosftp::Connection.check?(ssh_str)
33
+ puts "is not [username@host:port]"
34
+ exit
35
+ end
36
+
37
+ ssh_hash = Autosftp::Connection.explode ssh_str
38
+
39
+ puts "password:"
40
+ ssh_hash[:password] = STDIN.noecho(&:gets).chomp
41
+
42
+ puts "remote path:"
43
+ ssh_hash[:remote_path] = STDIN.gets.chomp
44
+
45
+ begin
46
+ Net::SSH.start(ssh_hash[:host], ssh_hash[:user], {:password => ssh_hash[:password], :port => ssh_hash[:port]}) do |ssh|
47
+ command = 'ls ' + ssh_hash[:remote_path]
48
+ ssh.exec!(command) do |channel, stream, data|
49
+ if stream == :stderr
50
+ raise
51
+ end
52
+ end
53
+ end
54
+ rescue
55
+ puts "ssh connection ERROR"
56
+ exit
57
+ end
58
+
59
+ puts "local path: --If you enter a blank, the current directory is set"
60
+ ssh_hash[:local_path] = STDIN.gets.chomp
61
+ if '' == ssh_hash[:local_path]
62
+ ssh_hash[:local_path] = Dir.pwd
63
+ puts ssh_hash[:local_path]
64
+ end
65
+
66
+ Autosftp::FileAccess.save word, ssh_hash
67
+
68
+ rescue Interrupt
69
+ end
70
+ end
71
+
72
+ desc "delete [remote name]", "remove the configuration."
73
+ def delete(word)
74
+ Autosftp::FileAccess.delete word
75
+ end
76
+
77
+ desc "list", "setting list"
78
+ def list()
79
+ Autosftp::FileAccess.read.each do |key, value|
80
+ puts <<"EOS"
81
+
82
+ #{key}:
83
+ host: #{value[:host]}
84
+ local_path: #{value[:local_path]}
85
+ remote_path: #{value[:remote_path]}
86
+
87
+ EOS
88
+ end
89
+ end
90
+
91
+ desc "init", "Creating the initial file. file called '.autosftp' is created"
92
+ def init
93
+ init_file = Autosftp::FileAccess
94
+ if true == init_file.exist?
95
+ if yes? "File exists. Overwrite the file? [y/n]"
96
+ init_file.create
97
+ puts 'overwite!!! (' + init_file.path + ')'
98
+ else
99
+ puts 'cancel'
100
+ end
101
+ else
102
+ init_file.create
103
+ puts 'create!!! (' + init_file.path + ')'
104
+ end
105
+ end
106
+
107
+ end
108
+ end
@@ -0,0 +1,78 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'net/ssh'
3
+ require 'net/sftp'
4
+
5
+ module Autosftp
6
+ class Connection
7
+
8
+ # username@host:port形式かどうかチェックする。
9
+ def self.check? sftp
10
+ if /.*\@.*|.*\@.*\:.*/ =~ sftp
11
+ true
12
+ else
13
+ false
14
+ end
15
+ end
16
+
17
+ # username@host:port形式を分解する。
18
+ def self.explode sftp
19
+ ssh_hash = {}
20
+ if /.*\@.*/ =~ sftp
21
+ user_host_ary = sftp.split("@")
22
+ ssh_hash[:user] = user_host_ary[0]
23
+ if /.*\:.*/ =~ user_host_ary[1]
24
+ host_port_ary = user_host_ary[1].split(":")
25
+ ssh_hash[:host] = host_port_ary[0]
26
+ ssh_hash[:port] = host_port_ary[1].to_i
27
+ else
28
+ ssh_hash[:host] = user_host_ary[1]
29
+ ssh_hash[:port] = 22
30
+ end
31
+ end
32
+ ssh_hash
33
+ end
34
+
35
+ # ファイルの新規作成+更新
36
+ def self.create ssh_hash, local_file, remote_file
37
+ Net::SSH.start(ssh_hash[:host], ssh_hash[:user], {:password => ssh_hash[:password], :port => ssh_hash[:port]}) do |ssh|
38
+ ssh.sftp.connect do |sftp|
39
+
40
+ ssh_dir(sftp, File.dirname(remote_file))
41
+ begin
42
+ if File.exist? local_file
43
+ sftp.upload!(local_file, remote_file)
44
+ end
45
+ rescue Net::SFTP::StatusException => e
46
+ raise unless e.code == 2
47
+ if File.exist? local_file
48
+ sftp.upload!(local_file, remote_file)
49
+ sftp.setstat(remote_file, :permissions => 0644)
50
+ end
51
+ end
52
+
53
+ end
54
+ end
55
+ end
56
+
57
+ # ファイルの削除
58
+ def self.delete ssh_hash, remote_file
59
+ Net::SSH.start(ssh_hash[:host], ssh_hash[:user], {:password => ssh_hash[:password], :port => ssh_hash[:port]}) do |ssh|
60
+ ssh.sftp.connect do |sftp|
61
+ sftp.remove!(remote_file)
62
+ end
63
+ end
64
+ end
65
+
66
+ # ディレクトリの作成
67
+ def self.ssh_dir sftp, path
68
+ sftp.stat!(path)
69
+ rescue Net::SFTP::StatusException => e
70
+ parent = File::dirname(path);
71
+ ssh_dir(sftp, parent)
72
+ sftp.mkdir!(path, :permissions => 0755)
73
+ end
74
+
75
+ end
76
+
77
+ end
78
+
@@ -0,0 +1,48 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'yaml'
3
+
4
+ module Autosftp
5
+ class FileAccess
6
+ class << self
7
+
8
+ INIT_FILE = '.autosftp'
9
+ def create
10
+ File.open(path, "w").close()
11
+ end
12
+
13
+ def path
14
+ File.join(Dir.pwd, INIT_FILE)
15
+ end
16
+
17
+ def exist?
18
+ File.exist? path
19
+ end
20
+
21
+ def save word, hash
22
+ save_file = YAML.load_file path
23
+ if false == save_file
24
+ save_file = {}
25
+ end
26
+ save_file[word] = hash
27
+
28
+ f = open(path, "w")
29
+ f.write(YAML.dump(save_file))
30
+ f.close
31
+ end
32
+
33
+ def read
34
+ YAML.load_file path
35
+ end
36
+
37
+ def delete word
38
+ read_file = YAML.load_file path
39
+ read_file.delete(word)
40
+
41
+ f = open(path, "w")
42
+ f.write(YAML.dump(read_file))
43
+ f.close
44
+ end
45
+
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,46 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'fssm'
3
+ require 'autosftp/connection'
4
+
5
+ module Autosftp
6
+ class Monitor
7
+ def self.start setting
8
+ FSSM.monitor(setting[:local_path], '**/*') do
9
+ puts "C: create U: update D: delete E: error"
10
+ puts ""
11
+ puts "Host #{setting[:host]}"
12
+ puts "Accepted #{Time.now}"
13
+ puts ""
14
+
15
+ update do |base, file|
16
+ begin
17
+ Autosftp::Connection.create setting, "#{base}/#{file}", "#{setting[:remote_path]}/#{file}"
18
+ puts "U: #{Time.now} #{setting[:remote_path]}/#{file}"
19
+ rescue
20
+ puts "EU: #{Time.now} #{setting[:remote_path]}/#{file}"
21
+ end
22
+ end
23
+
24
+ create do |base, file|
25
+ begin
26
+ Autosftp::Connection.create setting, "#{base}/#{file}", "#{setting[:remote_path]}/#{file}"
27
+ puts "C: #{Time.now} #{setting[:remote_path]}/#{file}"
28
+ rescue
29
+ puts "EC: #{Time.now} #{setting[:remote_path]}/#{file}"
30
+ end
31
+ end
32
+
33
+ delete do |base, file|
34
+ begin
35
+ Autosftp::Connection.delete setting, "#{setting[:remote_path]}/#{file}"
36
+ puts "D: #{Time.now} #{setting[:remote_path]}/#{file}"
37
+ rescue
38
+ puts "ED: #{Time.now} #{setting[:remote_path]}/#{file}"
39
+ end
40
+ end
41
+
42
+ end
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,3 @@
1
+ module Autosftp
2
+ VERSION = "0.0.3"
3
+ end
@@ -0,0 +1,38 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.expand_path('spec_helper', File.dirname(__FILE__))
3
+ require 'autosftp/connection'
4
+
5
+ describe Autosftp::Connection do
6
+ before :each do
7
+ @connection = Autosftp::Connection
8
+ end
9
+
10
+ it '正規表現チェックOK' do
11
+ expect(@connection.check?('aaa@0000.0000.0000.0000:22')).to eq true
12
+ end
13
+
14
+ it '正規表現チェックOK' do
15
+ expect(@connection.check?('aaa@0000.0000.0000.0000')).to eq true
16
+ end
17
+
18
+ it '正規表現チェックNG' do
19
+ expect(@connection.check?('aaa')).to eq false
20
+ end
21
+
22
+ it '正規表現チェックNG' do
23
+ expect(@connection.check?('aaa:22')).to eq false
24
+ end
25
+
26
+ it 'username@host:portを分解OK' do
27
+ expect(@connection.explode('aaa@0000.0000.0000.0000:2200')).to include(:user => "aaa", :host => "0000.0000.0000.0000", :port => 2200)
28
+ end
29
+
30
+ it 'username@hostを分解OK' do
31
+ expect(@connection.explode('aaa@0000.0000.0000.0000')).to include(:user => "aaa", :host => "0000.0000.0000.0000", :port => 22)
32
+ end
33
+
34
+ it 'usernameを分解NG' do
35
+ expect(@connection.explode('aaa')).to include()
36
+ end
37
+
38
+ end
@@ -0,0 +1,3 @@
1
+ # encoding: utf-8
2
+ require 'rubygems'
3
+ require 'autosftp'
metadata ADDED
@@ -0,0 +1,160 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: autosftp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - hirabaru
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: net-ssh
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 2.6.7
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 2.6.7
27
+ - !ruby/object:Gem::Dependency
28
+ name: net-sftp
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 2.1.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 2.1.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: fssm
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 0.2.10
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 0.2.10
55
+ - !ruby/object:Gem::Dependency
56
+ name: thor
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.19.1
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 0.19.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '1.3'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '1.3'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: '"sftp automatically"'
112
+ email:
113
+ - hirabaru@n-create.co.jp
114
+ executables:
115
+ - autosftp
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - .gitignore
120
+ - Gemfile
121
+ - LICENSE.txt
122
+ - README.md
123
+ - Rakefile
124
+ - autosftp.gemspec
125
+ - bin/autosftp
126
+ - lib/autosftp.rb
127
+ - lib/autosftp/cli.rb
128
+ - lib/autosftp/connection.rb
129
+ - lib/autosftp/file_access.rb
130
+ - lib/autosftp/monitor.rb
131
+ - lib/autosftp/version.rb
132
+ - spec/connection_spec.rb
133
+ - spec/spec_helper.rb
134
+ homepage: https://github.com/pikonori/auto_sftp
135
+ licenses:
136
+ - MIT
137
+ metadata: {}
138
+ post_install_message:
139
+ rdoc_options: []
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - '>='
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - '>='
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ requirements: []
153
+ rubyforge_project:
154
+ rubygems_version: 2.1.11
155
+ signing_key:
156
+ specification_version: 4
157
+ summary: '"autosftp"'
158
+ test_files:
159
+ - spec/connection_spec.rb
160
+ - spec/spec_helper.rb