fcrepo_wrapper 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.rubocop_todo.yml ADDED
File without changes
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ sudo: false
3
+ rvm:
4
+ - 2.2.0
5
+ - jruby
6
+ cache:
7
+ directories:
8
+ - /tmp/solr-5.1.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fcrepo_wrapper.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Chris Beer
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # FcrepoWrapper
2
+
3
+ Wrap any task with a Solr instance:
4
+
5
+ ```ruby
6
+ FcrepoWrapper.wrap do |solr|
7
+ # Something that requires Solr
8
+ end
9
+ ```
10
+
11
+ Or with Solr and a solr collection:
12
+
13
+ ```ruby
14
+ subject.wrap do |solr|
15
+ solr.with_collection(dir: File.join(FIXTURES_DIR, "basic_configs")) do |collection_name|
16
+ end
17
+ end
18
+ ```
19
+
20
+ ## Basic Options
21
+
22
+ ```ruby
23
+ FcrepoWrapper.wrap port: 8983, verbose: true, managed: true
24
+ ```
25
+
26
+ ```ruby
27
+ solr.with_collection(name: 'collection_name', dir: 'path_to_solr_confiigs')
28
+ ```
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task default: ['spec']
data/coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ service_name: travis-ci
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'fcrepo_wrapper'
4
+ require 'optparse'
5
+
6
+ options = {}
7
+ collection_options = {}
8
+ OptionParser.new do |opts|
9
+ opts.banner = "Usage: fcrepo_wrapper [options]"
10
+
11
+ opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
12
+ options[:verbose] = v
13
+ end
14
+
15
+ opts.on("--version VERSION", "Specify a fcrepo version to download (default: #{FcrepoWrapper.default_solr_version})") do |v|
16
+ options[:version] = v
17
+ end
18
+
19
+ opts.on("-pPORT", "--port PORT", "Specify the port fcrepo should run at (default: 8080)") do |p|
20
+ options[:port] = p
21
+ end
22
+ end.parse!
23
+
24
+ # default to verbose
25
+ options[:verbose] = true if options[:verbose].nil?
26
+
27
+ FcrepoWrapper.wrap(options) do |conn|
28
+ with_collection(collection_options)
29
+ while conn.status
30
+ sleep 1
31
+ end
32
+ end
@@ -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 'fcrepo_wrapper/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "fcrepo_wrapper"
8
+ spec.version = FcrepoWrapper::VERSION
9
+ spec.authors = ["Chris Beer"]
10
+ spec.email = ["chris@cbeer.info"]
11
+ spec.summary = %q{Solr 5 service wrapper}
12
+ spec.homepage = "https://github.com/cbeer/fcrepo_wrapper"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.bindir = 'exe'
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |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 "ruby-progressbar"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+
26
+ spec.add_development_dependency "rspec"
27
+ spec.add_development_dependency "simple_solr_client"
28
+ spec.add_development_dependency "coveralls"
29
+ end
@@ -0,0 +1,258 @@
1
+ require 'digest'
2
+ require 'fileutils'
3
+ require 'open-uri'
4
+ require 'ruby-progressbar'
5
+ require 'securerandom'
6
+ require 'stringio'
7
+ require 'tmpdir'
8
+ require 'byebug'
9
+
10
+ module FcrepoWrapper
11
+ class Instance
12
+ attr_reader :options, :pid
13
+
14
+ ##
15
+ # @param [Hash] options
16
+ # @option options [String] :url
17
+ # @option options [String] :version
18
+ # @option options [String] :port
19
+ # @option options [String] :version_file
20
+ # @option options [String] :instance_dir
21
+ # @option options [String] :download_path
22
+ # @option options [String] :md5sum
23
+ # @option options [String] :xml
24
+ # @option options [Boolean] :verbose
25
+ # @option options [Boolean] :managed
26
+ # @option options [Boolean] :ignore_md5sum
27
+ # @option options [Hash] :fcrepo_options
28
+ # @option options [Hash] :env
29
+ def initialize(options = {})
30
+ @options = options
31
+ end
32
+
33
+ def wrap(&_block)
34
+ start
35
+ yield self
36
+ ensure
37
+ stop
38
+ end
39
+
40
+ ##
41
+ # Start Solr and wait for it to become available
42
+ def start
43
+ extract
44
+ if managed?
45
+ args = ["java", "-jar", fcrepo_binary] + fcrepo_options.merge(port: port).map { |k, v| ["--#{k}", "#{v}"].reject(&:empty?) }.flatten
46
+
47
+ @pid = spawn(env, *args)
48
+
49
+ # Wait for fcrepo to start
50
+ until status
51
+ sleep 1
52
+ end
53
+ end
54
+ end
55
+
56
+ ##
57
+ # Stop fcrepo and wait for it to finish exiting
58
+ def stop
59
+ if managed? && started?
60
+ Process.kill 'HUP', pid
61
+
62
+ # Wait for fcrepo to stop
63
+ while status
64
+ sleep 1
65
+ end
66
+
67
+ Process.waitpid(pid)
68
+ end
69
+
70
+ @pid = nil
71
+ end
72
+
73
+ ##
74
+ # Check the status of a managed fcrepo service
75
+ def status
76
+ return true unless managed?
77
+ return false if pid.nil?
78
+
79
+ begin
80
+ Process.getpgid(pid)
81
+
82
+ TCPSocket.new('127.0.0.1', port).close
83
+ true
84
+ rescue Errno::ESRCH, Errno::ECONNREFUSED
85
+ false
86
+ end
87
+ end
88
+
89
+ ##
90
+ # Is Solr running?
91
+ def started?
92
+ !!status
93
+ end
94
+
95
+ ##
96
+ # Get the port this fcrepo instance is running at
97
+ def port
98
+ options.fetch(:port, "8080").to_s
99
+ end
100
+
101
+ ##
102
+ # Clean up any files fcrepo_wrapper may have downloaded
103
+ def clean!
104
+ stop
105
+ FileUtils.remove_entry(download_path) if File.exists? download_path
106
+ FileUtils.remove_entry(tmp_save_dir, true) if File.exists? tmp_save_dir
107
+ FileUtils.remove_entry(instance_dir, true) if File.exists? instance_dir
108
+ FileUtils.remove_entry(md5sum_path) if File.exists? md5sum_path
109
+ FileUtils.remove_entry(version_file) if File.exists? version_file
110
+ end
111
+
112
+ ##
113
+ # Get a (likely) URL to the fcrepo instance
114
+ def url
115
+ "http://127.0.0.1:#{port}/fcrepo/"
116
+ end
117
+
118
+ protected
119
+
120
+ def extract
121
+ return fcrepo_dir if File.exists?(fcrepo_binary) && extracted_version == version
122
+
123
+ jar_path = download
124
+
125
+ FileUtils.mkdir_p fcrepo_path
126
+ FileUtils.cp jar_path, fcrepo_binary
127
+ self.extracted_version = version
128
+
129
+ configure
130
+
131
+ fcrepo_dir
132
+ end
133
+
134
+ def download
135
+ unless File.exists?(download_path) && validate?(download_path)
136
+ fetch_with_progressbar download_url, download_path
137
+ validate! download_path
138
+ end
139
+
140
+ download_path
141
+ end
142
+
143
+ def validate?(file)
144
+ Digest::MD5.file(file).hexdigest == expected_md5sum
145
+ end
146
+
147
+ def validate!(file)
148
+ unless validate? file
149
+ raise "MD5 mismatch" unless options[:ignore_md5sum]
150
+ end
151
+ end
152
+
153
+ private
154
+
155
+ def download_url
156
+ @download_url ||= options.fetch(:url, default_download_url)
157
+ end
158
+
159
+ def default_download_url
160
+ @default_url ||= "https://github.com/fcrepo4/fcrepo4/releases/download/fcrepo-#{version}/fcrepo-webapp-#{version}-jetty-console.jar"
161
+ end
162
+
163
+ def md5url
164
+ "https://github.com/fcrepo4/fcrepo4/releases/download/fcrepo-#{version}/fcrepo-webapp-#{version}-jetty-console.jar.md5"
165
+ end
166
+
167
+ def version
168
+ @version ||= options.fetch(:version, default_fcrepo_version)
169
+ end
170
+
171
+ def fcrepo_options
172
+ options.fetch(:fcrepo_options, headless: nil)
173
+ end
174
+
175
+ def env
176
+ options.fetch(:env, {})
177
+ end
178
+
179
+ def default_fcrepo_version
180
+ FcrepoWrapper.default_fcrepo_version
181
+ end
182
+
183
+ def download_path
184
+ @download_path ||= options.fetch(:download_path, default_download_path)
185
+ end
186
+
187
+ def default_download_path
188
+ File.join(Dir.tmpdir, File.basename(download_url))
189
+ end
190
+
191
+ def fcrepo_dir
192
+ @fcrepo_dir ||= options.fetch(:instance_dir, File.join(Dir.tmpdir, File.basename(download_url, ".jar")))
193
+ end
194
+
195
+ def verbose?
196
+ !!options.fetch(:verbose, false)
197
+ end
198
+
199
+ def managed?
200
+ !!options.fetch(:managed, true)
201
+ end
202
+
203
+ def version_file
204
+ options.fetch(:version_file, File.join(fcrepo_dir, "VERSION"))
205
+ end
206
+
207
+ def expected_md5sum
208
+ @md5sum ||= options.fetch(:md5sum, open(md5file).read.split(" ").first)
209
+ end
210
+
211
+ def fcrepo_binary
212
+ File.join(fcrepo_dir, "fcrepo-webapp-#{version}-jetty-console.jar")
213
+ end
214
+
215
+ def md5sum_path
216
+ File.join(Dir.tmpdir, File.basename(md5url))
217
+ end
218
+
219
+ def tmp_save_dir
220
+ @tmp_save_dir ||= Dir.mktmpdir
221
+ end
222
+
223
+ def fetch_with_progressbar(url, output)
224
+ pbar = ProgressBar.create(title: File.basename(url), total: nil, format: "%t: |%B| %p%% (%e )")
225
+ open(url, content_length_proc: lambda do|t|
226
+ if t && 0 < t
227
+ pbar.total = t
228
+ end
229
+ end,
230
+ progress_proc: lambda do|s|
231
+ pbar.progress = s
232
+ end) do |io|
233
+ IO.copy_stream(io, output)
234
+ end
235
+ end
236
+
237
+ def md5file
238
+ unless File.exists? md5sum_path
239
+ fetch_with_progressbar md5url, md5sum_path
240
+ end
241
+
242
+ md5sum_path
243
+ end
244
+
245
+ def extracted_version
246
+ File.read(version_file).strip if File.exists? version_file
247
+ end
248
+
249
+ def extracted_version=(version)
250
+ File.open(version_file, "w") do |f|
251
+ f.puts version
252
+ end
253
+ end
254
+
255
+ def configure
256
+ end
257
+ end
258
+ end
@@ -0,0 +1,3 @@
1
+ module FcrepoWrapper
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,18 @@
1
+ require 'fcrepo_wrapper/version'
2
+ require 'fcrepo_wrapper/instance'
3
+
4
+ module FcrepoWrapper
5
+ def self.default_fcrepo_version
6
+ '4.3.0'
7
+ end
8
+
9
+ def self.default_instance(options = {})
10
+ @default_instance ||= FcrepoWrapper::Instance.new options
11
+ end
12
+
13
+ ##
14
+ # Ensures a Solr service is running before executing the block
15
+ def self.wrap(options = {}, &block)
16
+ default_instance(options).wrap &block
17
+ end
18
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe FcrepoWrapper do
4
+ describe ".wrap" do
5
+ it "should launch fcrepo" do
6
+ FcrepoWrapper.wrap do |fcrepo|
7
+ expect do
8
+ Timeout::timeout(15) do
9
+ TCPSocket.new('127.0.0.1', fcrepo.port).close
10
+ end
11
+ end.not_to raise_exception
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,12 @@
1
+ require 'coveralls'
2
+ Coveralls.wear!
3
+
4
+ require 'fcrepo_wrapper'
5
+ require 'simple_solr_client'
6
+
7
+ require 'rspec'
8
+
9
+ FIXTURES_DIR = File.expand_path("fixtures", File.dirname(__FILE__))
10
+
11
+ RSpec.configure do |_config|
12
+ end
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fcrepo_wrapper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Chris Beer
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-09-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ruby-progressbar
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simple_solr_client
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: coveralls
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
+ description:
98
+ email:
99
+ - chris@cbeer.info
100
+ executables:
101
+ - fcrepo_wrapper
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rubocop.yml"
107
+ - ".rubocop_hound.yml"
108
+ - ".rubocop_todo.yml"
109
+ - ".travis.yml"
110
+ - Gemfile
111
+ - LICENSE
112
+ - README.md
113
+ - Rakefile
114
+ - coveralls.yml
115
+ - exe/fcrepo_wrapper
116
+ - fcrepo_wrapper.gemspec
117
+ - lib/fcrepo_wrapper.rb
118
+ - lib/fcrepo_wrapper/instance.rb
119
+ - lib/fcrepo_wrapper/version.rb
120
+ - spec/lib/fcrepo_wrapper_spec.rb
121
+ - spec/spec_helper.rb
122
+ homepage: https://github.com/cbeer/fcrepo_wrapper
123
+ licenses:
124
+ - MIT
125
+ metadata: {}
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 2.4.8
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: Solr 5 service wrapper
146
+ test_files:
147
+ - spec/lib/fcrepo_wrapper_spec.rb
148
+ - spec/spec_helper.rb