dustcart 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4d97b71787f1d65b045c0a12aa49bbfb3c188574
4
- data.tar.gz: 6e9eec49566a1d84ba6aa59f41ab1d2e7a4f7263
3
+ metadata.gz: 803c7cadc641f88d2592bd95bf795972fbebcb75
4
+ data.tar.gz: 7ce783b1ba4b835c1f2ee7b8d9dbf304f00ef352
5
5
  SHA512:
6
- metadata.gz: 972e8c1b5db68163ed5f751260e2b0dd49fe8c9b53cc8a5c5baa9a610f370ffad6933c92ca30a3b648d17af53703118ab292c2122f9e159bb27c60132b13bfe3
7
- data.tar.gz: 80d23388d7cfb4ba87fb0d21df73144e1db4506496d87983ea0c0939d8f66530a545e27114f519eb4c7acd825c6cd82e88c6cdd76e1610bad4c2545b61ceea3b
6
+ metadata.gz: 7fc2e4e4134815ecd506695bed407a2846f9d705a2d8cb028e1b510af1b19a52c3a7e0ea5000c0fc49127a660fa83381d4c22b6f2d9cd5f2d0d98ea89c2086f5
7
+ data.tar.gz: 01114180e209608f15000c31e2e3c41665276220e0c876e383f0e28a9801372cdb55edf480ca1ccd382b7bebc243adf58d7e44c441eab839b575c0f64b9c68f0
@@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
12
12
  spec.summary = 'S3 integrated backup tool.'
13
13
  spec.description = 'CLI tool to send files/db_dump to S3.'
14
14
  spec.homepage = 'https://github.com/tamano/dustcart'
15
- spec.license = "MIT"
15
+ spec.license = 'MIT'
16
16
 
17
17
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
18
  spec.bindir = 'exe'
@@ -3,8 +3,10 @@ module Dustcart
3
3
  class Group
4
4
  INPUT_RESOURCES = [
5
5
  'dustcart/input/base',
6
+ 'dustcart/input/file_base',
6
7
  'dustcart/input/directory',
7
- 'dustcart/input/file'
8
+ 'dustcart/input/file',
9
+ 'dustcart/input/postgres'
8
10
  ].freeze
9
11
 
10
12
  OUTPUT_RESOURCES = [
@@ -3,19 +3,18 @@ module Dustcart
3
3
  module Input
4
4
  # base class for input
5
5
  class Base < Resource::Base
6
- attr_reader :from
6
+ define_attribute :label
7
7
  alias_method :to_dir, :dump_dir
8
8
 
9
- def initialize(to_dir, from, &block)
9
+ def initialize(to_dir, &block)
10
10
  super(to_dir, &block)
11
- @from = from
12
11
  end
13
12
 
14
13
  def precheck
15
14
  super
16
15
 
17
- raise <<-EOS.unindent unless Object::File.exists?(from)
18
- target(#{from}) does not exists
16
+ raise <<-EOS.unindent if attributes.key?(:label) && label !~ /^[\w.]+$/
17
+ label should be word characters ([a-zA-Z0-9_.]+)
19
18
  EOS
20
19
  end
21
20
  end
@@ -2,19 +2,13 @@ module Dustcart
2
2
  module Resource
3
3
  module Input
4
4
  # input: directory
5
- class Directory < Base
6
- define_attribute :label
7
-
5
+ class Directory < FileBase
8
6
  def precheck
9
7
  super
10
8
 
11
9
  raise <<-EOS.unindent unless Object::File.directory?(from)
12
10
  target(#{from}) is not a directory
13
11
  EOS
14
-
15
- raise <<-EOS.unindent if attributes.key?(:label) && label !~ /^\w+$/
16
- label should be word characters ([a-zA-Z0-9_]+)
17
- EOS
18
12
  end
19
13
 
20
14
  def run
@@ -2,7 +2,7 @@ module Dustcart
2
2
  module Resource
3
3
  module Input
4
4
  # input: file
5
- class File < Base
5
+ class File < FileBase
6
6
  define_attribute :label
7
7
 
8
8
  def precheck
@@ -11,10 +11,6 @@ module Dustcart
11
11
  raise <<-EOS.unindent unless Object::File.file?(from)
12
12
  target(#{from}) is not a regular file
13
13
  EOS
14
-
15
- raise <<-EOS.unindent if attributes.key?(:label) && label !~ /^\w+$/
16
- label should be word characters ([a-zA-Z0-9_]+)
17
- EOS
18
14
  end
19
15
 
20
16
  def run
@@ -0,0 +1,23 @@
1
+ module Dustcart
2
+ module Resource
3
+ module Input
4
+ # base class for file system input
5
+ class FileBase < Base
6
+ attr_reader :from
7
+
8
+ def initialize(to_dir, from, &block)
9
+ super(to_dir, &block)
10
+ @from = from
11
+ end
12
+
13
+ def precheck
14
+ super
15
+
16
+ raise <<-EOS.unindent unless Object::File.exists?(from)
17
+ target(#{from}) does not exists
18
+ EOS
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,104 @@
1
+ require 'helper/system_command'
2
+ require 'shellwords'
3
+
4
+ module Dustcart
5
+ module Resource
6
+ module Input
7
+ # input: postgresql
8
+ class Postgres < Base
9
+ define_attribute :host
10
+ define_attribute :port
11
+ define_attribute :user
12
+ define_attribute :pass
13
+ attr_reader :database_name
14
+
15
+ COMMAND = 'pg_dump'.freeze
16
+
17
+ def initialize(to_dir, database_name, &block)
18
+ super(to_dir, &block)
19
+ @database_name = database_name
20
+
21
+ host '127.0.0.1' unless host
22
+ port '5432' unless port
23
+ label 'database.dump' unless label
24
+ end
25
+
26
+ def precheck
27
+ super
28
+
29
+ requirements
30
+
31
+ raise <<-EOS.unindent unless SystemCommand.exists?(COMMAND)
32
+ #{COMMAND} command is not installed
33
+ EOS
34
+
35
+ raise <<-EOS.unindent unless database_name
36
+ 'database_name' is required
37
+ EOS
38
+ end
39
+
40
+ def run
41
+ super
42
+
43
+ create_dump
44
+ end
45
+
46
+ private
47
+
48
+ def requirements
49
+ %i(host port user pass label).each do |var|
50
+ raise <<-EOS.unindent unless attributes.key?(var)
51
+ '#{var}' is required
52
+ EOS
53
+ end
54
+ end
55
+
56
+ def create_dump
57
+ pipeline = CommandPipeline.new
58
+ pipeline.add(
59
+ "#{password_env} #{COMMAND} #{options} #{Shellwords.escape(database_name)}"
60
+ )
61
+ pipeline.run
62
+ end
63
+
64
+ def password_env
65
+ "PGPASSWORD=#{Shellwords.escape(pass)}"
66
+ end
67
+
68
+ # ignore :reek:FeatureEnvy:
69
+ def options
70
+ options = []
71
+ options << general_options
72
+ options << user_options
73
+ options << target_options
74
+ options.join(' ')
75
+ end
76
+
77
+ # ignore :reek:FeatureEnvy:
78
+ def general_options
79
+ options = []
80
+ options << '--format=custom'
81
+ options << '--blobs'
82
+ options << "--file=#{Shellwords.escape(to_dir)}/#{Shellwords.escape(label)}"
83
+ options.join(' ')
84
+ end
85
+
86
+ # ignore :reek:FeatureEnvy:
87
+ def user_options
88
+ options = []
89
+ options << "--host=#{Shellwords.escape(host)}"
90
+ options << "--port=#{Shellwords.escape(port)}"
91
+ options << "--username=#{Shellwords.escape(user)}"
92
+ options.join(' ')
93
+ end
94
+
95
+ # ignore :reek:FeatureEnvy:
96
+ def target_options
97
+ options = []
98
+ options << "--file=#{Shellwords.escape(to_dir)}/#{Shellwords.escape(label)}"
99
+ options.join(' ')
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
@@ -1,3 +1,3 @@
1
1
  module Dustcart
2
- VERSION = '0.1.1'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
@@ -0,0 +1,43 @@
1
+ require 'open3'
2
+ require 'shellwords'
3
+
4
+ # class to call system command
5
+ class SystemCommand
6
+ class << self
7
+ def exists?(command_name)
8
+ exec_command("which #{Shellwords.escape(command_name)}")
9
+ true
10
+ rescue
11
+ false
12
+ end
13
+
14
+ def exec_command(command)
15
+ output, error, status = Open3.capture3(command)
16
+
17
+ raise <<-EOS.unindent unless status.exitstatus.zero?
18
+ can not execute command: '#{command}'
19
+ Output: #{output}
20
+ Error: #{error}
21
+ EOS
22
+ end
23
+ end
24
+ end
25
+
26
+ # pipeline for command list
27
+ class CommandPipeline
28
+ attr_reader :pipeline
29
+
30
+ def initialize
31
+ @pipeline = []
32
+ end
33
+
34
+ def add(command)
35
+ @pipeline << command
36
+ end
37
+
38
+ def run
39
+ @pipeline.each do |command|
40
+ SystemCommand.exec_command(command)
41
+ end
42
+ end
43
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dustcart
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuya TAMANO
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-08-24 00:00:00.000000000 Z
11
+ date: 2016-09-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -179,10 +179,13 @@ files:
179
179
  - lib/dustcart/input/base.rb
180
180
  - lib/dustcart/input/directory.rb
181
181
  - lib/dustcart/input/file.rb
182
+ - lib/dustcart/input/file_base.rb
183
+ - lib/dustcart/input/postgres.rb
182
184
  - lib/dustcart/output/amazon_s3.rb
183
185
  - lib/dustcart/output/base.rb
184
186
  - lib/dustcart/resource_base.rb
185
187
  - lib/dustcart/version.rb
188
+ - lib/helper/system_command.rb
186
189
  - lib/helper/zip_file_generator.rb
187
190
  homepage: https://github.com/tamano/dustcart
188
191
  licenses: