filecluster 0.3.3 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -62,6 +62,7 @@ module FC
62
62
  policy_id int NOT NULL,
63
63
  dir tinyint(1) NOT NULL DEFAULT 0,
64
64
  size bigint NOT NULL DEFAULT 0,
65
+ md5 varchar(32) DEFAULT NULL,
65
66
  status ENUM('new', 'ready', 'error', 'delete') NOT NULL DEFAULT 'new',
66
67
  time int DEFAULT NULL,
67
68
  copies int NOT NULL DEFAULT 0,
@@ -3,7 +3,7 @@ require 'shellwords'
3
3
 
4
4
  module FC
5
5
  class Item < DbBase
6
- set_table :items, 'name, tag, outer_id, policy_id, dir, size, status, time, copies'
6
+ set_table :items, 'name, tag, outer_id, policy_id, dir, size, md5, status, time, copies'
7
7
 
8
8
  # Create item by local path.
9
9
  # Additional options:
@@ -16,20 +16,21 @@ module FC
16
16
  :name => item_name.to_s.gsub('//', '/').sub(/\/$/, '').sub(/^\//, '').strip,
17
17
  :policy_id => policy.id,
18
18
  :dir => File.directory?(local_path),
19
- :size => `du -sb #{local_path.shellescape}`.to_i
19
+ :size => FC::Storage.new(:host => FC::Storage.curr_host).file_size(local_path),
20
+ :md5 => FC::Storage.new(:host => FC::Storage.curr_host).md5_sum(local_path)
20
21
  })
21
22
  item_params.delete(:replace)
22
23
  item_params.delete(:inplace)
23
24
  raise 'Name is empty' if item_params[:name].empty?
24
25
  raise 'Zero size path' if item_params[:size] == 0
25
-
26
+
26
27
  if local_path.include?(item_name)
27
- storage = policy.get_create_storages.detect do |s|
28
+ storage = policy.get_create_storages.detect do |s|
28
29
  s.host == FC::Storage.curr_host && local_path.index(s.path) == 0 && local_path.sub(s.path, '') == item_params[:name]
29
30
  end
30
31
  FC::Error.raise "local_path #{local_path} is not valid path for policy ##{policy.id}" unless storage
31
32
  end
32
-
33
+
33
34
  # new item?
34
35
  item = FC::Item.where('name=? AND policy_id=?', item_params[:name], policy.id).first
35
36
  if item
@@ -78,7 +79,7 @@ module FC
78
79
  else
79
80
  storage.copy_path(src, name)
80
81
  end
81
- size_on_storage = storage.file_size(name)
82
+ md5_on_storage = storage.md5_sum(name)
82
83
  rescue Exception => e
83
84
  item_storage.status = 'error'
84
85
  item_storage.save
@@ -89,10 +90,10 @@ module FC
89
90
  rescue Exception => e
90
91
  FC::Error.raise "After copy error: #{e.message}", :item_id => id, :item_storage_id => item_storage.id
91
92
  else
92
- if size_on_storage != size
93
+ if md5_on_storage != md5
93
94
  item_storage.status = 'error'
94
95
  item_storage.save
95
- FC::Error.raise "Check size after copy error", :item_id => id, :item_storage_id => item_storage.id
96
+ FC::Error.raise "Check md5 after copy error", :item_id => id, :item_storage_id => item_storage.id
96
97
  else
97
98
  item_storage.status = 'ready'
98
99
  item_storage.save
@@ -53,14 +53,14 @@ module FC
53
53
  # copy local_path to storage
54
54
  def copy_path(local_path, file_name)
55
55
  dst_path = "#{self.path}#{file_name}"
56
-
56
+
57
57
  cmd = "rm -rf #{dst_path.shellescape}; mkdir -p #{File.dirname(dst_path).shellescape}"
58
58
  cmd = self.class.curr_host == host ? cmd : "ssh -oBatchMode=yes -oStrictHostKeyChecking=no #{self.host} \"#{cmd}\""
59
59
  r = `#{cmd} 2>&1`
60
60
  raise r if $?.exitstatus != 0
61
-
62
- cmd = self.class.curr_host == host ?
63
- "cp -r #{local_path.shellescape} #{dst_path.shellescape}" :
61
+
62
+ cmd = self.class.curr_host == host ?
63
+ "cp -r #{local_path.shellescape} #{dst_path.shellescape}" :
64
64
  "scp -r -oBatchMode=yes -oStrictHostKeyChecking=no #{local_path.shellescape} #{self.host}:\"#{dst_path.shellescape}\""
65
65
  r = `#{cmd} 2>&1`
66
66
  raise r if $?.exitstatus != 0
@@ -108,6 +108,16 @@ module FC
108
108
  r.to_i
109
109
  end
110
110
 
111
+ # return object md5_sum on storage
112
+ def md5_sum(file_name)
113
+ dst_path = "#{self.path}#{file_name}"
114
+ cmd = "find #{dst_path} -type f -exec md5sum {} \\; | awk '{print $1}' | sort | md5sum"
115
+ cmd = "ssh -oBatchMode=yes -oStrictHostKeyChecking=no #{self.host} \"#{cmd}\"" if self.class.curr_host != host
116
+ r = `#{cmd} 2>&1`
117
+ raise r if $?.exitstatus != 0
118
+ r.to_s[0..31]
119
+ end
120
+
111
121
  # get available storage for copy by size
112
122
  def get_proper_storage_for_copy(size, exclude = [])
113
123
  get_copy_storages.select do |storage|
@@ -1,3 +1,3 @@
1
1
  module FC
2
- VERSION = "0.3.3"
2
+ VERSION = "0.3.4"
3
3
  end
@@ -103,11 +103,14 @@ class FunctionalTest < Test::Unit::TestCase
103
103
  assert_raise(RuntimeError) { item_storage.reload }
104
104
  end
105
105
 
106
- should "item create_from_local check size" do
107
- FC::Storage.any_instance.stubs(:copy_path => true, :file_size => 10)
106
+ should "item create_from_local check md5" do
108
107
  errors_count = FC::Error.where.count
109
- assert_raise(RuntimeError) { FC::Item.create_from_local(@@test_file_path, 'test5', @@policies[1], {:tag => 'test'}) }
110
- assert_equal errors_count+1, FC::Error.where.count, "Error not saved after check size"
108
+ @item = FC::Item.create_from_local(@@test_file_path, 'test5', @@policies[0], {:tag => 'test'})
109
+ item_storage = @item.make_item_storage(@@storages[0], status = 'copy')
110
+ `dd if=/dev/urandom of=#{@@storages[0].path}#{@item.name} bs=100K count=1 2>&1`
111
+ #`dd if=/dev/urandom of=#{@@storages[0].path}#{@item.name} bs=100K count=1 2>&1`
112
+ assert_raise(RuntimeError) { @item.copy_item_storage(@@storages[0], @@storages[1], item_storage) }
113
+ assert_equal errors_count+1, FC::Error.where.count, "Error not saved after check md5"
111
114
  end
112
115
 
113
116
  should "item create_from_local inplace" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: filecluster
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-03-18 00:00:00.000000000 Z
12
+ date: 2014-03-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rb-readline
@@ -193,7 +193,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
193
193
  version: '0'
194
194
  segments:
195
195
  - 0
196
- hash: -312355588801708268
196
+ hash: -1977585421026350925
197
197
  required_rubygems_version: !ruby/object:Gem::Requirement
198
198
  none: false
199
199
  requirements:
@@ -202,7 +202,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
202
202
  version: '0'
203
203
  segments:
204
204
  - 0
205
- hash: -312355588801708268
205
+ hash: -1977585421026350925
206
206
  requirements: []
207
207
  rubyforge_project:
208
208
  rubygems_version: 1.8.24